mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +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:
parent
da372cf8ad
commit
d5f7bd7abd
@ -12,6 +12,10 @@ class API::PaymentsController < API::ApiController
|
|||||||
|
|
||||||
protected
|
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)
|
def get_wallet_debit(user, total_amount)
|
||||||
wallet_amount = (user.wallet.amount * 100).to_i
|
wallet_amount = (user.wallet.amount * 100).to_i
|
||||||
wallet_amount >= total_amount ? total_amount : wallet_amount
|
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
|
raise InvalidGroupError if plan.group_id != current_user.group_id
|
||||||
end
|
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
|
def reservation_params
|
||||||
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places,
|
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places,
|
||||||
tickets_attributes: %i[event_price_category_id booked],
|
tickets_attributes: %i[event_price_category_id booked],
|
||||||
|
@ -62,48 +62,10 @@ class API::PayzenController < API::PaymentsController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def on_reservation_success(order_id, details)
|
def on_reservation_success(order_id, details)
|
||||||
@reservation = Reservation.new(reservation_params)
|
super(order_id, 'PayZen::Order', details)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_subscription_success(order_id, details)
|
def on_subscription_success(order_id, details)
|
||||||
@subscription = Subscription.new(subscription_params)
|
super(order_id, 'PayZen::Order', details)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -106,65 +106,32 @@ class API::StripeController < API::PaymentsController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def on_reservation_success(intent, details)
|
def post_reservation_save(intent_id, intent_type)
|
||||||
@reservation = Reservation.new(reservation_params)
|
return unless intent_type == 'Stripe::PaymentIntent'
|
||||||
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(
|
Stripe::PaymentIntent.update(
|
||||||
intent.id,
|
intent_id,
|
||||||
{ description: "Invoice reference: #{@reservation.invoice.reference}" },
|
{ description: "Invoice reference: #{@reservation.invoice.reference}" },
|
||||||
{ api_key: Setting.get('stripe_secret_key') }
|
{ api_key: Setting.get('stripe_secret_key') }
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_reserve
|
def post_subscription_save(intent_id, intent_type)
|
||||||
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible
|
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(
|
Stripe::PaymentIntent.update(
|
||||||
intent.id,
|
intent_id,
|
||||||
{ description: "Invoice reference: #{@subscription.invoices.first.reference}" },
|
{ description: "Invoice reference: #{@subscription.invoices.first.reference}" },
|
||||||
{ api_key: Setting.get('stripe_secret_key') }
|
{ api_key: Setting.get('stripe_secret_key') }
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_subscribe
|
def on_reservation_success(intent, details)
|
||||||
{ template: 'api/subscriptions/show', status: :created, location: @subscription }
|
super(intent.id, intent.class.name, details)
|
||||||
else
|
|
||||||
{ json: @subscription.errors, status: :unprocessable_entity }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on_subscription_success(intent, details)
|
||||||
|
super(intent.id, intent.class.name, details)
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_payment_response(intent, res = nil)
|
def generate_payment_response(intent, res = nil)
|
||||||
|
@ -79,7 +79,7 @@ class InvoicesService
|
|||||||
end
|
end
|
||||||
|
|
||||||
invoice = Invoice.new(
|
invoice = Invoice.new(
|
||||||
invoiced: subscription || reservation,
|
invoiced: reservation || subscription,
|
||||||
invoicing_profile: user.invoicing_profile,
|
invoicing_profile: user.invoicing_profile,
|
||||||
statistic_profile: user.statistic_profile,
|
statistic_profile: user.statistic_profile,
|
||||||
operator_profile_id: operator_profile_id,
|
operator_profile_id: operator_profile_id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user