example of parsing a receipt for an in-app purchas

2020-07-27 05:55发布

I am trying to implement purchase validation for my app. I see that I can send the purchase receipt to my server to verify with Apple. However, I cannot figure out the correct way to POST the NSData to my URL for validation. Something like this:

public void CompleteTransaction (SKPaymentTransaction transaction) {
    var productId = transaction.Payment.ProductIdentifier;
    NSUrl receiptURL = NSBundle.MainBundle.AppStoreReceiptUrl;
    NSData theData = NSData.FromUrl (receiptURL);

    RestRequest request = new RestRequest(validationURL, Method.POST);
    request.AddBody(theData); // ??  
    restClient.ExecuteAsync<bool>((response) =>
        {
            FinishTransaction(transaction, response.Data);
        });
}

Does anyone have an example? I am using RestSharp.

Thanks!

  • davevr

1条回答
Evening l夕情丶
2楼-- · 2020-07-27 06:10

OK, found how to do it. The trick was to parse the receipt into a dictionary and then pull the key out of that. Sample code:

public void CompleteTransaction (SKPaymentTransaction transaction) {
    var productId = transaction.Payment.ProductIdentifier;
    NSUrl receiptURL = NSBundle.MainBundle.AppStoreReceiptUrl;
    NSData receipt = NSData.FromUrl (receiptURL);

    // here is the code I was missing
    NSDictionary requestContents = NSDictionary.FromObjectAndKey((NSString)receipt.GetBase64EncodedString(
                NSDataBase64EncodingOptions.None), 
                (NSString)"receipt-data");

    string receiptData = (requestContents["receipt-data"] as NSString).ToString();

    RestRequest request = new RestRequest(<url to your server>, Method.POST);

    request.AddParameter ("receipt-data", receiptData );

    apiClient.ExecuteAsync<bool>(request, (response) => 
        {
            FinishTransaction (transaction, response.Data);
        });

Once that is done, you can do the validation on the Apple server. There is lots of sample code on the net for that part.

查看更多
登录 后发表回答