Last active
August 16, 2016 09:01
-
-
Save fathermerry/600bcfb03ffbdb3e3687ae54324e54dd to your computer and use it in GitHub Desktop.
New Cards Module
This file contains 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 CardForm = function(transaction) { | |
this.transaction = transaction; | |
this.attemptedAuthentication = false; | |
function _encryptPin(pin) { | |
var encrypted_pin = cryptico.encrypt(pin, config.publishable_key); | |
return encrypted_pin.cipher | |
} | |
function _getFields(paymentForm) { | |
return new Promise(function(resolve, reject) { | |
var cardField = paymentform.find('[data-paystack="number"]')[0]; | |
if (cardField) { | |
cardNumber = cardField.value; | |
cardNumber = cardNumber.replace(/\s+/g, ''); | |
} | |
var cvcField = paymentform.find('[data-paystack="cvc"]')[0]; | |
if (cvcField) cvc = cvcField.value; | |
var expiryField = paymentform.find('[data-paystack="exp"]')[0]; | |
if (expiryField) { | |
expiry = $.payment.cardExpiryVal(expiryField.value); | |
expMonth = expiry.month; | |
expYear = expiry.year; | |
} | |
resolve(cardNumber, cvc, expMonth, expYear); | |
}) | |
} | |
function _validateCardValues(cardNumber, cvc, expMonth, expYear) { | |
var response = {}; | |
return new Promise(function() { | |
var cardVal = ($.payment.validateCardNumber(cardNumber)); | |
var cardType = $.payment.cardType(cardNumber); | |
var expVal = ($.payment.validateCardExpiry(expMonth, expYear)); | |
if (cardVal == false) { | |
response.message = 'Invalid Card Number'; | |
response.field = 'cardnumber'; | |
reject(response); | |
} else if (expiryval == false) { | |
response.message = 'Invalid Expiry Date'; | |
response.field = 'expiry'; | |
reject(response); | |
} else if (cvc == 0) { | |
response.message = 'Invalid CVV'; | |
if (cvc.length != 3) { | |
response.message = 'CVV must be 3 digits'; | |
} | |
response.field = 'cvv'; | |
reject(response); | |
} else { | |
resolve(cardNumber, cvc, expMonth, expYear); | |
} | |
}) | |
} | |
function _tokenizeCard(cardNumber, cvc, expMonth, expYear) { | |
return new Promise(function(resolve, reject) { | |
var card = new Array(4); | |
card[0] = cardNumber; | |
card[1] = cvc; | |
card[2] = expMonth; | |
card[3] = expYear; | |
var cardString = card.join("*") | |
var enc = cryptico.encrypt(cardString, config.publishableKey); | |
var clientData = (enc.cipher); | |
var last4 = cardNumber.substring(cardNumber.length - 4); | |
return [clientData, last4]; | |
}) | |
} | |
function _makeCallToServer(response) { | |
return new Promise(function(resolve, reject) { | |
params.clientData = response[0]; | |
params.last4 = response[1]; | |
$.ajax({ | |
type: 'POST', | |
url: config.paymentBaseUrl + 'charge/', | |
data: params, | |
dataType: 'json', | |
success: function(response) { | |
resolve(response); | |
}, | |
error: function(error) { | |
reject(error); | |
} | |
}); | |
}) | |
} | |
return { | |
createToken: function(paymentForm) { | |
return _getFields(paymentForm) | |
.then(_validateCardValues) | |
.then(_tokenizeCard); | |
}, | |
sendCharge: function(params) { | |
return this.createToken($('#paystack-form')) | |
.then(_makeCallToServer) | |
.catch(function(error) { | |
UI.showError.paymentError(error); | |
}); | |
}, | |
validateCharge: function(token) { | |
$.ajax({ | |
type: 'POST', | |
url: config.paymentBaseUrl + 'charge/validate', | |
data: { | |
trans: this.transaction.id, | |
token: token | |
}, | |
dataType: 'json', | |
success: function(response) { | |
this.handleResponse(response); | |
}, | |
error: function(error) { | |
UI.showError.paymentError(error); | |
} | |
}); | |
}, | |
charge: function() { | |
this.sendCharge({ | |
trans: this.transaction.id, | |
device: this.transaction.device | |
}).then(function(response) { | |
this.handleResponse(response); | |
}) | |
}, | |
chargeWithPin: function(pin, retrial) { | |
this.handle = _encryptPin(pin); | |
this.sendCharge({ | |
trans: this.transaction.id, | |
device: this.transaction.device, | |
handle: this.handle | |
}).then(function(response) { | |
this.handleResponse(response); | |
}) | |
}, | |
resendToken: function() { | |
this.sendCharge({ | |
trans: this.transaction.id, | |
device: this.transaction.device, | |
handle: this.handle | |
}) | |
}, | |
closeSecurecode: function(response) { | |
UI.closeSecurecode(); | |
this.handleResponse(response); | |
}, | |
handleResponse: function(response) { | |
if (response.status == 'success') { | |
paystackInstance.showSuccess(response); | |
return; | |
} | |
if (response.auth) { | |
if (this.attemptedAuthentication == response.auth) { | |
UI.showError.paymentError(response); | |
} | |
this.authenticateCard(response); | |
return; | |
} | |
UI.showError.paymentError(); | |
}, | |
authenticateCard: function(response) { | |
this.attemptedAuthentication = response.auth; | |
switch (response.auth) { | |
case "pin": | |
UI.setupPinEntry(); | |
Analytics.track('auth', 'pin'); | |
break; | |
case "otp": | |
UI.setupOTPEntry(); | |
Analytics.track('auth', 'otp'); | |
break; | |
case "3DS": | |
UI.setup3DSEntry(response.otpmessage); | |
Analytics.track('auth', '3DS'); | |
break; | |
default: | |
UI.showError.paymentError(response); | |
break; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment