Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the handling of express method titles #3844

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*** Changelog ***

= 9.2.0 - xxxx-xx-xx =
* Dev - Improves how we handle express payment method titles by introducing new constants and methods to replace duplicate code.
* Fix - Hides "pay" and "cancel" buttons on the order received page when an Amazon Pay order is pending, since it may take a while to be confirmed.
* Dev - Introduces new payment method constants for the express methods: Google Pay, Apple Pay, Link, and Amazon Pay.
* Fix - Prevent an express checkout element's load errors from affecting other express checkout elements.
* Tweak - Process ECE cart requests using the Blocks (Store) API.
Expand Down
5 changes: 3 additions & 2 deletions includes/class-wc-stripe-blocks-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,12 @@ public function add_stripe_intents( PaymentContext $context, PaymentResult &$res
* @param string $payment_request_type The payment request type used for payment.
*/
private function add_order_meta( \WC_Order $order, $payment_request_type ) {
$payment_method_suffix = WC_Stripe_Express_Checkout_Helper::get_payment_method_title_suffix();
if ( 'apple_pay' === $payment_request_type ) {
$order->set_payment_method_title( 'Apple Pay (Stripe)' );
$order->set_payment_method_title( WC_Stripe_Express_Payment_Titles::APPLE_PAY . $payment_method_suffix );
$order->save();
} elseif ( 'google_pay' === $payment_request_type ) {
$order->set_payment_method_title( 'Google Pay (Stripe)' );
$order->set_payment_method_title( WC_Stripe_Express_Payment_Titles::GOOGLE_PAY . $payment_method_suffix );
$order->save();
} elseif ( 'payment_request_api' === $payment_request_type ) {
$order->set_payment_method_title( 'Payment Request (Stripe)' );
Expand Down
38 changes: 38 additions & 0 deletions includes/constants/class-wc-stripe-express-payment-titles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Class WC_Stripe_Express_Payment_Titles
*/
class WC_Stripe_Express_Payment_Titles {
/**
* Default title for Google Pay.
*
* @string
*/
const GOOGLE_PAY = 'Google Pay';

/**
* Default title for Apple Pay.
*
* @string
*/
const APPLE_PAY = 'Apple Pay';

/**
* Default title for Amazon Pay.
*
* @string
*/
const AMAZON_PAY = 'Amazon Pay';

/**
* Default title for Link.
*
* @string
*/
const LINK = 'Link';
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ public function add_order_meta( $order_id, $posted_data ) {
$order = wc_get_order( $order_id );

$express_checkout_type = wc_clean( wp_unslash( $_POST['express_checkout_type'] ) );

$payment_method_suffix = WC_Stripe_Express_Checkout_Helper::get_payment_method_title_suffix();
if ( 'apple_pay' === $express_checkout_type ) {
$order->set_payment_method_title( 'Apple Pay (Stripe)' );
$order->set_payment_method_title( WC_Stripe_Express_Payment_Titles::APPLE_PAY . $payment_method_suffix );
$order->save();
} elseif ( 'google_pay' === $express_checkout_type ) {
$order->set_payment_method_title( 'Google Pay (Stripe)' );
$order->set_payment_method_title( WC_Stripe_Express_Payment_Titles::GOOGLE_PAY . $payment_method_suffix );
$order->save();
}

Expand All @@ -407,6 +407,9 @@ public function add_order_meta( $order_id, $posted_data ) {

/**
* Filters the gateway title to reflect express checkout type
*
* @param string $title The gateway title.
* @param string $id The gateway ID.
*/
public function filter_gateway_title( $title, $id ) {
global $theorder;
Expand All @@ -422,8 +425,21 @@ public function filter_gateway_title( $title, $id ) {

$method_title = $theorder->get_payment_method_title();

$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );

if ( ! empty( $suffix ) ) {
$suffix = " ($suffix)";
}

if ( 'stripe' === $id && ! empty( $method_title ) ) {
if ( in_array( $method_title, [ 'Apple Pay (Stripe)', 'Google Pay (Stripe)', 'Amazon Pay (Stripe)' ], true ) ) {
$express_method_titles = WC_Stripe_Express_Checkout_Helper::get_payment_method_titles();
array_walk(
$express_method_titles,
function( &$value, $key ) use ( $suffix ) {
$value .= $suffix;
}
);
if ( in_array( $method_title, $express_method_titles, true ) ) {
return $method_title;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,37 @@ public function __construct() {
$this->total_label = ! empty( $this->stripe_settings['statement_descriptor'] ) ? WC_Stripe_Helper::clean_statement_descriptor( $this->stripe_settings['statement_descriptor'] ) : '';

$this->total_label = str_replace( "'", '', $this->total_label ) . apply_filters( 'wc_stripe_payment_request_total_label_suffix', ' (via WooCommerce)' );
}

/**
* Returns a list of the payment method titles.
*
* @param bool $include_link Whether to include the Link payment method.
* @return array
*/
public static function get_payment_method_titles( $include_link = false ) {
$titles = [
'apple_pay' => WC_Stripe_Express_Payment_Titles::APPLE_PAY,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another implementation option would be translated names, like:

'apple_pay' => __( 'Apple Pay', 'woocommerce-gateway-stripe' ),

But not sure if it is worth since those are brands, which I think are immutable.

'google_pay' => WC_Stripe_Express_Payment_Titles::GOOGLE_PAY,
WC_Stripe_Payment_Methods::AMAZON_PAY => WC_Stripe_Express_Payment_Titles::AMAZON_PAY,
];
if ( $include_link ) {
$titles[ WC_Stripe_Payment_Methods::LINK ] = WC_Stripe_Express_Payment_Titles::LINK;
}
return $titles;
}

/**
* Returns the suffix set for the express payment method titles.
*
* @return mixed
*/
public static function get_payment_method_title_suffix() {
$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );
if ( ! empty( $suffix ) ) {
$suffix = " ($suffix)";
}
return $suffix;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ public function __construct() {
// Add hooks for clearing appearance transients when theme is updated
add_action( 'customize_save_after', [ $this, 'clear_appearance_transients' ] );
add_action( 'save_post', [ $this, 'clear_appearance_transients_block_theme' ], 10, 2 );

// Hide action buttons for pending Amazon Pay orders (as they take a while to be confirmed).
add_filter( 'woocommerce_my_account_my_orders_actions', [ $this, 'filter_my_account_my_orders_actions' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -2874,4 +2877,19 @@ public function clear_appearance_transients_block_theme( $post_id, $post ) {
public function clear_appearance_transients() {
delete_transient( $this->get_appearance_transient_key() );
}

/**
* Hide "Pay" and "Cancel" action buttons for pending Amazon Pay orders (as they take a while to be confirmed).
*
* @param $actions array An array with the default actions.
* @param $order WC_Order The order.
* @return array
*/
public function filter_my_account_my_orders_actions( $actions, $order ) {
$amazon_pay_title = WC_Stripe_Express_Payment_Titles::AMAZON_PAY . WC_Stripe_Express_Checkout_Helper::get_payment_method_title_suffix();
if ( is_order_received_page() && $order->get_payment_method_title() === $amazon_pay_title && $order->has_status( 'pending' ) ) {
unset( $actions['pay'], $actions['cancel'] );
}
return $actions;
}
}
23 changes: 8 additions & 15 deletions includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public function __construct() {
* @return string
*/
public function get_title( $payment_details = false ) {
$wallet_type = WC_Stripe_Payment_Methods::AMAZON_PAY === ( $payment_details->type ?? null ) ? WC_Stripe_Payment_Methods::AMAZON_PAY : ( $payment_details->card->wallet->type ?? null );
if ( WC_Stripe_Payment_Methods::AMAZON_PAY === ( $payment_details->type ?? null ) ) {
return $this->get_card_wallet_type_title( WC_Stripe_Payment_Methods::AMAZON_PAY );
}

$wallet_type = $payment_details->card->wallet->type ?? null;
if ( $payment_details && $wallet_type ) {
return $this->get_card_wallet_type_title( $wallet_type );
}
Expand Down Expand Up @@ -126,24 +130,13 @@ public function get_testing_instructions() {
* @return string The title for the card wallet type.
*/
private function get_card_wallet_type_title( $express_payment_type ) {
$express_payment_titles = [
'apple_pay' => 'Apple Pay',
'google_pay' => 'Google Pay',
WC_Stripe_Payment_Methods::AMAZON_PAY => 'Amazon Pay',
];

$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;
$express_payment_titles = WC_Stripe_Express_Checkout_Helper::get_payment_method_titles();
$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;

if ( ! $payment_method_title ) {
return parent::get_title();
}

$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );

if ( ! empty( $suffix ) ) {
$suffix = " ($suffix)";
}

return $payment_method_title . $suffix;
return $payment_method_title . WC_Stripe_Express_Checkout_Helper::get_payment_method_title_suffix();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public function requires_automatic_capture() {

/**
* Filters the gateway title to reflect Link as the payment method.
*
* @param string $title The gateway title.
* @param string $id The gateway ID.
*/
public function filter_gateway_title( $title, $id ) {
global $theorder;
Expand All @@ -130,7 +133,7 @@ public function filter_gateway_title( $title, $id ) {
$method_title = $theorder->get_payment_method_title();

if ( 'stripe' === $id && ! empty( $method_title ) ) {
if ( 'Link' === $method_title ) {
if ( WC_Stripe_Express_Payment_Titles::LINK === $method_title ) {
return $method_title;
}
}
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.2.0 - xxxx-xx-xx =
* Dev - Improves how we handle express payment method titles by introducing new constants and methods to replace duplicate code.
* Fix - Hides "pay" and "cancel" buttons on the order received page when an Amazon Pay order is pending, since it may take a while to be confirmed.
* Dev - Introduces new payment method constants for the express methods: Google Pay, Apple Pay, Link, and Amazon Pay.
* Fix - Prevent an express checkout element's load errors from affecting other express checkout elements.
* Tweak - Process ECE cart requests using the Blocks (Store) API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2755,4 +2755,53 @@ public function test_set_payment_method_title_for_order_ECE_title() {
// Unset the feature flag.
delete_option( WC_Stripe_Feature_Flags::ECE_FEATURE_FLAG_NAME );
}

/**
* Test for `filter_my_account_my_orders_actions`.
*
* @return void
*/
public function test_filter_my_account_my_orders_actions() {
add_filter(
'woocommerce_is_order_received_page',
function() {
return true;
}
);

$order = WC_Helper_Order::create_order();
$order->set_payment_method_title( 'Amazon Pay (Stripe)' );
$order->set_status( 'pending' );

$actions = [
'pay' => [
'url' => $order->get_checkout_payment_url(),
'name' => 'Pay',
'aria-label' => sprintf( 'Pay for order %s', $order->get_order_number() ),
],
'view' => [
'url' => $order->get_view_order_url(),
'name' => 'View',
'aria-label' => sprintf( 'View order %s', $order->get_order_number() ),
],
'cancel' => [
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => 'Cancel',
'aria-label' => sprintf( 'Cancel order %s', $order->get_order_number() ),
],
];

$actual = $this->mock_gateway->filter_my_account_my_orders_actions( $actions, $order );

$this->assertEquals(
[
'view' => [
'url' => $order->get_view_order_url(),
'name' => 'View',
'aria-label' => sprintf( 'View order %s', $order->get_order_number() ),
],
],
$actual
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,36 @@ public function provide_test_get_normalized_postal_code() {
],
];
}

/**
* Test for `get_payment_method_titles`.
*
* @return void
*/
public function test_get_payment_method_titles() {
$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
$actual = $wc_stripe_ece_helper->get_payment_method_titles( true );

$this->assertEquals(
[
'apple_pay' => WC_Stripe_Express_Payment_Titles::APPLE_PAY,
'google_pay' => WC_Stripe_Express_Payment_Titles::GOOGLE_PAY,
WC_Stripe_Payment_Methods::AMAZON_PAY => WC_Stripe_Express_Payment_Titles::AMAZON_PAY,
WC_Stripe_Payment_Methods::LINK => WC_Stripe_Express_Payment_Titles::LINK,
],
$actual
);
}

/**
* Test for `get_payment_method_title_suffix`
*
* @return void
*/
public function test_get_payment_method_title_suffix() {
$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
$actual = $wc_stripe_ece_helper->get_payment_method_title_suffix();

$this->assertEquals( ' (Stripe)', $actual );
}
}
1 change: 1 addition & 0 deletions woocommerce-gateway-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function init() {
require_once __DIR__ . '/includes/constants/class-wc-stripe-currency-code.php';
require_once __DIR__ . '/includes/constants/class-wc-stripe-payment-methods.php';
require_once __DIR__ . '/includes/constants/class-wc-stripe-intent-status.php';
require_once __DIR__ . '/includes/constants/class-wc-stripe-express-payment-titles.php';
require_once __DIR__ . '/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php';
require_once __DIR__ . '/includes/payment-methods/class-wc-stripe-upe-payment-method.php';
require_once __DIR__ . '/includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php';
Expand Down
Loading