Skip to content

Commit e401cbf

Browse files
committed
Improve checks in voucher purchase flow
1 parent d2fcdd0 commit e401cbf

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Fix - Fixes a possible fatal error with Multibanco purchases when generating the email instructions.
1010
* Fix - Fix buggy unsaved changes warning in settings page
1111
* Fix - Use the platform's payment method configuration id constant when rendering the Optimized Checkout
12+
* Update - Improve checks in voucher purchase flow
1213

1314
= 9.5.2 - 2025-05-22 =
1415
* Add - Implement custom database cache for persistent caching with in-memory optimization.

client/classic/upe/payment-processing.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ export const createAndConfirmSetupIntent = (
567567
* @param {Object} jQueryForm The jQuery object for the form being submitted.
568568
*/
569569
export const confirmVoucherPayment = async ( api, jQueryForm ) => {
570-
const isOrderPay = getStripeServerData()?.isOrderPay;
570+
const stripeServerData = getStripeServerData();
571+
const isOrderPay = stripeServerData?.isOrderPay;
571572

572573
// The Order Pay page does a hard refresh when the hash changes, so we need to block the UI again.
573574
if ( isOrderPay ) {
@@ -596,7 +597,7 @@ export const confirmVoucherPayment = async ( api, jQueryForm ) => {
596597
// Verify the request using the data added to the URL.
597598
if (
598599
! clientSecret ||
599-
( isOrderPay && orderId !== getStripeServerData()?.orderId )
600+
( isOrderPay && orderId !== stripeServerData?.orderId )
600601
) {
601602
jQueryForm.removeClass( 'processing' ).unblock();
602603
return;
@@ -624,13 +625,63 @@ export const confirmVoucherPayment = async ( api, jQueryForm ) => {
624625
if ( confirmPayment.error ) {
625626
throw confirmPayment.error;
626627
}
627-
628-
// Once the customer closes the voucher and there are no errors, redirect them to the order received page.
629-
window.location.href = decodeURIComponent( partials[ 4 ] );
630628
} catch ( error ) {
631629
jQueryForm.removeClass( 'processing' ).unblock();
632630
showErrorCheckout( error.message );
631+
return;
633632
}
633+
634+
let postPaymentUrl = null;
635+
try {
636+
postPaymentUrl = decodeURIComponent( partials[ 4 ] || '' );
637+
} catch ( error ) {}
638+
639+
let validatedRedirectUrl = null;
640+
if ( postPaymentUrl ) {
641+
try {
642+
const redirectUrl = new URL(
643+
postPaymentUrl,
644+
window.location.origin
645+
);
646+
647+
if ( redirectUrl.origin === window.location.origin ) {
648+
validatedRedirectUrl = redirectUrl;
649+
}
650+
} catch ( error ) {}
651+
}
652+
653+
if ( validatedRedirectUrl ) {
654+
window.location.href = validatedRedirectUrl.toString();
655+
return;
656+
}
657+
658+
if ( ! stripeServerData?.orderReceivedURL ) {
659+
showErrorCheckout(
660+
__(
661+
'There was a problem processing the payment. Please refresh the page to try again.',
662+
'woocommerce-gateway-stripe'
663+
)
664+
);
665+
return;
666+
}
667+
668+
// We didn't get a valid redirect URL, so redirect to the order received page.
669+
// If we have a numeric order ID, navigate to the order received page for that order.
670+
if (
671+
orderId &&
672+
orderId !== 'NaN' &&
673+
orderId === String( parseInt( orderId, 10 ) )
674+
) {
675+
window.location.href =
676+
stripeServerData.orderReceivedURL +
677+
'/' +
678+
encodeURIComponent( orderId ) +
679+
'/';
680+
return;
681+
}
682+
683+
// Otherwise go to the generic page.
684+
window.location.href = stripeServerData.orderReceivedURL;
634685
};
635686

636687
/**

includes/payment-methods/class-wc-stripe-upe-payment-gateway.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ public function javascript_params() {
511511
$stripe_params['genericErrorMessage'] = __( 'There was a problem processing the payment. Please check your email inbox and refresh the page to try again.', 'woocommerce-gateway-stripe' );
512512
$stripe_params['accountDescriptor'] = $this->statement_descriptor;
513513
$stripe_params['addPaymentReturnURL'] = wc_get_account_endpoint_url( 'payment-methods' );
514+
$stripe_params['orderReceivedURL'] = $this->get_return_url(); // $order argument is intentionally left empty as a fallback.
514515
$stripe_params['enabledBillingFields'] = $enabled_billing_fields;
515516
$stripe_params['cartContainsSubscription'] = $this->is_subscription_item_in_cart();
516517
$stripe_params['accountCountry'] = WC_Stripe::get_instance()->account->get_account_country();

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
119119
* Fix - Fixes a possible fatal error with Multibanco purchases when generating the email instructions
120120
* Fix - Fix buggy unsaved changes warning in settings page
121121
* Fix - Use the platform's payment method configuration id constant when rendering the Optimized Checkout
122+
* Update - Improve checks in voucher purchase flow
122123
* Tweak - Track charge completed via webhooks in order notes
123124

124125
= 9.5.2 - 2025-05-22 =

0 commit comments

Comments
 (0)