gpt4 book ai didi

php - IOS收据验证错误21002

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:02 26 4
gpt4 key购买 nike

我正尝试在我的服务器端使用收据验证。一切正常,但有时我会看到奇怪的情况:10 次验证正常,但在 11 次时出现 21002 错误。我不知道该怎么办。有时我在启动应用程序后第一次验证收据时收到错误 21002。

应用端:

func validateReceipt(productID: String) {

let receipt = NSData(contentsOfURL: NSBundle.mainBundle().appStoreReceiptURL!)!

let receiptdata = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

let request = NSMutableURLRequest(URL: NSURL(string: "my_server_url")!)

let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

request.HTTPBody = receiptdata.dataUsingEncoding(NSUTF8StringEncoding)

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary

if (error != nil) {
print(error!.localizedDescription)
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
else {
if let parseJSON = json {
if String(parseJSON["status"]! == "ok" {
//do something
print("Validate OK")
}else{
print("Validate NOK")
}
}
else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Receipt Error: \(jsonStr)")
}
}
})

task.resume()
}

服务器端 php 脚本:

function getReceiptData($receipt)
{
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $receipt);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
$msg = $response.' - '.$errno.' - '.$errmsg;
echo $response;
}

foreach ($_POST as $key=>$value){
$newcontent .= $key.' '.$value;
}

$new = trim($newcontent);
$new = trim($newcontent);
$new = str_replace('_','+',$new);
$new = str_replace(' =','==',$new);

if (substr_count($new,'=') == 0){
if (strpos('=',$new) === false){
$new .= '=';
}
}

$new = '{"receipt-data":"'.$new.'"}';
$info = getReceiptData($new);

我所做的一切都是基于示例 http://www.brianjcoleman.com/tutorial-receipt-validation-in-swift/

所以,有时我觉得应用程序发送到服务器端错误的收据并且 php 脚本无法解析它并且我收到 21002 错误状态。有什么建议吗?

最佳答案

尝试从 receipt 中删除字符“\n”和“\r”,并在将其发送到服务器之前将“+”替换为“%2B”。像这样:

 NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
NSString *receiptDataString = [receipt base64EncodedStringWithOptions:0];
receiptDataString=[receiptDataString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
receiptDataString=[receiptDataString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
receiptDataString=[receiptDataString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
NSString *postDataString = [NSString stringWithFormat:@"receipt-data=%@", receiptDataString];
NSString *length = [NSString stringWithFormat:@"%lu", (unsigned long)[postDataString length]];
[request setValue:length forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[postDataString dataUsingEncoding:NSASCIIStringEncoding]];

关于php - IOS收据验证错误21002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32836058/

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