Home  >  Article  >  Backend Development  >  How to Resolve Invalid Receipt Status When Verifying In-App Purchases in iOS?

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

DDD
DDDOriginal
2024-10-17 20:21:02677browse

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

Verifying In-App Purchase Receipts

In-app purchases provide a convenient way for users to acquire additional content or features within your app. To ensure transactions are legitimate, validating receipts with the App Store is crucial.

Problem:

One developer encountered difficulties verifying receipts, consistently receiving an invalid status. Despite extensive troubleshooting, the issue persisted.

Solution:

To assist, the following steps are recommended:

  1. Verify Receipt Method:

    <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. Encoding Method:

    <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. Server-Side Validation:

    <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. Internal Methods:

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

    • Consider asynchronous HTTP posting due to potential request length and processing time.
    • Provide user feedback during receipt validation.

The above is the detailed content of How to Resolve Invalid Receipt Status When Verifying In-App Purchases in iOS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn