mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-04-10 00:53:51 +02:00
improve code for stripe subscription payment
This commit is contained in:
parent
745e2ae6cf
commit
6b763cc1c9
@ -1,6 +1,6 @@
|
|||||||
import apiClient from './clients/api-client';
|
import apiClient from './clients/api-client';
|
||||||
import { AxiosResponse } from 'axios';
|
import { AxiosResponse } from 'axios';
|
||||||
import { ShoppingCart, IntentConfirmation, PaymentConfirmation, UpdateCardResponse } from '../models/payment';
|
import { ShoppingCart, IntentConfirmation, PaymentConfirmation, UpdateCardResponse, StripeSubscription } from '../models/payment';
|
||||||
import { PaymentSchedule } from '../models/payment-schedule';
|
import { PaymentSchedule } from '../models/payment-schedule';
|
||||||
import { Invoice } from '../models/invoice';
|
import { Invoice } from '../models/invoice';
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ export default class StripeAPI {
|
|||||||
return res?.data;
|
return res?.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async paymentSchedule (paymentMethodId: string, cartItems: ShoppingCart): Promise<any> {
|
static async paymentSchedule (paymentMethodId: string, cartItems: ShoppingCart): Promise<StripeSubscription> {
|
||||||
const res: AxiosResponse = await apiClient.post('/api/stripe/payment_schedule', {
|
const res: AxiosResponse = await apiClient.post('/api/stripe/payment_schedule', {
|
||||||
payment_method_id: paymentMethodId,
|
payment_method_id: paymentMethodId,
|
||||||
cart_items: cartItems
|
cart_items: cartItems
|
||||||
|
@ -2,7 +2,7 @@ import React, { FormEvent } from 'react';
|
|||||||
import { CardElement, useElements, useStripe } from '@stripe/react-stripe-js';
|
import { CardElement, useElements, useStripe } from '@stripe/react-stripe-js';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { GatewayFormProps } from '../abstract-payment-modal';
|
import { GatewayFormProps } from '../abstract-payment-modal';
|
||||||
import { PaymentConfirmation } from '../../../models/payment';
|
import { PaymentConfirmation, StripeSubscription } from '../../../models/payment';
|
||||||
import StripeAPI from '../../../api/stripe';
|
import StripeAPI from '../../../api/stripe';
|
||||||
import { Invoice } from '../../../models/invoice';
|
import { Invoice } from '../../../models/invoice';
|
||||||
|
|
||||||
@ -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: any = await StripeAPI.paymentSchedule(paymentMethod.id, cart);
|
const subscription: StripeSubscription = await StripeAPI.paymentSchedule(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);
|
||||||
@ -71,7 +71,7 @@ export const StripeForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
|
|||||||
onError(error.message);
|
onError(error.message);
|
||||||
});
|
});
|
||||||
} else if (paymentIntent.status === 'requires_payment_method') {
|
} else if (paymentIntent.status === 'requires_payment_method') {
|
||||||
onError('Your card was declined.');
|
onError(t('app.shared.messages.payment_card_declined'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -34,3 +34,14 @@ export interface UpdateCardResponse {
|
|||||||
updated: boolean,
|
updated: boolean,
|
||||||
error?: string
|
error?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StripeSubscription {
|
||||||
|
id: string,
|
||||||
|
status: string,
|
||||||
|
latest_invoice: {
|
||||||
|
payment_intent: {
|
||||||
|
status: string,
|
||||||
|
client_secret: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -73,7 +73,8 @@ class ShoppingCart
|
|||||||
price = total
|
price = total
|
||||||
objects = []
|
objects = []
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
rails InvalidSubscriptionError unless item.valid?(@items)
|
raise InvalidSubscriptionError unless item.valid?(@items)
|
||||||
|
|
||||||
object = item.to_object
|
object = item.to_object
|
||||||
objects.push(object)
|
objects.push(object)
|
||||||
raise InvalidSubscriptionError unless object.errors.empty?
|
raise InvalidSubscriptionError unless object.errors.empty?
|
||||||
|
@ -22,6 +22,7 @@ de:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Sie verlieren jede ungespeicherte Änderung, wenn Sie diese Seite verlassen"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Sie verlieren jede ungespeicherte Änderung, wenn Sie diese Seite verlassen"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Ungespeicherte Änderungen gehen verloren, wenn Sie die Seite neu laden"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Ungespeicherte Änderungen gehen verloren, wenn Sie die Seite neu laden"
|
||||||
payment_card_error: "Mit Ihrer Kreditkarte ist ein Problem aufgetreten:"
|
payment_card_error: "Mit Ihrer Kreditkarte ist ein Problem aufgetreten:"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Männlich"
|
man: "Männlich"
|
||||||
|
@ -22,6 +22,7 @@ en:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "You will lose any unsaved modification if you quit this page"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "You will lose any unsaved modification if you quit this page"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "You will lose any unsaved modification if you reload this page"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "You will lose any unsaved modification if you reload this page"
|
||||||
payment_card_error: "A problem has occurred with your credit card:"
|
payment_card_error: "A problem has occurred with your credit card:"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Man"
|
man: "Man"
|
||||||
|
@ -22,6 +22,7 @@ es:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Si cierra la página se perderán todas las modificaciones que no se hayan guardado"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Si cierra la página se perderán todas las modificaciones que no se hayan guardado"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Si recarga la página se perderán todas las modificaciones que no se hayan guardado"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Si recarga la página se perderán todas las modificaciones que no se hayan guardado"
|
||||||
payment_card_error: "A problem has occurred with your credit card:"
|
payment_card_error: "A problem has occurred with your credit card:"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Man"
|
man: "Man"
|
||||||
|
@ -22,6 +22,7 @@ fr:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Vous perdrez les modifications non enregistrées si vous quittez cette page"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Vous perdrez les modifications non enregistrées si vous quittez cette page"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Vous perdrez les modifications non enregistrées si vous rechargez cette page"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Vous perdrez les modifications non enregistrées si vous rechargez cette page"
|
||||||
payment_card_error: "Un problème est survenu avec votre carte bancaire :"
|
payment_card_error: "Un problème est survenu avec votre carte bancaire :"
|
||||||
|
payment_card_declined: "Votre carte a été refusée."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Homme"
|
man: "Homme"
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Du vil miste noen ulagrede endringer hvis du avslutter denne siden"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Du vil miste noen ulagrede endringer hvis du avslutter denne siden"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Du vil miste ulagrede endringer hvis du gjeninnlaster denne siden"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Du vil miste ulagrede endringer hvis du gjeninnlaster denne siden"
|
||||||
payment_card_error: "Et problem har oppstått med ditt kredittkort:"
|
payment_card_error: "Et problem har oppstått med ditt kredittkort:"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Mann"
|
man: "Mann"
|
||||||
|
@ -22,6 +22,7 @@ pt:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Você irá perder todas as modificações não salvas se sair desta página"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "Você irá perder todas as modificações não salvas se sair desta página"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Você irá perder todas as modificações não salvas se recarregar desta página"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "Você irá perder todas as modificações não salvas se recarregar desta página"
|
||||||
payment_card_error: "Ocorreu um problema com o seu cartão de crédito:"
|
payment_card_error: "Ocorreu um problema com o seu cartão de crédito:"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "Homem"
|
man: "Homem"
|
||||||
|
@ -22,6 +22,7 @@ zu:
|
|||||||
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "crwdns9405:0crwdne9405:0"
|
you_will_lose_any_unsaved_modification_if_you_quit_this_page: "crwdns9405:0crwdne9405:0"
|
||||||
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "crwdns9407:0crwdne9407:0"
|
you_will_lose_any_unsaved_modification_if_you_reload_this_page: "crwdns9407:0crwdne9407:0"
|
||||||
payment_card_error: "crwdns9409:0crwdne9409:0"
|
payment_card_error: "crwdns9409:0crwdne9409:0"
|
||||||
|
payment_card_declined: "Your card was declined."
|
||||||
#user edition form
|
#user edition form
|
||||||
user:
|
user:
|
||||||
man: "crwdns9411:0crwdne9411:0"
|
man: "crwdns9411:0crwdne9411:0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user