且构网

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

使用 Parse iOS SDK 检查用户是否具有有效的自动续订订阅

更新时间:2023-11-28 11:38:10

如前所述,收据验证仅内置于 Parse SDK 中,用于可下载的内容,但创建一个 Cloud Code 函数来发布应用收据是相当简单的到 iTunes Store 进行验证.以下是用于服务器端验证的 Apple 文档:Validating Receipts with应用商店

As mentioned, receipt validation is only built into the Parse SDK for downloadable content, but it is fairly simple to to create a Cloud Code function that POSTs the app receipt to the iTunes Store for validation. Here are the Apple docs for server side validation: Validating Receipts with the App Store

这是一个基本函数的样子:

Here is a what a basic function would look like:

Parse.Cloud.define('validateReceipt', function (request, response) {
    var receiptAsBase64EncodedString = request.params.receiptData;
    var postData = {
        method: 'POST',
        url: 'http://buy.itunes.apple.com/verifyReceipt',
        body: { 'receipt-data': receiptAsBase64EncodedString,
                'password': SHARED_SECRET }
    }

    Parse.Cloud.httpRequest(postData).then(function (httpResponse) {
        // httpResponse is a Parse.Cloud.HTTPResponse
        var json = httpResponse.data; // Response body as a JavaScript object.
        var validationStatus = json.status; // App Store validation status code
        var receiptJSON = json.receipt; // Receipt data as a JSON object

        // TODO: You'll need to check the IAP receipts to retrieve the
        //       expiration date of the auto-renewing subscription, and
        //       determine if it is expired yet.
        var subscriptionIsActive = true;

       if (subscriptionIsActive) {
           return response.success('Subscription Active');
       }
       else {
           return response.error('Subscription Expired');
       }
    });
});

参见 收据字段 了解有关解释收据 JSON 的详细信息.对于 iOS 7+ 来说这相当简单,但对于 iOS 6 及更早版本的自动续订订阅收据很乏味.

See Receipt Fields for details on interpreting the receipt JSON. It's fairly straight forward for iOS 7+, but auto-renewing subscription receipts for iOS 6 and earlier are tedious.