example of parsing a receipt for an in-app purchase using iOS Xamarin?

北城余情 提交于 2019-12-06 14:18:02

问题


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:


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.



来源:https://stackoverflow.com/questions/37505020/example-of-parsing-a-receipt-for-an-in-app-purchase-using-ios-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!