Skip to content

Commit

Permalink
Consolidate gateways implementation - step I (#7937)
Browse files Browse the repository at this point in the history
Co-authored-by: Timur Karimov <[email protected]>
  • Loading branch information
timur27 and Timur Karimov authored Dec 27, 2023
1 parent b24197f commit b6a3669
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 2,312 deletions.
4 changes: 4 additions & 0 deletions changelog/cleanup-upe-gateways-part-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Cleanup the deprecated payment gateway processing - part V
104 changes: 0 additions & 104 deletions client/checkout/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,24 +335,6 @@ export default class WCPayAPI {
};
}

/**
* Creates a setup intent without confirming it.
*
* @return {Promise} The final promise for the request to the server.
*/
initSetupIntent() {
const path = 'init_setup_intent';

return this.request( buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ), {
_ajax_nonce: getConfig( 'createSetupIntentNonce' ),
} ).then( ( response ) => {
if ( ! response.success ) {
throw response.data.error;
}
return response.data;
} );
}

/**
* Sets up an intent based on a payment method.
*
Expand Down Expand Up @@ -389,92 +371,6 @@ export default class WCPayAPI {
} );
}

/**
* Creates an intent based on a payment method.
*
* @param {Object} options Object containing intent optional parameters (fingerprint, paymentMethodType, orderId)
*
* @return {Promise} The final promise for the request to the server.
*/
createIntent( options ) {
const { fingerprint, orderId } = options;
const path = 'create_payment_intent';
const params = {
_ajax_nonce: getConfig( 'createPaymentIntentNonce' ),
'wcpay-fingerprint': fingerprint,
};

if ( orderId ) {
params.wcpay_order_id = orderId;
}

return this.request(
buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ),
params
)
.then( ( response ) => {
if ( ! response.success ) {
throw response.data.error;
}
return response.data;
} )
.catch( ( error ) => {
if ( error.message ) {
throw error;
} else {
// Covers the case of error on the Ajax request.
throw new Error( error.statusText );
}
} );
}

/**
* Updates a payment intent with data from order: customer, level3 data and maybe sets the payment for future use.
*
* @param {string} paymentIntentId The id of the payment intent.
* @param {int} orderId The id of the order.
* @param {string} savePaymentMethod 'yes' if saving.
* @param {string} selectedUPEPaymentType The name of the selected UPE payment type or empty string.
* @param {string?} paymentCountry The payment two-letter iso country code or null.
* @param {string?} fingerprint User fingerprint.
*
* @return {Promise} The final promise for the request to the server.
*/
updateIntent(
paymentIntentId,
orderId,
savePaymentMethod,
selectedUPEPaymentType,
paymentCountry,
fingerprint
) {
const path = 'update_payment_intent';

return this.request( buildAjaxURL( getConfig( 'wcAjaxUrl' ), path ), {
wcpay_order_id: orderId,
wc_payment_intent_id: paymentIntentId,
save_payment_method: savePaymentMethod,
wcpay_selected_upe_payment_type: selectedUPEPaymentType,
wcpay_payment_country: paymentCountry,
_ajax_nonce: getConfig( 'updatePaymentIntentNonce' ),
'wcpay-fingerprint': fingerprint,
} )
.then( ( response ) => {
if ( response.result === 'failure' ) {
throw new Error( response.messages );
}
return response;
} )
.catch( ( error ) => {
if ( error.message ) {
throw error;
} else {
// Covers the case of error on the Ajaxrequest.
throw new Error( error.statusText );
}
} );
}

