Skip to content

Instantly share code, notes, and snippets.

@satiani
Created May 27, 2014 12:02
Show Gist options
  • Save satiani/925bf6acb2db11d8c617 to your computer and use it in GitHub Desktop.
Save satiani/925bf6acb2db11d8c617 to your computer and use it in GitHub Desktop.
define(['jquery',
'underscore',
'balanced',
'lib/forge/cipher',
'lib/forge/aes',
'lib/forge/md5',
'lib/forge/util'], function ($, _, balanced, _Cipher, _CipherAES, _CipherMD5, _CipherUtil) {
var forge = {},
Cipher = _Cipher(forge),
CipherAES = _CipherAES(forge),
CipherMD5 = _CipherMD5(forge),
CipherUtil = _CipherUtil(forge);
var Propay = function (credential_id, temp_token, payer_id, pmi_url) {
this.credential_id = credential_id;
this.payer_id = payer_id;
this.temp_token = temp_token;
this.pmi_url = pmi_url;
var md = CipherMD5.create();
md.update(this.temp_token);
this.key = md.digest().toHex();
};
_.extend(Propay.prototype, {
// converts balanced's strings to propay
getPaymentTypeId : function (card_number) {
var cardType = balanced.card.cardType(card_number);
switch (cardType) {
case 'American Express':
return 'AMEX';
case 'Mastercard':
return 'MasterCard';
case 'VISA':
return 'Visa';
case 'Discover Card':
return 'Discover';
default:
throw "Invalid card type";
}
},
createPaymentMethod : function ($form, responseHandler) {
var card_number = $form.find('[data-propay="number"]').val(),
exp_month = $form.find('[data-propay="exp-month"]').val(),
exp_year = $form.find('[data-propay="exp-year"]').val(),
cvc = $form.find('[data-propay="cvc"]').val();
var settings = {
AuthToken : this.temp_token,
PayerId : this.payer_id,
PaymentProcessType : 'CreditCard',
ProcessMethod : 'None',
PaymentMethodStorageOption : 'Always',
PaymentTypeId : this.getPaymentTypeId(card_number),
CardNumber : card_number,
ExpMonth : exp_month,
ExpYear : exp_year,
CVV : cvc
}
var name_value_str = $.param(settings);
var cipher = Cipher.createCipher('AES-CBC', this.key);
cipher.start({iv: this.key});
cipher.update(CipherUtil.createBuffer(name_value_str));
cipher.finish();
var encrypted64 = CipherUtil.encode64(cipher.output.bytes());
$.ajax({
url : this.pmi_url,
method : 'POST',
dataType : 'jsonp',
data : {
CID : this.credential_id,
SettingsCipher : encrypted64
}
}).done(function (response) {
console.log(response);
});
},
});
return Propay;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment