>  기사  >  백엔드 개발  >  인앱 구매 영수증을 확인하는 방법은 무엇입니까?

인앱 구매 영수증을 확인하는 방법은 무엇입니까?

Linda Hamilton
Linda Hamilton원래의
2024-10-17 20:12:03770검색

How to Validate In-App Purchase Receipts?

Validating In-App Purchase Receipts

In-app purchases provide an essential revenue stream for many iOS apps. To ensure the legitimacy of these transactions, app developers need to validate their receipts with Apple's App Store. However, this process can be complex and prone to errors.

Receipt Validation: An Overview

To validate a receipt on the client side, developers must retrieve it from the SKPaymentTransaction object and encode it in base64. The encoded receipt is then transmitted to the developer's server.

Server-Side Verification

The server then forwards the encoded receipt to Apple's App Store using an HTTP POST request. Apple's response will indicate whether the receipt is valid, along with the status of the transaction (e.g., purchased, refunded).

Client-Side Integration

Once the receipt has been verified on the server, the client-side app can retrieve the data and store it locally. This record can then be used to unlock content or grant access to features within the app.

Sample Code

Verifying a receipt in code involves these steps:

Objective-C Client-Side:

<code class="objective-c">- (void)verifyReceipt:(SKPaymentTransaction *)transaction {
    NSString *receiptData = [self encode:transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
    // Combine with server URL and query string, perform HTTP request
}</code>

Base64 Encoding:

<code class="objective-c">- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    // Encode the receipt data using base64
}</code>

Server-Side PHP:

<code class="php">$url = "https://sandbox.itunes.apple.com/verifyReceipt";
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
$response = json_decode(postToURL($url, $receipt));

// Parse and process the Apple response</code>

Troubleshooting and Precautions

Ensure the receipt data is properly formatted and encoded before sending it to Apple. Use an up-to-date version of iOS and the App Store SDK. Consider using asynchronous HTTP requests to avoid blocking the UI thread. It's recommended to use secure HTTPS connections for both client-side and server-side communications.

위 내용은 인앱 구매 영수증을 확인하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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