首頁  >  文章  >  後端開發  >  在 iOS 中驗證應用程式內購買時如何解決無效收據狀態?

在 iOS 中驗證應用程式內購買時如何解決無效收據狀態?

DDD
DDD原創
2024-10-17 20:21:02677瀏覽

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. 其他注意事項:
  6. 由於潛在的請求長度和處理時間,請考慮非同步HTTP 發布。
  7. 在收據驗證期間提供使用者回饋。

以上是在 iOS 中驗證應用程式內購買時如何解決無效收據狀態?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn