| 1 |
<?php |
| 2 |
/** |
| 3 |
* Payment Method Formatter Service |
| 4 |
* |
| 5 |
* Formats payment method information for display in admin and customer-facing areas. |
| 6 |
* Similar to PrestaShop's PaymentMethodFormatter. |
| 7 |
* |
| 8 |
* @package Monei |
| 9 |
* @since 6.5.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Monei\Services; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Class PaymentMethodFormatter |
| 20 |
*/ |
| 21 |
class PaymentMethodFormatter { |
| 22 |
|
| 23 |
/** |
| 24 |
* Card brand display names |
| 25 |
*/ |
| 26 |
private const CARD_BRANDS = array( |
| 27 |
'visa' => 'Visa', |
| 28 |
'mastercard' => 'MasterCard', |
| 29 |
'amex' => 'American Express', |
| 30 |
'americanexpress' => 'American Express', |
| 31 |
'discover' => 'Discover', |
| 32 |
'dinersclub' => 'Diners Club', |
| 33 |
'jcb' => 'JCB', |
| 34 |
'unionpay' => 'UnionPay', |
| 35 |
'maestro' => 'Maestro', |
| 36 |
); |
| 37 |
|
| 38 |
/** |
| 39 |
* Payment method display names |
| 40 |
*/ |
| 41 |
private const PAYMENT_METHODS = array( |
| 42 |
'card' => 'Card', |
| 43 |
'bizum' => 'Bizum', |
| 44 |
'paypal' => 'PayPal', |
| 45 |
'applepay' => 'Apple Pay', |
| 46 |
'apple_pay' => 'Apple Pay', |
| 47 |
'googlepay' => 'Google Pay', |
| 48 |
'google_pay' => 'Google Pay', |
| 49 |
'multibanco' => 'Multibanco', |
| 50 |
'mbway' => 'MB Way', |
| 51 |
); |
| 52 |
|
| 53 |
/** |
| 54 |
* Format payment method display name |
| 55 |
* |
| 56 |
* @param string $method Payment method (card, bizum, paypal, etc). |
| 57 |
* @param string|null $brand Card brand (visa, mastercard, etc). |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function format_payment_method_name( string $method, ?string $brand = null ): string { |
| 61 |
$method = strtolower( $method ); |
| 62 |
|
| 63 |
if ( 'card' === $method && $brand ) { |
| 64 |
$brand = strtolower( $brand ); |
| 65 |
return self::CARD_BRANDS[ $brand ] ?? ucfirst( $brand ); |
| 66 |
} |
| 67 |
|
| 68 |
return self::PAYMENT_METHODS[ $method ] ?? ucfirst( $method ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Format card number with last 4 digits |
| 73 |
* |
| 74 |
* @param string|null $last4 Last 4 digits of card. |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function format_card_number( ?string $last4 ): string { |
| 78 |
if ( empty( $last4 ) ) { |
| 79 |
return ''; |
| 80 |
} |
| 81 |
|
| 82 |
return '•••• ' . $last4; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Obfuscate email address for security |
| 87 |
* Uses fixed-length dots to prevent length-based identification |
| 88 |
* |
| 89 |
* @param string $email Email address. |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function obfuscate_email( string $email ): string { |
| 93 |
if ( empty( $email ) ) { |
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
$parts = explode( '@', $email ); |
| 98 |
if ( 2 !== count( $parts ) ) { |
| 99 |
return $email; |
| 100 |
} |
| 101 |
|
| 102 |
$local_part = $parts[0]; |
| 103 |
$domain = $parts[1]; |
| 104 |
|
| 105 |
$local_length = strlen( $local_part ); |
| 106 |
|
| 107 |
// Security-focused obfuscation: |
| 108 |
// - Use fixed-length dots to prevent length-based guessing |
| 109 |
// - Show only first character to minimize information leakage |
| 110 |
if ( $local_length <= 1 ) { |
| 111 |
// Single character email - just show dots |
| 112 |
$obfuscated_local = '•••'; |
| 113 |
} else { |
| 114 |
// Show only first character with fixed 3 dots |
| 115 |
$obfuscated_local = substr( $local_part, 0, 1 ) . '•••'; |
| 116 |
} |
| 117 |
|
| 118 |
// Keep full domain visible for service identification |
| 119 |
return $obfuscated_local . '@' . $domain; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Format payment method display based on payment information |
| 124 |
* |
| 125 |
* Examples: |
| 126 |
* - "Visa •••• 1234" |
| 127 |
* - "Apple Pay •••• 5678" |
| 128 |
* - "Bizum ••••1234" |
| 129 |
* - "PayPal (a•••@gmail.com)" |
| 130 |
* |
| 131 |
* @param array $payment_info Flattened payment information array. |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
public function format_payment_method_display( array $payment_info ): string { |
| 135 |
$payment_method_display = ''; |
| 136 |
$method_type = $payment_info['method'] ?? ''; |
| 137 |
|
| 138 |
// Check tokenizationMethod first for wallet payments (Apple Pay, Google Pay) |
| 139 |
if ( ! empty( $payment_info['tokenizationMethod'] ) ) { |
| 140 |
$tokenization_method = $payment_info['tokenizationMethod']; |
| 141 |
|
| 142 |
switch ( $tokenization_method ) { |
| 143 |
case 'applePay': |
| 144 |
$payment_method_display = 'Apple Pay'; |
| 145 |
break; |
| 146 |
case 'googlePay': |
| 147 |
$payment_method_display = 'Google Pay'; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
// Add card details if available |
| 152 |
if ( $payment_method_display && ! empty( $payment_info['last4'] ) ) { |
| 153 |
$payment_method_display .= ' ' . $this->format_card_number( $payment_info['last4'] ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// If no tokenization method or not a recognized wallet, use existing logic |
| 158 |
if ( ! $payment_method_display ) { |
| 159 |
if ( ! empty( $payment_info['brand'] ) ) { |
| 160 |
// Card payment display |
| 161 |
$brand = strtolower( $payment_info['brand'] ); |
| 162 |
$payment_method_display = $this->format_payment_method_name( 'card', $brand ); |
| 163 |
|
| 164 |
// Add card type inline if available |
| 165 |
if ( ! empty( $payment_info['type'] ) ) { |
| 166 |
$payment_method_display .= ' ' . ucfirst( $payment_info['type'] ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! empty( $payment_info['last4'] ) ) { |
| 170 |
$payment_method_display .= ' ' . $this->format_card_number( $payment_info['last4'] ); |
| 171 |
} |
| 172 |
} elseif ( ! empty( $method_type ) ) { |
| 173 |
// Non-card payment methods |
| 174 |
$payment_method_display = $this->format_payment_method_name( $method_type ); |
| 175 |
|
| 176 |
// Handle specific methods with extra formatting |
| 177 |
switch ( $method_type ) { |
| 178 |
case 'bizum': |
| 179 |
// Add phone number for Bizum if available |
| 180 |
if ( ! empty( $payment_info['phoneNumber'] ) ) { |
| 181 |
$last4 = substr( $payment_info['phoneNumber'], -4 ); |
| 182 |
$payment_method_display .= ' ••••' . $last4; |
| 183 |
} |
| 184 |
break; |
| 185 |
|
| 186 |
case 'paypal': |
| 187 |
// Add PayPal email if available (obfuscated) |
| 188 |
if ( ! empty( $payment_info['email'] ) ) { |
| 189 |
$payment_method_display .= ' (' . $this->obfuscate_email( $payment_info['email'] ) . ')'; |
| 190 |
} |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return $payment_method_display; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Flatten payment method data structure from MONEI SDK Payment object |
| 201 |
* |
| 202 |
* @param \Monei\Model\Payment $payment MONEI payment object. |
| 203 |
* @return array Flattened payment data. |
| 204 |
*/ |
| 205 |
public function flatten_payment_method_data( $payment ): array { |
| 206 |
$flattened = array(); |
| 207 |
|
| 208 |
$payment_method = $payment->getPaymentMethod(); |
| 209 |
if ( ! $payment_method ) { |
| 210 |
return $flattened; |
| 211 |
} |
| 212 |
|
| 213 |
// Get base method type |
| 214 |
$flattened['method'] = $payment_method->getMethod(); |
| 215 |
|
| 216 |
// Card details |
| 217 |
$card = $payment_method->getCard(); |
| 218 |
if ( $card ) { |
| 219 |
// Get tokenization method (for Apple Pay, Google Pay) from card |
| 220 |
if ( method_exists( $card, 'getTokenizationMethod' ) && $card->getTokenizationMethod() ) { |
| 221 |
$flattened['tokenizationMethod'] = $card->getTokenizationMethod(); |
| 222 |
} |
| 223 |
if ( $card->getBrand() ) { |
| 224 |
$flattened['brand'] = $card->getBrand(); |
| 225 |
} |
| 226 |
if ( $card->getLast4() ) { |
| 227 |
$flattened['last4'] = $card->getLast4(); |
| 228 |
} |
| 229 |
if ( $card->getType() ) { |
| 230 |
$flattened['type'] = $card->getType(); |
| 231 |
} |
| 232 |
if ( $card->getCardholderName() ) { |
| 233 |
$flattened['cardholderName'] = $card->getCardholderName(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Bizum details |
| 238 |
$bizum = $payment_method->getBizum(); |
| 239 |
if ( $bizum && $bizum->getPhoneNumber() ) { |
| 240 |
$flattened['phoneNumber'] = $bizum->getPhoneNumber(); |
| 241 |
} |
| 242 |
|
| 243 |
// PayPal details |
| 244 |
$paypal = $payment_method->getPaypal(); |
| 245 |
if ( $paypal && $paypal->getEmail() ) { |
| 246 |
$flattened['email'] = $paypal->getEmail(); |
| 247 |
} |
| 248 |
|
| 249 |
return $flattened; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Extract and format payment method information from MONEI payment object |
| 254 |
* |
| 255 |
* @param \Monei\Model\Payment $payment MONEI payment object. |
| 256 |
* @return string Formatted payment method display. |
| 257 |
*/ |
| 258 |
public function get_payment_method_display_from_payment( $payment ): string { |
| 259 |
$flattened = $this->flatten_payment_method_data( $payment ); |
| 260 |
return $this->format_payment_method_display( $flattened ); |
| 261 |
} |
| 262 |
} |
| 263 |
|