我試圖查看所有其他問題,而且似乎沒有任何東西可以解決此問題。我已經縮小了它的範圍,因此https://sandbox.itunes.apple.com/verifyReceipt的響應僅僅是{"status":21002}
。任何幫助將不勝感激,我已經複製下面的相關代碼。狀態21002驗證交易收據時
NSString *completeString = @"http://www.mysite.com/verify.php";
NSURL *urlForValidation = [NSURL URLWithString:completeString];
NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
[validationRequest setHTTPMethod:@"POST"];
NSString *strTest = [NSString stringWithFormat:@"receipt=%@", [self createEncodedString:transaction.transactionReceipt]];
[validationRequest setHTTPBody:[NSData dataFromBase64String:strTest]];
NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
NSLog(@"%@", response);
- (NSString*) createEncodedString:(NSData*)data {
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=";
const int size = ((data.length + 2)/3)*4;
uint8_t output[size];
const uint8_t* input = (const uint8_t*)[data bytes];
for (int i = 0; i < data.length; i += 3)
{
int value = 0;
for (int j = i; j < (i + 3); j++)
{
value <<= 8;
if (j < data.length)
value |= (0xFF & input[j]);
}
const int index = (i/3) * 4;
output[index + 0] = table[(value >> 18) & 0x3F];
output[index + 1] = table[(value >> 12) & 0x3F];
output[index + 2] = (i + 1) < data.length ? table[(value >> 6) & 0x3F] : '=';
output[index + 3] = (i + 2) < data.length ? table[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithBytes:output length:size encoding:NSASCIIStringEncoding];
}
最後這是所使用的PHP代碼:
$url = 'https://sandbox.itunes.apple.com/verifyReceipt';
$receipt = "{%s}" % $_POST["receipt"];
$purchase_encoded = base64_encode($receipt);
$encodedData = json_encode(Array(
'receipt-data' => $purchase_encoded
));
//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);
//Decode response data using json_decode method to get an object.
echo $encodedResponse;
$response = json_decode($encodedResponse);
echo $response;