ios 苹果内购订单验证 --- nodejs实现

寻技术 JS脚本 / Node.Js 2023年07月12日 87

实现代码

function IosPlayVerify(data,orderid,cb) {
    itunesPost(data,function (error,responseData) {
        if (error)
            cb({status:0,ptime:0});

        var purcaseTime = 0;
        var status = 0;
        if(responseData['status'] == 0){
            var inapp = responseData['receipt']['in_app'];

            status = 1;
            purcaseTime = inapp[0]['purchase_date_ms'];

            inapp.some(function (item) {
                if(orderid == item['transaction_id']){
                    purcaseTime = item['purchase_date_ms'];
                    return true;
                }
            });
        }

        cb({status:status,ptime:purcaseTime});
    });
}

function itunesPost(data,cb) {
    let receiptData = {};
    receiptData['receipt-data'] = data;
    let post_data = JSON.stringify(receiptData);

    var options = {
        "method": "POST",
        "hostname": "buy.itunes.apple.com",
        "port": null,
        "path": "/verifyReceipt",
        "headers": {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
    };

    var req = https.request(options, function (res) {
        var chunks = [];
        res.on("data", function (chunk) {
            if (res.statusCode !== 200) {
                 return cb("response.statusCode != 200");
            }
            chunks.push(chunk);
        });

        res.on("end", function () {
            var responseData, totalData;
            try {
                totalData = Buffer.concat(chunks);
                totalData = totalData.toString();
responseData = JSON.parse(totalData); } catch (_error) { return cb(_error); } return cb(null,responseData); }); req.on('error', function (exp) { return cb('error:' + exp.message); }); }); req.write(post_data); req.end(); }

调用:

 

var tokens = 'MIIVewYJKoZIhvcNpDzg==';
var orderId = '2700005012' ; IosPlayVerify(tokens,orderId,function (responseData) { console.log(responseData); });

参数:tokens : 为客户端购买时苹果服务器返回的订单信息.

orderId:为购买的订单ID.

 

返回信息:  请求返回的数据(responseData参数数据):

{
  "receipt": {
    "receipt_type": "Production",
    "adam_id": 127345,
    "app_item_id": 12732445,
    "bundle_id": "com.frd.cros",
    "application_version": "62",
    "download_id": 6703453,
    "version_external_identifier": 829054,
    "receipt_creation_date": "2019-03-29 02:03:00 Etc/GMT",
    "receipt_creation_date_ms": "1553820000",
    "receipt_creation_date_pst": "2019-03-28 19:03:00 America/Los_Angeles",
    "request_date": "2019-04-03 07:04:10 Etc/GMT",
    "request_date_ms": "1554275050780",
    "request_date_pst": "2019-04-03 00:04:10 America/Los_Angeles",
    "original_purchase_date": "2017-10-27 19:28:27 Etc/GMT",
    "original_purchase_date_ms": "1509132507000",
    "original_purchase_date_pst": "2017-10-27 12:28:27 America/Los_Angeles",
    "original_application_version": "3",
    "in_app": [
      {
        "quantity": "1",
        "product_id": "com.wordguess.go.in2",
        "transaction_id": "270004425012",
        "original_transaction_id": "270000425012",
        "purchase_date": "2019-03-29 02:02:59 Etc/GMT",
        "purchase_date_ms": "1553879000",
        "purchase_date_pst": "2019-03-28 19:02:59 America/Los_Angeles",
        "original_purchase_date": "2019-03-29 02:02:59 Etc/GMT",
        "original_purchase_date_ms": "1553824979000",
        "original_purchase_date_pst": "2019-03-28 19:02:59 America/Los_Angeles",
        "is_trial_period": "false"
      },
      {
        "quantity": "1",
        "product_id": "com.fil.removeads10",
        "transaction_id": "2700005471",
        "original_transaction_id": "27005471",
        "purchase_date": "2017-11-16 16:44:08 Etc/GMT",
        "purchase_date_ms": "1510850648000",
        "purchase_date_pst": "2017-11-16 08:44:08 America/Los_Angeles",
        "original_purchase_date": "2017-11-16 16:44:08 Etc/GMT",
        "original_purchase_date_ms": "1510850648000",
        "original_purchase_date_pst": "2017-11-16 08:44:08 America/Los_Angeles",
        "is_trial_period": "false"
      }
    ]
  },
  "status": 0,
  "environment": "Production"
}

 

 

       

关闭

用微信“扫一扫”