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 all 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.9.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
* Fix - Payment method test instructions not showing up for the Optimized Checkout payment element
* Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page
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 = 'card'; // This is a special case for the Optimized Checkout

// Express method constants
const AMAZON_PAY = 'amazon_pay';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ private function get_enabled_payment_method_config() {

$enabled_payment_methods = $this->get_upe_enabled_at_checkout_payment_method_ids();

// If the Optimized Checkout is enabled, we need to return just the card payment method + express methods.
// All payment methods are rendered inside the card container.
// If the Optimized Checkout is enabled, we need to return just the OC payment method + express methods.
// All payment methods are rendered inside the OC container.
if ( $this->oc_enabled ) {
$enabled_express_methods = array_intersect(
$enabled_payment_methods,
WC_Stripe_Payment_Methods::EXPRESS_PAYMENT_METHODS
);
$enabled_payment_methods = array_merge( [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ], $enabled_express_methods );
$enabled_payment_methods = array_merge( [ WC_Stripe_UPE_Payment_Method_OC::STRIPE_ID ], $enabled_express_methods );
}

foreach ( $enabled_payment_methods as $payment_method_id ) {
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();
}
}
115 changes: 115 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,115 @@
<?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();
$main_settings = WC_Stripe_Helper::get_stripe_settings();
$is_stripe_enabled = ! empty( $main_settings['enabled'] ) && 'yes' === $main_settings['enabled'];

$this->enabled = $is_stripe_enabled && $this->oc_enabled ? 'yes' : 'no';
$this->id = WC_Gateway_Stripe::ID; // Force the ID to be the same as the main payment gateway.
$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>'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public function get_id() {
*/
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 );
// When OC is enabled, we use the OC payment container to render all the methods.
|| ( $this->oc_enabled && WC_Stripe_Payment_Methods::OC === $this->stripe_id );
}

/**
Expand All @@ -186,9 +186,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 OC payment container to render all the methods.
if ( $this->oc_enabled ) {
return WC_Stripe_Payment_Methods::CARD === $this->stripe_id;
return WC_Stripe_Payment_Methods::OC === $this->stripe_id;
}

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.9.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
* Fix - Payment method test instructions not showing up for the Optimized Checkout payment element
* Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Exception;
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
use WC_Stripe_Database_Cache;
use WC_Stripe_Payment_Method_Configurations;
use WooCommerce\Stripe\Tests\Helpers\PMC_Test_Helper;
use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper;
use WC_Data_Exception;
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
Loading