refactored the code a bit to use new https request library

This commit is contained in:
Almir Kadric
2014-08-28 07:56:13 +00:00
parent e64281bcf0
commit ce778d0628
3 changed files with 36 additions and 53 deletions

View File

@@ -1,9 +1,9 @@
var assert = require('assert');
var https = require('https');
var https = require('../https');
var hosts = {
sandbox: 'sandbox.itunes.apple.com',
production: 'buy.itunes.apple.com'
var apiUrls = {
sandbox: 'https://sandbox.itunes.apple.com/verifyReceipt',
production: 'https://buy.itunes.apple.com/verifyReceipt'
};
var responses = {
@@ -42,47 +42,22 @@ function parseResult(result) {
}
function verify(data, host, cb) {
var options = {
method: 'POST',
hostname: host,
port: 443,
path: '/verifyReceipt',
headers: {
'content-type': 'text/plain',
'content-length': Buffer.byteLength(data)
function verify(environmentUrl, options, cb) {
https.post(environmentUrl, options, function (error, resultString) {
if (error) {
return cb(error);
}
};
var req = https.request(options, function (res) {
res.setEncoding('utf8');
var resultObject;
var data = '';
try {
resultObject = parseResult(resultString);
} catch (error) {
return cb(error);
}
res.on('data', function (str) {
data += str;
});
res.on('end', function () {
if (res.statusCode !== 200) {
return cb(new Error('Received status code: ' + res.statusCode));
}
var result;
try {
result = parseResult(data);
} catch (error) {
return cb(error);
}
cb(null, result);
});
cb(null, resultObject);
});
req.on('error', cb);
req.end(data);
}
@@ -92,20 +67,16 @@ function isBase64like(str) {
exports.verifyPayment = function (payment, cb) {
var data;
var jsonData = {};
try {
assert.equal(typeof payment.receipt, 'string', 'Receipt must be a string');
data = {};
if (isBase64like(payment.receipt)) {
data['receipt-data'] = payment.receipt;
jsonData['receipt-data'] = payment.receipt;
} else {
data['receipt-data'] = (new Buffer(payment.receipt, 'utf8')).toString('base64');
jsonData['receipt-data'] = (new Buffer(payment.receipt, 'utf8')).toString('base64');
}
data = JSON.stringify(data);
} catch (error) {
return process.nextTick(function () {
cb(error);
@@ -113,7 +84,7 @@ exports.verifyPayment = function (payment, cb) {
}
function checkReceipt(error, result) {
function checkReceipt(error, resultString) {
if (error) {
return cb(error);
}
@@ -132,13 +103,12 @@ exports.verifyPayment = function (payment, cb) {
}
return verify(data, hosts.production, function (error, result) {
verify(apiUrls.production, { json: jsonData }, function (error, resultString) {
// 21007: this is a sandbox receipt, so take it there
if (error && error.status === 21007) {
return verify(data, hosts.sandbox, checkReceipt);
return verify(apiUrls.sandbox, { json: jsonData }, checkReceipt);
}
return checkReceipt(error, result);
return checkReceipt(error, resultString);
});
};

View File

@@ -25,7 +25,13 @@ exports.verifyPayment = function (payment, cb) {
return cb(error);
}
var requestUrl = apiUrls.purchasesProductsGet(payment.packageName, payment.productId, payment.receipt, requestToken.access_token);
var requestUrl = apiUrls.purchasesProductsGet(
payment.packageName,
payment.productId,
payment.receipt,
requestToken.access_token
);
https.get(requestUrl, null, function (error, responseString) {
if (error) {
return cb(error);

View File

@@ -15,6 +15,13 @@ module.exports = function (url, options, cb) {
'content-type': 'application/x-www-form-urlencoded',
'content-length': Buffer.byteLength(data)
};
} else if (data.json) {
data = JSON.stringify(data.json);
delete options.json;
options.headers = {
'content-type': 'application/json',
'content-length': Buffer.byteLength(data)
};
}
} catch (error) {
cb(error);