/**
* Confirm Stripe payment with fallback for rate limit error.
*
Expand Down
66 changes: 0 additions & 66 deletions client/checkout/utils/test/upe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getTerms,
getCookieValue,
isWCPayChosen,
getPaymentIntentFromSession,
generateCheckoutEventNames,
getUpeSettings,
getStripeElementOptions,
Expand Down Expand Up @@ -168,71 +167,6 @@ describe( 'UPE checkout utils', () => {
} );
} );

describe( 'getPaymentIntentFromSession', () => {
const paymentMethodsConfig = {
card: {
upePaymentIntentData:
'abcd1234-pi_abc123-pi_abc123_secret_5678xyz',
},
eps: {
upePaymentIntentData: null,
},
};

const cardData = {
clientSecret: 'pi_abc123_secret_5678xyz',
intentId: 'pi_abc123',
};

it( 'should return the correct client secret and intent ID', () => {
Object.defineProperty( document, 'cookie', {
get: () => {
return 'woocommerce_cart_hash=abcd1234;';
},
configurable: true,
} );
expect(
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
).toEqual( cardData );
} );

it( 'should return an empty object if no payment intent exists', () => {
Object.defineProperty( document, 'cookie', {
get: () => {
return 'woocommerce_cart_hash=abcd1234;';
},
configurable: true,
} );
expect(
getPaymentIntentFromSession( paymentMethodsConfig, 'eps' )
).toEqual( {} );
} );

it( 'should return an empty object if no cart hash exists', () => {
Object.defineProperty( document, 'cookie', {
get: () => {
return 'woocommerce_cart_items=1;';
},
configurable: true,
} );
expect(
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
).toEqual( {} );
} );

it( 'should return an empty object if the payment intent data does not start with the cart hash', () => {
Object.defineProperty( document, 'cookie', {
get: () => {
return 'woocommerce_cart_hash=xyz9876;';
},
configurable: true,
} );
expect(
getPaymentIntentFromSession( paymentMethodsConfig, 'card' )
).toEqual( {} );
} );
} );

describe( 'getUPESettings', () => {
afterEach( () => {
const checkboxElement = document.getElementById(
Expand Down
28 changes: 0 additions & 28 deletions client/checkout/utils/upe.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,6 @@ export const isWCPayChosen = function () {
.checked;
};

/**
* Returns the cached payment intent for the current cart state.
*
* @param {Object} paymentMethodsConfig Array of configs for payment methods.
* @param {string} paymentMethodType Type of the payment method.
* @return {Object} The intent id and client secret required for mounting the UPE element.
*/
export const getPaymentIntentFromSession = (
paymentMethodsConfig,
paymentMethodType
) => {
const cartHash = getCookieValue( 'woocommerce_cart_hash' );
const upePaymentIntentData =
paymentMethodsConfig[ paymentMethodType ].upePaymentIntentData;

if (
cartHash &&
upePaymentIntentData &&
upePaymentIntentData.startsWith( cartHash )
) {
const intentId = upePaymentIntentData.split( '-' )[ 1 ];
const clientSecret = upePaymentIntentData.split( '-' )[ 2 ];
return { intentId, clientSecret };
}

return {};
};

/**
* Finds selected payment gateway and returns matching Stripe payment method for gateway.
*
Expand Down
18 changes: 3 additions & 15 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use WCPay\Logger;
use WCPay\Payment_Information;
use WCPay\Payment_Methods\UPE_Payment_Gateway;
use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
use WCPay\Payment_Methods\Link_Payment_Method;
use WCPay\WooPay\WooPay_Order_Status_Sync;
use WCPay\WooPay\WooPay_Utilities;
Expand Down Expand Up @@ -1644,19 +1643,8 @@ public function set_payment_method_title_for_order( $order, $payment_method_type
* @return array Array of keyed metadata values.
*/
protected function get_metadata_from_order( $order, $payment_type ) {
$service = wcpay_get_container()->get( OrderService::class );
$metadata = $service->get_payment_metadata( $order->get_id(), $payment_type );

if ( $this instanceof UPE_Split_Payment_Gateway ) {
$gateway_type = 'split_upe_with_deferred_intent_creation';
} elseif ( $this instanceof UPE_Payment_Gateway ) {
$gateway_type = 'legacy_upe';
} else {
$gateway_type = 'legacy_card';
}
$metadata['gateway_type'] = $gateway_type;

return $metadata;
$service = wcpay_get_container()->get( OrderService::class );
return $service->get_payment_metadata( $order->get_id(), $payment_type );
}

