Javascript 使用Node.js验证iOS收据

Javascript 使用Node.js验证iOS收据,javascript,ios,iphone,node.js,Javascript,Ios,Iphone,Node.js,在努力工作了几天之后,我想知道是否有人在Node.js上进行了iOS收据验证。我尝试了iap_验证器找到的节点模块,但无法使其正常工作。我从苹果服务器收到的唯一回复是21002,数据格式不正确 有一件事对我很有用,那就是我直接从苹果提供的教程中获得了一个对苹果服务器的客户端验证请求,代码如下所示 // The transaction looks ok, so start the verify process. // Encode the receiptData for the itms rec

在努力工作了几天之后,我想知道是否有人在Node.js上进行了iOS收据验证。我尝试了iap_验证器找到的节点模块,但无法使其正常工作。我从苹果服务器收到的唯一回复是21002,数据格式不正确

有一件事对我很有用,那就是我直接从苹果提供的教程中获得了一个对苹果服务器的客户端验证请求,代码如下所示

// The transaction looks ok, so start the verify process.

// Encode the receiptData for the itms receipt verification POST request.
NSString *jsonObjectString = [self encodeBase64:(uint8_t *)transaction.transactionReceipt.bytes
                                         length:transaction.transactionReceipt.length];

// Create the POST request payload.
NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\", \"password\" : \"%@\"}",
                     jsonObjectString, ITC_CONTENT_PROVIDER_SHARED_SECRET];

NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];


// Use ITMS_SANDBOX_VERIFY_RECEIPT_URL while testing against the sandbox.
NSString *serverURL = ITMS_SANDBOX_VERIFY_RECEIPT_URL;

// Create the POST request to the server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:payloadData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
我有一堆不同的代码,用来向我的节点服务器发送各种各样的东西。我所有的尝试都失败了。我甚至尝试过将我在上面的客户端验证示例中构建的“payloadData”导入我的服务器,并使用以下代码将其发送到Apple服务器:

function verifyReceipt(receiptData, responder)
{

var options = {
    host: 'sandbox.itunes.apple.com',
    port: 443,
    path: '/verifyReceipt',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(receiptData)
    }
};

var req = https.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});

req.write(receiptData);
req.end();
}
其中传递函数的是payloadData。从苹果收到的回复总是21002。我基本上还是一个节点新手,所以我不知道到底出了什么问题。我认为当我将数据从ObjC发送到我的节点服务器时,可能会发生一些数据损坏,因此可能我没有正确地传输数据

如果有人能给我指出正确的方向,或者提供一些示例,说明他们是如何让收据验证在节点中为他们工作的,这将是一个很大的帮助。如果有人对iap_验证器模块有任何经验,以及它需要什么样的数据,那就太好了。我将提供我需要的任何代码示例,因为我已经为此过程奋斗了几天


谢谢

您是否已正确编写接收数据?根据苹果的规格,它应该有格式

{"receipt-data": "your base64 receipt"}
修改用收据数据对象包装base64收据字符串的代码验证应该有效

function (receiptData_base64, production, cb)
{
    var url = production ? 'buy.itunes.apple.com' : 'sandbox.itunes.apple.com'
    var receiptEnvelope = {
        "receipt-data": receiptData_base64
    };
    var receiptEnvelopeStr = JSON.stringify(receiptEnvelope);
    var options = {
        host: url,
        port: 443,
        path: '/verifyReceipt',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(receiptEnvelopeStr)
        }
    };

    var req = https.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log("body: " + chunk);
            cb(true, chunk);
        });
        res.on('error', function (error) {
            console.log("error: " + error);
            cb(false, error);
        });
    });
    req.write(receiptEnvelopeStr);
    req.end();
}
对于使用的任何人,以下是如何避免令人烦恼的21002错误

formFields = {
  'receipt-data': receiptData_64
  'password': yourAppleSecret
}

verifyURL = 'https://buy.itunes.apple.com/verifyReceipt' // or 'https://sandbox.itunes.apple.com/verifyReceipt'

req = request.post({url: verifyURL, json: formFields}, function(err, res, body) {
    console.log('Response:', body);
})

这是我使用npm请求承诺库为自动续费订阅提供的工作解决方案。 没有JSON字符串化主体表单,我收到了21002错误(receipt data属性中的数据格式不正确或丢失)


如果你让它在一个地方工作,为什么不记录网络流量,然后复制发生的任何事情?你有没有让它工作过!?如果是的话,你能分享一下吗。。
const rp = require('request-promise');

var verifyURL = 'https://sandbox.itunes.apple.com/verifyReceipt';
// use 'https://buy.itunes.apple.com/verifyReceipt' for production

var options = {
    uri: verifyURL,
    method: 'POST',
    headers: {
        'User-Agent': 'Request-Promise',
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    json: true
};

options.form = JSON.stringify({
    'receipt-data': receiptData,
    'password': password
});

rp(options).then(function (resData) {
    devLog.log(resData); // 0
}).catch(function (err) {
    devLog.log(err);
});