>  기사  >  백엔드 개발  >  인앱 구매 영수증 확인 문제 해결: \"잘못된 상태\" 응답을 처리하는 방법은 무엇입니까?

인앱 구매 영수증 확인 문제 해결: \"잘못된 상태\" 응답을 처리하는 방법은 무엇입니까?

Linda Hamilton
Linda Hamilton원래의
2024-10-17 20:13:03224검색

Troubleshooting In-App Purchase Receipt Validation: How to Handle

인앱 구매 영수증 확인

인앱 구매 확인은 사용자가 합법적인 구매를 했는지 확인하고 앱에 대한 액세스 권한을 부여하는 데 중요합니다. 프리미엄 콘텐츠 또는 기능. 문서가 있음에도 불구하고 효과적인 영수증 검증을 구현하는 것은 어려울 수 있습니다.

한 가지 접근 방식은 영수증 데이터를 PHP 서버로 보낸 다음 확인을 위해 Apple App Store로 전달하는 것입니다. 성공적으로 응답하면 구매의 유효성이 확인되어 서버에 거래 기록을 계속 진행할 수 있습니다.

그러나 영수증 확인 중에 "잘못된 상태" 응답이 표시되는 경우에는 오타가 있는지 확인하는 것이 중요합니다. 당신의 코드. 다음 샘플 코드는 솔루션을 제공합니다.

- (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 < 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];
}

또한 서버에서 다음 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;

교체하는 것을 기억하세요 선호하는 HTTP 게시 메커니즘을 사용하여 "call-your-http-post-here"를 선택하세요. 이 코드를 구현하고 정확성을 보장함으로써 영수증 구매를 효과적으로 확인하고 인앱 거래를 자신 있게 관리할 수 있습니다.

위 내용은 인앱 구매 영수증 확인 문제 해결: \"잘못된 상태\" 응답을 처리하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.