Apple IAP收据验证返回代码21002

时间:2017-02-05 23:53:12

标签: ios xamarin xamarin.ios in-app-purchase httpclient

我已经尝试提交到沙箱(和实时)verifyReceipt端点,但我总是从Apple获得以下JSON响应...

{"status":21002, "exception":"java.lang.IllegalArgumentException"}

这是邮递员截图

Postman testing

我发布数据并设置标题(application / json)

{"receipt-data":"MIIVbwYJKoZIhvcNAQcCoIIVYDCCFVwCAQExCzAJBgUrDgMCGgUAMIIFEAYJKoZIhvcNAQcBoIIFAQSCBP0xggT5MAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDgIBAQQDAgFSMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDQIBDQIBAQQFAgMBh2gwDQIBEwIBAQQFDAMxLjAwDgIBCQIBAQQGAgRQMjQ3MBACAQMCAQEECAwGMS4wLjI1MBgCAQQCAQIEEF/oX9LMmvqyAj2stAJzCkwGwIBAAIBAQQTDBFQcm9kdWN0aW9uU2FuZGJveDAcAgEFAgEBBBSdVfsFONtpXYexzWJbbskLrjqDAeAgEMAgEBBBYWFDIwMTctMDItMDVUMjM6MzA6NDBaMB4CARICAQEEFhYUMjAxMy0wOC0wMVQwNzowMDowMFowKgIBAgIBAQQiDCBjb20uZ2V0dmVoaWNsZXNtYXJ0LnZlaGljbGVzbWFydDBKAgEGAgEBBELMHlpKMWF8kHtwHgdygKIhevAiUBra8O/S/LJ2nDoFaLvE2AHWybr72qz2jfS7RtCZJI4yG5IIfxyeeeaWDb9K6BEwSgIBBwIBAQRCtZDfwHj0puyfpvVzRhVGUVvZn0ISOO45j7YowXVk24fFqwGD4ZGe/wuJRhfOoYyWw916pTdzRvC7RC9SUNDHriG4MIIBbQIBEQIBAQSCAWMxggFfMAsCAgasAgEBBAIWADALAgIGrQIBAQQCDAAwCwICBrACAQEEAhYAMAsCAgayAgEBBAIMADALAgIGswIBAQ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应用程序执行的。

我不确定Base64字符串是否格式正确或者其他东西 - 它确实有一些空格,但我现在删除它们(因为我已经看过报告说它有帮助)

以下是我在移动应用中执行的捕获base 64编码的操作,请注意这是Xamarin代码(C#),但它会映射到您在objective-c / swift中非常接近的内容...

String purchaseToken = null;
NSUrl receiptUrl = NSBundle.MainBundle.AppStoreReceiptUrl;
NSData receipt = null;

if (receiptUrl != null) {
    receipt = NSData.FromUrl (receiptUrl);
}

if (receipt == null) {
    // No local receipt -- handle the error
    // https://forums.xamarin.com/discussion/25304/how-do-i-exit-with-reason
    Environment.Exit (173);
} else {
    // http://stackoverflow.com/questions/37505020/example-of-parsing-a-receipt-for-an-in-app-purchase-using-ios-xamarin
    NSDictionary requestContents = NSDictionary.FromObjectAndKey ((NSString)receipt.GetBase64EncodedString (
                NSDataBase64EncodingOptions.None),
                (NSString)"receipt-data");
     string receiptData = (requestContents ["receipt-data"] as NSString).ToString ();

    purchaseToken = receiptData;
}

this.iapConsumableEvent.PurchaseToken = purchaseToken;

服务器代码(Java)使用HttpClient将此传输给Apple ...

HttpClient client = HttpClientHelper.createHttpClient(new DefaultRedirectStrategy());
final HttpPost postRequest = new HttpPost(appleEnvironmentUrl);
StringEntity input = new StringEntity(receiptRequestJson);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = client.execute(postRequest);

String rawResponse = EntityUtils.toString(response.getEntity(), "UTF-8");
BasicDBObject appleJson = BasicDBObject.parse(rawResponse);

int statusCode = appleJson.getInt("status", 99999);

//ALWAYS RETURNING 21002 The data in the receipt-data property was malformed or missing.

我感谢任何想法/答案!

3 个答案:

答案 0 :(得分:2)

我试图通过邮递员来解决这个问题。

这在node.js中适用于我。在它工作之前我必须使用JSON.stringify:

function createReceiptRequestJSON(base64String, secret) {
    return JSON.stringify({
        "receipt-data":base64String,
        password:secret
    });
}

你可以通过itunes connect找到你的秘密,但我很确定你需要包含在请求中。

修改

与邮递员再看一遍。收到了这个答案Apple receipt_data sample的收据。

enter image description here

似乎工作......

答案 1 :(得分:0)

事实证明我是使用HTTP get作为查询参数将令牌发送到服务器,一旦我将其添加到POST主体,那么一切都没问题!

答案 2 :(得分:0)

有简单的代码可以帮助您检查当前购买的产品:

它可在iOS 13.1上运行

func verifyPurchaseWithPayment() {
    let appsToreUrlString = "https://sandbox.itunes.apple.com/verifyReceipt"
    let receiptUrl = Bundle.main.appStoreReceiptURL
    do {
        let receipt = try Data(contentsOf: receiptUrl!)
        let encodedString = receipt.base64EncodedString()
        let requestContents = ["receipt-data" : encodedString] as [String : Any]
        do {
            let requestJsonData = try JSONSerialization.data(withJSONObject: requestContents, options: [])
            let session = URLSession.shared
            let request = NSMutableURLRequest(url: URL(string: appsToreUrlString)!)
            request.httpMethod = "POST"
            request.httpBody = requestJsonData
            let dataTask = session.dataTask(with: request as URLRequest) { (data, response, err) in
                let json = try? JSONSerialization.jsonObject(with: data!, options: [])
                print(json ?? "")
            }
            dataTask.resume()

        } catch { return }
    } catch { return }
}

我在Alamofire库中遇到了一个问题,该库更改了一些符号,因此在发送到服务器进行验证之前我屏蔽了base64字符串

let receiptString = receiptData.base64EncodedString()
let allowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
let allowedCharSet = CharacterSet(charactersIn: allowedCharacters)
let encodedString = receiptString.addingPercentEncoding(withAllowedCharacters: allowedCharSet)