| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Utils; |
| 4 |
|
| 5 |
/** |
| 6 |
* Build display labels for payment methods based on wallet and instrument details. |
| 7 |
*/ |
| 8 |
class PaymentMethodTitleBuilder { |
| 9 |
|
| 10 |
/** |
| 11 |
* Normalize known payment instrument labels for display. |
| 12 |
* |
| 13 |
* The same keys are reused to validate whether wallet + instrument titles can be combined. |
| 14 |
*/ |
| 15 |
private const PAYMENT_INSTRUMENT_TITLE_MAP = array( |
| 16 |
'VISA' => 'Visa', |
| 17 |
'AMEX' => 'American Express', |
| 18 |
'MASTERCARD' => 'Mastercard', |
| 19 |
); |
| 20 |
|
| 21 |
/** |
| 22 |
* Return the underlying payment instrument label |
| 23 |
* |
| 24 |
* @param string $payment_instrument_gateway_code |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function get_underlying_payment_method_title( string $payment_instrument_gateway_code ): string { |
| 28 |
$trimmed_payment_method_gateway_code = trim( $payment_instrument_gateway_code ); |
| 29 |
|
| 30 |
return self::PAYMENT_INSTRUMENT_TITLE_MAP[ $trimmed_payment_method_gateway_code ] ?? $trimmed_payment_method_gateway_code; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Check whether wallet and payment instrument titles can be combined. |
| 35 |
* |
| 36 |
* @param string $payment_instrument_gateway_code |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public function can_build_wallet_combined_payment_method_title( string $payment_instrument_gateway_code ): bool { |
| 40 |
return isset( self::PAYMENT_INSTRUMENT_TITLE_MAP[ trim( $payment_instrument_gateway_code ) ] ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Build the combined payment method title, e.g., Google Pay (Visa) |
| 45 |
* |
| 46 |
* @param string $wallet_title |
| 47 |
* @param string $payment_instrument_title |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function build_combined_payment_method_title( string $wallet_title, string $payment_instrument_title ): string { |
| 51 |
if ( '' === $wallet_title ) { |
| 52 |
return $payment_instrument_title; |
| 53 |
} |
| 54 |
|
| 55 |
if ( '' === $payment_instrument_title ) { |
| 56 |
return $wallet_title; |
| 57 |
} |
| 58 |
|
| 59 |
return $wallet_title . ' (' . $payment_instrument_title . ')'; |
| 60 |
} |
| 61 |
} |
| 62 |
|