Rumah > pembangunan bahagian belakang > tutorial php > Bagaimana untuk Mengesahkan Resit Pembelian Dalam Apl dalam iOS?

Bagaimana untuk Mengesahkan Resit Pembelian Dalam Apl dalam iOS?

Patricia Arquette
Lepaskan: 2024-10-17 20:20:02
asal
663 orang telah melayarinya

How to Validate In-App Purchase Receipts in iOS?

Verifying In-App Purchase Receipts

In-app purchase receipt validation is a crucial step to ensure the authenticity and validity of transactions made through your app. This article aims to provide guidance for developers struggling with receipt validation by sharing a comprehensive code example that has been successfully implemented.

Code Implementation

To verify the receipt, follow these steps:

  1. Define the verifyReceipt method as shown below:
<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 with encoded receipt
    NSString *completeString = [NSString stringWithFormat:@"http://url-for-your-php?receipt=%@", jsonObjectString];
    NSURL *urlForValidation = [NSURL URLWithString:completeString];
    
    // Create request with HTTP GET method
    NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
    [validationRequest setHTTPMethod:@"GET"];
    
    // Send request synchronously
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];
    
    // Parse server response
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSInteger response = [responseString integerValue];
    
    return (response == 0);
}</code>
Salin selepas log masuk
  1. Implement the encode method to encode the receipt data in Base64:
<code class="objective-c">- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    // Define encoding table
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
    // Create mutable data buffer
    NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t *output = (uint8_t *)data.mutableBytes;
    
    // Encode data in loop
    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]);
            }
        }
        
        // Add encoded bytes to output
        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>
Salin selepas log masuk
  1. Call the verifyReceipt method from your SKPaymentTransactionObserver delegate method.
  2. On the server-side, use PHP to handle the receipt verification:
<code class="php"><?php

// Fetch receipt data from request parameter
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));

// Set URL for receipt verification
$url = "https://sandbox.itunes.apple.com/verifyReceipt";

// Send POST request with receipt data
$response_json = call-your-http-post-here($url, $receipt);

// Decode JSON response
$response = json_decode($response_json);

// Perform receipt verification and save data accordingly
echo $response->status;

?></code>
Salin selepas log masuk

Additional Considerations

  • Ensure to use the correct URL for receipt verification, depending on the environment (sandbox or production).
  • Pay attention to URL length limits and consider asynchronous HTTP request handling for large receipts.
  • For added security, consider using server-side certificate validation.

Atas ialah kandungan terperinci Bagaimana untuk Mengesahkan Resit Pembelian Dalam Apl dalam iOS?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan