Skip to content

Commit 0903542

Browse files
Update PRB payment method generation to use PaymentMethods rather than sources (#3510)
* Update PRB payment method generation to use PaymentMethods rather than sources * Pass deferred intent flag * Add missing , * Add changelog entries * Update the cart and checkout block integration of PRBs * Pass stripe_source arg for compatibility with legacy checkout mode --------- Co-authored-by: Diego Curbelo <[email protected]>
1 parent 68380da commit 0903542

File tree

5 files changed

+63
-55
lines changed

5 files changed

+63
-55
lines changed

assets/js/stripe-payment-request.js

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ jQuery( function( $ ) {
6969
};
7070
},
7171

72-
processSource: function( source, paymentRequestType ) {
73-
var data = wc_stripe_payment_request.getOrderData( source, paymentRequestType );
72+
processPaymentMethod: function( paymentMethod, paymentRequestType ) {
73+
var data = wc_stripe_payment_request.getOrderData( paymentMethod, paymentRequestType );
7474

7575
return $.ajax( {
7676
type: 'POST',
@@ -85,46 +85,49 @@ jQuery( function( $ ) {
8585
*
8686
* @since 3.1.0
8787
* @version 4.0.0
88-
* @param {PaymentResponse} source Payment Response instance.
88+
* @param {Object} evt The event object containing payment details and user information.
89+
* @param {string} paymentRequestType The type of payment request, either 'google_pay' or 'apple_pay'.
8990
*
90-
* @return {Object}
91+
* @return {Object} Processed order data for further handling, including shipping, billing, and payment method information.
9192
*/
9293
getOrderData: function( evt, paymentRequestType ) {
93-
var source = evt.source;
94-
var email = source.owner.email;
95-
var phone = source.owner.phone;
96-
var billing = source.owner.address;
97-
var name = source.owner.name ?? evt.payerName;
98-
var shipping = evt.shippingAddress;
99-
var data = {
100-
_wpnonce: wc_stripe_payment_request_params.nonce.checkout,
101-
billing_first_name: name?.split( ' ' )?.slice( 0, 1 )?.join( ' ' ) ?? '',
102-
billing_last_name: name?.split( ' ' )?.slice( 1 )?.join( ' ' ) || '-',
103-
billing_company: '',
104-
billing_email: null !== email ? email : evt.payerEmail,
105-
billing_phone: null !== phone ? phone : evt.payerPhone && evt.payerPhone.replace( '/[() -]/g', '' ),
106-
billing_country: null !== billing ? billing.country : '',
107-
billing_address_1: null !== billing ? billing.line1 : '',
108-
billing_address_2: null !== billing ? billing.line2 : '',
109-
billing_city: null !== billing ? billing.city : '',
110-
billing_state: null !== billing ? billing.state : '',
111-
billing_postcode: null !== billing ? billing.postal_code : '',
112-
shipping_first_name: '',
113-
shipping_last_name: '',
114-
shipping_company: '',
115-
shipping_country: '',
116-
shipping_address_1: '',
117-
shipping_address_2: '',
118-
shipping_city: '',
119-
shipping_state: '',
120-
shipping_postcode: '',
121-
shipping_method: [ null === evt.shippingOption ? null : evt.shippingOption.id ],
122-
order_comments: '',
123-
payment_method: 'stripe',
124-
ship_to_different_address: 1,
125-
terms: 1,
126-
stripe_source: source.id,
127-
payment_request_type: paymentRequestType
94+
var paymentMethod = evt.paymentMethod;
95+
var email = paymentMethod.billing_details.email;
96+
var phone = paymentMethod.billing_details.phone;
97+
var billing = paymentMethod.billing_details.address;
98+
var name = paymentMethod.billing_details.name ?? evt.payerName;
99+
var shipping = evt.shippingAddress;
100+
var data = {
101+
_wpnonce: wc_stripe_payment_request_params.nonce.checkout,
102+
billing_first_name: name?.split( ' ' )?.slice( 0, 1 )?.join( ' ' ) ?? '',
103+
billing_last_name: name?.split( ' ' )?.slice( 1 )?.join( ' ' ) || '-',
104+
billing_company: '',
105+
billing_email: null !== email ? email : evt.payerEmail,
106+
billing_phone: null !== phone ? phone : evt.payerPhone && evt.payerPhone.replace( '/[() -]/g', '' ),
107+
billing_country: null !== billing ? billing.country : '',
108+
billing_address_1: null !== billing ? billing.line1 : '',
109+
billing_address_2: null !== billing ? billing.line2 : '',
110+
billing_city: null !== billing ? billing.city : '',
111+
billing_state: null !== billing ? billing.state : '',
112+
billing_postcode: null !== billing ? billing.postal_code : '',
113+
shipping_first_name: '',
114+
shipping_last_name: '',
115+
shipping_company: '',
116+
shipping_country: '',
117+
shipping_address_1: '',
118+
shipping_address_2: '',
119+
shipping_city: '',
120+
shipping_state: '',
121+
shipping_postcode: '',
122+
shipping_method: [ null === evt.shippingOption ? null : evt.shippingOption.id ],
123+
order_comments: '',
124+
payment_method: 'stripe',
125+
ship_to_different_address: 1,
126+
terms: 1,
127+
'wc-stripe-payment-method': paymentMethod.id,
128+
stripe_source: paymentMethod.id, // Needed to process the payment in legacy checkout mode.
129+
payment_request_type: paymentRequestType,
130+
'wc-stripe-is-deferred-intent': true,
128131
};
129132

130133
if ( shipping ) {
@@ -480,12 +483,12 @@ jQuery( function( $ ) {
480483
} );
481484
} );
482485

483-
paymentRequest.on( 'source', function( evt ) {
486+
paymentRequest.on( 'paymentmethod', function( evt ) {
484487
// Check if we allow prepaid cards.
485488
if ( 'no' === wc_stripe_payment_request_params.stripe.allow_prepaid_card && 'prepaid' === evt.source.card.funding ) {
486489
wc_stripe_payment_request.abortPayment( evt, wc_stripe_payment_request.getErrorMessageHTML( wc_stripe_payment_request_params.i18n.no_prepaid_card ) );
487490
} else {
488-
$.when( wc_stripe_payment_request.processSource( evt, paymentRequestType ) ).then( function( response ) {
491+
$.when( wc_stripe_payment_request.processPaymentMethod( evt, paymentRequestType ) ).then( function( response ) {
489492
if ( 'success' === response.result ) {
490493
wc_stripe_payment_request.completePayment( evt, response.redirect );
491494
} else {

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Fix - Resolved an issue which caused the WeChat Pay payment icon to not be displayed on shortcode checkout pages.
3232
* Fix - Set order payment method title to the customizable title setting rather than the default label.
3333
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
34+
* Fix - Switch Google Pay and Apple Pay PRB flow to create PaymentMethod (pm_) instead of Source (src_), utilizing deferred intent processing.
3435
* Tweak - Add order lock for redirect payments.
3536
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
3637
* Fix - Allow legacy `src_` payment methods linked to a saved credit card to be displayed on the checkout and My Account pages when the new checkout experience is enabled.

client/blocks/normalize.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,27 @@ import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
1414
/**
1515
* Normalizes order data received upon creating an order using the store's AJAX API.
1616
*
17-
* @param {Object} sourceEvent - The source event that triggered the creation of the order.
17+
* @param {Object} paymentMethodEvent - The payment method event that triggered the creation of the order.
1818
* @param {string} paymentRequestType - The payment request type.
1919
*/
20-
const normalizeOrderData = ( sourceEvent, paymentRequestType ) => {
21-
const { source } = sourceEvent;
22-
const email = source?.owner?.email;
23-
const phone = source?.owner?.phone;
24-
const billing = source?.owner?.address;
25-
const name = source?.owner?.name ?? sourceEvent.payerName;
26-
const shipping = sourceEvent?.shippingAddress;
20+
const normalizeOrderData = ( paymentMethodEvent, paymentRequestType ) => {
21+
const paymentMethod = paymentMethodEvent.paymentMethod;
22+
const email = paymentMethod.billing_details.email;
23+
const phone = paymentMethod.billing_details.phone;
24+
const billing = paymentMethod.billing_details.address;
25+
const shipping = paymentMethodEvent.shippingAddress;
26+
const name =
27+
paymentMethod.billing_details.name ?? paymentMethodEvent.payerName;
2728

2829
const data = {
2930
_wpnonce: getBlocksConfiguration()?.nonce?.checkout,
3031
billing_first_name:
3132
name?.split( ' ' )?.slice( 0, 1 )?.join( ' ' ) ?? '',
3233
billing_last_name: name?.split( ' ' )?.slice( 1 )?.join( ' ' ) ?? '',
3334
billing_company: '',
34-
billing_email: email ?? sourceEvent?.payerEmail,
35+
billing_email: email ?? paymentMethodEvent?.payerEmail,
3536
billing_phone:
36-
phone ?? sourceEvent?.payerPhone?.replace( '/[() -]/g', '' ),
37+
phone ?? paymentMethodEvent?.payerPhone?.replace( '/[() -]/g', '' ),
3738
billing_country: billing?.country ?? '',
3839
billing_address_1: billing?.line1 ?? '',
3940
billing_address_2: billing?.line2 ?? '',
@@ -49,13 +50,15 @@ const normalizeOrderData = ( sourceEvent, paymentRequestType ) => {
4950
shipping_city: '',
5051
shipping_state: '',
5152
shipping_postcode: '',
52-
shipping_method: [ sourceEvent?.shippingOption?.id ],
53+
shipping_method: [ paymentMethodEvent?.shippingOption?.id ],
5354
order_comments: '',
5455
payment_method: 'stripe',
5556
ship_to_different_address: 1,
5657
terms: 1,
57-
stripe_source: source.id,
58+
'wc-stripe-payment-method': paymentMethod.id,
59+
stripe_source: paymentMethod.id, // Needed to process the payment in legacy checkout mode.
5860
payment_request_type: paymentRequestType,
61+
'wc-stripe-is-deferred-intent': true,
5962
};
6063

6164
if ( shipping ) {

client/blocks/payment-request/hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export const useProcessPaymentHandler = (
186186
) => {
187187
useEffect( () => {
188188
const handler = paymentRequest?.on(
189-
'source',
189+
'paymentmethod',
190190
paymentProcessingHandler(
191191
stripe,
192192
paymentRequestType,
@@ -196,7 +196,7 @@ export const useProcessPaymentHandler = (
196196

197197
return () => {
198198
// Need to use `?.` here in case paymentRequest is null.
199-
handler?.removeEventListener( 'source' );
199+
handler?.removeEventListener( 'paymentmethod' );
200200
};
201201
}, [ stripe, paymentRequest, paymentRequestType, setExpressPaymentError ] );
202202
};

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
159159
* Fix - Resolved an issue which caused the WeChat Pay payment icon to not be displayed on shortcode checkout pages.
160160
* Fix - Set order payment method title to the customizable title setting rather than the default label.
161161
* Fix - Update Cash App payments to avoid confirming on creation, resolving issues with generic payment failures in live mode.
162+
* Fix - Switch Google Pay and Apple Pay PRB flow to create PaymentMethod (pm_) instead of Source (src_), utilizing deferred intent processing.
162163
* Tweak - Add order lock for redirect payments.
163164
* Fix - Missing Stripe Fee and Stripe Payout details on orders that were captured manually.
164165
* Fix - Allow legacy `src_` payment methods linked to a saved credit card to be displayed on the checkout and My Account pages when the new checkout experience is enabled.

0 commit comments

Comments
 (0)