1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

improve code for stripe subscription payment

This commit is contained in:
Du Peng 2021-09-10 15:16:08 +02:00
parent 745e2ae6cf
commit 6b763cc1c9
11 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import apiClient from './clients/api-client';
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 { Invoice } from '../models/invoice';
@ -21,7 +21,7 @@ export default class StripeAPI {
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', {
payment_method_id: paymentMethodId,
cart_items: cartItems

View File

@ -2,7 +2,7 @@ import React, { FormEvent } from 'react';
import { CardElement, useElements, useStripe } from '@stripe/react-stripe-js';
import { useTranslation } from 'react-i18next';
import { GatewayFormProps } from '../abstract-payment-modal';
import { PaymentConfirmation } from '../../../models/payment';
import { PaymentConfirmation, StripeSubscription } from '../../../models/payment';
import StripeAPI from '../../../api/stripe';
import { Invoice } from '../../../models/invoice';
@ -44,7 +44,7 @@ export const StripeForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
await handleServerConfirmation(res);
} else {
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') {
// Subscription is active, no customer actions required.
const res = await StripeAPI.confirmPaymentSchedule(subscription.id, cart);
@ -71,7 +71,7 @@ export const StripeForm: React.FC<GatewayFormProps> = ({ onSubmit, onSuccess, on
onError(error.message);
});
} else if (paymentIntent.status === 'requires_payment_method') {
onError('Your card was declined.');
onError(t('app.shared.messages.payment_card_declined'));
}
}
} catch (err) {

View File

@ -34,3 +34,14 @@ export interface UpdateCardResponse {
updated: boolean,
error?: string
}
export interface StripeSubscription {
id: string,
status: string,
latest_invoice: {
payment_intent: {
status: string,
client_secret: string
}
}
}

View File

@ -73,7 +73,8 @@ class ShoppingCart
price = total
objects = []
items.each do |item|
rails InvalidSubscriptionError unless item.valid?(@items)
raise InvalidSubscriptionError unless item.valid?(@items)
object = item.to_object
objects.push(object)
raise InvalidSubscriptionError unless object.errors.empty?

View File

@ -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_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_declined: "Your card was declined."
#user edition form
user:
man: "Männlich"

View File

@ -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_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_declined: "Your card was declined."
#user edition form
user:
man: "Man"

View File

@ -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_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_declined: "Your card was declined."
#user edition form
user:
man: "Man"

View File

@ -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_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_declined: "Votre carte a été refusée."
#user edition form
user:
man: "Homme"

View File

@ -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_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_declined: "Your card was declined."
#user edition form
user:
man: "Mann"

View File

@ -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_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_declined: "Your card was declined."
#user edition form
user:
man: "Homem"

View File

@ -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_reload_this_page: "crwdns9407:0crwdne9407:0"
payment_card_error: "crwdns9409:0crwdne9409:0"
payment_card_declined: "Your card was declined."
#user edition form
user:
man: "crwdns9411:0crwdne9411:0"