gpt4 book ai didi

ios - 收据验证不正确

转载 作者:行者123 更新时间:2023-11-29 12:08:47 25 4
gpt4 key购买 nike

我已经在 Sandbox 环境下对 Consumable 类型的 IAP 进行了测试。我想确定我购买的商品是否有效。但结果总是以状态“21004”返回。我对共享 secret 不做任何事情。所以大家可以看看我关注apple购买成功时的示例代码:

- (void)verifyStatus:(SKPaymentTransaction *)transaction {

NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
NSError *receiptError;
BOOL isPresent = [[[NSBundle mainBundle] appStoreReceiptURL] checkResourceIsReachableAndReturnError:&receiptError];
if (!isPresent) {
NSLog(@"Validation failed");
}
NSString *receiptStr = [receiptData base64EncodedStringWithOptions:0];
NSDictionary *requestContents = @{@"receipt-data":receiptStr};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:nil];

if (!requestData) { /* ... Handle error ... */ }

NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"Respond : %@",jsonResponse);
if (!jsonResponse) { /* ... Handle error ...*/ }
/* ... Send a response back to the device ... */
}
}];

}

我遇到了什么问题?请分享您的经验。谢谢

最佳答案

请为 SECRECT KEY 尝试以下代码和步骤。

 #define SHARED_SECRET @"SECRECT KEY"

-(void)checkReceipt {
// verifies receipt with Apple
NSError *jsonError = nil;

NSString *receiptBase64 = [receiptData base64EncodedStringWithOptions:0];//receiptData NSData for validate Receipt
NSLog(@"Receipt Base64: %@",receiptBase64);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObjectsAndKeys:
receiptBase64,@"receipt-data",
SHARED_SECRET,@"password",
nil]
options:NSJSONWritingPrettyPrinted
error:&jsonError
];
NSLog(@"%@",jsonData);
NSError * error=nil;
NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSLog(@"%@",parsedData);
NSLog(@"JSON: %@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
// URL for sandbox receipt validation; replace "sandbox" with "buy" in production or you will receive
// error codes 21006 or 21007
NSURL *requestURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:requestURL];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:jsonData];


NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:req queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"Respond : %@",jsonResponse);
if (!jsonResponse) { /* ... Handle error ...*/ }
/* ... Send a response back to the device ... */
}
}];

}

您需要使用从 iTunes Connect 获得的 key 更改“SECRECT KEY”。示例看起来像 39fkjc38jd02mg72k9cn29dfkm39fk00

以下是创建/查看Secreate Key的步骤。

Login into iTunes Connect -> My Apps > Select your app > In-App Purchases > View or generate a shared secret.

关于ios - 收据验证不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com