Skip to content

Commit

Permalink
Pre-fill user info for Link in the Payment Element (classic checkout)
Browse files Browse the repository at this point in the history
  • Loading branch information
annemirasol committed Nov 22, 2024
1 parent ab1536f commit 662605d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 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 @@ -82,6 +84,7 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
const elements = api.getStripe().elements( options );
const createdStripePaymentElement = elements.create( 'payment', {
...getUpeSettings(),
...getDefaultValues(),
wallets: {
applePay: 'never',
googlePay: 'never',
Expand All @@ -92,9 +95,25 @@ function createStripePaymentElement( api, paymentMethodType = null ) {
gatewayUPEComponents[
paymentMethodType
].upeElement = createdStripePaymentElement;

if ( isLinkEnabled() && paymentMethodType === 'card' ) {
document.getElementById( 'billing_email' ).onblur = function () {
updateStripePaymentElement();
};
}

return createdStripePaymentElement;
}

function updateStripePaymentElement() {
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
19 changes: 19 additions & 0 deletions client/stripe-utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,25 @@ export const getUpeSettings = () => {
return upeSettings;
};

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

0 comments on commit 662605d

Please sign in to comment.