From d5f7bd7abdfa5f8e4d82e2a5b9fd6a4103ad6e03 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 26 Apr 2021 17:42:03 +0200 Subject: [PATCH] fix test Reservations::CreateTest create subscription w/ the reservation if applicable + refactor payments_controller to factorize the on_*_success codes --- app/controllers/api/payments_controller.rb | 57 ++++++++++++++++ app/controllers/api/payzen_controller.rb | 42 +----------- app/controllers/api/stripe_controller.rb | 77 +++++++--------------- app/models/cart_item/event_reservation.rb | 8 +-- app/services/invoices_service.rb | 2 +- 5 files changed, 86 insertions(+), 100 deletions(-) diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index 5cdc5c2b6..d0d237b39 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -12,6 +12,10 @@ class API::PaymentsController < API::ApiController protected + def post_reservation_save(_gateway_item_id, _gateway_item_type); end + + def post_subscription_save(_gateway_item_id, _gateway_item_type); end + def get_wallet_debit(user, total_amount) wallet_amount = (user.wallet.amount * 100).to_i wallet_amount >= total_amount ? total_amount : wallet_amount @@ -48,6 +52,59 @@ class API::PaymentsController < API::ApiController raise InvalidGroupError if plan.group_id != current_user.group_id end + def on_reservation_success(gateway_item_id, gateway_item_type, details) + @reservation = Reservation.new(reservation_params) + if params[:cart_items][:subscription] && params[:cart_items][:subscription][:plan_id] + @reservation.plan_id = params[:cart_items][:subscription][:plan_id] + end + payment_method = params[:cart_items][:reservation][:payment_method] || 'card' + user_id = if current_user.admin? || current_user.manager? + params[:cart_items][:reservation][:user_id] + else + current_user.id + end + is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) + .pay_and_save(@reservation, + payment_details: details, + payment_id: gateway_item_id, + payment_type: gateway_item_type, + schedule: params[:cart_items][:reservation][:payment_schedule], + payment_method: payment_method) + post_reservation_save(gateway_item_id, gateway_item_type) + + if is_reserve + SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible + + { template: 'api/reservations/show', status: :created, location: @reservation } + else + { json: @reservation.errors, status: :unprocessable_entity } + end + end + + def on_subscription_success(gateway_item_id, gateway_item_type, details) + @subscription = Subscription.new(subscription_params) + user_id = if current_user.admin? || current_user.manager? + params[:cart_items][:subscription][:user_id] + else + current_user.id + end + is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) + .pay_and_save(@subscription, + payment_details: details, + payment_id: gateway_item_id, + payment_type: gateway_item_type, + schedule: params[:cart_items][:subscription][:payment_schedule], + payment_method: 'card') + + post_subscription_save(gateway_item_id, gateway_item_type) + + if is_subscribe + { template: 'api/subscriptions/show', status: :created, location: @subscription } + else + { json: @subscription.errors, status: :unprocessable_entity } + end + end + def reservation_params params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places, tickets_attributes: %i[event_price_category_id booked], diff --git a/app/controllers/api/payzen_controller.rb b/app/controllers/api/payzen_controller.rb index 79f3d8b64..89396afc1 100644 --- a/app/controllers/api/payzen_controller.rb +++ b/app/controllers/api/payzen_controller.rb @@ -62,48 +62,10 @@ class API::PayzenController < API::PaymentsController private def on_reservation_success(order_id, details) - @reservation = Reservation.new(reservation_params) - payment_method = params[:cart_items][:reservation][:payment_method] || 'card' - user_id = if current_user.admin? || current_user.manager? - params[:cart_items][:reservation][:user_id] - else - current_user.id - end - is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) - .pay_and_save(@reservation, - payment_details: details, - payment_id: order_id, - payment_type: 'PayZen::Order', - schedule: params[:cart_items][:reservation][:payment_schedule], - payment_method: payment_method) - if is_reserve - SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible - - { template: 'api/reservations/show', status: :created, location: @reservation } - else - { json: @reservation.errors, status: :unprocessable_entity } - end + super(order_id, 'PayZen::Order', details) end def on_subscription_success(order_id, details) - @subscription = Subscription.new(subscription_params) - user_id = if current_user.admin? || current_user.manager? - params[:cart_items][:subscription][:user_id] - else - current_user.id - end - is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) - .pay_and_save(@subscription, - payment_details: details, - payment_id: order_id, - payment_type: 'PayZen::Order', - schedule: params[:cart_items][:subscription][:payment_schedule], - payment_method: 'card') - - if is_subscribe - { template: 'api/subscriptions/show', status: :created, location: @subscription } - else - { json: @subscription.errors, status: :unprocessable_entity } - end + super(order_id, 'PayZen::Order', details) end end diff --git a/app/controllers/api/stripe_controller.rb b/app/controllers/api/stripe_controller.rb index e0769b57c..fd0fed599 100644 --- a/app/controllers/api/stripe_controller.rb +++ b/app/controllers/api/stripe_controller.rb @@ -106,65 +106,32 @@ class API::StripeController < API::PaymentsController private + def post_reservation_save(intent_id, intent_type) + return unless intent_type == 'Stripe::PaymentIntent' + + Stripe::PaymentIntent.update( + intent_id, + { description: "Invoice reference: #{@reservation.invoice.reference}" }, + { api_key: Setting.get('stripe_secret_key') } + ) + end + + def post_subscription_save(intent_id, intent_type) + return unless intent_type == 'Stripe::PaymentIntent' + + Stripe::PaymentIntent.update( + intent_id, + { description: "Invoice reference: #{@subscription.invoices.first.reference}" }, + { api_key: Setting.get('stripe_secret_key') } + ) + end + def on_reservation_success(intent, details) - @reservation = Reservation.new(reservation_params) - payment_method = params[:cart_items][:reservation][:payment_method] || 'card' - user_id = if current_user.admin? || current_user.manager? - params[:cart_items][:reservation][:user_id] - else - current_user.id - end - is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) - .pay_and_save(@reservation, - payment_details: details, - payment_id: intent.id, - payment_type: intent.class.name, - schedule: params[:cart_items][:reservation][:payment_schedule], - payment_method: payment_method) - if intent.class == Stripe::PaymentIntent - Stripe::PaymentIntent.update( - intent.id, - { description: "Invoice reference: #{@reservation.invoice.reference}" }, - { api_key: Setting.get('stripe_secret_key') } - ) - end - - if is_reserve - SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible - - { template: 'api/reservations/show', status: :created, location: @reservation } - else - { json: @reservation.errors, status: :unprocessable_entity } - end + super(intent.id, intent.class.name, details) end 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] - else - current_user.id - end - is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id) - .pay_and_save(@subscription, - payment_details: details, - payment_id: intent.id, - payment_type: intent.class.name, - schedule: params[:cart_items][:subscription][:payment_schedule], - payment_method: 'card') - if intent.class == Stripe::PaymentIntent - Stripe::PaymentIntent.update( - intent.id, - { description: "Invoice reference: #{@subscription.invoices.first.reference}" }, - { api_key: Setting.get('stripe_secret_key') } - ) - end - - if is_subscribe - { template: 'api/subscriptions/show', status: :created, location: @subscription } - else - { json: @subscription.errors, status: :unprocessable_entity } - end + super(intent.id, intent.class.name, details) end def generate_payment_response(intent, res = nil) diff --git a/app/models/cart_item/event_reservation.rb b/app/models/cart_item/event_reservation.rb index 17098edb6..7890316a4 100644 --- a/app/models/cart_item/event_reservation.rb +++ b/app/models/cart_item/event_reservation.rb @@ -25,10 +25,10 @@ class CartItem::EventReservation < CartItem::Reservation @slots.each do |slot| total += get_slot_price(amount, - slot, - is_privileged, - elements: elements, - is_division: false) + slot, + is_privileged, + elements: elements, + is_division: false) end { elements: elements, amount: total } diff --git a/app/services/invoices_service.rb b/app/services/invoices_service.rb index 397ab8626..d677bc4a2 100644 --- a/app/services/invoices_service.rb +++ b/app/services/invoices_service.rb @@ -79,7 +79,7 @@ class InvoicesService end invoice = Invoice.new( - invoiced: subscription || reservation, + invoiced: reservation || subscription, invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, operator_profile_id: operator_profile_id,