Skip to content

Commit 8d544ae

Browse files
committed
feat: add user-friendly localized error messages
Implement comprehensive error handling with localized messages following PrestaShop plugin pattern: - Add MoneiStatusCodeHandler service with all MONEI status codes (E000-E700) mapped to user-friendly localized messages - Smart error detection: distinguish between MONEI status codes (E-prefixed like "E209") and HTTP status codes (numeric like 400) - For MONEI codes: translate to localized messages via __() - For HTTP codes: use API's raw message (already user-friendly) - Update all 6 gateway classes to inject MoneiStatusCodeHandler - Add proper use statements instead of inline fully qualified names - Update error handling in both Component and Hosted gateway types - Register service in DI container for autowiring This fixes issues where users saw "Unknown status code: 400" instead of the actual error message like "The order has already been paid". Also includes fixes from previous work: - Standardize description field display across payment methods - Remove description field from Apple/Google Pay settings - Fix Credit Card settings redirect mode selector
1 parent 624872e commit 8d544ae

11 files changed

+335
-21
lines changed

src/Core/container-definitions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Monei\Services\ApiKeyService;
99
use Monei\Services\BlockSupportService;
1010
use Monei\Services\MoneiApplePayVerificationService;
11+
use Monei\Services\MoneiStatusCodeHandler;
1112
use Monei\Services\payment\MoneiPaymentServices;
1213
use Monei\Services\PaymentMethodsService;
1314
use Monei\Services\sdk\MoneiSdkClientFactory;
@@ -58,6 +59,7 @@ function ( ApiKeyService $apiKeyService, MoneiSdkClientFactory $sdkClientFactory
5859
CardBrandHelper::class => DI\create( CardBrandHelper::class )
5960
->constructor( DI\get( PaymentMethodsService::class ) ),
6061
MoneiPaymentServices::class => DI\autowire( MoneiPaymentServices::class ),
62+
MoneiStatusCodeHandler::class => DI\autowire( MoneiStatusCodeHandler::class ),
6163
BlockSupportService::class => DI\create( BlockSupportService::class )
6264
->constructor( $blocksPath, $blockNamespacePrefix ),
6365
MoneiApplePayVerificationService::class => DI\autowire( MoneiApplePayVerificationService::class )

src/Gateways/Abstracts/WCMoneiPaymentGateway.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Monei\Model\PaymentStatus;
77
use Monei\Services\ApiKeyService;
8+
use Monei\Services\MoneiStatusCodeHandler;
89
use Monei\Services\payment\MoneiPaymentServices;
910
use Monei\Services\PaymentMethodsService;
1011
use Monei\Templates\TemplateManager;
@@ -121,16 +122,23 @@ abstract class WCMoneiPaymentGateway extends WC_Payment_Gateway {
121122
private ApiKeyService $apiKeyService;
122123
protected MoneiPaymentServices $moneiPaymentServices;
123124

125+
/**
126+
* @var MoneiStatusCodeHandler
127+
*/
128+
protected $statusCodeHandler;
129+
124130
public function __construct(
125131
PaymentMethodsService $paymentMethodsService,
126132
TemplateManager $templateManager,
127133
ApiKeyService $apiKeyService,
128-
MoneiPaymentServices $moneiPaymentServices
134+
MoneiPaymentServices $moneiPaymentServices,
135+
MoneiStatusCodeHandler $statusCodeHandler
129136
) {
130137
$this->paymentMethodsService = $paymentMethodsService;
131138
$this->templateManager = $templateManager;
132139
$this->apiKeyService = $apiKeyService;
133140
$this->moneiPaymentServices = $moneiPaymentServices;
141+
$this->statusCodeHandler = $statusCodeHandler;
134142
}
135143

136144
/**

src/Gateways/Abstracts/WCMoneiPaymentGatewayComponent.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ public function process_payment( $order_id, $allowed_payment_method = null ) {
9999

100100
} catch ( ApiException $e ) {
101101
do_action( 'wc_gateway_monei_process_payment_error', $e, $order );
102-
// Extract and log the responseBody message
103-
$response_body = json_decode( $e->getResponseBody(), true );
104-
if ( isset( $response_body['message'] ) ) {
105-
WC_Monei_Logger::log( $response_body['message'], 'error' );
106-
wc_add_notice( $response_body['message'], 'error' );
107-
return array(
108-
'result' => 'failure',
109-
);
102+
// Parse API exception and get user-friendly error message
103+
$error_info = $this->statusCodeHandler->parse_api_exception( $e );
104+
105+
// Log the technical details
106+
if ( $error_info['statusCode'] ) {
107+
WC_Monei_Logger::log( sprintf( 'Payment error - Status Code: %s, Raw Message: %s', $error_info['statusCode'], $error_info['rawMessage'] ), 'error' );
108+
} else {
109+
WC_Monei_Logger::log( sprintf( 'Payment error - Raw Message: %s', $error_info['rawMessage'] ?? $e->getMessage() ), 'error' );
110110
}
111-
WC_Monei_Logger::log( $e->getMessage(), 'error' );
112-
wc_add_notice( $e->getMessage(), 'error' );
111+
112+
// Show user-friendly error message to customer
113+
wc_add_notice( $error_info['message'], 'error' );
114+
113115
return array(
114116
'result' => 'failure',
115117
);

src/Gateways/Abstracts/WCMoneiPaymentGatewayHosted.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ public function process_payment( $order_id, $allowed_payment_method = null ) {
152152
'result' => 'success',
153153
'redirect' => $payment->getNextAction()->getRedirectUrl(),
154154
);
155+
} catch ( \Monei\ApiException $e ) {
156+
do_action( 'wc_gateway_monei_process_payment_error', $e, $order );
157+
// Parse API exception and get user-friendly error message
158+
$error_info = $this->statusCodeHandler->parse_api_exception( $e );
159+
160+
// Log the technical details
161+
if ( $error_info['statusCode'] ) {
162+
$this->log( sprintf( 'Payment error - Status Code: %s, Raw Message: %s', $error_info['statusCode'], $error_info['rawMessage'] ), 'error' );
163+
} else {
164+
$this->log( sprintf( 'Payment error - Raw Message: %s', $error_info['rawMessage'] ?? $e->getMessage() ), 'error' );
165+
}
166+
167+
// Show user-friendly error message to customer
168+
wc_add_notice( $error_info['message'], 'error' );
169+
170+
return array(
171+
'result' => 'failure',
172+
);
155173
} catch ( Exception $e ) {
156174
$this->log( $e->getMessage(), 'error' );
157175
wc_add_notice( $e->getMessage(), 'error' );

src/Gateways/PaymentMethods/WCGatewayMoneiAppleGoogle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Monei\Features\Subscriptions\SubscriptionService;
66
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayComponent;
77
use Monei\Services\ApiKeyService;
8+
use Monei\Services\MoneiStatusCodeHandler;
89
use Monei\Services\payment\MoneiPaymentServices;
910
use Monei\Services\PaymentMethodsService;
1011
use Monei\Templates\TemplateManager;
@@ -55,9 +56,10 @@ public function __construct(
5556
TemplateManager $templateManager,
5657
ApiKeyService $apiKeyService,
5758
MoneiPaymentServices $moneiPaymentServices,
59+
MoneiStatusCodeHandler $statusCodeHandler,
5860
SubscriptionService $subscriptionService
5961
) {
60-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
62+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
6163
$this->id = 'monei_apple_google';
6264
$this->method_title = __( 'MONEI - Apple Pay / Google Pay', 'monei' );
6365
$this->method_description = __( 'Accept Apple Pay and Google Pay payments.', 'monei' );

src/Gateways/PaymentMethods/WCGatewayMoneiBizum.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayHosted;
66
use Monei\Services\ApiKeyService;
7+
use Monei\Services\MoneiStatusCodeHandler;
78
use Monei\Services\payment\MoneiPaymentServices;
89
use Monei\Services\PaymentMethodsService;
910
use Monei\Templates\TemplateManager;
@@ -39,9 +40,10 @@ public function __construct(
3940
PaymentMethodsService $paymentMethodsService,
4041
TemplateManager $templateManager,
4142
ApiKeyService $apiKeyService,
42-
MoneiPaymentServices $moneiPaymentServices
43+
MoneiPaymentServices $moneiPaymentServices,
44+
MoneiStatusCodeHandler $statusCodeHandler
4345
) {
44-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
46+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
4547

4648
$this->id = MONEI_GATEWAY_ID . '_bizum';
4749
$this->method_title = __( 'MONEI - Bizum', 'monei' );

src/Gateways/PaymentMethods/WCGatewayMoneiCC.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayComponent;
99
use Monei\Helpers\CardBrandHelper;
1010
use Monei\Services\ApiKeyService;
11+
use Monei\Services\MoneiStatusCodeHandler;
1112
use Monei\Services\payment\MoneiPaymentServices;
1213
use Monei\Services\PaymentMethodsService;
1314
use Monei\Templates\TemplateManager;
@@ -63,10 +64,11 @@ public function __construct(
6364
TemplateManager $templateManager,
6465
ApiKeyService $apiKeyService,
6566
MoneiPaymentServices $moneiPaymentServices,
67+
MoneiStatusCodeHandler $statusCodeHandler,
6668
SubscriptionService $subscriptionService,
6769
CardBrandHelper $cardBrandHelper
6870
) {
69-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
71+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
7072
$this->cardBrandHelper = $cardBrandHelper;
7173
$this->id = MONEI_GATEWAY_ID;
7274
$this->method_title = __( 'MONEI - Credit Card', 'monei' );

src/Gateways/PaymentMethods/WCGatewayMoneiMBWay.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayHosted;
66
use Monei\Services\ApiKeyService;
7+
use Monei\Services\MoneiStatusCodeHandler;
78
use Monei\Services\payment\MoneiPaymentServices;
89
use Monei\Services\PaymentMethodsService;
910
use Monei\Templates\TemplateManager;
@@ -34,9 +35,10 @@ public function __construct(
3435
PaymentMethodsService $paymentMethodsService,
3536
TemplateManager $templateManager,
3637
ApiKeyService $apiKeyService,
37-
MoneiPaymentServices $moneiPaymentServices
38+
MoneiPaymentServices $moneiPaymentServices,
39+
MoneiStatusCodeHandler $statusCodeHandler
3840
) {
39-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
41+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
4042

4143
$this->id = MONEI_GATEWAY_ID . '_mbway';
4244
$this->method_title = __( 'MONEI - MBWay', 'monei' );

src/Gateways/PaymentMethods/WCGatewayMoneiMultibanco.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayHosted;
66
use Monei\Services\ApiKeyService;
7+
use Monei\Services\MoneiStatusCodeHandler;
78
use Monei\Services\payment\MoneiPaymentServices;
89
use Monei\Services\PaymentMethodsService;
910
use Monei\Templates\TemplateManager;
@@ -33,9 +34,10 @@ public function __construct(
3334
PaymentMethodsService $paymentMethodsService,
3435
TemplateManager $templateManager,
3536
ApiKeyService $apiKeyService,
36-
MoneiPaymentServices $moneiPaymentServices
37+
MoneiPaymentServices $moneiPaymentServices,
38+
MoneiStatusCodeHandler $statusCodeHandler
3739
) {
38-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
40+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
3941

4042
$this->id = MONEI_GATEWAY_ID . '_multibanco';
4143
$this->method_title = __( 'MONEI - Multibanco', 'monei' );

src/Gateways/PaymentMethods/WCGatewayMoneiPaypal.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monei\Gateways\Abstracts\WCMoneiPaymentGatewayHosted;
66
use Monei\Services\ApiKeyService;
7+
use Monei\Services\MoneiStatusCodeHandler;
78
use Monei\Services\payment\MoneiPaymentServices;
89
use Monei\Services\PaymentMethodsService;
910
use Monei\Templates\TemplateManager;
@@ -39,9 +40,10 @@ public function __construct(
3940
PaymentMethodsService $paymentMethodsService,
4041
TemplateManager $templateManager,
4142
ApiKeyService $apiKeyService,
42-
MoneiPaymentServices $moneiPaymentServices
43+
MoneiPaymentServices $moneiPaymentServices,
44+
MoneiStatusCodeHandler $statusCodeHandler
4345
) {
44-
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices );
46+
parent::__construct( $paymentMethodsService, $templateManager, $apiKeyService, $moneiPaymentServices, $statusCodeHandler );
4547
$this->id = MONEI_GATEWAY_ID . '_paypal';
4648
$this->method_title = __( 'MONEI - PayPal', 'monei' );
4749
$this->method_description = __( 'Accept PayPal payments.', 'monei' );

0 commit comments

Comments
 (0)