Skip to content

Commit 55d0811

Browse files
committed
feat: display payment method label in admin and customer views
- Create PaymentMethodFormatter service similar to PrestaShop - Format payment method names (Card, Bizum, PayPal, Apple Pay, etc.) - Format card brands (Visa, MasterCard, Amex, etc.) - Format card numbers with last 4 digits (•••• 1234) - Obfuscate email addresses for PayPal (a•••@example.com) - Format phone numbers for Bizum (••••1234) - Store formatted payment method in order meta - IPN handler fetches payment from API and saves display - Redirect hooks save payment method display - Stored in _monei_payment_method_display meta key - Display formatted payment method in WooCommerce - Override payment method title in order list - Show payment details in admin order page - Display in customer order view and emails Examples: - "Visa •••• 1234" - "Apple Pay •••• 5678" - "Bizum ••••1234" - "PayPal (a•••@gmail.com)"
1 parent 53db43d commit 55d0811

File tree

5 files changed

+400
-6
lines changed

5 files changed

+400
-6
lines changed

includes/class-wc-monei-ipn.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
use Monei\Core\ContainerProvider;
34
use Monei\Services\ApiKeyService;
45
use Monei\Services\payment\MoneiPaymentServices;
6+
use Monei\Services\PaymentMethodFormatter;
57
use Monei\Services\sdk\MoneiSdkClientFactory;
68

79
if ( ! defined( 'ABSPATH' ) ) {
@@ -18,6 +20,7 @@ class WC_Monei_IPN {
1820

1921
private $logging;
2022
private MoneiPaymentServices $moneiPaymentServices;
23+
private PaymentMethodFormatter $paymentMethodFormatter;
2124

2225
/**
2326
* Constructor.
@@ -27,9 +30,11 @@ public function __construct( bool $logging = false ) {
2730
// Handles request from MONEI.
2831
add_action( 'woocommerce_api_monei_ipn', array( $this, 'check_ipn_request' ) );
2932
//TODO use the container
30-
$apiKeyService = new ApiKeyService();
31-
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
32-
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
33+
$apiKeyService = new ApiKeyService();
34+
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
35+
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
36+
$container = ContainerProvider::getContainer();
37+
$this->paymentMethodFormatter = $container->get( PaymentMethodFormatter::class );
3338
}
3439

3540
/**
@@ -154,6 +159,20 @@ protected function handle_valid_ipn( $payload ) {
154159
$order->update_meta_data( '_payment_order_status_code_monei', $status_code );
155160
$order->update_meta_data( '_payment_order_status_message_monei', $status_message );
156161
$order->update_meta_data( '_monei_payment_id_processed', $monei_id );
162+
163+
// Fetch payment from API to get payment method information
164+
try {
165+
$this->moneiPaymentServices->set_order( $order );
166+
$payment = $this->moneiPaymentServices->get_payment( $monei_id );
167+
$payment_method_display = $this->paymentMethodFormatter->get_payment_method_display_from_payment( $payment );
168+
if ( $payment_method_display ) {
169+
$order->update_meta_data( '_monei_payment_method_display', $payment_method_display );
170+
}
171+
} catch ( \Exception $e ) {
172+
// Log but don't fail - payment method display is not critical
173+
WC_Monei_Logger::log( '[MONEI] Failed to get payment method display: ' . $e->getMessage(), 'warning' );
174+
}
175+
157176
$order->save();
158177

159178
if ( 'PENDING' === $status ) {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Payment Method Display Handler
4+
*
5+
* Handles displaying formatted payment method information in admin and customer-facing areas.
6+
*
7+
* @package Monei
8+
* @since 6.5.0
9+
*/
10+
11+
if ( ! defined( 'ABSPATH' ) ) {
12+
exit;
13+
}
14+
15+
/**
16+
* Class WC_Monei_Payment_Method_Display
17+
*/
18+
class WC_Monei_Payment_Method_Display {
19+
20+
/**
21+
* Constructor.
22+
*/
23+
public function __construct() {
24+
// Display payment method in admin order list
25+
add_filter( 'woocommerce_order_get_payment_method_title', array( $this, 'get_payment_method_title' ), 10, 2 );
26+
27+
// Display payment method in order details (admin and customer)
28+
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'display_payment_method_details_admin' ), 10, 1 );
29+
}
30+
31+
/**
32+
* Override payment method title to show formatted payment details
33+
*
34+
* @param string $title Payment method title.
35+
* @param \WC_Order $order Order object.
36+
* @return string
37+
*/
38+
public function get_payment_method_title( $title, $order ) {
39+
// Only modify MONEI payment methods
40+
$payment_method = $order->get_payment_method();
41+
if ( strpos( $payment_method, 'monei' ) !== 0 ) {
42+
return $title;
43+
}
44+
45+
// Get stored payment method display
46+
$payment_method_display = $order->get_meta( '_monei_payment_method_display', true );
47+
48+
if ( $payment_method_display ) {
49+
return $payment_method_display;
50+
}
51+
52+
return $title;
53+
}
54+
55+
/**
56+
* Display payment method details in admin order page
57+
*
58+
* @param \WC_Order $order Order object.
59+
* @return void
60+
*/
61+
public function display_payment_method_details_admin( $order ) {
62+
// Only show for MONEI payment methods
63+
$payment_method = $order->get_payment_method();
64+
if ( strpos( $payment_method, 'monei' ) !== 0 ) {
65+
return;
66+
}
67+
68+
$payment_method_display = $order->get_meta( '_monei_payment_method_display', true );
69+
if ( ! $payment_method_display ) {
70+
return;
71+
}
72+
73+
$payment_id = $order->get_meta( '_payment_order_number_monei', true );
74+
$payment_status = $order->get_meta( '_payment_order_status_monei', true );
75+
76+
?>
77+
<div class="order_data_column">
78+
<h3><?php esc_html_e( 'MONEI Payment Details', 'monei' ); ?></h3>
79+
<p>
80+
<strong><?php esc_html_e( 'Payment Method:', 'monei' ); ?></strong><br/>
81+
<?php echo esc_html( $payment_method_display ); ?>
82+
</p>
83+
<?php if ( $payment_id ) : ?>
84+
<p>
85+
<strong><?php esc_html_e( 'Transaction ID:', 'monei' ); ?></strong><br/>
86+
<?php echo esc_html( $payment_id ); ?>
87+
</p>
88+
<?php endif; ?>
89+
<?php if ( $payment_status ) : ?>
90+
<p>
91+
<strong><?php esc_html_e( 'Payment Status:', 'monei' ); ?></strong><br/>
92+
<?php echo esc_html( $payment_status ); ?>
93+
</p>
94+
<?php endif; ?>
95+
</div>
96+
<?php
97+
}
98+
}
99+
100+
new WC_Monei_Payment_Method_Display();

