| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Services; |
| 4 |
|
| 5 |
use Monei\Repositories\PaymentMethodsRepositoryInterface; |
| 6 |
|
| 7 |
class PaymentMethodsService { |
| 8 |
|
| 9 |
public const GOOGLE_API = 'googlePay'; |
| 10 |
public const APPLE_API = 'applePay'; |
| 11 |
public const PAYPAL_API = 'paypal'; |
| 12 |
private $repository; |
| 13 |
|
| 14 |
public function __construct( PaymentMethodsRepositoryInterface $repository ) { |
| 15 |
$this->repository = $repository; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Parse and return enabled payment methods. |
| 20 |
*/ |
| 21 |
public function getEnabledPaymentMethods(): array { |
| 22 |
$data = $this->repository->getPaymentMethods(); |
| 23 |
|
| 24 |
$enabledMethods = array(); |
| 25 |
foreach ( $data['paymentMethods'] ?? array() as $method ) { |
| 26 |
$metadata = $data['metadata'][ $method ] ?? array(); |
| 27 |
$enabledMethods[ $method ] = array( |
| 28 |
'countries' => $metadata['countries'] ?? null, |
| 29 |
'details' => $metadata, |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
return $enabledMethods; |
| 34 |
} |
| 35 |
|
| 36 |
public function isGoogleEnabled(): bool { |
| 37 |
$enabledMethods = $this->getEnabledPaymentMethods(); |
| 38 |
return isset( $enabledMethods[ self::GOOGLE_API ] ); |
| 39 |
} |
| 40 |
|
| 41 |
public function isAppleEnabled(): bool { |
| 42 |
$enabledMethods = $this->getEnabledPaymentMethods(); |
| 43 |
return isset( $enabledMethods[ self::APPLE_API ] ); |
| 44 |
} |
| 45 |
|
| 46 |
public function isPaypalEnabled(): bool { |
| 47 |
$enabledMethods = $this->getEnabledPaymentMethods(); |
| 48 |
return isset( $enabledMethods[ self::PAYPAL_API ] ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get available card brands from MONEI API metadata. |
| 53 |
* |
| 54 |
* @return array Array of lowercase brand names (e.g., ['visa', 'mastercard', 'amex']). |
| 55 |
*/ |
| 56 |
public function getCardBrands(): array { |
| 57 |
$data = $this->repository->getPaymentMethods(); |
| 58 |
|
| 59 |
// Extract card brands from metadata.card.brands |
| 60 |
if ( isset( $data['metadata']['card']['brands'] ) && is_array( $data['metadata']['card']['brands'] ) ) { |
| 61 |
return array_map( 'strtolower', $data['metadata']['card']['brands'] ); |
| 62 |
} |
| 63 |
|
| 64 |
// Fallback to common brands if API doesn't return any |
| 65 |
return array( 'visa', 'mastercard', 'amex', 'discover' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get availability of a specific payment method. |
| 70 |
* |
| 71 |
* @param string $methodId Payment method ID (e.g., 'bizum', 'card'). |
| 72 |
* @return array|null Availability details or null if the method is not enabled. |
| 73 |
*/ |
| 74 |
public function getMethodAvailability( string $methodId ): ?array { |
| 75 |
$paymentData = $this->repository->getPaymentMethods(); |
| 76 |
$methodIdToApiMap = array( |
| 77 |
'monei' => 'card', |
| 78 |
'monei_apple_google' => 'applePay', |
| 79 |
'monei_google' => 'googlePay', |
| 80 |
'monei_bizum' => 'bizum', |
| 81 |
'monei_mbway' => 'mbway', |
| 82 |
'monei_multibanco' => 'multibanco', |
| 83 |
'monei_paypal' => 'paypal', |
| 84 |
); |
| 85 |
|
| 86 |
if ( isset( $methodIdToApiMap[ $methodId ] ) ) { |
| 87 |
$apiName = $methodIdToApiMap[ $methodId ]; |
| 88 |
if ( in_array( $apiName, $paymentData['paymentMethods'] ?? array(), true ) ) { |
| 89 |
$metadata = $paymentData['metadata'][ $apiName ] ?? array(); |
| 90 |
$data = array( |
| 91 |
'enabled' => true, |
| 92 |
'countries' => $metadata['countries'] ?? null, |
| 93 |
'details' => $metadata, |
| 94 |
); |
| 95 |
if ( $methodId === 'monei_apple_google' ) { |
| 96 |
$data['googlePay'] = in_array( 'googlePay', $paymentData['paymentMethods'], true ); |
| 97 |
$data['applePay'] = in_array( 'applePay', $paymentData['paymentMethods'], true ); |
| 98 |
} |
| 99 |
return $data; |
| 100 |
} |
| 101 |
} |
| 102 |
return null; |
| 103 |
} |
| 104 |
} |
| 105 |
|