Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PE with deferred intent for the card payment method in the classic checkout #2753

Merged
merged 26 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e597d13
Separate adding hooks from the constructor for WC_Stripe_Intent_Contr…
a-danae Nov 10, 2023
beca67a
Make wc-stripe-upe-element a class instead of an id
a-danae Nov 10, 2023
9104244
Render the Payment element in the classic checkout page without creat…
a-danae Nov 10, 2023
5612e99
Process payments with deferred intent in the classic checkout
a-danae Nov 12, 2023
7fc37e0
Fix expected dom element in unit tests
a-danae Nov 13, 2023
75a876b
Update process_payment_with_deferred_intent
a-danae Nov 13, 2023
93339f5
Add base uni test for PE with deferred intent processing
a-danae Nov 13, 2023
60c3529
Move some logic out of create_and_confirm_payment_intent
a-danae Nov 14, 2023
df620b9
Add validation for the payment information array
a-danae Nov 14, 2023
f30d1e8
Update unit tests for processing payments with a deferred intent
a-danae Nov 14, 2023
61b31e6
Merge branch 'develop' into add/card-classic-with-deferred-intent
a-danae Nov 14, 2023
fc72a63
Improve the error messaging on processing failures
a-danae Nov 17, 2023
bd1ff2d
Remove unneeded test
a-danae Nov 17, 2023
1903089
Fix typo in doc block
a-danae Nov 21, 2023
8098d58
Remove resolved TODO comment
a-danae Nov 21, 2023
3c4fb1e
Use the order currency instead of the store currency when creating an…
a-danae Nov 21, 2023
cf90c3c
Remove inline TODO comments that already have issues to address them
a-danae Nov 22, 2023
fef7018
Remove unnecesary order status assignment
a-danae Nov 22, 2023
7a4d634
Remove TODO that will be addressed in a separate issue
a-danae Nov 22, 2023
36f4f53
Set the selected payment method type as the order's payment method title
a-danae Nov 23, 2023
61d9e34
Include shipping information for the payment intent when shipping is …
a-danae Nov 23, 2023
6697749
Pass the appearance and fonts parameters to the Elements initializati…
a-danae Nov 23, 2023
646299c
Validate whether the provided payment method type is valid and allowe…
a-danae Nov 23, 2023
863cfc7
Check whether the dom elements exist before changing their attributes
a-danae Nov 24, 2023
1c2f308
Fix console error when checking out as a guest
a-danae Nov 24, 2023
59c3428
Adjust unit tests
a-danae Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions client/classic/upe/deferred-intent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import jQuery from 'jquery';
import WCStripeAPI from '../../api';
import {
generateCheckoutEventNames,
getSelectedUPEGatewayPaymentMethod,
getStripeServerData,
isUsingSavedPaymentMethod,
} from '../../stripe-utils';
import './style.scss';
import {
processPayment,
mountStripePaymentElement,
} from './payment-processing';

jQuery( function ( $ ) {
// Create an API object, which will be used throughout the checkout.
const api = new WCStripeAPI(
getStripeServerData(),
// A promise-based interface to jQuery.post.
( url, args ) => {
return new Promise( ( resolve, reject ) => {
jQuery.post( url, args ).then( resolve ).fail( reject );
} );
}
);

// Only attempt to mount the card element once that section of the page has loaded.
// We can use the updated_checkout event for this.
$( document.body ).on( 'updated_checkout', () => {
maybeMountStripePaymentElement();
} );

$( 'form.checkout' ).on( generateCheckoutEventNames(), function () {
return processPaymentIfNotUsingSavedMethod( $( this ) );
} );

function processPaymentIfNotUsingSavedMethod( $form ) {
const paymentMethodType = getSelectedUPEGatewayPaymentMethod();
if ( ! isUsingSavedPaymentMethod( paymentMethodType ) ) {
return processPayment( api, $form, paymentMethodType );
}
}

// If the card element selector doesn't exist, then do nothing.
// For example, when a 100% discount coupon is applied).
// We also don't re-mount if already mounted in DOM.
async function maybeMountStripePaymentElement() {
if (
$( '.wc-stripe-upe-element' ).length &&
! $( '.wc-stripe-upe-element' ).children().length
) {
for ( const upeElement of $(
'.wc-stripe-upe-element'
).toArray() ) {
await mountStripePaymentElement( api, upeElement );
}
}
}
} );
29 changes: 6 additions & 23 deletions client/classic/upe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getFontRulesFromPage, getAppearance } from '../../styles/upe';
import enableStripeLinkPaymentMethod from '../../stripe-link';
import { legacyHashchangeHandler } from './legacy-support';
import './style.scss';
import './deferred-intent.js';

jQuery( function ( $ ) {
const key = getStripeServerData()?.key;
Expand Down Expand Up @@ -200,7 +201,7 @@ jQuery( function ( $ ) {
// Do not recreate UPE element unnecessarily.
if ( upeElement ) {
upeElement.unmount();
upeElement.mount( '#wc-stripe-upe-element' );
upeElement.mount( '.wc-stripe-upe-element' );
return;
}

Expand All @@ -221,7 +222,7 @@ jQuery( function ( $ ) {
// I repeat, do NOT recreate UPE element unnecessarily.
if ( upeElement || paymentIntentId ) {
upeElement.unmount();
upeElement.mount( '#wc-stripe-upe-element' );
upeElement.mount( '.wc-stripe-upe-element' );
return;
}

Expand Down Expand Up @@ -302,7 +303,7 @@ jQuery( function ( $ ) {
}

upeElement = elements.create( 'payment', upeSettings );
upeElement.mount( '#wc-stripe-upe-element' );
upeElement.mount( '.wc-stripe-upe-element' );

upeElement.on( 'ready', () => {
unblockUI( $( upeLoadingSelector ) );
Expand Down Expand Up @@ -339,31 +340,13 @@ jQuery( function ( $ ) {
} );
};

// Only attempt to mount the card element once that section of the page has loaded. We can use the updated_checkout
// event for this. This part of the page can also reload based on changes to checkout details, so we call unmount
// first to ensure the card element is re-mounted correctly.
$( document.body ).on( 'updated_checkout', () => {
// If the card element selector doesn't exist, then do nothing (for example, when a 100% discount coupon is applied).
// We also don't re-mount if already mounted in DOM.
if (
$( '#wc-stripe-upe-element' ).length &&
! $( '#wc-stripe-upe-element' ).children().length &&
isUPEEnabled
) {
const isSetupIntent = ! (
getStripeServerData()?.isPaymentNeeded ?? true
);
mountUPEElement( isSetupIntent );
}
} );

if (
$( 'form#add_payment_method' ).length ||
$( 'form#order_review' ).length
) {
if (
$( '#wc-stripe-upe-element' ).length &&
! $( '#wc-stripe-upe-element' ).children().length &&
$( '.wc-stripe-upe-element' ).length &&
! $( '.wc-stripe-upe-element' ).children().length &&
isUPEEnabled &&
! upeElement
) {
Expand Down
Loading