Encode purchase payload as JSON and pass to request.httpBody

Hi All, I have this code and I would like to send the payload of the completed purchase to my server. How to do that? Regards Thomas S

`
SubscriptionStoreView(...)
.onInAppPurchaseCompletion { product, result in
    if case .success(.success(let transaction)) = result {
        print("Purchased successfully: \(transaction.signedDate)") // this looks good in Xcode
        // Pass payload of transaction to my server
        let url = URL(string: "... my server ...")!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = ???? // how do I pass the payload of the transaction to the httpBody
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            let statusCode = (response as! HTTPURLResponse).statusCode
            if statusCode == 200 {
                print("SUCCESS")
            } else {
                print("FAILURE")
            }
        }
        task.resume()
    } else {
        print("Something else happened")
    }
}
`

Replies

Hello Thomas,

onInAppPurchaseCompletion returns a PurchaseResult https://developer.apple.com/documentation/storekit/product/purchaseresult

One of the fields on this is https://developer.apple.com/documentation/storekit/verificationresult/3868429-jwsrepresentation

This is the signed data that is encoded in such a way that you can send this to your server.

On your server, you can then verify the transaction with the App Store Server Library (https://developer.apple.com/documentation/appstoreserverapi/simplifying_your_implementation_by_using_the_app_store_server_library) to validate the authenticity of the transaction and get a decoded version

Hello, Thanks al lot. Works great! By the way I use NPM module app-store-server-api to decode property jwsRepresentation on the server.