| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Services; |
| 4 |
|
| 5 |
/** |
| 6 |
* Service for handling MONEI status codes and messages |
| 7 |
* |
| 8 |
* @since 6.4.1 |
| 9 |
*/ |
| 10 |
class MoneiStatusCodeHandler { |
| 11 |
|
| 12 |
/** |
| 13 |
* Get a localized status message for a status code |
| 14 |
* |
| 15 |
* @param string $status_code MONEI status code (e.g., E000, E209). |
| 16 |
* |
| 17 |
* @return string Localized error message |
| 18 |
*/ |
| 19 |
public function get_status_message( string $status_code ): string { |
| 20 |
switch ( $status_code ) { |
| 21 |
case 'E000': |
| 22 |
return __( 'Transaction approved', 'monei' ); |
| 23 |
case 'E999': |
| 24 |
return __( 'Service internal error. Please contact support', 'monei' ); |
| 25 |
case 'E101': |
| 26 |
return __( 'Error with payment processor configuration. Check this in your dashboard or contact MONEI for support', 'monei' ); |
| 27 |
case 'E102': |
| 28 |
return __( 'Invalid or inactive MID. Please contact the acquiring entity', 'monei' ); |
| 29 |
case 'E103': |
| 30 |
return __( 'Operation not allowed/configured for this merchant. Please contact the acquiring entity or MONEI for support', 'monei' ); |
| 31 |
case 'E104': |
| 32 |
return __( 'Partial captures are not enabled in your account, please contact MONEI support', 'monei' ); |
| 33 |
case 'E105': |
| 34 |
return __( 'MOTO Payment are not enabled in your account, please contact MONEI support', 'monei' ); |
| 35 |
case 'E150': |
| 36 |
return __( 'Invalid or malformed request. Please check the message format', 'monei' ); |
| 37 |
case 'E151': |
| 38 |
return __( 'Missing or malformed signature/auth', 'monei' ); |
| 39 |
case 'E152': |
| 40 |
return __( 'Error while decrypting request', 'monei' ); |
| 41 |
case 'E153': |
| 42 |
return __( 'Pre-authorization is expired and cannot be canceled or captured', 'monei' ); |
| 43 |
case 'E154': |
| 44 |
return __( 'The payment date cannot be less than the cancellation or capture date', 'monei' ); |
| 45 |
case 'E155': |
| 46 |
return __( 'The cancellation date exceeded the date allowed for pre-authorized operations', 'monei' ); |
| 47 |
case 'E200': |
| 48 |
return __( 'Transaction failed during payment processing', 'monei' ); |
| 49 |
case 'E201': |
| 50 |
return __( 'Transaction declined by the card-issuing bank', 'monei' ); |
| 51 |
case 'E202': |
| 52 |
return __( 'Transaction declined by the issuing bank', 'monei' ); |
| 53 |
case 'E203': |
| 54 |
return __( 'Payment method not allowed', 'monei' ); |
| 55 |
case 'E204': |
| 56 |
return __( 'Wrong or not allowed currency', 'monei' ); |
| 57 |
case 'E205': |
| 58 |
return __( 'Incorrect reference / transaction does not exist', 'monei' ); |
| 59 |
case 'E207': |
| 60 |
return __( 'Transaction failed: process time exceeded', 'monei' ); |
| 61 |
case 'E208': |
| 62 |
return __( 'Transaction is currently being processed', 'monei' ); |
| 63 |
case 'E209': |
| 64 |
return __( 'Duplicated operation', 'monei' ); |
| 65 |
case 'E210': |
| 66 |
return __( 'Wrong or not allowed payment amount', 'monei' ); |
| 67 |
case 'E211': |
| 68 |
return __( 'Refund declined by processor', 'monei' ); |
| 69 |
case 'E212': |
| 70 |
return __( 'Transaction has already been captured', 'monei' ); |
| 71 |
case 'E213': |
| 72 |
return __( 'Transaction has already been canceled', 'monei' ); |
| 73 |
case 'E214': |
| 74 |
return __( 'The amount to be captured cannot exceed the pre-authorized amount', 'monei' ); |
| 75 |
case 'E215': |
| 76 |
return __( 'The transaction to be captured has not been pre-authorized yet', 'monei' ); |
| 77 |
case 'E216': |
| 78 |
return __( 'The transaction to be canceled has not been pre-authorized yet', 'monei' ); |
| 79 |
case 'E217': |
| 80 |
return __( 'Transaction denied by processor to avoid duplicated operations', 'monei' ); |
| 81 |
case 'E218': |
| 82 |
return __( 'Error during payment request validation', 'monei' ); |
| 83 |
case 'E219': |
| 84 |
return __( 'Refund declined due to exceeded amount', 'monei' ); |
| 85 |
case 'E220': |
| 86 |
return __( 'Transaction has already been fully refunded', 'monei' ); |
| 87 |
case 'E221': |
| 88 |
return __( 'Transaction declined due to insufficient funds', 'monei' ); |
| 89 |
case 'E300': |
| 90 |
return __( 'The user has canceled the payment', 'monei' ); |
| 91 |
case 'E301': |
| 92 |
return __( 'Waiting for the transaction to be completed', 'monei' ); |
| 93 |
case 'E302': |
| 94 |
return __( 'No reason to decline', 'monei' ); |
| 95 |
case 'E303': |
| 96 |
return __( 'Refund not allowed', 'monei' ); |
| 97 |
case 'E304': |
| 98 |
return __( 'Transaction cannot be completed, violation of law', 'monei' ); |
| 99 |
case 'E305': |
| 100 |
return __( 'Stop Payment Order', 'monei' ); |
| 101 |
case 'E400': |
| 102 |
return __( 'Strong Customer Authentication required', 'monei' ); |
| 103 |
case 'E401': |
| 104 |
return __( 'Transaction declined due to security restrictions', 'monei' ); |
| 105 |
case 'E402': |
| 106 |
return __( '3D Secure authentication failed', 'monei' ); |
| 107 |
case 'E403': |
| 108 |
return __( 'Authentication process timed out. Please try again', 'monei' ); |
| 109 |
case 'E404': |
| 110 |
return __( 'An error occurred during the 3D Secure process', 'monei' ); |
| 111 |
case 'E405': |
| 112 |
return __( 'Invalid or malformed 3D Secure request', 'monei' ); |
| 113 |
case 'E406': |
| 114 |
return __( 'Exemption not allowed', 'monei' ); |
| 115 |
case 'E407': |
| 116 |
return __( 'Exemption error', 'monei' ); |
| 117 |
case 'E408': |
| 118 |
return __( 'Fraud control error', 'monei' ); |
| 119 |
case 'E409': |
| 120 |
return __( 'External MPI received wrong. Please check the data', 'monei' ); |
| 121 |
case 'E410': |
| 122 |
return __( 'External MPI not enabled. Please contact support', 'monei' ); |
| 123 |
case 'E500': |
| 124 |
return __( 'Transaction confirmation rejected by the merchant', 'monei' ); |
| 125 |
case 'E501': |
| 126 |
return __( 'Transaction declined during card payment process', 'monei' ); |
| 127 |
case 'E502': |
| 128 |
return __( 'Card rejected: invalid card number', 'monei' ); |
| 129 |
case 'E503': |
| 130 |
return __( 'Card rejected: wrong expiration date', 'monei' ); |
| 131 |
case 'E504': |
| 132 |
return __( 'Card rejected: wrong CVC/CVV2 number', 'monei' ); |
| 133 |
case 'E505': |
| 134 |
return __( 'Card number not registered', 'monei' ); |
| 135 |
case 'E506': |
| 136 |
return __( 'Card is expired', 'monei' ); |
| 137 |
case 'E507': |
| 138 |
return __( 'Error during payment authorization. Please try again', 'monei' ); |
| 139 |
case 'E508': |
| 140 |
return __( 'Cardholder has canceled the payment', 'monei' ); |
| 141 |
case 'E509': |
| 142 |
return __( 'Transaction declined: AMEX cards not accepted by payment processor', 'monei' ); |
| 143 |
case 'E510': |
| 144 |
return __( 'Card blocked temporarily or under suspicion of fraud', 'monei' ); |
| 145 |
case 'E511': |
| 146 |
return __( 'Card does not allow pre-authorization operations', 'monei' ); |
| 147 |
case 'E512': |
| 148 |
return __( 'CVC/CVV2 number is required', 'monei' ); |
| 149 |
case 'E513': |
| 150 |
return __( 'Unsupported card type', 'monei' ); |
| 151 |
case 'E514': |
| 152 |
return __( 'Transaction type not allowed for this type of card', 'monei' ); |
| 153 |
case 'E515': |
| 154 |
return __( 'Transaction declined by card issuer', 'monei' ); |
| 155 |
case 'E516': |
| 156 |
return __( 'Implausible card data', 'monei' ); |
| 157 |
case 'E517': |
| 158 |
return __( 'Incorrect PIN', 'monei' ); |
| 159 |
case 'E518': |
| 160 |
return __( 'Transaction not allowed for cardholder', 'monei' ); |
| 161 |
case 'E519': |
| 162 |
return __( 'The amount exceeds the card limit', 'monei' ); |
| 163 |
case 'E600': |
| 164 |
return __( 'Transaction declined during ApplePay/GooglePay payment process', 'monei' ); |
| 165 |
case 'E601': |
| 166 |
return __( 'Incorrect ApplePay or GooglePay configuration', 'monei' ); |
| 167 |
case 'E620': |
| 168 |
return __( 'Transaction declined during PayPal payment process', 'monei' ); |
| 169 |
case 'E621': |
| 170 |
return __( 'Transaction declined during PayPal payment process: invalid currency', 'monei' ); |
| 171 |
case 'E640': |
| 172 |
return __( 'Bizum transaction declined after three authentication attempts', 'monei' ); |
| 173 |
case 'E641': |
| 174 |
return __( 'Bizum transaction declined due to failed authorization', 'monei' ); |
| 175 |
case 'E642': |
| 176 |
return __( 'Bizum transaction declined due to insufficient funds', 'monei' ); |
| 177 |
case 'E643': |
| 178 |
return __( 'Bizum transaction canceled: the user does not want to continue', 'monei' ); |
| 179 |
case 'E644': |
| 180 |
return __( 'Bizum transaction rejected by destination bank', 'monei' ); |
| 181 |
case 'E645': |
| 182 |
return __( 'Bizum transaction rejected by origin bank', 'monei' ); |
| 183 |
case 'E646': |
| 184 |
return __( 'Bizum transaction rejected by processor', 'monei' ); |
| 185 |
case 'E647': |
| 186 |
return __( 'Bizum transaction failed while connecting with processor. Please try again', 'monei' ); |
| 187 |
case 'E648': |
| 188 |
return __( 'Bizum transaction failed, payee is not found', 'monei' ); |
| 189 |
case 'E649': |
| 190 |
return __( 'Bizum transaction failed, payer is not found', 'monei' ); |
| 191 |
case 'E650': |
| 192 |
return __( 'Bizum REST not implemented', 'monei' ); |
| 193 |
case 'E651': |
| 194 |
return __( 'Bizum transaction declined due to failed authentication', 'monei' ); |
| 195 |
case 'E652': |
| 196 |
return __( 'The customer has disabled Bizum, please use another payment method', 'monei' ); |
| 197 |
case 'E680': |
| 198 |
return __( 'Transaction declined during ClickToPay payment process', 'monei' ); |
| 199 |
case 'E681': |
| 200 |
return __( 'Incorrect ClickToPay configuration', 'monei' ); |
| 201 |
case 'E700': |
| 202 |
return __( 'Transaction declined during Cofidis payment process', 'monei' ); |
| 203 |
default: |
| 204 |
// translators: %s is the status code from MONEI API. |
| 205 |
return sprintf( __( 'Unknown status code: %s', 'monei' ), $status_code ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Check if a status code indicates success |
| 211 |
* |
| 212 |
* @param string|null $status_code MONEI status code. |
| 213 |
* |
| 214 |
* @return bool True if success, false otherwise |
| 215 |
*/ |
| 216 |
public function is_success_code( ?string $status_code ): bool { |
| 217 |
return 'E000' === $status_code; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Check if a status code indicates an error |
| 222 |
* |
| 223 |
* @param string|null $status_code MONEI status code. |
| 224 |
* |
| 225 |
* @return bool True if error, false otherwise |
| 226 |
*/ |
| 227 |
public function is_error_code( ?string $status_code ): bool { |
| 228 |
return null !== $status_code && 'E000' !== $status_code; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Extract error information from API exception |
| 233 |
* |
| 234 |
* @param \Monei\ApiException $exception API exception from MONEI SDK. |
| 235 |
* |
| 236 |
* @return array{message: string, statusCode: string|null, rawMessage: string|null} |
| 237 |
*/ |
| 238 |
public function parse_api_exception( $exception ): array { |
| 239 |
$response_body = json_decode( $exception->getResponseBody(), true ); |
| 240 |
|
| 241 |
// The API can return either: |
| 242 |
// 1. A MONEI status code like "E209" (which we translate to user-friendly messages) |
| 243 |
// 2. An HTTP status code like 400 with a message (which is already user-friendly) |
| 244 |
$status_code_value = $response_body['statusCode'] ?? null; |
| 245 |
$raw_message = $response_body['message'] ?? $exception->getMessage(); |
| 246 |
|
| 247 |
// Check if statusCode is a MONEI status code (E-prefixed like "E209") |
| 248 |
// vs HTTP status code (numeric like 400) |
| 249 |
$is_monei_status_code = $status_code_value && is_string( $status_code_value ) && strpos( $status_code_value, 'E' ) === 0; |
| 250 |
|
| 251 |
if ( $is_monei_status_code ) { |
| 252 |
// We have a MONEI status code - translate it to user-friendly message |
| 253 |
$message = $this->get_status_message( $status_code_value ); |
| 254 |
$status_code = $status_code_value; |
| 255 |
} else { |
| 256 |
// HTTP status code or no status code - use the raw message as it's already user-friendly |
| 257 |
$message = $raw_message; |
| 258 |
$status_code = null; |
| 259 |
} |
| 260 |
|
| 261 |
// Final fallback |
| 262 |
if ( ! $message ) { |
| 263 |
$message = $exception->getMessage(); |
| 264 |
} |
| 265 |
|
| 266 |
return array( |
| 267 |
'message' => $message, |
| 268 |
'statusCode' => $status_code, |
| 269 |
'rawMessage' => $raw_message, |
| 270 |
); |
| 271 |
} |
| 272 |
} |
| 273 |
|