I had an Angular Stripe Checkout form, and I'm trying to upgrade my form to the new Stripe Card Elements , which was introduced recently.
After deleting 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"> </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>
Earlier in Angular, when the form was submitted, I processed submit() and stripeResponseHandler from the controller. After updating my Angular controller with 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 by this:
1) Since Stripe now uses DOM Manipulation to insert map elements inside the form, does this make my method higher than in Angular? Meaning, should I no longer do this in the controller and instead move them to a directive? Or it should not be necessary, as the managed element uses stripe.elements() .
2) If I need to have them inside a directive, I'm just not sure how to convert the Angular directive above. First, he manipulates the element, mounting it (which can be added to the directory link function), but later on he continues to use the card element to submit forms and event handlers. Should I do all this inside the directory link itself, are there subordinations inside the directory controller and manipulation of elements in the link?
I am so confused and stuck with what to do here. Can someone give me a sample of how I can address this, if I do it wrong, please?
I am using Angular 1.5.