Skip to content

Commit

Permalink
Reuse errorMessage for validateStripePayment after redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
alexookah committed Jul 31, 2024
1 parent 74452f7 commit cf8e60d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 10 additions & 3 deletions woonuxt_base/app/composables/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CheckoutInlineError } from '../types/CheckoutInlineError';
export function useCheckout() {
const { t } = useI18n();
const { storeSettings } = useAppConfig();
const errorMessage = useState<string | null>('errorMessage', () => null);
const orderInput = useState<any>('orderInput', () => {
return {
customerNote: '',
Expand Down Expand Up @@ -222,7 +223,7 @@ export function useCheckout() {

const validateStripePaymentFromRedirect = async (stripe: Stripe, clientSecret: string, redirectStatus: string) => {
try {
if (redirectStatus !== 'succeeded') throw new Error('Redirect status not suceeded');
if (redirectStatus !== 'succeeded') throw new CheckoutInlineError(t('messages.error.paymentFailed'));

isProcessingOrder.value = true;
const { paymentIntent, error } = await stripe.retrievePaymentIntent(clientSecret);
Expand All @@ -240,7 +241,7 @@ export function useCheckout() {
case "requires_payment_method":
// If the payment attempt fails (for example due to a decline),
// the PaymentIntent’s status returns to requires_payment_method so that the payment can be retried.
throw new Error(t('messages.error.paymentFailed'));
throw new CheckoutInlineError(t('messages.error.paymentFailed'));
default:
throw new Error("Something went wrong. ('" + paymentIntent?.status + "')");
}
Expand All @@ -250,7 +251,12 @@ export function useCheckout() {

useRouter().push({ query: {} });
manageCheckoutLocalStorage(false);
alert(error?.message || t('messages.error.orderFailed'));

if (error instanceof CheckoutInlineError) {
errorMessage.value = error.message;
} else {
alert(error);
}
}
};

Expand All @@ -274,6 +280,7 @@ export function useCheckout() {
return {
orderInput,
isProcessingOrder,
errorMessage,
stripeCheckout,
validateStripePaymentFromRedirect,
proccessCheckout,
Expand Down
3 changes: 1 addition & 2 deletions woonuxt_base/app/pages/checkout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { t } = useI18n();
const { query } = useRoute();
const { cart, isUpdatingCart, paymentGateways } = useCart();
const { customer, viewer } = useAuth();
const { orderInput, isProcessingOrder, proccessCheckout, stripeCheckout, validateStripePaymentFromRedirect } = useCheckout();
const { orderInput, isProcessingOrder, proccessCheckout, stripeCheckout, errorMessage, validateStripePaymentFromRedirect } = useCheckout();
const runtimeConfig = useRuntimeConfig();
const isCheckoutDisabled = computed<boolean>(
Expand All @@ -19,7 +19,6 @@ const isCheckoutDisabled = computed<boolean>(
);
const isInvalidEmail = ref<boolean>(false);
const errorMessage = useState<string | null>('errorMessage', () => null);
const stripe = ref<Stripe | null>(null);
const elements = ref<StripeElements | null>(null);
Expand Down

0 comments on commit cf8e60d

Please sign in to comment.