如何测试应用商店购买收据以获取原始应用程序版本 [英] How to test app store purchase receipt to obtain original application version

查看:125
本文介绍了如何测试应用商店购买收据以获取原始应用程序版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用启动时加载应用购买收据。我如何模拟应用程序购买(不是应用程序内购买,而是实际的应用程序购买),以便我有一张收据? (我正试图从付费到免费增值)。

I want to load the app purchase receipt on app launch. How can I simulate an app purchase (not an In-App Purchase, but an actual App purchase) so that I'll have a receipt? (I'm trying to go from paid to freemium).

我正在使用此代码加载收据

I'm using this code to load the receipts

(BOOL)isAppPreviouslyPurchased {
    BOOL wasPreviouslyPurchased = false;

    // Load the receipt from the app bundle.
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
    if (receiptData) {

        //read purchase version from receipt
        NSDictionary *receipt = [NSJSONSerialization JSONObjectWithData:receiptData options:0 error:nil];
        NSString *oldVersion = receipt[@"original_application_version"];
        float vFloat = [oldVersion floatValue];

        if (vFloat < 1.6) {
            wasPreviouslyPurchased = true;
        }
    }

    return wasPreviouslyPurchased;
}


推荐答案

首先:刷新收据

SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];
request.delegate = self;
[request start];

添加 SKPaymentTransactionObserver 协议和此方法

- (void)paymentQueue:(SKPaymentQueue *)queue
 updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            // Call the appropriate custom method for the transaction state.
            case SKPaymentTransactionStatePurchasing:
                [self showTransactionAsInProgress:transaction deferred:NO];
                break;
            case SKPaymentTransactionStateDeferred:
                [self showTransactionAsInProgress:transaction deferred:YES];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                break;
            default:
                // For debugging
                NSLog(@"Unexpected transaction state %@", @(transaction.transactionState));
                break;
        }
    }
}

然后,当这种方法将被叫,您的收据将被刷新;)

Then, when this method will be called, your receipt will be refreshed ;)

其次:您必须解密收据

NSData *receipt; // Sent to the server by the device

// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = @{
    @"receipt-data": [receipt base64EncodedStringWithOptions:0]
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                      options:0
                                                        error:&error];

if (!requestData) { /* ... Handle error ... */ }

// Create a POST request with the receipt data.
NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];

// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
        completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError) {
        /* ... Handle error ... */
    } else {
        NSError *error;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (!jsonResponse) { /* ... Handle error ...*/ }
        /* ... Send a response back to the device ... */
    }
}];

您可以使用此代码解密它,但Apple并不真正推荐它。您应该从服务器调用iTunes。

You can decrypt it with this code, but it's not really recommended by Apple. You should call iTunes from your server.

然后,您可以使用Apple服务器返回的响应调用您的方法。

Then, you can call your method with the response returned by Apple server.

像这样(使用本地验证,Apple说的不好的方式)

Like this (with locally validation, bad way as Apple said)

NSData *receipt; // Sent to the server by the device

// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = @{
    @"receipt-data": [receipt base64EncodedStringWithOptions:0]
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                      options:0
                                                        error:&error];

if (!requestData) { /* ... Handle error ... */ }

// Create a POST request with the receipt data.
NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];

// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
        completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError) {
        /* ... Handle error ... */
    } else {
        NSError *error;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (!jsonResponse) { /* ... Handle error ...*/ }
          [self isAppPreviouslyPurchased:jsonResponse];
    }
}];

-(BOOL)isAppPreviouslyPurchased:(NSDictionary *)receipt {
    BOOL wasPreviouslyPurchased = false;

        NSString *oldVersion = receipt[@"original_application_version"];
        float vFloat = [oldVersion floatValue];

        if (vFloat < 1.6) {
            wasPreviouslyPurchased = true;
        }

    return wasPreviouslyPurchased;
}

这篇关于如何测试应用商店购买收据以获取原始应用程序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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