Last active
September 3, 2015 12:58
-
-
Save malixsys/11275393 to your computer and use it in GitHub Desktop.
Beanstream with Angular and Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/* | |
4030000010001234 | |
Approved | |
4003050500040005 | |
Declined | |
*/ | |
(function(base) { | |
base.submitPayment = function(info, done) { | |
var qs = require('qs'); | |
var message = qs.stringify(info); | |
console.log('[BEANSTREAM] Requesting \n %s', message); | |
var req_options = { | |
host: 'www.beanstream.com', | |
method: 'POST', | |
path: '/scripts/process_transaction.asp', | |
headers: { | |
'Host': 'www.beanstream.com', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': message.length, | |
'Accept':'application/json' | |
} | |
}; | |
var req = require('https').request(req_options, function(res){ | |
res.on('data', function(d){ | |
var body = d.toString(); | |
if(body.indexOf('&') === -1) { | |
console.log('[BEANSTREAM] Invalid Request %j', body); | |
return done('Invalid request'); | |
} | |
var json = qs.parse(body); | |
if(json.trnApproved === '1') { | |
console.log('[BEANSTREAM] Transaction Succeeded %j', json); | |
done(null, json); | |
} else { | |
console.log('[BEANSTREAM] Transaction Failed %j', json); | |
done('Transaction_Failed_' + json.messageText, json); | |
} | |
}); | |
}); | |
req.write(message); | |
req.end(); | |
req.on('error', function request_error(e) { | |
console.log('[BEANSTREAM] Transaction Failed ', e); | |
done('Transaction_Failed'); | |
}); | |
}; | |
})(exports); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('app') | |
.factory('PaymentService', ['$http', '$q', 'Scripts', function($http, $q, Scripts) { | |
function parseExpiry(value) { | |
var month, prefix, year, _ref; | |
value = value || ''; | |
value = value.replace(/\s/g, ''); | |
_ref = value.split('/', 2), month = _ref[0], year = _ref[1]; | |
if ((year !== null ? year.length : void 0) === 2 && /^\d+$/.test(year)) { | |
prefix = (new Date()).getFullYear(); | |
prefix = prefix.toString().slice(0, 2); | |
year = prefix + year; | |
} | |
month = parseInt(month, 10); | |
year = parseInt(year, 10); | |
return { | |
month: month, | |
year: year | |
}; | |
} | |
var types = { | |
'beanstream': function() { | |
return function(info) { | |
var expiry = parseExpiry(info.expiry); | |
var options = { | |
number: info.number, | |
cvc: info.cvc, | |
exp_month: expiry.month, | |
exp_year: expiry.year, | |
address_zip: info.postal, | |
name: info.name | |
}; | |
var deferred = $q.defer(); | |
/* | |
4030000010001234 | |
Approved | |
4003050500040005 | |
Declined | |
*/ | |
var data = | |
{ | |
number: info.number, | |
expiry_month: expiry.month, | |
expiry_year: expiry.year > 2000 ? expiry.year - 2000 : expiry.year, | |
cvd: parseInt(info.cvc,10) | |
}; | |
$http({ | |
method: 'POST', | |
url: 'https://www.beanstream.com/scripts/tokenization/tokens', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
'Accept': '*/*' | |
}, | |
transformRequest: function transformRequest(obj) { | |
delete obj.language; | |
var str = JSON.stringify(obj); | |
console.log(str); | |
return str; | |
}, | |
data: data | |
}). | |
then(function(response) { | |
console.log(response.data); | |
if (response.data && response.data.code === 1) { | |
deferred.resolve({ | |
id: response.data.token, | |
provider: 'beanstream' | |
}); | |
} else { | |
deferred.reject({msg: response.data.message}); | |
} | |
}, function(err) { | |
console.log(err); | |
deferred.reject({msg: err}); | |
}); | |
return deferred.promise; | |
}; | |
} | |
} | |
; | |
return { | |
getService: function(type, stripe) { | |
return types[type](stripe); | |
} | |
}; | |
}]) | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var charge = { | |
trnAmount: cart.total.toFixed(2), | |
requestType: 'BACKEND', | |
trnType: 'P', | |
merchant_id: app.config.beanstream.merchantId, | |
passCode: app.config.beanstream.passcode, | |
singleUseToken: info.token, | |
trnOrderNumber: data.unique_id, | |
trnCardOwner: info.name | |
} | |
var beanstream = require('./beanstream'); | |
beanstream.submitPayment(charge, function (err, data) { | |
//data: {"trnApproved":"1","trnId":"10000006","messageId":"1","messageText":"Approved","trnOrderNumber"... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment