使用iOS Xamarin解析应用内购买收据的示例? [英] example of parsing a receipt for an in-app purchase using iOS Xamarin?

查看:207
本文介绍了使用iOS Xamarin解析应用内购买收据的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用实施购买验证。我看到我可以将购买收据发送到我的服务器以与Apple核实。但是,我无法弄清楚将NSData发布到我的URL进行验证的正确方法。这样的事情:

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);
        });
}

有没有人有例子?我正在使用RestSharp。

Does anyone have an example? I am using RestSharp.

谢谢!


  • davevr

推荐答案

好的,找到了怎么做。诀窍是将收据解析成字典然后从中拉出密钥。示例代码:

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);
        });

完成后,您可以在Apple服务器上进行验证。该网络上有很多示例代码。

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.

这篇关于使用iOS Xamarin解析应用内购买收据的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