diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index 2bc906e1f..ad1eaa2dd 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -49,7 +49,7 @@ class API::PaymentsController < API::ApiController if params[:cart_items][:reservation] res = on_reservation_success(intent, amount[:details]) elsif params[:cart_items][:subscription] - res = on_subscription_success(intent) + res = on_subscription_success(intent, amount[:details]) end end @@ -84,7 +84,7 @@ class API::PaymentsController < API::ApiController if params[:cart_items][:reservation] res = on_reservation_success(intent, amount[:details]) elsif params[:cart_items][:subscription] - res = on_subscription_success(intent) + res = on_subscription_success(intent, amount[:details]) end end @@ -125,7 +125,7 @@ class API::PaymentsController < API::ApiController end end - def on_subscription_success(intent) + def on_subscription_success(intent, details) @subscription = Subscription.new(subscription_params) user_id = if current_user.admin? || current_user.manager? params[:cart_items][:subscription][:user_id] @@ -134,8 +134,7 @@ class API::PaymentsController < API::ApiController end is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) .pay_and_save(@subscription, - coupon: coupon_params[:coupon_code], - invoice: true, + payment_details: details, payment_intent_id: intent.id, schedule: params[:cart_items][:subscription][:payment_schedule], payment_method: 'stripe') diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index f79017acd..db8c1ce12 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -14,14 +14,13 @@ class API::SubscriptionsController < API::ApiController # Managers can create subscriptions for other users def create user_id = current_user.admin? || current_user.manager? ? params[:subscription][:user_id] : current_user.id - amount = transaction_amount(current_user.admin? || (current_user.manager? && current_user.id != user_id), user_id) + transaction = transaction_amount(current_user.admin? || (current_user.manager? && current_user.id != user_id), user_id) - authorize SubscriptionContext.new(Subscription, amount, user_id) + authorize SubscriptionContext.new(Subscription, transaction[:amount], user_id) @subscription = Subscription.new(subscription_params) is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) - .pay_and_save(@subscription, coupon: coupon_params[:coupon_code], - invoice: true, + .pay_and_save(@subscription, payment_details: transaction[:details], schedule: params[:subscription][:payment_schedule], payment_method: params[:subscription][:payment_method]) @@ -65,7 +64,7 @@ class API::SubscriptionsController < API::ApiController # Subtract wallet amount from total total = price_details[:total] wallet_debit = get_wallet_debit(user, total) - total - wallet_debit + { amount: total - wallet_debit, details: price_details } end def get_wallet_debit(user, total_amount) diff --git a/app/frontend/src/javascript/api/api-client.ts b/app/frontend/src/javascript/api/api-client.ts index ad4aa60f2..eaead8232 100644 --- a/app/frontend/src/javascript/api/api-client.ts +++ b/app/frontend/src/javascript/api/api-client.ts @@ -19,6 +19,13 @@ client.interceptors.response.use(function (response) { }); function extractHumanReadableMessage(error: any): string { + if (error.match(/^/)) { + // parse ruby error pages + const parser = new DOMParser(); + const htmlDoc = parser.parseFromString(error, 'text/html'); + return htmlDoc.querySelector('h2').textContent; + } + if (typeof error === 'string') return error; let message = ''; diff --git a/app/frontend/src/javascript/controllers/admin/members.js b/app/frontend/src/javascript/controllers/admin/members.js index 114502eae..3f105e484 100644 --- a/app/frontend/src/javascript/controllers/admin/members.js +++ b/app/frontend/src/javascript/controllers/admin/members.js @@ -761,7 +761,6 @@ Application.Controllers.controller('EditMemberController', ['$scope', '$state', size: 'lg', controller: ['$scope', '$uibModalInstance', 'Subscription', function ($scope, $uibModalInstance, Subscription) { $scope.new_expired_at = angular.copy(subscription.expired_at); - $scope.scheduled = subscription.scheduled; $scope.free = free; $scope.datePicker = { opened: false, diff --git a/app/frontend/src/javascript/directives/cart.js b/app/frontend/src/javascript/directives/cart.js index dea22cab0..a209e394a 100644 --- a/app/frontend/src/javascript/directives/cart.js +++ b/app/frontend/src/javascript/directives/cart.js @@ -716,9 +716,14 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', * Open a modal window that allows the user to process a credit card payment for his current shopping cart. */ const payByStripe = function (reservation) { - $scope.toggleStripeModal(() => { - $scope.stripe.cartItems = mkCartItems(reservation, 'stripe'); - }); + // check that the online payment is enabled + if ($scope.settings.online_payment_module !== 'true') { + growl.error(_t('app.shared.cart.online_payment_disabled')); + } else { + $scope.toggleStripeModal(() => { + $scope.stripe.cartItems = mkCartItems(reservation, 'stripe'); + }); + } }; /** * Open a modal window that allows the user to process a local payment for his current shopping cart (admin only). @@ -751,10 +756,13 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', }, user () { return $scope.user; + }, + settings () { + return $scope.settings; } }, - controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'Subscription', 'wallet', 'helpers', '$filter', 'coupon', 'selectedPlan', 'schedule', 'cartItems', 'user', - function ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, Subscription, wallet, helpers, $filter, coupon, selectedPlan, schedule, cartItems, user) { + controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'Subscription', 'wallet', 'helpers', '$filter', 'coupon', 'selectedPlan', 'schedule', 'cartItems', 'user', 'settings', + function ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, Subscription, wallet, helpers, $filter, coupon, selectedPlan, schedule, cartItems, user, settings) { // user wallet amount $scope.wallet = wallet; @@ -797,7 +805,12 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', */ $scope.ok = function () { if ($scope.schedule && $scope.method.payment_method === 'stripe') { - return $scope.toggleStripeModal(); + // check that the online payment is enabled + if (settings.online_payment_module !== 'true') { + return growl.error(_t('app.shared.cart.online_payment_disabled')); + } else { + return $scope.toggleStripeModal(); + } } $scope.attempting = true; // save subscription (if there's only a subscription selected) @@ -927,11 +940,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', const amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount); if ((AuthService.isAuthorized(['member']) && amountToPay > 0) || (AuthService.isAuthorized('manager') && $scope.user.id === $rootScope.currentUser.id && amountToPay > 0)) { - if ($scope.settings.online_payment_module !== 'true') { - growl.error(_t('app.shared.cart.online_payment_disabled')); - } else { - return payByStripe(reservation); - } + return payByStripe(reservation); } else { if (AuthService.isAuthorized(['admin']) || (AuthService.isAuthorized('manager') && $scope.user.id !== $rootScope.currentUser.id) || diff --git a/app/frontend/templates/admin/subscriptions/expired_at_modal.html b/app/frontend/templates/admin/subscriptions/expired_at_modal.html index 6d02cd49f..02e0b71ac 100644 --- a/app/frontend/templates/admin/subscriptions/expired_at_modal.html +++ b/app/frontend/templates/admin/subscriptions/expired_at_modal.html @@ -10,6 +10,7 @@
{{ 'app.admin.members_edit.you_intentionally_decide_to_extend_the_user_s_subscription_by_charging_him_again_for_his_current_subscription' }}
{{ 'app.admin.members_edit.credits_will_be_reset' }}
+{{ 'app.admin.members_edit.payment_scheduled' }}