javascriptiosnode.jsswiftreceipt-validation

Apple IAP Receipt Validation Node.js


I'm trying to use Node.js from here for my IAP Receipt Validation, but it always returns error on a server's log: "The data in the receipt-data property was malformed."

Can someone help me to properly send base64 string to Node.js and to decode it there as same base64 string for receipt validation? I have zero experience with javascript and been trying to make this simple code work for two days now, to no avail.

Here is my Swift code:

let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)         
let receiptString = receiptData.base64EncodedString(options: [])


var request = URLRequest(url: URL(string: "https://us-central1-calendizer-6a849.cloudfunctions.net/receiptValidation")!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
  request.httpMethod = "POST"

// attach receipt data to a request
request.httpBody = receiptString.data(using: .utf8)

print("httpBody: \(request.httpBody?.base64EncodedString())")

Logs.log("✉️ Receipt Validation -> sending request ...")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
...
}

Here is my Node.js code (Firebase Cloud Functions):

const functions = require('firebase-functions');
var iap = require('in-app-purchase');


exports.receiptValidation = functions.https.onRequest((request, response) => {

var receipt_try1 = request.body
var receipt_try2 = request.body.toString('base64');
var receipt_try3 = JSON.stringify(receipt);
var receipt_try4 =  new Buffer(request.body.toString(), 'base64')

console.log("receipt: " + receipt_try1)

iap.config({
   applePassword: 'my shared key', 
   test: true
});

iap.setup(function (error) {
  if (error) {
   console.log("Setup error:" + error) // Failed to validate  
}

iap.validate(iap.APPLE, receipt_try1, function (error, appleResponse) {

    if (error) 
    {
      console.log("Validation error:" + error) // Failed to validate  
    }

    if (iap.isValidated(appleResponse)) {

      console.log("successful validation" + appleResponse)
      response.send(appleResponse)

    }

  });

}); 
});

Solution

  • did it using json:

    let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
    
    let receiptBase64String = receiptData.base64EncodedString(options: [])
    //.replacingOccurrences(of: "+", with: "%2B")
    
    
    // prepare json data
    let json: [String: Any] = ["receipt_string":  receiptBase64String]
    let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
    
    
    // create post request
    var request = URLRequest(url: URL(string: "https://us-central1-calendizer-6a849.cloudfunctions.net/receiptValidation")!)
    //request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"
    request.httpBody = jsonData
    

    and in Node.js simply:

    var receipt = request.body.receipt_string