2019-09-04 17:20:18 +02:00
|
|
|
/* global Stripe */
|
|
|
|
|
2019-09-05 17:17:51 +02:00
|
|
|
Application.Directives.directive('stripeForm', ['Payment', 'growl',
|
|
|
|
function (Payment, growl) {
|
2019-09-04 17:20:18 +02:00
|
|
|
return ({
|
|
|
|
restrict: 'A',
|
2019-09-09 17:37:54 +02:00
|
|
|
scope: {
|
|
|
|
cartItems: '='
|
|
|
|
},
|
2019-09-05 16:17:02 +02:00
|
|
|
link: function($scope, element, attributes) {
|
2019-09-04 17:20:18 +02:00
|
|
|
const stripe = Stripe('<%= Rails.application.secrets.stripe_publishable_key %>');
|
|
|
|
const elements = stripe.elements();
|
|
|
|
|
|
|
|
const style = {
|
|
|
|
base: {
|
|
|
|
color: '#32325d',
|
|
|
|
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
|
|
|
|
fontSmoothing: 'antialiased',
|
|
|
|
fontSize: '16px',
|
|
|
|
'::placeholder': {
|
|
|
|
color: '#aab7c4'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
invalid: {
|
|
|
|
color: '#fa755a',
|
|
|
|
iconColor: '#fa755a'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const card = elements.create('card', { style, hidePostalCode: true });
|
|
|
|
|
|
|
|
card.addEventListener('change', function ({ error }) {
|
|
|
|
const displayError = document.getElementById('card-errors');
|
|
|
|
if (error) {
|
|
|
|
displayError.textContent = error.message;
|
|
|
|
} else {
|
|
|
|
displayError.textContent = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add an instance of the card Element into the `card-element` <div>.
|
|
|
|
const form = angular.element(element);
|
|
|
|
const cardElement = form.find('#card-element');
|
|
|
|
card.mount(cardElement[0]);
|
|
|
|
|
2019-09-05 16:17:02 +02:00
|
|
|
form.bind('submit', function() {
|
2019-09-04 17:20:18 +02:00
|
|
|
const button = form.find('button');
|
|
|
|
button.prop('disabled', true);
|
|
|
|
|
|
|
|
// TODO https://stripe.com/docs/payments/payment-intents/web-manual
|
2019-09-05 16:17:02 +02:00
|
|
|
stripe.createPaymentMethod('card', card).then(function({ paymentMethod, error }) {
|
|
|
|
if (error) {
|
2019-09-05 17:17:51 +02:00
|
|
|
growl.error(error);
|
2019-09-05 16:17:02 +02:00
|
|
|
} else {
|
|
|
|
// Send paymentMethod.id to your server (see Step 2)
|
2019-09-09 17:37:54 +02:00
|
|
|
Payment.confirm({ payment_method_id: paymentMethod.id, cart_items: $scope.cartItems }, function (response) {
|
2019-09-05 16:17:02 +02:00
|
|
|
// Handle server response (see Step 3)
|
2019-09-05 17:17:51 +02:00
|
|
|
handleServerResponse(response);
|
2019-09-05 16:17:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-09-04 17:20:18 +02:00
|
|
|
});
|
2019-09-05 17:17:51 +02:00
|
|
|
|
|
|
|
function handleServerResponse(response) {
|
|
|
|
if (response.error) {
|
|
|
|
growl.error(error);
|
|
|
|
} else if (response.requires_action) {
|
|
|
|
// Use Stripe.js to handle required card action
|
|
|
|
stripe.handleCardAction(
|
|
|
|
response.payment_intent_client_secret
|
|
|
|
).then(function(result) {
|
|
|
|
if (result.error) {
|
|
|
|
growl.error(error);
|
|
|
|
} else {
|
|
|
|
// The card action has been handled
|
|
|
|
// The PaymentIntent can be confirmed again on the server
|
2019-09-09 17:37:54 +02:00
|
|
|
Payment.confirm({ payment_intent_id: result.paymentIntent.id, cart_items: $scope.cartItems }, function(confirmResult) {
|
2019-09-05 17:17:51 +02:00
|
|
|
paymentSuccess(confirmResult);
|
|
|
|
}).then(handleServerResponse);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
paymentSuccess(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function paymentSuccess(response) {
|
|
|
|
// TODO Show success message
|
|
|
|
$scope.$apply(function () {
|
|
|
|
$scope[form].apply($scope, response);
|
|
|
|
});
|
|
|
|
}
|
2019-09-04 17:20:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}]);
|