Folks, I am using https://github.com/pcrawfor/iap_verifier to verify apple receipts in the API.
The iOS app is simply POSTing the apple receipt with Content-Type: application/octet-stream.
What I now need to do, is forward that to Apple for verification.
code:
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);
}
});
output:
<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 }
Thanks!
req.body is already a Buffer, so you'd just need to do var receipt = req.body.toString('base64');
However, I think the main problem is that you're passing false as the second argument to verifyReceipt(). The second argument should be true if receipt is already a base64-encoded string. So I think what is happening currently is it is double-encoding the data.
My problem was with verifyAutoRenewReceipt
Working code:
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);
}
});