1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

fix test Reservations::CreateTest

create subscription w/ the reservation if applicable
+ refactor payments_controller to factorize the on_*_success codes
This commit is contained in:
Sylvain 2021-04-26 17:42:03 +02:00
parent da372cf8ad
commit d5f7bd7abd
5 changed files with 86 additions and 100 deletions

View File

@ -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],

View File

@ -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

View File

@ -106,65 +106,32 @@ class API::StripeController < API::PaymentsController
private
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
def post_reservation_save(intent_id, intent_type)
return unless intent_type == 'Stripe::PaymentIntent'
Stripe::PaymentIntent.update(
intent.id,
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
def post_subscription_save(intent_id, intent_type)
return unless intent_type == 'Stripe::PaymentIntent'
{ template: 'api/reservations/show', status: :created, location: @reservation }
else
{ json: @reservation.errors, status: :unprocessable_entity }
end
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,
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 }
def on_reservation_success(intent, details)
super(intent.id, intent.class.name, details)
end
def on_subscription_success(intent, details)
super(intent.id, intent.class.name, details)
end
def generate_payment_response(intent, res = nil)

View File

@ -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,