includes/class-wc-monei-redirect-hooks.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
use Monei\Core\ContainerProvider;
34
use Monei\Services\ApiKeyService;
45
use Monei\Services\payment\MoneiPaymentServices;
6+
use Monei\Services\PaymentMethodFormatter;
57
use Monei\Services\sdk\MoneiSdkClientFactory;
68

79
if ( ! defined( 'ABSPATH' ) ) {
@@ -19,6 +21,7 @@
1921
*/
2022
class WC_Monei_Redirect_Hooks {
2123
private MoneiPaymentServices $moneiPaymentServices;
24+
private PaymentMethodFormatter $paymentMethodFormatter;
2225

2326
/**
2427
* Constructor.
@@ -28,9 +31,11 @@ public function __construct() {
2831
add_action( 'template_redirect', array( $this, 'add_notice_monei_order_failed' ) );
2932
add_action( 'wp', array( $this, 'save_payment_token' ) );
3033
//TODO use the container
31-
$apiKeyService = new ApiKeyService();
32-
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
33-
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
34+
$apiKeyService = new ApiKeyService();
35+
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
36+
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
37+
$container = ContainerProvider::getContainer();
38+
$this->paymentMethodFormatter = $container->get( PaymentMethodFormatter::class );
3439
}
3540

3641
/**
@@ -237,6 +242,12 @@ private function verify_and_complete_order( $order_id, $payment ) {
237242
$order->update_meta_data( '_payment_order_status_code_monei', $payment->getStatusCode() );
238243
$order->update_meta_data( '_payment_order_status_message_monei', $payment->getStatusMessage() );
239244

245+
// Store formatted payment method display
246+
$payment_method_display = $this->paymentMethodFormatter->get_payment_method_display_from_payment( $payment );
247+
if ( $payment_method_display ) {
248+
$order->update_meta_data( '_monei_payment_method_display', $payment_method_display );
249+
}
250+
240251
if ( 'AUTHORIZED' === $payment_status ) {
241252
$order->update_meta_data( '_payment_not_captured_monei', 1 );
242253
$order_note = __( 'Payment verified via redirect - <strong>Payment Authorized</strong>', 'monei' ) . '. <br><br>';

src/Core/container-definitions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Monei\Services\MoneiApplePayVerificationService;
1111
use Monei\Services\MoneiStatusCodeHandler;
1212
use Monei\Services\payment\MoneiPaymentServices;
13+
use Monei\Services\PaymentMethodFormatter;
1314
use Monei\Services\PaymentMethodsService;
1415
use Monei\Services\sdk\MoneiSdkClientFactory;
1516
use Monei\Templates\NoticeAdminDependency;
@@ -60,6 +61,7 @@ function ( ApiKeyService $apiKeyService, MoneiSdkClientFactory $sdkClientFactory
6061
->constructor( DI\get( PaymentMethodsService::class ) ),
6162
MoneiPaymentServices::class => DI\autowire( MoneiPaymentServices::class ),
6263
MoneiStatusCodeHandler::class => DI\autowire( MoneiStatusCodeHandler::class ),
64+
PaymentMethodFormatter::class => DI\autowire( PaymentMethodFormatter::class ),
6365
BlockSupportService::class => DI\create( BlockSupportService::class )
6466
->constructor( $blocksPath, $blockNamespacePrefix ),
6567
MoneiApplePayVerificationService::class => DI\autowire( MoneiApplePayVerificationService::class )

0 commit comments

Comments
 (0)