1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-26 20:54:21 +01:00

handle 2nd confirmation on server + display confirmation modal on the client

This commit is contained in:
Sylvain 2019-09-05 17:17:51 +02:00
parent 12447698f8
commit 510533e080
3 changed files with 39 additions and 9 deletions

View File

@ -1,7 +1,7 @@
/* global Stripe */ /* global Stripe */
Application.Directives.directive('stripeForm', ['Payment', Application.Directives.directive('stripeForm', ['Payment', 'growl',
function (Payment) { function (Payment, growl) {
return ({ return ({
restrict: 'A', restrict: 'A',
link: function($scope, element, attributes) { link: function($scope, element, attributes) {
@ -47,18 +47,46 @@ Application.Directives.directive('stripeForm', ['Payment',
// TODO https://stripe.com/docs/payments/payment-intents/web-manual // TODO https://stripe.com/docs/payments/payment-intents/web-manual
stripe.createPaymentMethod('card', card).then(function({ paymentMethod, error }) { stripe.createPaymentMethod('card', card).then(function({ paymentMethod, error }) {
if (error) { if (error) {
// Show error in payment form growl.error(error);
} else { } else {
// Send paymentMethod.id to your server (see Step 2) // Send paymentMethod.id to your server (see Step 2)
Payment.confirm({ payment_method_id: paymentMethod.id }).then(function (response) { Payment.confirm({ payment_method_id: paymentMethod.id }, function (response) {
// Handle server response (see Step 3) // Handle server response (see Step 3)
handleServerResponse(response);
});
}
});
});
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
Payment.confirm({ payment_intent_id: result.paymentIntent.id }, function(confirmResult) {
paymentSuccess(confirmResult);
}).then(handleServerResponse);
}
});
} else {
paymentSuccess(response);
}
}
function paymentSuccess(response) {
// TODO Show success message
$scope.$apply(function () { $scope.$apply(function () {
$scope[form].apply($scope, response); $scope[form].apply($scope, response);
}); });
});
} }
});
});
} }
}); });
}]); }]);

View File

@ -1,4 +1,4 @@
<div> <div xmlns:stripe="http://www.w3.org/1999/xhtml">
<div class="modal-header"> <div class="modal-header">
<img ng-src="{{logoBlack.custom_asset_file_attributes.attachment_url}}" alt="{{logo.custom_asset_file_attributes.attachment}}" class="modal-logo"/> <img ng-src="{{logoBlack.custom_asset_file_attributes.attachment_url}}" alt="{{logo.custom_asset_file_attributes.attachment}}" class="modal-logo"/>
<h1 translate>{{ 'online_payment' }}</h1> <h1 translate>{{ 'online_payment' }}</h1>

View File

@ -10,6 +10,8 @@ class API::PaymentsController < API::ApiController
begin begin
if data['payment_method_id'] if data['payment_method_id']
# Create the PaymentIntent # Create the PaymentIntent
# TODO the client has to provide the reservation details. Then, we use Price.compute - user.walletAmount to get the amount
# currency is set in Rails.secrets
intent = Stripe::PaymentIntent.create( intent = Stripe::PaymentIntent.create(
payment_method: data['payment_method_id'], payment_method: data['payment_method_id'],
amount: 1099, amount: 1099,
@ -22,7 +24,7 @@ class API::PaymentsController < API::ApiController
end end
rescue Stripe::CardError => e rescue Stripe::CardError => e
# Display error on client # Display error on client
return [200, { error: e.message }.to_json] render status: 200, json: { error: e.message }
end end
render generate_payment_response(intent) render generate_payment_response(intent)