"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Troubleshooting In-App Purchase Receipt Validation: How to Handle \"Invalid Status\" Responses?

Troubleshooting In-App Purchase Receipt Validation: How to Handle \"Invalid Status\" Responses?

Published on 2024-11-09
Browse:562

Troubleshooting In-App Purchase Receipt Validation: How to Handle \

Verifying In-App Purchase Receipts

In-app purchase validation is crucial for ensuring that users have made legitimate purchases and granting them access to premium content or functionality. Despite the availability of documentation, it can be challenging to implement effective receipt validation.

One approach involves sending the receipt data to a PHP server, which then forwards it to the Apple App Store for verification. A successful response confirms the validity of the purchase, allowing you to proceed with recording the transaction on your server.

However, if you encounter "invalid status" responses during receipt validation, it's essential to check for any typos in your code. The following sample code provides a solution:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {
    NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];      
    NSString *completeString = [NSString stringWithFormat:@"http://url-for-your-php?receipt=%@", jsonObjectString];               
    NSURL *urlForValidation = [NSURL URLWithString:completeString];       
    NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];              
    [validationRequest setHTTPMethod:@"GET"];         
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];  
    [validationRequest release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
    NSInteger response = [responseString integerValue];
    [responseString release];
    return (response == 0);
}

- (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 > 18) & 0x3F];
        output[index   1] =                    table[(value >> 12) & 0x3F];
        output[index   2] = (i   1) > 6)  & 0x3F] : '=';
        output[index   3] = (i   2) > 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

Additionally, the following PHP code can be used on your server to handle receipt validation and record the transaction:

$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;

Remember to replace "call-your-http-post-here" with your preferred HTTP post mechanism. By implementing this code and ensuring its accuracy, you can effectively verify receipt purchases and manage in-app transactions with confidence.

Release Statement This article is reprinted at: 1729166962 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3