I used to have Angular Stripe Checkout form and I am trying to update my form to the new Stripe Card Elements which was introduced recently.
After removing the Form Input fields and replacing them with the Stripe Card element, my form looks like this:
<form name="payment" ng-submit="vm.submit()">
<div class="row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
<button class="btn btn-primary" type="submit" ng-disabled="vm.submitting">Pay!</button>
<div ng-show="vm.cardError" class="row">
<div class="has-error">
<p class="help-block">* {{vm.cardError}}</p>
</div>
</div>
</form>
Previously in Angular, when the form is submitted, I was handling the submit()
and stripeResponseHandler
from the controller. After updating my Angular Controller with the new changes, my controller now looks like this:
function PaymentController() {
var vm = this;
var elements = stripe.elements();
var style = {
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: 'Helvetica Neue',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
vm.card = elements.create('card', {style: style});
vm.card.mount('#card-element');
// Handle real-time validation errors from the card Element.
vm.card.addEventListener('change', function(event) {
if (event.error) {
vm.cardError = event.error.message;
} else {
vm.cardError = '';
}
});
function submit() {
vm.cardError = '';
vm.submitting = true;
createToken();
}
// Send data directly to stripe
function createToken() {
stripe.createToken(vm.card).then(function(result) {
if (result.error) {
vm.cardError = result.error.message;
vm.submitting = false;
} else {
// Send the token to your server
stripeTokenHandler(result.token);
}
});
}
// Response Handler callback to handle the response from Stripe server
function stripeTokenHandler(token) {
vm.tokenData = {
token: token.id
};
.. Process the rest in server ...
}
}
The above code works as is. But I am confused on these:
1) Since Stripe now uses DOM Manipulation to insert the card elements inside the form, does that make my above method wrong as in Angular way? Meaning, should I not be doing these in Controller anymore and move them to a directive instead? Or should that not be necessary since the element manipulated is using stripe.elements()
.
2) If I do need to have them inside directive, I am just not sure how to convert the above to an angular directive. It first manipulates the element by mounting it (which can be added to the directive link function), but later it goes on to use the card
element for form submit and event handlers. Do I need to do all this inside the directive link itself of have the submits inside directive controller and the element manipulation in link?
I am so confused and stuck on what to do here. Can someone give me a sample on how I can address this if I am doing it wrong please?
I am using Angular 1.5.