[英]error 21002 : receipt validation for IOS using server
我正在嘗試使用我的服務器使用收據驗證。 但我收到 21002 錯誤“收據數據屬性中的數據格式錯誤或丟失。”
我嘗試了我找到的所有 php 代碼,但沒有一個有效。 我嘗試直接連接到沙箱,它工作正常,但是當我將其更改為我的服務器時,會出現此錯誤。
private func validateReceipt(completion : @escaping (_ status : Bool) -> ()) {
// Get receipt if available
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
print(receiptData)
let receiptdata = receiptData.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 00))
let dict = ["receipt-data" : receiptdata]
let jsonData = try! JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0))
let request = NSMutableURLRequest(url: NSURL(string: ReceiptURL.myServer.rawValue)! as URL)
let session = URLSession.shared
request.httpMethod = "POST"
request.httpBody = jsonData
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
if let dataR = data
{
self.handleData(responseDatas: dataR as NSData, completion: { status in
completion(status)
})
} else {
completion(false)
}
})
task.resume()
}
catch { print("Couldn't read receipt data with error: " + error.localizedDescription) }
}
}
private func handleData(responseDatas : NSData, completion : (_ status : Bool) -> ())
{
do {
if let json = try JSONSerialization.jsonObject(with: responseDatas as Data, options: JSONSerialization.ReadingOptions.mutableLeaves) as? NSDictionary
{
if let value = json.value(forKeyPath: "status") as? Int
{
if value == 0
{
completion(true)
}
else
{
completion(false)
}
}
else
{
completion(false)
}
}
} catch {
print(error.localizedDescription)
}
}
PHP 代碼端
<?php
function getReceiptData($receipt)
{
$fh = fopen('showme.txt',w);
fwrite($fh,$receipt);
fclose($fh);
$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);
?>
有什么建議嗎? 為什么這段代碼不起作用?
編輯1:
我嘗試使用郵遞員並與 sandbox.itunes.com 連接,但我遇到了同樣的錯誤
我認為現在問題出在這行代碼上
let receiptdata = receiptData.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 00))
我正在努力解決它。 有什么建議嗎?
使用php代碼,參考我的回答。
使用 post,只需使用 json 格式的原始 post 數據。
在你的 php 代碼中,你不應該做str_replace
工作,數據已經是很好的 base64 編碼,只需使用它。
在您的帖子中,您應該使用 json 原始帖子數據。 請參閱Postman Chrome 應用程序中 form-data、x-www-form-urlencoded 和 raw 之間的區別是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.