2

我正在使用 itune connect 和我的服务器来实现应用程序内购买的代码以下载文件。

我想使用“POST”在 JSON 中发送产品 ID 和 identifierForVendor 和 transaction.bytes。这是显示层次结构的图像。 在此处输入图像描述

我在谷歌上搜索,我找到了这部分,但它在 NSstring 中,它通过 GET 方法发送

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {
NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
NSString *completeString = [NSString stringWithFormat:@"http://localhost:8888/amm/php_testing_json.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 integerValue];

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

}

任何帮助表示赞赏提前谢谢你

4

1 回答 1

0

首先确保您想要对您的任务进行同步请求。如果你这样做了,那么实际上你就快到了。但请注意,为了向您的服务器发送 POST 请求,您的服务器必须支持它们。您不应该将参数直接放入 url,因此在以下部分中将考虑到这一点:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction
{
    NSString *jsonObjectString = [self encode:(uint8_t*)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
    NSString *completeString = @"http://localhost:8888/amm/php_testing_json"; // just an URL your server developer will give you
    NSURL *urlForValidation = [NSURL URLWithString:completeString];
    NSMutableURLRequest *validationRequest = [NSMutableURLRequest requestWithURL:urlForValidation cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0f];
    [validationRequest setHTTPMethod:@"POST"];
    [validationRequest setHTTPBody:[jsonObjectString dataUsingEncoding:NSUTF8StringEncoding]]; // the string with receipt we set into the http body, not straight into parameters in url
    // Now, when the request is ready, send it and wait for a response
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

    NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
    if (responseString && [responseString length])
        return YES;
    return NO;
}

当然,最后您应该正确检查响应字符串(取决于您希望在不同情况下在该响应中看到的内容),而不仅仅是它的存在与否:-) 希望这会有所帮助

于 2013-05-30T13:51:15.160 回答