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

Add country-specific test card numbers for credit card testing #9688

Merged
merged 8 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
4 changes: 4 additions & 0 deletions changelog/add-country-specific-test-cards
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add country-specific test card numbers for credit card testing
6 changes: 3 additions & 3 deletions includes/class-wc-payments-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function get_enabled_payment_method_config() {
$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(),
$payment_method->get_testing_instructions( $account_country ),
[
'a' => '<a href="https://woocommerce.com/document/woopayments/testing-and-troubleshooting/testing/#test-cards" target="_blank">',
'strong' => '<strong>',
Expand Down Expand Up @@ -420,14 +420,14 @@ function () use ( $prepared_customer_data ) {
<?php
endif;

if ( WC_Payments::mode()->is_test() && false !== $this->gateway->get_payment_method()->get_testing_instructions() ) :
if ( WC_Payments::mode()->is_test() && false !== $this->gateway->get_payment_method()->get_testing_instructions( $this->account->get_account_country() ) ) :
?>
<p class="testmode-info">
<?php
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo WC_Payments_Utils::esc_interpolated_html(
/* translators: link to Stripe testing page */
$this->gateway->get_payment_method()->get_testing_instructions(),
$this->gateway->get_payment_method()->get_testing_instructions( $this->account->get_account_country() ),
[
'a' => '<a href="https://woocommerce.com/document/woopayments/testing-and-troubleshooting/testing/#test-cards" target="_blank">',
'strong' => '<strong>',
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ public static function init() {
include_once __DIR__ . '/exceptions/class-rate-limiter-enabled-exception.php';
include_once __DIR__ . '/exceptions/class-invalid-address-exception.php';
include_once __DIR__ . '/constants/class-base-constant.php';
include_once __DIR__ . '/constants/class-country-test-cards.php';
include_once __DIR__ . '/constants/class-country-code.php';
include_once __DIR__ . '/constants/class-currency-code.php';
include_once __DIR__ . '/constants/class-fraud-meta-box-type.php';
Expand Down
95 changes: 95 additions & 0 deletions includes/constants/class-country-test-cards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Class Country_Test_Cards
*
* @package WooCommerce\Payments
*/

namespace WCPay\Constants;

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


/**
* Class handling country-specific data for WooPayments
timur27 marked this conversation as resolved.
Show resolved Hide resolved
*/
class Country_Test_Cards extends Base_Constant {
/**
* Map of country codes to their test card numbers
* Source: https://docs.stripe.com/testing?testing-method=card-numbers#international-cards
*
* @var array
*/
private static $country_test_cards = [
'US' => '4242 4242 4242 4242',
'AR' => '4000 0003 2000 0021',
'BR' => '4000 0007 6000 0002',
'CA' => '4000 0012 4000 0000',
'CL' => '4000 0015 2000 0001',
'CO' => '4000 0017 0000 0003',
'CR' => '4000 0018 8000 0005',
'EC' => '4000 0021 8000 0000',
'MX' => '4000 0048 4000 8001',
'PA' => '4000 0059 1000 0000',
'PY' => '4000 0060 0000 0066',
'PE' => '4000 0060 4000 0068',
'UY' => '4000 0085 8000 0003',
'AE' => '4000 0078 4000 0001',
'AT' => '4000 0004 0000 0008',
'BE' => '4000 0005 6000 0004',
'BG' => '4000 0010 0000 0000',
'BY' => '4000 0011 2000 0005',
'HR' => '4000 0019 1000 0009',
'CY' => '4000 0019 6000 0008',
'CZ' => '4000 0020 3000 0002',
'DK' => '4000 0020 8000 0001',
'EE' => '4000 0023 3000 0009',
'FI' => '4000 0024 6000 0001',
'FR' => '4000 0025 0000 0003',
'DE' => '4000 0027 6000 0016',
'GI' => '4000 0029 2000 0005',
'GR' => '4000 0030 0000 0030',
'HU' => '4000 0034 8000 0005',
'IE' => '4000 0037 2000 0005',
'IT' => '4000 0038 0000 0008',
'LV' => '4000 0042 8000 0005',
'LI' => '4000 0043 8000 0004',
'LT' => '4000 0044 0000 0000',
'LU' => '4000 0044 2000 0006',
'MT' => '4000 0047 0000 0007',
'NL' => '4000 0052 8000 0002',
'NO' => '4000 0057 8000 0007',
'PL' => '4000 0061 6000 0005',
'PT' => '4000 0062 0000 0007',
'RO' => '4000 0064 2000 0001',
'SA' => '4000 0068 2000 0007',
'SI' => '4000 0070 5000 0006',
'SK' => '4000 0070 3000 0001',
'ES' => '4000 0072 4000 0007',
'SE' => '4000 0075 2000 0008',
'CH' => '4000 0075 6000 0009',
'GB' => '4000 0082 6000 0000',
'AU' => '4000 0003 6000 0006',
'CN' => '4000 0015 6000 0002',
'HK' => '4000 0034 4000 0004',
'IN' => '4000 0035 6000 0008',
'JP' => '4000 0039 2000 0003',
'MY' => '4000 0045 8000 0002',
'NZ' => '4000 0055 4000 0008',
'SG' => '4000 0070 2000 0003',
'TW' => '4000 0015 8000 0008',
'TH' => '4000 0076 4000 0003',
];

/**
* Get test card number for a specific country.
*
* @param string $country_code Two-letter country code.
* @return string Test card number
*/
public static function get_test_card_for_country( string $country_code ) {
timur27 marked this conversation as resolved.
Show resolved Hide resolved
return self::$country_test_cards[ $country_code ] ?? self::$country_test_cards['US'];
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of using the country codes in Country_Code for consistency? (you'll probably need to flip the include_once in includes/class-wc-payments.php)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, applied in 11a914b but let me know if I misunderstood your comment and you meant something different.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's great, thank you! Just this one missing:

Suggested change
return self::$country_test_cards[ $country_code ] ?? self::$country_test_cards['US'];
return self::$country_test_cards[ $country_code ] ?? self::$country_test_cards[ Country_Code::UNITED_STATES ];

}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-affirm-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-afterpay-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public function get_icon( string $account_country = null ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-bancontact-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-becs-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return __( '<strong>Test mode:</strong> use the test account number <number>000123456</number>. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a>here</a>.', 'woocommerce-payments' );
}
}
12 changes: 10 additions & 2 deletions includes/payment-methods/class-cc-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WCPay\Payment_Methods;

use WC_Payments_Token_Service;
use WCPay\Constants\Country_Test_Cards;

/**
* Credit card Payment Method class extending UPE base class
Expand Down Expand Up @@ -67,9 +68,16 @@ public function get_title( string $account_country = null, $payment_details = fa
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
return __( 'Use test card <number>4242 4242 4242 4242</number> or refer to our <a>testing guide</a>.', 'woocommerce-payments' );
public function get_testing_instructions( string $account_country = null ) {
$test_card_number = Country_Test_Cards::get_test_card_for_country( $account_country );

return sprintf(
// Translators: %s is a test card number.
__( 'Use test card <number>%s</number> or refer to our <a>testing guide</a>.', 'woocommerce-payments' ),
$test_card_number
);
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-eps-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-giropay-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-ideal-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-klarna-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public function get_countries() {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-link-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-p24-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-sepa-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return __( '<strong>Test mode:</strong> use the test account number <number>AT611904300234573201</number>. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a>here</a>.', 'woocommerce-payments' );
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-sofort-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function __construct( $token_service ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
public function get_testing_instructions() {
public function get_testing_instructions( string $account_country = null ) {
return '';
}
}
3 changes: 2 additions & 1 deletion includes/payment-methods/class-upe-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ public function get_payment_token_for_user( $user, $payment_method_id ) {
/**
* Returns testing credentials to be printed at checkout in test mode.
*
* @param string $account_country The country of the account.
* @return string
*/
abstract public function get_testing_instructions();
abstract public function get_testing_instructions( string $account_country = null );
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it a little bit confusing to type the argument's $account_country as string (both with PHP 7.0 typing and with the PHP docblock), but setting null as its default value.

A few questions:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, no nullable default is needed. I removed it in 5712fbb


/**
* Returns the payment method icon URL or an empty string.
Expand Down
Loading
Loading