Skip to content

Commit b6a3669

Browse files
timur27Timur Karimov
andauthored
Consolidate gateways implementation - step I (#7937)
Co-authored-by: Timur Karimov <[email protected]>
1 parent b24197f commit b6a3669

19 files changed

+231
-2312
lines changed

changelog/cleanup-upe-gateways-part-1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: dev
3+
4+
Cleanup the deprecated payment gateway processing - part V

client/checkout/api/index.js

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,6 @@ export default class WCPayAPI {
335335
};
336336
}
337337

338-
/**
339-
* Creates a setup intent without confirming it.
340-
*
341-
* @return {Promise} The final promise for the request to the server.
342-
*/
343-
initSetupIntent() {
344-
const path = 'init_setup_intent';
345-
346-
return this.request( buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ), {
347-
_ajax_nonce: getConfig( 'createSetupIntentNonce' ),
348-
} ).then( ( response ) => {
349-
if ( ! response.success ) {
350-
throw response.data.error;
351-
}
352-
return response.data;
353-
} );
354-
}
355-
356338
/**
357339
* Sets up an intent based on a payment method.
358340
*
@@ -389,92 +371,6 @@ export default class WCPayAPI {
389371
} );
390372
}
391373

392-
/**
393-
* Creates an intent based on a payment method.
394-
*
395-
* @param {Object} options Object containing intent optional parameters (fingerprint, paymentMethodType, orderId)
396-
*
397-
* @return {Promise} The final promise for the request to the server.
398-
*/
399-
createIntent( options ) {
400-
const { fingerprint, orderId } = options;
401-
const path = 'create_payment_intent';
402-
const params = {
403-
_ajax_nonce: getConfig( 'createPaymentIntentNonce' ),
404-
'wcpay-fingerprint': fingerprint,
405-
};
406-
407-
if ( orderId ) {
408-
params.wcpay_order_id = orderId;
409-
}
410-
411-
return this.request(
412-
buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ),
413-
params
414-
)
415-
.then( ( response ) => {
416-
if ( ! response.success ) {
417-
throw response.data.error;
418-
}
419-
return response.data;
420-
} )
421-
.catch( ( error ) => {
422-
if ( error.message ) {
423-
throw error;
424-
} else {
425-
// Covers the case of error on the Ajax request.
426-
throw new Error( error.statusText );
427-
}
428-
} );
429-
}
430-
431-
/**
432-
* Updates a payment intent with data from order: customer, level3 data and maybe sets the payment for future use.
433-
*
434-
* @param {string} paymentIntentId The id of the payment intent.
435-
* @param {int} orderId The id of the order.
436-
* @param {string} savePaymentMethod 'yes' if saving.
437-
* @param {string} selectedUPEPaymentType The name of the selected UPE payment type or empty string.
438-
* @param {string?} paymentCountry The payment two-letter iso country code or null.
439-
* @param {string?} fingerprint User fingerprint.
440-
*
441-
* @return {Promise} The final promise for the request to the server.
442-
*/
443-
updateIntent(
444-
paymentIntentId,
445-
orderId,
446-
savePaymentMethod,
447-
selectedUPEPaymentType,
448-
paymentCountry,
449-
fingerprint
450-
) {
451-
const path = 'update_payment_intent';
452-
453-
return this.request( buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ), {
454-
wcpay_order_id: orderId,
455-
wc_payment_intent_id: paymentIntentId,
456-
save_payment_method: savePaymentMethod,
457-
wcpay_selected_upe_payment_type: selectedUPEPaymentType,
458-
wcpay_payment_country: paymentCountry,
459-
_ajax_nonce: getConfig( 'updatePaymentIntentNonce' ),
460-
'wcpay-fingerprint': fingerprint,
461-
} )
462-
.then( ( response ) => {
463-
if ( response.result === 'failure' ) {
464-
throw new Error( response.messages );
465-
}
466-
return response;
467-
} )
468-
.catch( ( error ) => {
469-
if ( error.message ) {
470-
throw error;
471-
} else {
472-
// Covers the case of error on the Ajaxrequest.
473-
throw new Error( error.statusText );
474-
}
475-
} );
476-
}
477-
478374
/**
479375
* Confirm Stripe payment with fallback for rate limit error.
480376
*

client/checkout/utils/test/upe.test.js

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
getTerms,
66
getCookieValue,
77
isWCPayChosen,
8-
getPaymentIntentFromSession,
98
generateCheckoutEventNames,
109
getUpeSettings,
1110
getStripeElementOptions,
@@ -168,71 +167,6 @@ describe( 'UPE checkout utils', () => {
168167
} );
169168
} );
170169

171-
describe( 'getPaymentIntentFromSession', () => {
172-
const paymentMethodsConfig = {
173-
card: {
174-
upePaymentIntentData:
175-
'abcd1234-pi_abc123-pi_abc123_secret_5678xyz',
176-
},
177-
eps: {
178-
upePaymentIntentData: null,
179-
},
180-
};
181-
182-
const cardData = {
183-
clientSecret: 'pi_abc123_secret_5678xyz',
184-
intentId: 'pi_abc123',
185-
};
186-
187-
it( 'should return the correct client secret and intent ID', () => {
188-
Object.defineProperty( document, 'cookie', {
189-
get: () => {
190-
return 'woocommerce_cart_hash=abcd1234;';
191-
},
192-
configurable: true,
193-
} );
194-
expect(
195-
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
196-
).toEqual( cardData );
197-
} );
198-
199-
it( 'should return an empty object if no payment intent exists', () => {
200-
Object.defineProperty( document, 'cookie', {
201-
get: () => {
202-
return 'woocommerce_cart_hash=abcd1234;';
203-
},
204-
configurable: true,
205-
} );
206-
expect(
207-
getPaymentIntentFromSession( paymentMethodsConfig, 'eps' )
208-
).toEqual( {} );
209-
} );
210-
211-
it( 'should return an empty object if no cart hash exists', () => {
212-
Object.defineProperty( document, 'cookie', {
213-
get: () => {
214-
return 'woocommerce_cart_items=1;';
215-
},
216-
configurable: true,
217-
} );
218-
expect(
219-
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
220-
).toEqual( {} );
221-
} );
222-
223-
it( 'should return an empty object if the payment intent data does not start with the cart hash', () => {
224-
Object.defineProperty( document, 'cookie', {
225-
get: () => {
226-
return 'woocommerce_cart_hash=xyz9876;';
227-
},
228-
configurable: true,
229-
} );
230-
expect(
231-
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
232-
).toEqual( {} );
233-
} );
234-
} );
235-
236170
describe( 'getUPESettings', () => {
237171
afterEach( () => {
238172
const checkboxElement = document.getElementById(

client/checkout/utils/upe.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,6 @@ export const isWCPayChosen = function () {
4444
.checked;
4545
};
4646

47-
/**
48-
* Returns the cached payment intent for the current cart state.
49-
*
50-
* @param {Object} paymentMethodsConfig Array of configs for payment methods.
51-
* @param {string} paymentMethodType Type of the payment method.
52-
* @return {Object} The intent id and client secret required for mounting the UPE element.
53-
*/
54-
export const getPaymentIntentFromSession = (
55-
paymentMethodsConfig,
56-
paymentMethodType
57-
) => {
58-
const cartHash = getCookieValue( 'woocommerce_cart_hash' );
59-
const upePaymentIntentData =
60-
paymentMethodsConfig[ paymentMethodType ].upePaymentIntentData;
61-
62-
if (
63-
cartHash &&
64-
upePaymentIntentData &&
65-
upePaymentIntentData.startsWith( cartHash )
66-
) {
67-
const intentId = upePaymentIntentData.split( '-' )[ 1 ];
68-
const clientSecret = upePaymentIntentData.split( '-' )[ 2 ];
69-
return { intentId, clientSecret };
70-
}
71-
72-
return {};
73-
};
74-
7547
/**
7648
* Finds selected payment gateway and returns matching Stripe payment method for gateway.
7749
*

includes/class-wc-payment-gateway-wcpay.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use WCPay\Logger;
3838
use WCPay\Payment_Information;
3939
use WCPay\Payment_Methods\UPE_Payment_Gateway;
40-
use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
4140
use WCPay\Payment_Methods\Link_Payment_Method;
4241
use WCPay\WooPay\WooPay_Order_Status_Sync;
4342
use WCPay\WooPay\WooPay_Utilities;
@@ -1644,19 +1643,8 @@ public function set_payment_method_title_for_order( $order, $payment_method_type
16441643
* @return array Array of keyed metadata values.
16451644
*/
16461645
protected function get_metadata_from_order( $order, $payment_type ) {
1647-
$service = wcpay_get_container()->get( OrderService::class );
1648-
$metadata = $service->get_payment_metadata( $order->get_id(), $payment_type );
1649-
1650-
if ( $this instanceof UPE_Split_Payment_Gateway ) {
1651-
$gateway_type = 'split_upe_with_deferred_intent_creation';
1652-
} elseif ( $this instanceof UPE_Payment_Gateway ) {
1653-
$gateway_type = 'legacy_upe';
1654-
} else {
1655-
$gateway_type = 'legacy_card';
1656-
}
1657-
$metadata['gateway_type'] = $gateway_type;
1658-
1659-
return $metadata;
1646+
$service = wcpay_get_container()->get( OrderService::class );
1647+
return $service->get_payment_metadata( $order->get_id(), $payment_type );
16601648
}
16611649

16621650
/**
@@ -3729,7 +3717,7 @@ private function upe_needs_redirection( $payment_methods ) {
37293717
/**
37303718
* Modifies the create intent parameters when processing a payment.
37313719
*
3732-
* Currently used by child UPE_Split_Payment_Gateway to add required shipping information for Afterpay.
3720+
* Currently used by child UPE_Payment_Gateway to add required shipping information for Afterpay.
37333721
*
37343722
* @param Create_And_Confirm_Intention $request The request object for creating and confirming intention.
37353723
* @param Payment_Information $payment_information The payment information object.

includes/class-wc-payments-checkout.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,12 @@ public function __construct(
9595
public function init_hooks() {
9696
add_action( 'wc_payments_set_gateway', [ $this, 'set_gateway' ] );
9797
add_action( 'wc_payments_add_upe_payment_fields', [ $this, 'payment_fields' ] );
98-
add_action( 'woocommerce_after_account_payment_methods', [ $this->gateway, 'remove_upe_setup_intent_from_session' ], 10, 0 );
99-
add_action( 'woocommerce_subscription_payment_method_updated', [ $this->gateway, 'remove_upe_setup_intent_from_session' ], 10, 0 );
100-
add_action( 'woocommerce_order_payment_status_changed', [ get_class( $this->gateway ), 'remove_upe_payment_intent_from_session' ], 10, 0 );
10198
add_action( 'wp', [ $this->gateway, 'maybe_process_upe_redirect' ] );
10299
add_action( 'wc_ajax_wcpay_log_payment_error', [ $this->gateway, 'log_payment_error_ajax' ] );
103100
add_action( 'wp_ajax_save_upe_appearance', [ $this->gateway, 'save_upe_appearance_ajax' ] );
104101
add_action( 'wp_ajax_nopriv_save_upe_appearance', [ $this->gateway, 'save_upe_appearance_ajax' ] );
105102
add_action( 'switch_theme', [ $this->gateway, 'clear_upe_appearance_transient' ] );
106103
add_action( 'woocommerce_woocommerce_payments_updated', [ $this->gateway, 'clear_upe_appearance_transient' ] );
107-
add_action( 'wc_ajax_wcpay_init_setup_intent', [ $this->gateway, 'init_setup_intent_ajax' ] );
108104
add_action( 'wc_ajax_wcpay_log_payment_error', [ $this->gateway, 'log_payment_error_ajax' ] );
109105

110106
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
@@ -183,8 +179,6 @@ public function get_payment_fields_js_config() {
183179
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
184180
'wcAjaxUrl' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
185181
'createSetupIntentNonce' => wp_create_nonce( 'wcpay_create_setup_intent_nonce' ),
186-
'createPaymentIntentNonce' => wp_create_nonce( 'wcpay_create_payment_intent_nonce' ),
187-
'updatePaymentIntentNonce' => wp_create_nonce( 'wcpay_update_payment_intent_nonce' ),
188182
'logPaymentErrorNonce' => wp_create_nonce( 'wcpay_log_payment_error_nonce' ),
189183
'initWooPayNonce' => wp_create_nonce( 'wcpay_init_woopay_nonce' ),
190184
'saveUPEAppearanceNonce' => wp_create_nonce( 'wcpay_save_upe_appearance_nonce' ),
@@ -318,10 +312,8 @@ public function get_enabled_payment_method_config() {
318312
'countries' => $payment_method->get_countries(),
319313
];
320314

321-
$gateway_for_payment_method = $this->gateway->wc_payments_get_payment_gateway_by_id( $payment_method_id );
322-
$settings[ $payment_method_id ]['upePaymentIntentData'] = $this->gateway->get_payment_intent_data_from_session( $payment_method_id );
323-
$settings[ $payment_method_id ]['upeSetupIntentData'] = $this->gateway->get_setup_intent_data_from_session( $payment_method_id );
324-
$settings[ $payment_method_id ]['testingInstructions'] = WC_Payments_Utils::esc_interpolated_html(
315+
$gateway_for_payment_method = $this->gateway->wc_payments_get_payment_gateway_by_id( $payment_method_id );
316+
$settings[ $payment_method_id ]['testingInstructions'] = WC_Payments_Utils::esc_interpolated_html(
325317
/* translators: link to Stripe testing page */
326318
$payment_method->get_testing_instructions(),
327319
[

includes/class-wc-payments-payment-method-messaging-element.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
exit;
1212
}
1313
use WCPay\Payment_Methods\UPE_Payment_Gateway;
14-
use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
1514
/**
1615
* WC_Payments_Payment_Method_Messaging_Element class.
1716
*/
@@ -25,15 +24,15 @@ class WC_Payments_Payment_Method_Messaging_Element {
2524
/**
2625
* WC_Payments_Gateway instance to get information about the enabled payment methods.
2726
*
28-
* @var UPE_Payment_Gateway|UPE_Split_Payment_Gateway
27+
* @var UPE_Payment_Gateway
2928
*/
3029
private $gateway;
3130

3231
/**
3332
* WC_Payments_Payment_Method_Messaging_Element constructor
3433
*
35-
* @param WC_Payments_Account $account Account instance.
36-
* @param UPE_Payment_Gateway|UPE_Split_Payment_Gateway $gateway Gateway instance.
34+
* @param WC_Payments_Account $account Account instance.
35+
* @param UPE_Payment_Gateway $gateway Gateway instance.
3736
* @return void
3837
*/
3938
public function __construct( WC_Payments_Account $account, $gateway ) {

0 commit comments

Comments
 (0)