Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple IAP receipt verification returns code 21002

I've tried to submit to the sandbox (and live) verifyReceipt endpoints, but I'm always getting the following JSON response from Apple...

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

Here's a postman screenshot

Postman testing

I'm POSTing the data and setting the headers (application/json)

{"receipt-data":"MIIVbwYJKoZIhvcNAQcCoIIVYDCCFVwCAQExCzAJBgUrDgMCGgUAMIIFEAYJKoZIhvcNAQcBoIIFAQSCBP0xggT5MAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDgIBAQQDAgFSMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDQIBDQIBAQQFAgMBh2gwDQIBEwIBAQQFDAMxLjAwDgIBCQIBAQQGAgRQMjQ3MBACAQMCAQEECAwGMS4wLjI1MBgCAQQCAQIEEF/oX9LMmvqyAj2stAJzCkwGwIBAAIBAQQTDBFQcm9kdWN0aW9uU2FuZGJveDAcAgEFAgEBBBSdVfsFONtpXYexzWJbbskLrjqDAeAgEMAgEBBBYWFDIwMTctMDItMDVUMjM6MzA6NDBaMB4CARICAQEEFhYUMjAxMy0wOC0wMVQwNzowMDowMFowKgIBAgIBAQQiDCBjb20uZ2V0dmVoaWNsZXNtYXJ0LnZlaGljbGVzbWFydDBKAgEGAgEBBELMHlpKMWF8kHtwHgdygKIhevAiUBra8O/S/LJ2nDoFaLvE2AHWybr72qz2jfS7RtCZJI4yG5IIfxyeeeaWDb9K6BEwSgIBBwIBAQRCtZDfwHj0puyfpvVzRhVGUVvZn0ISOO45j7YowXVk24fFqwGD4ZGe/wuJRhfOoYyWw916pTdzRvC7RC9SUNDHriG4MIIBbQIBEQIBAQSCAWMxggFfMAsCAgasAgEBBAIWADALAgIGrQIBAQQCDAAwCwICBrACAQEEAhYAMAsCAgayAgEBBAIMADALAgIGswIBAQQCDA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GUgcG9saWN5IGFuZCBjZXJ0aWZpY2F0aW9uIHByYWN0aWNlIHN0YXRlbWVudHMuMA0GCSqGSIb3DQEBBQUAA4IBAQBcNplMLXi37Yyb3PN3m/J20ncwT8EfhYOFG5k9RzfyqZtAjizUsZAS2L70c5vu0mQPy3lPNNiiPvl4/2vIBx9OYOLUyDTOMSxv5pPCmv/K/xZpwUJfBdAVhEedNO3iyM7R6PVbyTi69G3cN8PReEnyvFteO3ntRcXqNxIjXKJdXZD9Zr1KIkIxH3oayPc4FgxhtbCSSsvhESPBgOJ4V9T0mZyCKM2r3DYLP3uujL/lTaltkwGMzd/c6ByxW69oPIQ7aunMZT7XZNn/Bh1XZp5m5MkL72NVxnn6hUrcbvZNCJBIqxw8dtk2cXmPIS4AXUKqK1drk/NAJBzewdXUhMYIByzCCAccCAQEwgaMwgZYxCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9uczFEMEIGA1UEAww7QXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkCCA7rV4fnngmNMAkGBSsOAwIaBQAwDQYJKoZIhvcNAQEBBQAEggEATHc0qVthvyN6b4x7is8L/tsHB0/f3BRiM6Kmdl/4ewzvA9iarX9zjK826vKhZeq7ClfaUzS84tKZoHgYDQSbzlk8n9wfqT5rbHN7/VL5Fqom1QwXeImJY6pBedblbBPsN55iRTqhk2yj73OSIdmQGTFL8ltrjF8vJvICtA7DjDCSqqt9La87fwiCOr3lYJS0iEl0OCraRptwVujw4R5b7SYU3hc7lYpdS3PJup5CjBjaF/N/J7wIz9UgZ5IHBJottI1eeTcqHrhEe3hh/WLqY5TqVw4uNjAwxQFca1hspDgPJvhYDzdylKbMpbvBM50llxqPuGmyH8kqAJvNg=="}

This is being executed from an application running on the iOS device through a TestFlight internal test deployment.

I'm a little unsure whether the Base64 string isn't well formed or something - it did have some spaces in, but I now remove those (because I've seen reports saying that it helps).

Here's what I do in the mobile app to capture the base 64 encoding, please note this is Xamarin code (C#), but it maps to what you do in objective-c / swift very closely...

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;

The server code (Java) transmits this to Apple using HttpClient...

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.

I appreciate any thoughts / answers!

like image 566
Devology Ltd Avatar asked Feb 05 '17 23:02

Devology Ltd


2 Answers

I had issues trying to get it to work through postman.

This works for me in node.js. I had to JSON.stringify before it did work :

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

You can find your secret through itunes connect but I'm fairly sure you need to include in the request.

Edit:

Had another look with postman. Got the receipt from this answer Apple receipt_data sample.

enter image description here

Seems to work...

like image 151
jeh Avatar answered Oct 14 '22 00:10

jeh


There is simple code that can help you to check your current purchased products:

It works on 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 }
}

I had a problem with Alamofire lib that changed some symbols, so i shielded base64 string before sending to server for validation

let receiptString = receiptData.base64EncodedString()
let allowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
let allowedCharSet = CharacterSet(charactersIn: allowedCharacters)
let encodedString = receiptString.addingPercentEncoding(withAllowedCharacters: allowedCharSet)
like image 41
Serg Smyk Avatar answered Oct 13 '22 23:10

Serg Smyk