民间, 我正在使用https://github.com/pcrawfor/iap_verifier来验证API中的Apple收据。
iOS应用只是使用Content-Type: application/octet-stream
张贴苹果收据。
我现在需要做的是将其转发给Apple进行验证。
代码:
console.log(req.body);
var receipt = req.body.toString('base64');
console.log(receipt);
var client = new IAPVerifier('appleSecret');
client.verifyReceipt(receipt, true, function(valid, msg, data) {
if (valid) {
console.log("Valid receipt",valid,msg,data);
var responseObject = {
valid: valid,
msg: msg,
data: data
};
responses.respond200(responseObject,res);
} else {
console.log("Invalid receipt",valid, msg, data);
var responseObject = {
valid: valid,
msg: msg,
data: data
};
responses.respond200(responseObject,res);
}
});
输出:
<Buffer 30 82 1a f8 06 09 2a 86 48 86 f7 0d 01 07 02 a0 82 1a e9 30 82 1a e5 02 01 01 31 0b 30 09 06 05 2b 0e 03 02 1a 05 00 30 82 0a a9 06 09 2a 86 48 86 f7 0d ...>
MIIa+AYJKoZIhvcNAQcCoIIa6TCCGuUCAQExCzAJBgUrDgMCGgUAMIIKqQYJKoZIhvcNAQcBoIIKmgSCCpYxggqSMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDgIBAQQDAgFZMAsCAQ8CA
QEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDQI........... lots of chars
Invalid receipt false Data was malformed { status: 21002 }
谢谢!
答案 0 :(得分:1)
req.body
已经是缓冲区了,所以您只需要var receipt = req.body.toString('base64');
但是,我认为主要问题是您将false
作为第二个参数传递给verifyReceipt()
。如果true
已经是base64编码的字符串,则second argument应为receipt
。所以我认为目前正在发生的是对数据进行双重编码。
答案 1 :(得分:0)
我的问题出在verifyAutoRenewReceipt
工作代码:
var receipt = req.body.toString('base64');
//https://github.com/pcrawfor/iap_verifier
var client = new IAPVerifier('appleSecret');
client.verifyAutoRenewReceipt(receipt, true, function(valid, msg, data) {
if (valid) {
// update status of payment in your system
console.log("Valid receipt",valid,msg,data);
var responseObject = {
valid: valid,
msg: msg,
data: data
};
responses.respond200(responseObject,res);
} else {
console.log("Invalid receipt",valid, msg, data);
var responseObject = {
valid: valid,
msg: msg,
data: data
};
responses.respond200(responseObject,res);
}
});