Adding support for co-branded credit cards (#45903)

This commit is contained in:
Wesley Rosa 2024-04-15 18:59:05 -03:00 committed by GitHub
parent aa52be1ee7
commit fed46ed1e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 63 additions and 5 deletions

View File

@ -36,7 +36,7 @@ const getCcOrEcheckLabel = ( {
return sprintf(
/* translators: %1$s is referring to the payment method brand, %2$s is referring to the last 4 digits of the payment card, %3$s is referring to the expiry date. */
__( '%1$s ending in %2$s (expires %3$s)', 'woocommerce' ),
method.brand,
method?.display_brand ?? method?.networks?.preferred ?? method.brand,
method.last4,
expires
);

View File

@ -60,6 +60,16 @@ mockedUseSelect.mockImplementation(
last4: '3456',
},
},
{
tokenId: 4,
expires: '1/2099',
method: {
brand: 'Cartes Bancaires',
gateway:
'can-pay-true-test-payment-method',
last4: '1001',
},
},
],
};
},
@ -104,6 +114,13 @@ describe( 'SavedPaymentMethodOptions', () => {
expect(
screen.queryByText( 'Visa ending in 3456 (expires 1/2099)' )
).not.toBeInTheDocument();
// Fourth saved token for can-pay-true-test-payment-method - co-branded credit card.
expect(
screen.getByText(
'Cartes Bancaires ending in 1001 (expires 1/2099)'
)
).toBeInTheDocument();
} );
it( "does not show saved methods when the method's canPay function returns false", () => {
registerPaymentMethod( {

View File

@ -19,10 +19,16 @@ import type { EventObserversType } from '../../base/context/event-emit';
import type { DispatchFromMap } from '../mapped-types';
import * as actions from './actions';
export interface CardNetworks {
preferred: string;
}
export interface CustomerPaymentMethodConfiguration {
gateway: string;
brand: string;
last4: string;
display_brand?: string;
networks?: CardNetworks;
}
export interface SavedPaymentMethod {
method: CustomerPaymentMethodConfiguration;

View File

@ -0,0 +1,4 @@
Significance: minor
Type: enhancement
Adds support for co-branded credit cards

View File

@ -393,7 +393,7 @@ function wc_get_account_saved_payment_methods_list_item_cc( $item, $payment_toke
$card_type = $payment_token->get_card_type();
$item['method']['last4'] = $payment_token->get_last4();
$item['method']['brand'] = ( ! empty( $card_type ) ? ucfirst( $card_type ) : esc_html__( 'Credit card', 'woocommerce' ) );
$item['method']['brand'] = ( ! empty( $card_type ) ? ucwords( str_replace( '_', ' ', $card_type ) ) : esc_html__( 'Credit card', 'woocommerce' ) );
$item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), -2 );
return $item;

View File

@ -1539,12 +1539,18 @@ function wc_get_credit_card_type_label( $type ) {
'visa' => _x( 'Visa', 'Name of credit card', 'woocommerce' ),
'discover' => _x( 'Discover', 'Name of credit card', 'woocommerce' ),
'american express' => _x( 'American Express', 'Name of credit card', 'woocommerce' ),
'cartes bancaires' => _x( 'Cartes Bancaires', 'Name of credit card', 'woocommerce' ),
'diners' => _x( 'Diners', 'Name of credit card', 'woocommerce' ),
'jcb' => _x( 'JCB', 'Name of credit card', 'woocommerce' ),
)
);
return apply_filters( 'woocommerce_get_credit_card_type_label', ( array_key_exists( $type, $labels ) ? $labels[ $type ] : ucfirst( $type ) ) );
/**
* Fallback to title case, uppercasing the first letter of each word.
*
* @since 8.9.0
*/
return apply_filters( 'woocommerce_get_credit_card_type_label', ( array_key_exists( $type, $labels ) ? $labels[ $type ] : ucwords( $type ) ) );
}
/**

View File

@ -14,7 +14,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 7.8.0
* @version 8.9.0
*/
defined( 'ABSPATH' ) || exit;

View File

@ -259,6 +259,29 @@ class WC_Tests_Account_Functions extends WC_Unit_Test_Case {
);
$token->delete( true );
// Co-branded credit card.
$token = new WC_Payment_Token_CC();
$token->set_token( '1001' );
$token->set_gateway_id( 'bacs' );
$token->set_card_type( 'cartes_bancaires' );
$token->set_last4( '1001' );
$token->set_expiry_month( '12' );
$token->set_expiry_year( '2020' );
$token->save();
$this->assertEquals(
array(
'method' => array(
'last4' => '1001',
'brand' => 'Cartes Bancaires',
),
'expires' => '12/20',
),
wc_get_account_saved_payment_methods_list_item_cc( array(), $token )
);
$token->delete( true );
}
/**

View File

@ -961,8 +961,10 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
$this->assertEquals( 'MasterCard', wc_get_credit_card_type_label( 'Mastercard' ) );
$this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american_express' ) );
$this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american-express' ) );
$this->assertEquals( 'Cartes Bancaires', wc_get_credit_card_type_label( 'cartes_bancaires' ) );
$this->assertEquals( 'Cartes Bancaires', wc_get_credit_card_type_label( 'cartes-bancaires' ) );
$this->assertEquals( '', wc_get_credit_card_type_label( '' ) );
$this->assertEquals( 'Random name', wc_get_credit_card_type_label( 'random-name' ) );
$this->assertEquals( 'Random Name', wc_get_credit_card_type_label( 'random-name' ) );
}
/**