|
| 1 | +var assert = require('assert'); |
| 2 | +var https = require('../https'); |
| 3 | + |
| 4 | +var apiUrl = { |
| 5 | + production: 'https://appstore-sdk.amazon.com/version/1.0/verifyReceiptId/developer/' |
| 6 | +}; |
| 7 | + |
| 8 | +exports.verifyPayment = function (payment, cb) { |
| 9 | + try { |
| 10 | + assert.equal(typeof payment.secret, 'string', 'Shared secret must be a string'); |
| 11 | + assert.equal(typeof payment.userId, 'string', 'User ID must be a string'); |
| 12 | + assert.equal(typeof payment.receipt, 'string', 'Receipt must be a string'); |
| 13 | + } catch (error) { |
| 14 | + return process.nextTick(function () { |
| 15 | + cb(error); |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + var requestUrl = apiUrl.production + payment.secret + '/user/' + payment.userId + '/receiptId/' + payment.receipt; |
| 20 | + |
| 21 | + https.get(requestUrl, null, function (error, res, resultString) { |
| 22 | + if (error) { |
| 23 | + return cb(error); |
| 24 | + } |
| 25 | + |
| 26 | + var resultObject = null; |
| 27 | + |
| 28 | + try { |
| 29 | + resultObject = JSON.parse(resultString); |
| 30 | + } catch (error) { |
| 31 | + return cb(error); |
| 32 | + } |
| 33 | + |
| 34 | + if (res.statusCode === 400) { |
| 35 | + return cb(new Error('receiptId is invalid, or no transaction was found for this receiptId')); |
| 36 | + } |
| 37 | + |
| 38 | + if (res.statusCode === 496 ) { |
| 39 | + return cb(new Error('Invalid sharedSecret')); |
| 40 | + } |
| 41 | + |
| 42 | + if (res.statusCode === 497) { |
| 43 | + return cb(new Error('Invalid User ID')); |
| 44 | + } |
| 45 | + |
| 46 | + if (res.statusCode === 500) { |
| 47 | + return cb(new Error('There was an Internal Servor Error')); |
| 48 | + } |
| 49 | + |
| 50 | + if (res.statusCode !== 200) { |
| 51 | + return cb(new Error('Unknown operation exception')); |
| 52 | + } |
| 53 | + |
| 54 | + cb(null, { |
| 55 | + receipt: resultObject, |
| 56 | + transactionId: resultObject.receiptId, |
| 57 | + productId: resultObject.productId |
| 58 | + }); |
| 59 | + }); |
| 60 | +}; |
0 commit comments