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

Pre-fill user info for Link in the Payment Element (classic checkout) #3621

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*** Changelog ***

= 9.0.0 - xxxx-xx-xx =
* Add - Pre-fill user email and phone number for Link in the Payment Element and block checkout.
* Add - Pre-fill user email and phone number for Link in the Payment Element.
* Remove - Remove Link autofill modal feature.
* Update - Improve accuracy of webhook status information displayed in settings page.
* Tweak - Standardize ECE Express payment buttons on Pay for Order page to match cart and checkout itemization behavior.
Expand Down
36 changes: 36 additions & 0 deletions client/classic/upe/payment-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
appendPaymentMethodIdToForm,
getPaymentMethodTypes,
initializeUPEAppearance,
isLinkEnabled,
getDefaultValues,
getStripeServerData,
getUpeSettings,
showErrorCheckout,
Expand Down Expand Up @@ -80,8 +82,18 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
};

const elements = api.getStripe().elements( options );

const attachDefaultValuesUpdateEvent = ( element ) => {
if ( document.getElementById( element ) ) {
document.getElementById( element ).onblur = function () {
updatePaymentElementDefaultValues();
};
}
};

const createdStripePaymentElement = elements.create( 'payment', {
...getUpeSettings(),
...getDefaultValues(),
wallets: {
applePay: 'never',
googlePay: 'never',
Expand All @@ -92,9 +104,33 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
gatewayUPEComponents[
paymentMethodType
].upeElement = createdStripePaymentElement;

// When email or phone is updated and Link is enabled, we need to
// update the payment element to update its default values.
if (
getStripeServerData()?.isCheckout &&
isLinkEnabled() &&
paymentMethodType === 'card'
) {
attachDefaultValuesUpdateEvent( 'billing_email' );
attachDefaultValuesUpdateEvent( 'billing_phone' );
}

return createdStripePaymentElement;
}

/**
* Updates the payment element's default values.
*/
function updatePaymentElementDefaultValues() {
if ( ! gatewayUPEComponents?.card?.upeElement ) {
return;
}

const paymentElement = gatewayUPEComponents.card.upeElement;
paymentElement.update( getDefaultValues() );
}

/**
* Submits the provided jQuery form and removes the 'processing' class from it.
*
Expand Down
26 changes: 26 additions & 0 deletions client/stripe-utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,32 @@ export const getUpeSettings = () => {
return upeSettings;
};

/**
* Craft the defaultValues parameter, used to pre-fill
* user email and phone number for Link in the Payment Element.
*
* @return {Object} The defaultValues object for the Payment Element.
*/
export const getDefaultValues = () => {
const userEmail = document.getElementById( 'billing_email' )?.value;
if ( ! userEmail ) {
return {};
}

const userPhone =
document.getElementById( 'billing_phone' )?.value ||
document.getElementById( 'shipping_phone' )?.value;

return {
defaultValues: {
billingDetails: {
email: userEmail,
phone: userPhone,
},
},
};
};

/**
* Show error notice at top of checkout form.
* Will try to use a translatable message using the message code if available
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.0.0 - xxxx-xx-xx =
* Add - Pre-fill user email and phone number for Link in the Payment Element and block checkout.
* Add - Pre-fill user email and phone number for Link in the Payment Element.
* Remove - Remove Link autofill modal feature.
* Update - Improve accuracy of webhook status information displayed in settings page.
* Tweak - Standardize ECE Express payment buttons on Pay for Order page to match cart and checkout itemization behavior.
Expand Down
Loading