Skip to content
This repository was archived by the owner on Sep 24, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ And then:

Basically the directive sends the credit card details directly to stripe, which then returns a token that you can use to charge the card, subscribe a user or to do other things. This ensures that the card details themselves never hit your backend and thus you have to worry a little bit less.

### flexible submit button

In default behavior, submit button will disable during request submit and enable again after finish. Although sometimes user might need more flexible action, like turn off or disable but not enable after stripe return. So try to add new attribute to control submit button disable timing.

<button btn-auto-disabled="{0,1,2}">

* 0: Just use the default behavior, if user did not assign `btn-auto-disabled` would become this option too.
* 1: Do not disable submit button.
* 2: Disable button when user click but did not enable it.

## Example

Expand Down
63 changes: 38 additions & 25 deletions lib/angular-payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ angular.module('angularPayments', []);angular.module('angularPayments')
;angular.module('angularPayments')

.directive('stripeForm', ['$window', '$parse', 'Common', function($window, $parse, Common) {

// directive intercepts form-submission, obtains Stripe's cardToken using stripe.js
// and then passes that to callback provided in stripeForm, attribute.

Expand All @@ -734,13 +734,13 @@ angular.module('angularPayments', []);angular.module('angularPayments')

// filter valid stripe-values from scope and convert them from camelCase to snake_case
_getDataToSend = function(data){
var possibleKeys = ['number', 'expMonth', 'expYear',
'cvc', 'name','addressLine1',

var possibleKeys = ['number', 'expMonth', 'expYear',
'cvc', 'name','addressLine1',
'addressLine2', 'addressCity',
'addressState', 'addressZip',
'addressCountry']

var camelToSnake = function(str){
return str.replace(/([A-Z])/g, function(m){
return "_"+m.toLowerCase();
Expand All @@ -751,7 +751,9 @@ angular.module('angularPayments', []);angular.module('angularPayments')

for(i in possibleKeys){
if(possibleKeys.hasOwnProperty(i)){
ret[camelToSnake(possibleKeys[i])] = angular.copy(data[possibleKeys[i]]);
if (data[possibleKeys[i]] !== undefined) {
ret[camelToSnake(possibleKeys[i])] = angular.copy(data[possibleKeys[i]]);
}
}
}

Expand All @@ -769,43 +771,54 @@ angular.module('angularPayments', []);angular.module('angularPayments')
}

var form = angular.element(elem);
var isProcessing = false;

form.bind('submit', function() {

expMonthUsed = scope.expMonth ? true : false;
expYearUsed = scope.expYear ? true : false;

if(!(expMonthUsed && expYearUsed)){
exp = Common.parseExpiry(scope.expiry)
scope.expMonth = exp.month
scope.expYear = exp.year
if (isProcessing === true) return;

exp = Common.parseExpiry(scope.expiry)
scope.expMonth = exp.month
scope.expYear = exp.year

var button = form.find('button[type=submit]');
// btn-auto-disabled
// 0: disable button, 1: do not disable button, 2: keep disable
var autoDisable = ~~button.attr('btn-auto-disabled');
if (autoDisable === 0 || autoDisable === 2) {
button.prop('disabled', true);
}

var button = form.find('button');
button.prop('disabled', true);
isProcessing = true;

if(form.hasClass('ng-valid')) {


$window.Stripe.createToken(_getDataToSend(scope), function() {
var args = arguments;
scope.$apply(function() {
scope[attr.stripeForm].apply(scope, args);
});
button.prop('disabled', false);

if (autoDisable === 0) {
button.prop('disabled', false);
}
isProcessing = false;
});

} else {
var errorItem = [];
if (form.hasClass('ng-invalid-card')) errorItem.push('Credit Card');
if (form.hasClass('ng-invalid-expiry')) errorItem.push('Expiry');
if (form.hasClass('ng-invalid-cvc')) errorItem.push('CVC');
if (form.hasClass('ng-invalid-required')) errorItem.push('Missing required fields');
var errorMessage = 'Invalid form submitted: ' + errorItem.join(', ');

scope.$apply(function() {
scope[attr.stripeForm].apply(scope, [400, {error: 'Invalid form submitted.'}]);
scope[attr.stripeForm].apply(scope, [400, {error: errorMessage }]);
});
button.prop('disabled', false);
if (autoDisable === 0) {
button.prop('disabled', false);
}
isProcessing = false;
}

scope.expiryMonth = expMonthUsed ? scope.expMonth : null;
scope.expiryYear = expYearUsed ? scope.expMonth : null;

});
}
}
Expand Down
Loading