Skip to content

Instantly share code, notes, and snippets.

@spaomalley
Last active December 20, 2015 12:49
Show Gist options
  • Save spaomalley/6133573 to your computer and use it in GitHub Desktop.
Save spaomalley/6133573 to your computer and use it in GitHub Desktop.
Make a payment using stored card information for a customer. NOTE:: The CVV must be captured per transaction.
function demoAPI(){
var DA = new API_Demo();
DA.setTokens();
DA.storedCardPayment();
}
function OAuthObject(linkObject, tokenObject, idObject, queryParameters){
this.linkObject = linkObject;
this.credentialObject = getCredentials();
this.tokenObject = tokenObject;
this.idObject = idObject;
this.queryParameters = queryParameters;
this.parameterAggregate = [];
this.authorization_Headers = '';
this.OAuthParams = function(){
params = {};
if(this.queryParameters){
for(name in this.queryParameters){
params[name] = this.queryParameters[name];
}
}
params['oauth_consumer_key'] = this.credentialObject.consumerKey;
params['oauth_nonce'] = generateNonce();
params['oauth_signature_method'] = 'HMAC-SHA1';
params['oauth_timestamp'] = generateTimestamp();
if(this.tokenObject){
params['oauth_token'] = this.tokenObject.oauth_token;
}
params['oauth_version'] = '1.0';
return this.lexicograph(params);
function generateNonce(){
return parseInt( new Date().getTime() / 1000);
}
function generateTimestamp(){
return new Date().getTime();
}
}
this.determineParameters = function(){
var paramAggregate;
paramAggregate = this.OAuthParams();
this.parameterAggregate = paramAggregate;
return paramAggregate;
}
this.getURL = function() {
var temp = this.linkObject.url;
if(this.idObject){
temp = this.linkObject.url.replace("INITIAL_ID", this.idObject.id);
this.linkObject.url = temp;
}
return this.linkObject;
}
this.assembleBaseString = function(){
basestring = '';
basestring += encodeURIComponent(this.linkObject.method) + "&";
basestring += encodeURIComponent(this.linkObject.url) + "&";
basestring += encodeURIComponent(normalizeString(this.parameterAggregate));
return basestring;
function normalizeString(normPieces){
normString = '';
count = 0;
pieceCount = 0;
for (x in normPieces){
count++;
}
for (x in normPieces){
if(pieceCount == count - 1){
normString += x + "=" + normPieces[x];
}else{
normString += x + "=" + normPieces[x] + "&";
}
pieceCount++;
}
return normString;
}
}
this.makeBaseString = function(){
this.getURL();
return this.assembleBaseString();
}
this.makeKey = function(){
var keysig = encodeURIComponent(_2LegCSHash(this.credentialObject.consumerSecret)) + "&";
if(this.tokenObject){
keysig += encodeURIComponent(this.tokenObject.oauth_token_secret);
}
return keysig;
function _2LegCSHash(inputString){
var sha = CryptoJS.SHA1(inputString);
return sha.toString(CryptoJS.enc.Base64);
}
}
this.recaptureParameters = function(){
var actualParameters = this.determineParameters();
actualParameters['oauth_signature'] = generateHMAC_SHA1Hash(this.makeBaseString(), this.makeKey());
return actualParameters;
function generateHMAC_SHA1Hash(basestring,signatureKey){
var s = CryptoJS.HmacSHA1( basestring, signatureKey);
var sig = CryptoJS.enc.Base64.stringify(s);
return sig;
}
}
this.setOAuthHeaders = function(){
var headers = "OAuth " + this.headerCreate();
this.authorization_Headers = headers;
}
this.headerCreate = function(){
return this.concatHeader(this.NVPConvert(this.lexicograph(this.recaptureParameters())));
}
this.NVPConvert = function(arrayPieces){
var chunks = [];
for ( var key in arrayPieces){
chunks.push (key + "=" + arrayPieces[key]);
}
return chunks;
}
this.lexicograph = function(inputArr, sort_flags){
var tmp_arr = {},
keys = [],
sorter, i, k, that = this,
strictForIn = false,
populateArr = {};
switch (sort_flags) {
case 'SORT_STRING':
// compare items as strings
sorter = function (a, b) {
return that.strnatcmp(a, b);
};
break;
case 'SORT_LOCALE_STRING':
// compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
var loc = this.i18n_loc_get_default();
sorter = this.php_js.i18nLocales[loc].sorting;
break;
case 'SORT_NUMERIC':
// compare items numerically
sorter = function (a, b) {
return ((a + 0) - (b + 0));
};
break;
// case 'SORT_REGULAR': // compare items normally (don't change types)
default:
sorter = function (a, b) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric) {
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
} else if (aNumeric && !bNumeric) {
return 1;
} else if (!aNumeric && bNumeric) {
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
};
break;
}
// Make a list of key names
for (k in inputArr) {
if (inputArr.hasOwnProperty(k)) {
keys.push(k);
}
}
keys.sort(sorter);
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
this.php_js.ini = this.php_js.ini || {};
// END REDUNDANT
strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js.ini['phpjs.strictForIn'].local_value !== 'off';
populateArr = strictForIn ? inputArr : populateArr;
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
k = keys[i];
tmp_arr[k] = inputArr[k];
if (strictForIn) {
delete inputArr[k];
}
}
for (i in tmp_arr) {
if (tmp_arr.hasOwnProperty(i)) {
populateArr[i] = tmp_arr[i];
}
}
return strictForIn || populateArr;
}
this.concatHeader = function(normPieces){
var headerString = '';
for(i = 0; i <= normPieces.length - 1; i++){
if (i == normPieces.length - 1){
headerString += normPieces[i];
}
else{
headerString += normPieces[i] + ",";
}
}
return headerString;
}
this.makeHTTPObject = function(){
try {return new XMLHttpRequest();}
catch (error) {}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Could not create HTTP request object.");
}
this.sendRequest = function(json){
httpCall = this.makeHTTPObject();
queryAppendArray = [];
queryAppendString = '';
if(this.queryParameters){
for(name in this.queryParameters) {
// console.log(name);
// console.log(this.queryParameters[name]);
queryAppendArray.push( name + "=" + this.queryParameters[name]);
}
queryAppendString = queryAppendArray.join("&");
this.linkObject.url = this.linkObject.url + "?" + queryAppendString;
}
httpCall.open(this.linkObject.method, this.linkObject.url, false);
httpCall.setRequestHeader("Authorization", this.authorization_Headers);
httpCall.setRequestHeader("Accept", "application/json");
httpCall.setRequestHeader("Content-Length", "0");
if(json != null){
httpCall.setRequestHeader("Content-Type", "application/json");
}
httpCall.send(json);
return httpCall;
}
}
function API_Demo(){
this.tokenObject={};
this.idObject={};
this.endPoints = getEndpoints();
this.setIdObject = function(id){
this.idObject = {
"id" : id
};
}
this.captureResourceId = function (location){
return location.substr(location.lastIndexOf('/') + 1, location.length);
}
this.setTokenData = function (response){
this.tokenObject = {
"oauth_token": response.oauth_token,
"oauth_token_secret": response.oauth_token_secret
};
}
this.getAPIResponse = function(object, json){
object.setOAuthHeaders();
return object.sendRequest(json);
}
this.setTokens = function() {
rt = new OAuthObject(this.endPoints.requestToken);
httpResponse = this.getAPIResponse(rt);
this.setTokenData(JSON.parse(httpResponse.responseText));
at = new OAuthObject(this.endPoints.accessToken, this.tokenObject);
httpResponse = this.getAPIResponse(at);
this.setTokenData(JSON.parse(httpResponse.responseText));
}
this.postCustomer = function() {
customer = new Object();
customer.name = "john";
postCustomer = new OAuthObject(this.endPoints.postCustomer, this.tokenObject);
httpResponse = this.getAPIResponse(postCustomer, JSON.stringify(customer));
id = this.captureResourceId(httpResponse.getResponseHeader('Location'))
this.setIdObject(id);
return id;
}
this.postCustomerCardAccount = function(){
cardAccount = new Object();
cardAccount.number = "4444555566667777";
cardAccount.expiryMonth = "07";
cardAccount.expiryYear = "2020";
cardAccount.cvv = "180";
cardAccount.avsZip = "12345";
queryParameters = new Object();
queryParameters.echo = 'true';
postCustomerCardAccount = new OAuthObject(this.endPoints.postCustomerCardAccount, this.tokenObject, this.idObject, queryParameters);
httpResponse = this.getAPIResponse(postCustomerCardAccount, JSON.stringify(cardAccount));
return httpResponse.responseText;
}
this.postPayment = function (){
Payment = new Object();
Payment.merchantId = getMerchantId() ;
Payment.tenderType = "Card";
Payment.amount = ".01";
Payment.cardAccount = new Object();
Payment.cardAccount.number = "4444555566667777";
Payment.cardAccount.expiryMonth = "07";
Payment.cardAccount.expiryYear = "2020";
Payment.cardAccount.cvv = "180";
Payment.cardAccount.avsZip = "30303";
echo = true;
postPayment = new OAuthObject(this.endPoints.postPayment, this.tokenObject, null, echo);
httpResponse = this.getAPIResponse(postPayment, JSON.stringify(Payment));
paymentId = this.captureResourceId(httpResponse.getResponseHeader('Location'));
this.setIdObject(paymentId);
}
this.getPaymentById = function(){
getPaymentById = new OAuthObject(this.endPoints.getPaymentById, this.tokenObject, this.idObject);
httpResponse = this.getAPIResponse(getPaymentById);
console.log(httpResponse);
}
this.storedCardPayment = function(){
customerId = this.postCustomer();
cardAccountData = JSON.parse(this.postCustomerCardAccount());
CVV = '180';
Payment = new Object();
Payment.merchantId = getMerchantId() ;
Payment.tenderType = "Card";
Payment.amount = ".01";
Payment.cardAccount = new Object();
Payment.cardAccount.id = cardAccountData.id;
Payment.cardAccount.created = cardAccountData.created;
Payment.cardAccount.hash = cardAccountData.hash;
Payment.cardAccount.cardType = cardAccountData.cardType;
Payment.cardAccount.last4 = cardAccountData.last4;
Payment.cardAccount.expiryMonth = cardAccountData.expiryMonth;
Payment.cardAccount.expiryYear = cardAccountData.expiryYear;
Payment.cardAccount.avsZip = cardAccountData.avsZip;
Payment.cardAccount.cvv = CVV;
queryParameters = new Object();
queryParameters.echo = 'true';
queryParameters.customerId = customerId;
queryParameters.id = cardAccountData.id;
postCardAccountToken = new OAuthObject(this.endPoints.postCardAccountToken, this.tokenObject, null, queryParameters);
httpResponse = this.getAPIResponse(postCardAccountToken, JSON.stringify(Payment));
paymentId = this.captureResourceId(httpResponse.getResponseHeader('Location'));
console.log(paymentId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment