From 5941446a72027e12507fb3851678fb4f4c0d17e4 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 2 Dec 2020 14:28:41 +0100 Subject: [PATCH] fixes for card payment --- .../api/subscriptions_controller.rb | 2 +- app/controllers/social_bot_controller.rb | 3 +- .../src/javascript/components/plan-card.tsx | 29 ++++++++++--------- .../javascript/controllers/machines.js.erb | 4 ++- .../src/javascript/controllers/plans.js | 4 ++- .../src/javascript/controllers/spaces.js.erb | 10 +++++-- .../javascript/controllers/trainings.js.erb | 10 +++++-- .../src/javascript/directives/cart.js | 12 ++++---- app/frontend/templates/plans/_plan.html | 24 ++++++++------- app/frontend/templates/plans/index.html | 3 +- app/models/subscription.rb | 1 + app/services/subscriptions/subscribe.rb | 2 +- 12 files changed, 62 insertions(+), 42 deletions(-) diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index ed769be33..f79017acd 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -23,7 +23,7 @@ class API::SubscriptionsController < API::ApiController .pay_and_save(@subscription, coupon: coupon_params[:coupon_code], invoice: true, schedule: params[:subscription][:payment_schedule], - payment_method: params[:reservation][:payment_method]) + payment_method: params[:subscription][:payment_method]) if is_subscribe render :show, status: :created, location: @subscription diff --git a/app/controllers/social_bot_controller.rb b/app/controllers/social_bot_controller.rb index 67bf8da77..e3da819ff 100644 --- a/app/controllers/social_bot_controller.rb +++ b/app/controllers/social_bot_controller.rb @@ -17,5 +17,4 @@ class SocialBotController < ActionController::Base puts "unknown bot request : #{request.original_url}" end end - -end \ No newline at end of file +end diff --git a/app/frontend/src/javascript/components/plan-card.tsx b/app/frontend/src/javascript/components/plan-card.tsx index 3689d1463..e74062a32 100644 --- a/app/frontend/src/javascript/components/plan-card.tsx +++ b/app/frontend/src/javascript/components/plan-card.tsx @@ -2,7 +2,7 @@ * This component is a "card" publicly presenting the details of a plan */ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { react2angular } from 'react2angular'; import moment from 'moment'; @@ -19,13 +19,14 @@ declare var Fablab: IFablab; interface PlanCardProps { plan: Plan, - user: User, + userId?: number, + subscribedPlanId?: number, operator: User, isSelected: boolean, onSelectPlan: (plan: Plan) => void, } -const PlanCard: React.FC = ({ plan, user, operator, onSelectPlan, isSelected }) => { +const PlanCard: React.FC = ({ plan, userId, subscribedPlanId, operator, onSelectPlan, isSelected }) => { const { t } = useTranslation('public'); /** * Return the formatted localized amount of the given plan (eg. 20.5 => "20,50 €") @@ -50,19 +51,19 @@ const PlanCard: React.FC = ({ plan, user, operator, onSelectPlan, * Check if the user can subscribe to the current plan, for himself */ const canSubscribeForMe = (): boolean => { - return operator?.role === UserRole.Member || (operator?.role === UserRole.Manager && user?.id === operator?.id) + return operator?.role === UserRole.Member || (operator?.role === UserRole.Manager && userId === operator?.id) } /** * Check if the user can subscribe to the current plan, for someone else */ const canSubscribeForOther = (): boolean => { - return operator?.role === UserRole.Admin || (operator?.role === UserRole.Manager && user?.id !== operator?.id) + return operator?.role === UserRole.Admin || (operator?.role === UserRole.Manager && userId !== operator?.id) } /** * Check it the user has subscribed to this plan or not */ const hasSubscribedToThisPlan = (): boolean => { - return user?.subscription?.plan?.id === plan.id; + return subscribedPlanId === plan.id; } /** * Check if the plan has an attached file @@ -102,18 +103,18 @@ const PlanCard: React.FC = ({ plan, user, operator, onSelectPlan, {canSubscribeForMe() &&
{!hasSubscribedToThisPlan() && } - {hasSubscribedToThisPlan() && }
} {canSubscribeForOther() &&
} @@ -122,12 +123,12 @@ const PlanCard: React.FC = ({ plan, user, operator, onSelectPlan, ); } -const PlanCardWrapper: React.FC = ({ plan, user, operator, onSelectPlan, isSelected }) => { +const PlanCardWrapper: React.FC = ({ plan, userId, subscribedPlanId, operator, onSelectPlan, isSelected }) => { return ( - + ); } -Application.Components.component('planCard', react2angular(PlanCardWrapper, ['plan', 'user', 'operator', 'onSelectPlan', 'isSelected'])); +Application.Components.component('planCard', react2angular(PlanCardWrapper, ['plan', 'userId', 'subscribedPlanId', 'operator', 'onSelectPlan', 'isSelected'])); diff --git a/app/frontend/src/javascript/controllers/machines.js.erb b/app/frontend/src/javascript/controllers/machines.js.erb index 5ac396637..dca0cf531 100644 --- a/app/frontend/src/javascript/controllers/machines.js.erb +++ b/app/frontend/src/javascript/controllers/machines.js.erb @@ -665,7 +665,9 @@ Application.Controllers.controller('ReserveMachineController', ['$scope', '$stat if ($scope.selectedPlan) { $scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan); - Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + } $scope.plansAreShown = false; $scope.selectedPlan = null; } diff --git a/app/frontend/src/javascript/controllers/plans.js b/app/frontend/src/javascript/controllers/plans.js index 611caf54c..db6c0e1ac 100644 --- a/app/frontend/src/javascript/controllers/plans.js +++ b/app/frontend/src/javascript/controllers/plans.js @@ -167,7 +167,9 @@ Application.Controllers.controller('PlansIndexController', ['$scope', '$rootScop */ $scope.afterPayment = function () { $scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan); - Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + } $scope.paid.plan = angular.copy($scope.selectedPlan); $scope.selectedPlan = null; $scope.coupon.applied = null; diff --git a/app/frontend/src/javascript/controllers/spaces.js.erb b/app/frontend/src/javascript/controllers/spaces.js.erb index 709de36f0..f0f679a22 100644 --- a/app/frontend/src/javascript/controllers/spaces.js.erb +++ b/app/frontend/src/javascript/controllers/spaces.js.erb @@ -571,14 +571,18 @@ Application.Controllers.controller('ReserveSpaceController', ['$scope', '$stateP if ($scope.selectedPlan) { $scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan); - Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + } $scope.plansAreShown = false; $scope.selectedPlan = null; } $scope.ctrl.member.training_credits = angular.copy(reservation.user.training_credits); $scope.ctrl.member.machine_credits = angular.copy(reservation.user.machine_credits); - Auth._currentUser.training_credits = angular.copy(reservation.user.training_credits); - Auth._currentUser.machine_credits = angular.copy(reservation.user.machine_credits); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.training_credits = angular.copy(reservation.user.training_credits); + Auth._currentUser.machine_credits = angular.copy(reservation.user.machine_credits); + } refetchCalendar(); }; diff --git a/app/frontend/src/javascript/controllers/trainings.js.erb b/app/frontend/src/javascript/controllers/trainings.js.erb index 0c553393b..e6fc3ab2c 100644 --- a/app/frontend/src/javascript/controllers/trainings.js.erb +++ b/app/frontend/src/javascript/controllers/trainings.js.erb @@ -361,14 +361,18 @@ Application.Controllers.controller('ReserveTrainingController', ['$scope', '$sta if ($scope.selectedPlan) { $scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan); - Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan); + } $scope.plansAreShown = false; $scope.selectedPlan = null; } $scope.ctrl.member.training_credits = angular.copy(reservation.user.training_credits); $scope.ctrl.member.machine_credits = angular.copy(reservation.user.machine_credits); - Auth._currentUser.training_credits = angular.copy(reservation.user.training_credits); - Auth._currentUser.machine_credits = angular.copy(reservation.user.machine_credits); + if ($scope.ctrl.member.id === Auth._currentUser.id) { + Auth._currentUser.training_credits = angular.copy(reservation.user.training_credits); + Auth._currentUser.machine_credits = angular.copy(reservation.user.machine_credits); + } refetchCalendar(); }; diff --git a/app/frontend/src/javascript/directives/cart.js b/app/frontend/src/javascript/directives/cart.js index 7c593afe4..5c0214e69 100644 --- a/app/frontend/src/javascript/directives/cart.js +++ b/app/frontend/src/javascript/directives/cart.js @@ -779,7 +779,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', * Callback to process the local payment, triggered on button click */ $scope.ok = function () { - if ($scope.method.payment_method === 'stripe') { + if ($scope.schedule && $scope.method.payment_method === 'stripe') { return $scope.toggleStripeModal(); } $scope.attempting = true; @@ -843,10 +843,12 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs', */ const computeValidButtonName = function () { let method = ''; - if (AuthService.isAuthorized(['admin', 'manager']) && $rootScope.currentUser.id !== reservation.user_id) { - method = $scope.method.payment_method; - } else { - method = 'stripe'; + if ($scope.schedule) { + if (AuthService.isAuthorized(['admin', 'manager']) && $rootScope.currentUser.id !== reservation.user_id) { + method = $scope.method.payment_method; + } else { + method = 'stripe'; + } } if ($scope.amount > 0) { return _t('app.shared.cart.confirm_payment_of_html', { METHOD: method, AMOUNT: $filter('currency')($scope.amount) }); diff --git a/app/frontend/templates/plans/_plan.html b/app/frontend/templates/plans/_plan.html index 00187c9bd..359b55525 100644 --- a/app/frontend/templates/plans/_plan.html +++ b/app/frontend/templates/plans/_plan.html @@ -6,18 +6,22 @@
- -
- - +
+
+ + + + +
{{ 'app.shared.plan_subscribe.do_not_subscribe' | translate }} diff --git a/app/frontend/templates/plans/index.html b/app/frontend/templates/plans/index.html index 65162aad2..0706c9c6e 100644 --- a/app/frontend/templates/plans/index.html +++ b/app/frontend/templates/plans/index.html @@ -32,7 +32,8 @@ ng-repeat="(key, plan) in plansGroup.plans.filter(filterDisabledPlans) | orderBy: '-ui_weight'"> diff --git a/app/models/subscription.rb b/app/models/subscription.rb index ca4377f76..4689a1f4d 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -21,6 +21,7 @@ class Subscription < ApplicationRecord # @param invoice if true then only the subscription is payed, without reservation # if false then the subscription is payed with reservation + # @param payment_method is only used for schedules def save_with_payment(operator_profile_id, invoice: true, coupon_code: nil, payment_intent_id: nil, schedule: nil, payment_method: nil) return false unless valid? diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index c83ec3fa6..bcfc9fc82 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -15,7 +15,7 @@ class Subscriptions::Subscribe # @param invoice {Boolean} # @param payment_intent_id {String} from stripe # @param schedule {Boolean} - # @param payment_method {String} + # @param payment_method {String} only for schedules ## def pay_and_save(subscription, coupon: nil, invoice: false, payment_intent_id: nil, schedule: false, payment_method: nil) return false if user_id.nil?