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

pass payment method to the ruby service

This commit is contained in:
Sylvain 2020-11-23 11:48:44 +01:00
parent 6abee0cea0
commit 34a9084501
8 changed files with 44 additions and 21 deletions

View File

@ -92,7 +92,11 @@ class API::PaymentsController < API::ApiController
def on_subscription_success(intent)
@subscription = Subscription.new(subscription_params)
is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, current_user.id)
.pay_and_save(@subscription, coupon: coupon_params[:coupon_code], invoice: true, payment_intent_id: intent.id)
.pay_and_save(@subscription,
coupon: coupon_params[:coupon_code],
invoice: true,
payment_intent_id: intent.id,
payment_method: 'stripe')
Stripe::PaymentIntent.update(
intent.id,

View File

@ -22,7 +22,8 @@ class API::SubscriptionsController < API::ApiController
is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id)
.pay_and_save(@subscription, coupon: coupon_params[:coupon_code],
invoice: true,
schedule: params[:subscription][:payment_schedule])
schedule: params[:subscription][:payment_schedule],
payment_method: params[:reservation][:payment_method])
if is_subscribe
render :show, status: :created, location: @subscription

View File

@ -530,6 +530,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
$scope.events.paid = [];
$scope.events.modifiable = null;
$scope.events.placable = null;
$scope.schedule.requested_schedule = false;
$scope.schedule.payment_schedule = null;
};
/**
@ -649,14 +651,16 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
* @param planId {number}
* @param userId {number}
* @param schedule {boolean}
* @param method {String} 'stripe' | ''
* @return {{subscription: {payment_schedule: boolean, user_id: number, plan_id: number}}}
*/
const mkSubscription = function (planId, userId, schedule) {
const mkSubscription = function (planId, userId, schedule, method) {
return {
subscription: {
plan_id: planId,
user_id: userId,
payment_schedule: schedule
payment_schedule: schedule,
payment_method: method
}
};
};
@ -687,7 +691,9 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
cartItems () {
let request = { reservation };
if (reservation.slots_attributes.length === 0 && reservation.plan_id) {
request = mkSubscription($scope.selectedPlan.id, reservation.user_id, $scope.schedule.requested_schedule);
request = mkSubscription($scope.selectedPlan.id, reservation.user_id, $scope.schedule.requested_schedule, 'stripe');
} else {
request.reservation.payment_method = 'stripe';
}
return mkRequestParams(request, $scope.coupon.applied);
},
@ -796,8 +802,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
$scope.ok = function () {
$scope.attempting = true;
// save subscription (if there's only a subscription selected)
if (reservation.slots_attributes.length === 0 && selectedPlan) {
const sub = mkSubscription(selectedPlan.id, reservation.user_id, schedule.requested_schedule);
if ($scope.reservation.slots_attributes.length === 0 && selectedPlan) {
const sub = mkSubscription(selectedPlan.id, $scope.reservation.user_id, schedule.requested_schedule, $scope.method.payment_method);
return Subscription.save(mkRequestParams(sub, coupon),
function (subscription) {
@ -810,7 +816,8 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
});
}
// otherwise, save the reservation (may include a subscription)
Reservation.save(mkRequestParams({ reservation: $scope.reservation }, coupon), function (reservation) {
const rsrv = Object.assign({}, $scope.reservation, { payment_method: $scope.method.payment_method });
Reservation.save(mkRequestParams({ reservation: rsrv }, coupon), function (reservation) {
$uibModalInstance.close(reservation);
$scope.attempting = true;
}, function (response) {
@ -830,8 +837,7 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
* Kind of constructor: these actions will be realized first when the directive is loaded
*/
const initialize = function () {
$scope.$watch('method.payment_method', function (newValue, oldValue) {
console.log(`watch triggered: ${newValue}`);
$scope.$watch('method.payment_method', function () {
$scope.validButtonName = computeValidButtonName();
});
};
@ -846,7 +852,6 @@ Application.Directives.directive('cart', ['$rootScope', '$uibModal', 'dialogs',
} else {
method = 'stripe';
}
console.log(method);
if ($scope.amount > 0) {
return _t('app.shared.cart.confirm_payment_of_html', { METHOD: method, AMOUNT: $filter('currency')($scope.amount) });
} else {

View File

@ -38,7 +38,8 @@
</li>
</ul>
</div>
</div>
<div class="row">
<wallet-info current-user="currentUser"
reservation="reservation"
price="price"

View File

@ -138,7 +138,7 @@ class Reservation < ApplicationRecord
if plan_id
self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id)
subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, expiration_date: nil }
if subscription.save_with_payment(operator_profile_id, false)
if subscription.save_with_payment(operator_profile_id, invoice: false)
invoice.invoice_items.push InvoiceItem.new(
amount: payment_details[:elements][:plan],
description: subscription.plan.name,

View File

@ -21,7 +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
def save_with_payment(operator_profile_id, invoice = true, coupon_code = nil, payment_intent_id = nil, schedule = nil)
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?
set_expiration_date
@ -35,7 +35,7 @@ class Subscription < ApplicationRecord
wallet_transaction = debit_user_wallet
payment = if schedule
generate_schedule(operator_profile_id, coupon_code, payment_intent_id)
generate_schedule(operator_profile_id, payment_method, coupon_code)
else
generate_invoice(operator_profile_id, coupon_code, payment_intent_id)
end
@ -49,12 +49,18 @@ class Subscription < ApplicationRecord
true
end
def generate_schedule(operator_profile_id, coupon_code = nil, payment_intent_id = nil)
def generate_schedule(operator_profile_id, payment_method, coupon_code = nil)
operator = InvoicingProfile.find(operator_profile_id)&.user
method = operator&.admin? || (operator&.manager? && operator != user) ? nil : 'stripe' # FIXME, paiement à l'accueil
coupon = Coupon.find_by(code: coupon_code) unless coupon_code.nil?
schedule = PaymentScheduleService.new.create(self, plan.amount, coupon: coupon, operator: operator, payment_method: method, user: user)
PaymentScheduleService.new.create(
self,
plan.amount,
coupon: coupon,
operator: operator,
payment_method: payment_method,
user: user
)
end
def generate_invoice(operator_profile_id, coupon_code = nil, payment_intent_id = nil)

View File

@ -61,7 +61,7 @@ class PaymentScheduleService
item.save!
end
StripeWorker.perform_async(:create_stripe_subscription, ps.id, reservation&.reservable&.stp_product_id)
StripeWorker.perform_async(:create_stripe_subscription, ps.id, reservation&.reservable&.stp_product_id) if payment_method == 'stripe'
ps
end
end

View File

@ -15,12 +15,18 @@ class Subscriptions::Subscribe
# @param invoice {Boolean}
# @param payment_intent_id {String} from stripe
# @param schedule {Boolean}
# @param payment_method {String}
##
def pay_and_save(subscription, coupon: nil, invoice: false, payment_intent_id: nil, schedule: false)
def pay_and_save(subscription, coupon: nil, invoice: false, payment_intent_id: nil, schedule: false, payment_method: nil)
return false if user_id.nil?
subscription.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id
subscription.save_with_payment(operator_profile_id, invoice, coupon, payment_intent_id, schedule)
subscription.save_with_payment(operator_profile_id,
invoice: invoice,
coupon_code: coupon,
payment_intent_id: payment_intent_id,
schedule: schedule,
payment_method: payment_method)
end
def extend_subscription(subscription, new_expiration_date, free_days)