Apple IAP 收据验证 Node.js

Posted

技术标签:

【中文标题】Apple IAP 收据验证 Node.js【英文标题】:Apple IAP Receipt Validation Node.js 【发布时间】:2018-06-16 13:22:51 【问题描述】:

我正在尝试使用来自 here 的 Node.js 进行 IAP 收据验证,但它总是在服务器日志上返回错误:“收据数据属性中的数据格式错误。”

有人可以帮我正确地将 base64 字符串发送到 Node.js 并将其解码为相同的 base64 字符串以进行收据验证吗?我对 javascript 的经验为零,并且试图让这个简单的代码工作两天,但无济于事。

这是我的 Swift 代码:

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
...

这是我的 Node.js 代码(Firebase 云函数):

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)

    

  );

); 
);

【问题讨论】:

【参考方案1】:

用 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

简单地在 Node.js 中:

var receipt = request.body.receipt_string

【讨论】:

以上是关于Apple IAP 收据验证 Node.js的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Amazon IAP API 验证购买

IOS7验证iap收据,in_app文件是啥意思?

在报亭中处理应用内购买

iOS - IAP 问题,对应用商店收据感到困惑

自动更新 IAP 订阅用户流和刷新收据

适用于iOS应用的收据验证码,无法使用