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

DDD
Release: 2024-10-17 20:21:02
Original
592 people have browsed it

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>
    Copy after login
  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>
    Copy after login
  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>
    Copy after login
  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>
    Copy after login
  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!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!