mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
[ongoing] refactoring stripe subscription
This commit is contained in:
parent
b32a20fb29
commit
d494b012d4
@ -69,19 +69,13 @@ class API::StripeController < API::PaymentsController
|
|||||||
render json: { id: @intent.id, client_secret: @intent.client_secret }
|
render json: { id: @intent.id, client_secret: @intent.client_secret }
|
||||||
end
|
end
|
||||||
|
|
||||||
def payment_schedule
|
def create_subscription
|
||||||
cart = shopping_cart
|
cart = shopping_cart
|
||||||
Stripe.api_key = Setting.get('stripe_secret_key')
|
intent = Stripe::Service.new.attach_method_as_default(
|
||||||
@intent = Stripe::PaymentMethod.attach(
|
|
||||||
params[:payment_method_id],
|
params[:payment_method_id],
|
||||||
customer: cart.customer.payment_gateway_object.gateway_object_id
|
cart.customer.payment_gateway_object.gateway_object_id
|
||||||
)
|
)
|
||||||
# Set the default payment method on the customer
|
@res = cart.pay_schedule(intent.id, intent.class.name)
|
||||||
Stripe::Customer.update(
|
|
||||||
cart.customer.payment_gateway_object.gateway_object_id,
|
|
||||||
invoice_settings: { default_payment_method: params[:payment_method_id] }
|
|
||||||
)
|
|
||||||
@res = cart.pay_schedule(@intent.id, @intent.class.name)
|
|
||||||
render json: @res.to_json
|
render json: @res.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ export default class StripeAPI {
|
|||||||
return res?.data;
|
return res?.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async paymentSchedule (paymentMethodId: string, cartItems: ShoppingCart): Promise<StripeSubscription> {
|
static async createSubscription (paymentMethodId: string, cartItems: ShoppingCart): Promise<StripeSubscription> {
|
||||||
const res: AxiosResponse = await apiClient.post('/api/stripe/payment_schedule', {
|
const res: AxiosResponse = await apiClient.post('/api/stripe/create_subscription', {
|
||||||
payment_method_id: paymentMethodId,
|
payment_method_id: paymentMethodId,
|
||||||
cart_items: cartItems
|
cart_items: cartItems
|
||||||
});
|
});
|
||||||
|
@ -44,7 +44,7 @@ export const StripeForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
|
|||||||
await handleServerConfirmation(res);
|
await handleServerConfirmation(res);
|
||||||
} else {
|
} else {
|
||||||
const paymentMethodId = paymentMethod.id;
|
const paymentMethodId = paymentMethod.id;
|
||||||
const subscription: StripeSubscription = await StripeAPI.paymentSchedule(paymentMethod.id, cart);
|
const subscription: StripeSubscription = await StripeAPI.createSubscription(paymentMethod.id, cart);
|
||||||
if (subscription && subscription.status === 'active') {
|
if (subscription && subscription.status === 'active') {
|
||||||
// Subscription is active, no customer actions required.
|
// Subscription is active, no customer actions required.
|
||||||
const res = await StripeAPI.confirmPaymentSchedule(subscription.id, cart);
|
const res = await StripeAPI.confirmPaymentSchedule(subscription.id, cart);
|
||||||
|
@ -180,7 +180,7 @@ Rails.application.routes.draw do
|
|||||||
# card payments handling
|
# card payments handling
|
||||||
## Stripe gateway
|
## Stripe gateway
|
||||||
post 'stripe/confirm_payment' => 'stripe/confirm_payment'
|
post 'stripe/confirm_payment' => 'stripe/confirm_payment'
|
||||||
post 'stripe/payment_schedule' => 'stripe/payment_schedule'
|
post 'stripe/create_subscription' => 'stripe/create_subscription'
|
||||||
get 'stripe/online_payment_status' => 'stripe/online_payment_status'
|
get 'stripe/online_payment_status' => 'stripe/online_payment_status'
|
||||||
get 'stripe/setup_intent/:user_id' => 'stripe#setup_intent'
|
get 'stripe/setup_intent/:user_id' => 'stripe#setup_intent'
|
||||||
post 'stripe/confirm_payment_schedule' => 'stripe#confirm_payment_schedule'
|
post 'stripe/confirm_payment_schedule' => 'stripe#confirm_payment_schedule'
|
||||||
|
@ -145,6 +145,22 @@ class Stripe::Service < Payment::Service
|
|||||||
{ status: stp_invoice.status, error: e }
|
{ status: stp_invoice.status, error: e }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def attach_method_as_default(payment_method_id, customer_id)
|
||||||
|
Stripe.api_key = Setting.get('stripe_secret_key')
|
||||||
|
|
||||||
|
# attach the payment method to the given customer
|
||||||
|
intent = Stripe::PaymentMethod.attach(
|
||||||
|
payment_method_id,
|
||||||
|
customer: customer_id
|
||||||
|
)
|
||||||
|
# then set it as the default payment method for this customer
|
||||||
|
Stripe::Customer.update(
|
||||||
|
cart.customer.payment_gateway_object.gateway_object_id,
|
||||||
|
invoice_settings: { default_payment_method: params[:payment_method_id] }
|
||||||
|
)
|
||||||
|
intent
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id)
|
def subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user