mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +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 }
|
||||
end
|
||||
|
||||
def payment_schedule
|
||||
def create_subscription
|
||||
cart = shopping_cart
|
||||
Stripe.api_key = Setting.get('stripe_secret_key')
|
||||
@intent = Stripe::PaymentMethod.attach(
|
||||
intent = Stripe::Service.new.attach_method_as_default(
|
||||
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
|
||||
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)
|
||||
@res = cart.pay_schedule(intent.id, intent.class.name)
|
||||
render json: @res.to_json
|
||||
end
|
||||
|
||||
|
@ -21,8 +21,8 @@ export default class StripeAPI {
|
||||
return res?.data;
|
||||
}
|
||||
|
||||
static async paymentSchedule (paymentMethodId: string, cartItems: ShoppingCart): Promise<StripeSubscription> {
|
||||
const res: AxiosResponse = await apiClient.post('/api/stripe/payment_schedule', {
|
||||
static async createSubscription (paymentMethodId: string, cartItems: ShoppingCart): Promise<StripeSubscription> {
|
||||
const res: AxiosResponse = await apiClient.post('/api/stripe/create_subscription', {
|
||||
payment_method_id: paymentMethodId,
|
||||
cart_items: cartItems
|
||||
});
|
||||
|
@ -44,7 +44,7 @@ export const StripeForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
|
||||
await handleServerConfirmation(res);
|
||||
} else {
|
||||
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') {
|
||||
// Subscription is active, no customer actions required.
|
||||
const res = await StripeAPI.confirmPaymentSchedule(subscription.id, cart);
|
||||
|
@ -180,7 +180,7 @@ Rails.application.routes.draw do
|
||||
# card payments handling
|
||||
## Stripe gateway
|
||||
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/setup_intent/:user_id' => 'stripe#setup_intent'
|
||||
post 'stripe/confirm_payment_schedule' => 'stripe#confirm_payment_schedule'
|
||||
|
@ -145,6 +145,22 @@ class Stripe::Service < Payment::Service
|
||||
{ status: stp_invoice.status, error: e }
|
||||
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
|
||||
|
||||
def subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user