在应用程序购买收据验证中

时间:2011-07-24 18:14:25

标签: iphone verification receipt

我在收到应用内购买验证时遇到了问题。你能看看下面的代码并告诉我为什么我得不到任何回报吗?此外,如果有关于测试收据验证的好方法的任何建议,请告诉我们!

非常感谢提前!

Xcode中:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {

NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];      
NSString *completeString = [NSString stringWithFormat:@"http://www.myURL.com/vReceipt.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];  
NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
NSInteger response = [responseString intValue];
 NSLog(@"Response String: %@", jsonObjectString);
 NSLog(@"Response Integer: %@", response);
 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代码:

<?php

$myreceipt = json_encode(array('receipt-data' => $_GET['receipt']));
$url = "https://sandbox.itunes.apple.com/verifyReceipt";

//create cURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myreceipt);

//execute cURL Request
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);

$response_json = json_decode($response);

print $response_json->{'status'};

?>

2 个答案:

答案 0 :(得分:1)

此字符串始终返回0:

return (response = 0);}

更正了一个:

return (response == 0);}

答案 1 :(得分:0)

你应该对你从iPhone上获得的数据执行striplashes()