/**
Expand Down Expand Up @@ -3729,7 +3717,7 @@ private function upe_needs_redirection( $payment_methods ) {
/**
* Modifies the create intent parameters when processing a payment.
*
* Currently used by child UPE_Split_Payment_Gateway to add required shipping information for Afterpay.
* Currently used by child UPE_Payment_Gateway to add required shipping information for Afterpay.
*
* @param Create_And_Confirm_Intention $request The request object for creating and confirming intention.
* @param Payment_Information $payment_information The payment information object.
Expand Down
12 changes: 2 additions & 10 deletions includes/class-wc-payments-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,12 @@ public function __construct(
public function init_hooks() {
add_action( 'wc_payments_set_gateway', [ $this, 'set_gateway' ] );
add_action( 'wc_payments_add_upe_payment_fields', [ $this, 'payment_fields' ] );
add_action( 'woocommerce_after_account_payment_methods', [ $this->gateway, 'remove_upe_setup_intent_from_session' ], 10, 0 );
add_action( 'woocommerce_subscription_payment_method_updated', [ $this->gateway, 'remove_upe_setup_intent_from_session' ], 10, 0 );
add_action( 'woocommerce_order_payment_status_changed', [ get_class( $this->gateway ), 'remove_upe_payment_intent_from_session' ], 10, 0 );
add_action( 'wp', [ $this->gateway, 'maybe_process_upe_redirect' ] );
add_action( 'wc_ajax_wcpay_log_payment_error', [ $this->gateway, 'log_payment_error_ajax' ] );
add_action( 'wp_ajax_save_upe_appearance', [ $this->gateway, 'save_upe_appearance_ajax' ] );
add_action( 'wp_ajax_nopriv_save_upe_appearance', [ $this->gateway, 'save_upe_appearance_ajax' ] );
add_action( 'switch_theme', [ $this->gateway, 'clear_upe_appearance_transient' ] );
add_action( 'woocommerce_woocommerce_payments_updated', [ $this->gateway, 'clear_upe_appearance_transient' ] );
add_action( 'wc_ajax_wcpay_init_setup_intent', [ $this->gateway, 'init_setup_intent_ajax' ] );
add_action( 'wc_ajax_wcpay_log_payment_error', [ $this->gateway, 'log_payment_error_ajax' ] );

add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
Expand Down Expand Up @@ -183,8 +179,6 @@ public function get_payment_fields_js_config() {
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'wcAjaxUrl' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
'createSetupIntentNonce' => wp_create_nonce( 'wcpay_create_setup_intent_nonce' ),
'createPaymentIntentNonce' => wp_create_nonce( 'wcpay_create_payment_intent_nonce' ),
'updatePaymentIntentNonce' => wp_create_nonce( 'wcpay_update_payment_intent_nonce' ),
'logPaymentErrorNonce' => wp_create_nonce( 'wcpay_log_payment_error_nonce' ),
'initWooPayNonce' => wp_create_nonce( 'wcpay_init_woopay_nonce' ),
'saveUPEAppearanceNonce' => wp_create_nonce( 'wcpay_save_upe_appearance_nonce' ),
Expand Down Expand Up @@ -318,10 +312,8 @@ public function get_enabled_payment_method_config() {
'countries' => $payment_method->get_countries(),
];

$gateway_for_payment_method = $this->gateway->wc_payments_get_payment_gateway_by_id( $payment_method_id );
$settings[ $payment_method_id ]['upePaymentIntentData'] = $this->gateway->get_payment_intent_data_from_session( $payment_method_id );
$settings[ $payment_method_id ]['upeSetupIntentData'] = $this->gateway->get_setup_intent_data_from_session( $payment_method_id );
$settings[ $payment_method_id ]['testingInstructions'] = WC_Payments_Utils::esc_interpolated_html(
$gateway_for_payment_method = $this->gateway->wc_payments_get_payment_gateway_by_id( $payment_method_id );
$settings[ $payment_method_id ]['testingInstructions'] = WC_Payments_Utils::esc_interpolated_html(
/* translators: link to Stripe testing page */
$payment_method->get_testing_instructions(),
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
exit;
}
use WCPay\Payment_Methods\UPE_Payment_Gateway;
use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
/**
* WC_Payments_Payment_Method_Messaging_Element class.
*/
Expand All @@ -25,15 +24,15 @@ class WC_Payments_Payment_Method_Messaging_Element {
/**
* WC_Payments_Gateway instance to get information about the enabled payment methods.
*
* @var UPE_Payment_Gateway|UPE_Split_Payment_Gateway
* @var UPE_Payment_Gateway
*/
private $gateway;

/**
* WC_Payments_Payment_Method_Messaging_Element constructor
*
* @param WC_Payments_Account $account Account instance.
* @param UPE_Payment_Gateway|UPE_Split_Payment_Gateway $gateway Gateway instance.
* @param WC_Payments_Account $account Account instance.
* @param UPE_Payment_Gateway $gateway Gateway instance.
* @return void
*/
public function __construct( WC_Payments_Account $account, $gateway ) {
Expand Down
Loading

0 comments on commit b6a3669

Please sign in to comment.