Skip to content

Moving OC card method logic to the new OC payment method class #4579

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

Open
wants to merge 24 commits into
base: fix/removing-card-requirement-for-oc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.8.0 - xxxx-xx-xx =
* Dev - Moves all the Optimized Checkout feature code of the card payment method class to a new, specific class
* Fix - Removes the credit card payment method requirement for the Optimized Checkout feature
* Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page
* Update - Increases the default font size for the Optimized Checkout payment element to match the rest of the checkout form
Expand Down
4 changes: 2 additions & 2 deletions client/classic/upe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ jQuery( function ( $ ) {
// Change the payment method container title when the Optimized Checkout is enabled
if (
getStripeServerData()?.isOCEnabled &&
$( 'input#payment_method_stripe' ).is( ':checked' )
$( 'input#payment_method_stripe_single' ).is( ':checked' )
) {
$( 'label[for=payment_method_stripe]' ).text( 'Stripe' );
$( 'label[for=payment_method_stripe_single]' ).text( 'Stripe' );
}

maybeClearBlikCodeValidation();
Expand Down
2 changes: 2 additions & 0 deletions client/stripe-utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const PAYMENT_METHOD_STRIPE_CASHAPP = 'stripe_cashapp';
export const PAYMENT_METHOD_STRIPE_ACSS = 'stripe_acss_debit';
export const PAYMENT_METHOD_STRIPE_BACS_DEBIT = 'stripe_bacs_debit';
export const PAYMENT_METHOD_STRIPE_BECS = 'stripe_au_becs_debit';
export const PAYMENT_METHOD_STRIPE_OC = 'stripe';

export function getPaymentMethodsConstants() {
return {
Expand All @@ -79,6 +80,7 @@ export function getPaymentMethodsConstants() {
cashapp: PAYMENT_METHOD_STRIPE_CASHAPP,
acss_debit: PAYMENT_METHOD_STRIPE_ACSS,
bacs_debit: PAYMENT_METHOD_STRIPE_BACS_DEBIT,
oc: PAYMENT_METHOD_STRIPE_OC, // Default payment method for Optimized Checkout
};
}

Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function init() {
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-stripe-upe-payment-method-wechat-pay.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-stripe-upe-payment-method-acss.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-stripe-upe-payment-method-amazon-pay.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-stripe-upe-payment-method-oc.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-gateway-stripe-bancontact.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-gateway-stripe-sofort.php';
require_once WC_STRIPE_PLUGIN_PATH . '/includes/payment-methods/class-wc-gateway-stripe-giropay.php';
Expand Down
1 change: 1 addition & 0 deletions includes/constants/class-wc-stripe-payment-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WC_Stripe_Payment_Methods {
const SEPA_DEBIT = 'sepa_debit';
const SOFORT = 'sofort';
const WECHAT_PAY = 'wechat_pay';
const OC = 'oc'; // This is a special case for the Optimized Checkout

// Express method constants
const AMAZON_PAY = 'amazon_pay';
Expand Down
10 changes: 8 additions & 2 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ public function __construct() {
$this->oc_enabled = WC_Stripe_Feature_Flags::is_oc_available() && 'yes' === $this->get_option( 'optimized_checkout_element' );

$this->payment_methods = [];
foreach ( self::UPE_AVAILABLE_METHODS as $payment_method_class ) {
$upe_available_methods = self::UPE_AVAILABLE_METHODS;

if ( $this->oc_enabled ) {
$upe_available_methods[] = WC_Stripe_UPE_Payment_Method_OC::class;
}

foreach ( $upe_available_methods as $payment_method_class ) {
/** Show Sofort if it's already enabled. Hide from the new merchants and keep it for the old ones who are already using this gateway, until we remove it completely.
* Stripe is deprecating Sofort https://support.stripe.com/questions/sofort-is-being-deprecated-as-a-standalone-payment-method.
*/
Expand Down Expand Up @@ -632,7 +638,7 @@ public function get_upe_enabled_at_checkout_payment_method_ids( $order_id = null
// If the Optimized Checkout is enabled, we need to return just the card payment method.
// All payment methods are rendered inside of it.
if ( $this->oc_enabled ) {
return [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ];
return [ WC_Stripe_UPE_Payment_Method_OC::STRIPE_ID ];
}

$is_automatic_capture_enabled = $this->is_automatic_capture_enabled();
Expand Down
31 changes: 0 additions & 31 deletions includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public function get_title( $payment_details = false ) {
return $this->get_card_wallet_type_title( $wallet_type );
}

// Optimized checkout
if ( $this->oc_enabled ) {
return $this->get_optimized_checkout_title( $payment_details );
}

// Default
return parent::get_title();
}
Expand Down Expand Up @@ -117,10 +112,6 @@ public function requires_automatic_capture() {
* @return string
*/
public function get_testing_instructions( $show_optimized_checkout_instruction = false ) {
if ( $this->oc_enabled && ! $show_optimized_checkout_instruction ) {
return WC_Stripe_UPE_Payment_Gateway::get_testing_instructions_for_optimized_checkout();
}

return sprintf(
/* translators: 1) HTML strong open tag 2) HTML strong closing tag 3) HTML anchor open tag 2) HTML anchor closing tag */
esc_html__( '%1$sTest mode:%2$s use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed %3$shere%4$s.', 'woocommerce-gateway-stripe' ),
Expand Down Expand Up @@ -149,26 +140,4 @@ private function get_card_wallet_type_title( $express_payment_type ) {

return $payment_method_title . WC_Stripe_Express_Checkout_Helper::get_payment_method_title_suffix();
}

/**
* Returns the title for the optimized checkout.
*
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
* @return string
*/
private function get_optimized_checkout_title( $payment_details = false ) {
if ( $payment_details ) { // Setting title for the order details page / thank you page.
$payment_method = WC_Stripe_UPE_Payment_Gateway::get_payment_method_instance( $payment_details->type );

// Avoid potential recursion by checking instance type. This fixes the title on pay for order confirmation page.
return $payment_method instanceof self ? parent::get_title() : $payment_method->get_title();
}

// Block checkout and pay for order (checkout) page.
if ( ( has_block( 'woocommerce/checkout' ) || ! empty( $_GET['pay_for_order'] ) ) && ! is_wc_endpoint_url( 'order-received' ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return 'Stripe';
}

return parent::get_title();
}
}
110 changes: 110 additions & 0 deletions includes/payment-methods/class-wc-stripe-upe-payment-method-oc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WC_Stripe_UPE_Payment_Method_OC
*
* This class represents the Stripe UPE payment method for the Optimized Checkout (OC) flow.
*/
class WC_Stripe_UPE_Payment_Method_OC extends WC_Stripe_UPE_Payment_Method {

const STRIPE_ID = WC_Stripe_Payment_Methods::OC;

const LPM_GATEWAY_CLASS = WC_Gateway_Stripe::class;

/**
* Constructor for card payment method
*/
public function __construct() {
parent::__construct();
$this->stripe_id = self::STRIPE_ID;
$this->title = 'Stripe';
$this->is_reusable = true;
$this->supports[] = 'subscriptions';
$this->supports[] = 'tokenization';
}

/**
* Returns payment method title
*
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
public function get_title( $payment_details = false ) {
if ( $payment_details ) { // Setting title for the order details page / thank you page.
$payment_method = WC_Stripe_UPE_Payment_Gateway::get_payment_method_instance( $payment_details->type );

// Avoid potential recursion by checking instance type. This fixes the title on pay for order confirmation page.
return $payment_method instanceof self ? parent::get_title() : $payment_method->get_title();
}

// Block checkout and pay for order (checkout) page.
if ( ( has_block( 'woocommerce/checkout' ) || ! empty( $_GET['pay_for_order'] ) ) && ! is_wc_endpoint_url( 'order-received' ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return 'Stripe';
}

return parent::get_title();
}

/**
* Returns true if the UPE method is available.
*
* @inheritDoc
*/
public function is_available() {
return true;
}

/**
* Returns string representing payment method type
* to query to retrieve saved payment methods from Stripe.
*
* @inheritDoc
*/
public function get_retrievable_type() {
return WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID;
}

/**
* Returns boolean dependent on whether capability
* for site account is enabled for payment method.
*
* @inheritDoc
*/
public function is_capability_active() {
return true;
}

/**
* The Credit Card method allows automatic capture.
*
* @inheritDoc
*/
public function requires_automatic_capture() {
return false;
}

/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param bool $show_optimized_checkout_instruction Whether this is being called through the Optimized Checkout instructions method. Used to avoid an infinite loop call.
* @return string
*/
public function get_testing_instructions( $show_optimized_checkout_instruction = false ) {
if ( ! $show_optimized_checkout_instruction ) {
return WC_Stripe_UPE_Payment_Gateway::get_testing_instructions_for_optimized_checkout();
}

return sprintf(
/* translators: 1) HTML strong open tag 2) HTML strong closing tag 3) HTML anchor open tag 2) HTML anchor closing tag */
esc_html__( '%1$sTest mode:%2$s use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed %3$shere%4$s.', 'woocommerce-gateway-stripe' ),
'<strong>',
'</strong>',
'<a href="https://docs.stripe.com/testing" target="_blank">',
'</a>'
);
}
}
12 changes: 7 additions & 5 deletions includes/payment-methods/class-wc-stripe-upe-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ public function get_id() {
* @return bool
*/
public function is_enabled() {
return 'yes' === $this->enabled
// When OC is enabled, we use the card payment container to render all the methods.
|| ( $this->oc_enabled && WC_Stripe_Payment_Methods::CARD === $this->stripe_id );
if ( $this->oc_enabled && has_block( 'woocommerce/checkout' ) ) {
return true;
}

return 'yes' === $this->enabled;
}

/**
Expand All @@ -186,9 +188,9 @@ public function is_enabled() {
* @return bool
*/
public function is_available() {
// When OC is enabled, we use the card payment container to render all the methods.
// When OC is enabled, we use the single payment container to render all the methods.
if ( $this->oc_enabled ) {
return WC_Stripe_Payment_Methods::CARD === $this->stripe_id;
return has_block( 'woocommerce/checkout' );
}

if ( is_add_payment_method_page() && ! $this->is_reusable() ) {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.8.0 - xxxx-xx-xx =
* Dev - Moves all the Optimized Checkout feature code of the card payment method class to a new, specific class
* Fix - Removes the credit card payment method requirement for the Optimized Checkout feature
* Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page
* Update - Increases the default font size for the Optimized Checkout payment element to match the rest of the checkout form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Automattic\WooCommerce\Enums\OrderStatus;
use Exception;
use WC_Stripe_UPE_Payment_Method_OC;
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
use WC_Stripe_Database_Cache;
use WC_Stripe_Payment_Method_Configurations;
Expand Down Expand Up @@ -424,7 +425,7 @@ public function provide_test_get_upe_enabled_at_checkout_payment_method_ids() {
],
'OC enabled' => true,
'expected' => [
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID,
WC_Stripe_UPE_Payment_Method_OC::STRIPE_ID,
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ class WC_Stripe_UPE_Payment_Method_CC_Test extends WP_UnitTestCase {
* Tests for `get_title`.
*
* @param array|bool $payment_details Payment details.
* @param bool $optimized_checkout_setting Optimized Checkout flag.
* @param array $query_params Query parameters.
* @param string $expected Expected title.
* @return void
*
* @dataProvider provide_test_get_title
*/
public function test_get_title( $payment_details, $optimized_checkout_setting, $query_params, $expected ) {
if ( $optimized_checkout_setting ) {
OC_Test_Helper::enable_oc();
}

public function test_get_title( $payment_details, $query_params, $expected ) {
if ( is_array( $payment_details ) ) {
$payment_details = json_decode( wp_json_encode( $payment_details ) );
}
Expand All @@ -39,9 +34,6 @@ public function test_get_title( $payment_details, $optimized_checkout_setting, $
$payment_method = new WC_Stripe_UPE_Payment_Method_CC();
$actual = $payment_method->get_title( $payment_details );

// Clean up.
OC_Test_Helper::disable_oc();

$this->assertEquals( $expected, $actual );
}

Expand All @@ -52,22 +44,6 @@ public function test_get_title( $payment_details, $optimized_checkout_setting, $
*/
public function provide_test_get_title() {
return [
'optimized checkout, with payment details' => [
'payment details' => [
'type' => WC_Stripe_Payment_Methods::ALIPAY,
],
'optimized checkout flag' => true,
'query params' => [],
'expected' => 'Alipay',
],
'optimized checkout, block checkout page / pay for order' => [
'payment details' => false,
'optimized checkout flag' => true,
'query params' => [
'pay_for_order' => 'true',
],
'expected' => 'Stripe',
],
'Google Pay' => [
'payment details' => [
'card' => [
Expand All @@ -76,13 +52,11 @@ public function provide_test_get_title() {
],
],
],
'optimized checkout flag' => false,
'query params' => [],
'expected' => 'Google Pay (Stripe)',
],
'default, hardcoded' => [
'payment details' => false,
'optimized checkout flag' => false,
'query params' => [],
'expected' => 'Credit / Debit Card',
],
Expand All @@ -92,41 +66,14 @@ public function provide_test_get_title() {
/**
* Test for `get_testing_instructions`.
*
* @param bool $optimized_checkout_setting Optimized Checkout setting.
* @param string $expected Expected instructions.
* @return void
*
* @dataProvider provide_test_get_testing_instructions
*/
public function test_get_testing_instructions( $optimized_checkout_setting, $expected ) {
if ( $optimized_checkout_setting ) {
OC_Test_Helper::enable_oc();
}
public function test_get_testing_instructions() {
$expected = '<strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.';

$payment_method = new WC_Stripe_UPE_Payment_Method_CC();
$actual = $payment_method->get_testing_instructions();

// Clean up
OC_Test_Helper::disable_oc();

$this->assertEquals( $expected, $actual );
}

/**
* Provider for `get_testing_instructions`.
*
* @return array
*/
public function provide_test_get_testing_instructions() {
return [
'Optimized Checkout enabled' => [
'optimized checkout setting' => true,
'expected' => '<div id="wc-stripe-payment-method-instructions-card" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.</div><div id="wc-stripe-payment-method-instructions-blik" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use any 6-digit number to authorize payment.</div><div id="wc-stripe-payment-method-instructions-sepa_debit" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test account number AT611904300234573201. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing?payment-method=sepa-direct-debit#non-card-payments" target="_blank">here</a>.</div>',
],
'Optimized Checkout disabled' => [
'optimized checkout setting' => false,
'expected' => '<strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.',
],
];
}
}
Loading