Skip to content

Commit 99cabe3

Browse files
authored
Pre-fill user info for Link in the Payment Element (classic checkout) (#3621)
* Pre-fill user info for Link in the Payment Element (classic checkout) * Update changelog and readme entries * Refactor: move common code to function
1 parent ab1536f commit 99cabe3

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
*** Changelog ***
22

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

client/classic/upe/payment-processing.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
appendPaymentMethodIdToForm,
44
getPaymentMethodTypes,
55
initializeUPEAppearance,
6+
isLinkEnabled,
7+
getDefaultValues,
68
getStripeServerData,
79
getUpeSettings,
810
showErrorCheckout,
@@ -80,8 +82,18 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
8082
};
8183

8284
const elements = api.getStripe().elements( options );
85+
86+
const attachDefaultValuesUpdateEvent = ( element ) => {
87+
if ( document.getElementById( element ) ) {
88+
document.getElementById( element ).onblur = function () {
89+
updatePaymentElementDefaultValues();
90+
};
91+
}
92+
};
93+
8394
const createdStripePaymentElement = elements.create( 'payment', {
8495
...getUpeSettings(),
96+
...getDefaultValues(),
8597
wallets: {
8698
applePay: 'never',
8799
googlePay: 'never',
@@ -92,9 +104,33 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
92104
gatewayUPEComponents[
93105
paymentMethodType
94106
].upeElement = createdStripePaymentElement;
107+
108+
// When email or phone is updated and Link is enabled, we need to
109+
// update the payment element to update its default values.
110+
if (
111+
getStripeServerData()?.isCheckout &&
112+
isLinkEnabled() &&
113+
paymentMethodType === 'card'
114+
) {
115+
attachDefaultValuesUpdateEvent( 'billing_email' );
116+
attachDefaultValuesUpdateEvent( 'billing_phone' );
117+
}
118+
95119
return createdStripePaymentElement;
96120
}
97121

122+
/**
123+
* Updates the payment element's default values.
124+
*/
125+
function updatePaymentElementDefaultValues() {
126+
if ( ! gatewayUPEComponents?.card?.upeElement ) {
127+
return;
128+
}
129+
130+
const paymentElement = gatewayUPEComponents.card.upeElement;
131+
paymentElement.update( getDefaultValues() );
132+
}
133+
98134
/**
99135
* Submits the provided jQuery form and removes the 'processing' class from it.
100136
*

client/stripe-utils/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,32 @@ export const getUpeSettings = () => {
416416
return upeSettings;
417417
};
418418

419+
/**
420+
* Craft the defaultValues parameter, used to pre-fill
421+
* user email and phone number for Link in the Payment Element.
422+
*
423+
* @return {Object} The defaultValues object for the Payment Element.
424+
*/
425+
export const getDefaultValues = () => {
426+
const userEmail = document.getElementById( 'billing_email' )?.value;
427+
if ( ! userEmail ) {
428+
return {};
429+
}
430+
431+
const userPhone =
432+
document.getElementById( 'billing_phone' )?.value ||
433+
document.getElementById( 'shipping_phone' )?.value;
434+
435+
return {
436+
defaultValues: {
437+
billingDetails: {
438+
email: userEmail,
439+
phone: userPhone,
440+
},
441+
},
442+
};
443+
};
444+
419445
/**
420446
* Show error notice at top of checkout form.
421447
* Will try to use a translatable message using the message code if available

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

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

0 commit comments

Comments
 (0)