首页  >  文章  >  后端开发  >  在 iOS 中验证应用内购买时如何解决无效收据状态?

在 iOS 中验证应用内购买时如何解决无效收据状态?

DDD
DDD原创
2024-10-17 20:21:02681浏览

How to Resolve Invalid Receipt Status When Verifying In-App Purchases in iOS?

验证应用内购买收据

应用内购买为用户提供了一种便捷的方式来获取应用内的其他内容或功能。为了确保交易合法,通过 App Store 验证收据至关重要。

问题:

一位开发者在验证收据时遇到困难,始终收到无效状态。尽管进行了大量的故障排除,问题仍然存在。

解决方案:

为了提供帮助,建议采取以下步骤:

  1. 验证收据方式:

    <code class="objective-c">- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {
        // Encode receipt data
        NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
    
        // Construct URL for validation
        NSString *completeString = [NSString stringWithFormat:@"http://url-for-your-php?receipt=%@", jsonObjectString];
        NSURL *urlForValidation = [NSURL URLWithString:completeString];
    
        // Create HTTP request
        NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
        [validationRequest setHTTPMethod:@"GET"];
    
        // Send request and receive response
        NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];
        [validationRequest release];
    
        // Parse response
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
        NSInteger response = [responseString integerValue];
        [responseString release];
    
        // Return verification result
        return (response == 0);
    }</code>
  2. 编码方式:

    <code class="objective-c">- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
        static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
        NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
        uint8_t *output = (uint8_t *)data.mutableBytes;
    
        for (NSInteger i = 0; i < length; i += 3) {
            NSInteger value = 0;
            for (NSInteger j = i; j < (i + 3); j++) {
                value <<= 8;
    
                if (j < length) {
                    value |= (0xFF & input[j]);
                }
            }
    
            NSInteger index = (i / 3) * 4;
            output[index + 0] = table[(value >> 18) & 0x3F];
            output[index + 1] = table[(value >> 12) & 0x3F];
            output[index + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
            output[index + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
        }
    
        return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
    }</code>
  3. 服务器端验证:

    <code class="php">$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
    // NOTE: use "buy" vs "sandbox" in production.
    $url = "https://sandbox.itunes.apple.com/verifyReceipt";
    $response_json = call-your-http-post-here($url, $receipt);
    $response = json_decode($response_json);
    
    // Save the data here!
    
    echo $response->status;</code>
  4. 内部方法:

    <code class="objective-c">@interface YourStoreClass (Internal)
    - (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction;
    - (NSString *)encode:(const uint8_t *)input length:(NSInteger)length;
    @end</code>
  5. 其他注意事项:

    • 由于潜在的请求长度和处理时间,请考虑异步 HTTP 发布。
    • 在收据验证期间提供用户反馈。

以上是在 iOS 中验证应用内购买时如何解决无效收据状态?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn