且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用iOS Xamarin解析应用内购买收据的示例?

更新时间:2022-12-31 09:38:15

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

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.