| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Helpers; |
| 4 |
|
| 5 |
use Monei\Services\PaymentMethodsService; |
| 6 |
|
| 7 |
class CardBrandHelper { |
| 8 |
|
| 9 |
private $paymentMethodsService; |
| 10 |
|
| 11 |
public function __construct( PaymentMethodsService $paymentMethodsService ) { |
| 12 |
$this->paymentMethodsService = $paymentMethodsService; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Get card brand configuration for JavaScript. |
| 17 |
* |
| 18 |
* @return array Array of brand configs with url, width, height, title. |
| 19 |
*/ |
| 20 |
public function getCardBrandsConfig(): array { |
| 21 |
$brands = $this->paymentMethodsService->getCardBrands(); |
| 22 |
$config = array(); |
| 23 |
|
| 24 |
foreach ( $brands as $brand ) { |
| 25 |
$config[ $brand ] = array( |
| 26 |
'url' => WC_Monei()->image_url( "cards/{$brand}.svg" ), |
| 27 |
'width' => 40, |
| 28 |
'height' => 24, |
| 29 |
'title' => $this->getBrandTitle( $brand ), |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
// Add default fallback |
| 34 |
$config['default'] = array( |
| 35 |
'url' => WC_Monei()->image_url( 'cards/default.svg' ), |
| 36 |
'width' => 40, |
| 37 |
'height' => 24, |
| 38 |
'title' => __( 'Card', 'monei' ), |
| 39 |
); |
| 40 |
|
| 41 |
return $config; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get human-readable brand title. |
| 46 |
* |
| 47 |
* @param string $brand Brand code. |
| 48 |
* @return string Brand title. |
| 49 |
*/ |
| 50 |
private function getBrandTitle( string $brand ): string { |
| 51 |
$titles = array( |
| 52 |
'visa' => 'Visa', |
| 53 |
'mastercard' => 'Mastercard', |
| 54 |
'amex' => 'American Express', |
| 55 |
'discover' => 'Discover', |
| 56 |
'diners' => 'Diners Club', |
| 57 |
'jcb' => 'JCB', |
| 58 |
'maestro' => 'Maestro', |
| 59 |
'unionpay' => 'UnionPay', |
| 60 |
); |
| 61 |
|
| 62 |
return $titles[ $brand ] ?? ucfirst( $brand ); |
| 63 |
} |
| 64 |
} |
| 65 |
|