Skip to content

Commit 97fdd93

Browse files
committed
Use 2 API keys
Add selector, copy old to new ones, add services to handle it using the container, update classes that consume the keys. Intermediate step with the classes still in included
1 parent 172b629 commit 97fdd93

24 files changed

+513
-274
lines changed

assets/js/monei-settings.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
jQuery( document ).ready(
2+
function ($) {
3+
// Function to toggle API key fields
4+
function toggleApiKeyFields() {
5+
var mode = $( '#monei_apikey_mode' ).val();
6+
if (mode === 'test') {
7+
$( '.monei-test-api-key-field' ).closest( 'tr' ).show();
8+
$( '.monei-live-api-key-field' ).closest( 'tr' ).hide();
9+
} else {
10+
$( '.monei-test-api-key-field' ).closest( 'tr' ).hide();
11+
$( '.monei-live-api-key-field' ).closest( 'tr' ).show();
12+
}
13+
}
14+
15+
// Initial call to set the correct fields on page load
16+
toggleApiKeyFields();
17+
18+
// Bind the function to the change event of the selector
19+
$( '#monei_apikey_mode' ).change( toggleApiKeyFields );
20+
}
21+
);

class-woocommerce-gateway-monei.php

Lines changed: 97 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
*/
1010

1111
use Monei\Core\ContainerProvider;
12+
use Monei\Services\ApiKeyService;
1213
use Monei\Services\BlockSupportService;
14+
use Monei\Services\MoneiApplePayVerificationService;
15+
use Monei\Services\payment\MoneiPaymentServices;
16+
use Monei\Services\sdk\MoneiSdkClientFactory;
1317
use Monei\Settings\MoneiSettings;
1418

1519
if ( ! class_exists( 'Woocommerce_Gateway_Monei' ) ) :
@@ -63,7 +67,7 @@ public function initalize_plugin() {
6367
}
6468

6569
self::$_initialized = true;
66-
70+
6771
// Declare block compatibility
6872
$this->block_compatiblity();
6973

@@ -72,23 +76,27 @@ public function initalize_plugin() {
7276

7377
public function block_compatiblity() {
7478
// Load checkout block class
75-
add_action( 'woocommerce_blocks_loaded', function() {
76-
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
77-
return;
79+
add_action(
80+
'woocommerce_blocks_loaded',
81+
function () {
82+
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
83+
return;
84+
}
85+
$container = ContainerProvider::getContainer();
86+
$blockSupportService = $container->get( BlockSupportService::class );
87+
$blockSupportClasses = $blockSupportService->getBlockSupportClasses();
88+
add_action(
89+
'woocommerce_blocks_payment_method_type_registration',
90+
function ( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) use ( $blockSupportClasses, $container ) {
91+
foreach ( $blockSupportClasses as $className ) {
92+
if ( $container->has( $className ) ) {
93+
$payment_method_registry->register( $container->get( $className ) );
94+
}
95+
}
96+
}
97+
);
7898
}
79-
$container = ContainerProvider::getContainer();
80-
$blockSupportService = $container->get(BlockSupportService::class);
81-
$blockSupportClasses = $blockSupportService->getBlockSupportClasses();
82-
add_action( 'woocommerce_blocks_payment_method_type_registration',
83-
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) use($blockSupportClasses, $container){
84-
foreach ($blockSupportClasses as $className) {
85-
if ($container->has($className)) {
86-
$payment_method_registry->register($container->get($className));
87-
}
88-
}
89-
} );
90-
91-
} );
99+
);
92100
}
93101

94102
/**
@@ -128,21 +136,22 @@ private function define_constants() {
128136
* Include required core files used in admin and on the frontend.
129137
*/
130138
private function includes() {
131-
$container = ContainerProvider::getContainer();
139+
$container = ContainerProvider::getContainer();
132140
include_once 'includes/woocommerce-gateway-monei-core-functions.php';
133141
include_once 'includes/class-wc-monei-ipn.php';
134-
include_once 'includes/class-wc-monei-api.php';
135142
include_once 'includes/class-wc-monei-logger.php';
136143
include_once 'includes/addons/trait-wc-monei-addons-helper.php';
137144
include_once 'includes/addons/trait-wc-monei-subscriptions.php';
138-
include_once 'includes/addons/class-wc-monei-apple-pay-verification.php';
139145

140146
if ( $this->is_request( 'admin' ) ) {
141147
include_once 'includes/class-wc-monei-pre-auth.php';
142-
add_filter('woocommerce_get_settings_pages', function ($settings) use ($container) {
143-
$settings[] = new MoneiSettings($container);
144-
return $settings;
145-
});
148+
add_filter(
149+
'woocommerce_get_settings_pages',
150+
function ( $settings ) use ( $container ) {
151+
$settings[] = new MoneiSettings( $container );
152+
return $settings;
153+
}
154+
);
146155
}
147156

148157
if ( $this->is_request( 'frontend' ) ) {
@@ -175,12 +184,12 @@ function admin_new_install_notice() {
175184
}
176185
return;
177186
}
178-
$container = \Monei\Core\ContainerProvider::getContainer();
179-
$templateManager = $container->get('Monei\Templates\TemplateManager' );
180-
$template = $templateManager->getTemplate('notice-admin-new-install');
181-
if ( $template ) {
182-
$template->render([]);
183-
}
187+
$container = \Monei\Core\ContainerProvider::getContainer();
188+
$templateManager = $container->get( 'Monei\Templates\TemplateManager' );
189+
$template = $templateManager->getTemplate( 'notice-admin-new-install' );
190+
if ( $template ) {
191+
$template->render( array() );
192+
}
184193
}
185194

186195
/**
@@ -189,12 +198,12 @@ function admin_new_install_notice() {
189198
* @return void
190199
*/
191200
public function dependency_notice() {
192-
$container = \Monei\Core\ContainerProvider::getContainer();
193-
$templateManager = $container->get('Monei\Templates\TemplateManager' );
194-
$template = $templateManager->getTemplate('notice-admin-dependency');
195-
if ( $template ) {
196-
$template->render([]);
197-
}
201+
$container = \Monei\Core\ContainerProvider::getContainer();
202+
$templateManager = $container->get( 'Monei\Templates\TemplateManager' );
203+
$template = $templateManager->getTemplate( 'notice-admin-dependency' );
204+
if ( $template ) {
205+
$template->render( array() );
206+
}
198207
}
199208

200209
/**
@@ -243,56 +252,40 @@ private function is_request( $type ) {
243252
public function init() {
244253
// Before init
245254
do_action( 'before_woocommerce_gateway_monei_init' );
255+
//TODO use the container
256+
$apiKeyService = new ApiKeyService();
257+
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
258+
$moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
259+
new MoneiApplePayVerificationService( $moneiPaymentServices );
246260

247261
// todo: not translation yet.
248262
//$this->load_plugin_textdomain();
249263

250-
add_filter( 'option_woocommerce_monei_bizum_settings', array( $this, 'monei_settings_by_default' ), 1 );
264+
add_filter( 'option_woocommerce_monei_bizum_settings', array( $this, 'monei_settings_by_default' ), 1 );
251265
add_filter( 'option_woocommerce_monei_paypal_settings', array( $this, 'monei_settings_by_default' ), 1 );
252-
add_filter( 'option_woocommerce_monei_multibanco_settings', array( $this, 'monei_settings_by_default' ), 1 );
253-
add_filter( 'option_woocommerce_monei_mbway_settings', array( $this, 'monei_settings_by_default' ), 1 );
254-
add_filter( 'option_woocommerce_monei_settings', array( $this, 'copyKeysToCentralSettings' ), 1 );
266+
add_filter( 'option_woocommerce_monei_multibanco_settings', array( $this, 'monei_settings_by_default' ), 1 );
267+
add_filter( 'option_woocommerce_monei_mbway_settings', array( $this, 'monei_settings_by_default' ), 1 );
255268

256269
// Init action.
257270
do_action( 'woocommerce_gateway_monei_init' );
258-
wp_register_style(
259-
'monei-icons',
271+
wp_register_style(
272+
'monei-icons',
260273
$this->plugin_url() . '/public/css/monei-icons-classic.css',
261-
[],
274+
array(),
262275
filemtime( $this->plugin_path() . '/public/css/monei-icons-classic.css' ),
263-
'screen'
276+
'screen'
264277
);
265278
wp_enqueue_style( 'monei-icons' );
266-
wp_register_style(
267-
'monei-blocks-checkout-cc',
268-
WC_Monei()->plugin_url(). '/public/css/monei-blocks-checkout.css',
269-
array(),
270-
WC_Monei()->version,
271-
'all'
272-
);
273-
wp_enqueue_style( 'monei-blocks-checkout-cc' );
279+
wp_register_style(
280+
'monei-blocks-checkout-cc',
281+
WC_Monei()->plugin_url() . '/public/css/monei-blocks-checkout.css',
282+
array(),
283+
WC_Monei()->version,
284+
'all'
285+
);
286+
wp_enqueue_style( 'monei-blocks-checkout-cc' );
274287
}
275288

276-
public function copyKeysToCentralSettings($default_params)
277-
{
278-
$centralApiKey = get_option('monei_apikey');
279-
$centralAccountId = get_option('monei_accountid');
280-
$ccApiKey = $default_params['apikey'] ?? false;
281-
$ccAccountId = $default_params['accountid'] ?? false;
282-
283-
// Update API key if centralApiKey is empty
284-
if ( empty( $centralApiKey ) && !empty( $ccApiKey ) ) {
285-
update_option( 'monei_apikey', $ccApiKey );
286-
}
287-
288-
// Update Account ID if centralAccountId is empty
289-
if ( empty( $centralAccountId ) && !empty( $ccAccountId ) ) {
290-
update_option( 'monei_accountid', $ccAccountId );
291-
}
292-
293-
return $default_params;
294-
}
295-
296289

297290
/**
298291
* We have more than a Monei payment provider, we will use by default the main monei set up in case they don't set them up.
@@ -301,32 +294,32 @@ public function copyKeysToCentralSettings($default_params)
301294
*
302295
* @return array
303296
*/
304-
public function monei_settings_by_default( $default_params ) {
305-
$default_params['testmode'] = $this->get_setting_with_default( 'testmode', $default_params );
306-
$default_params['apikey'] = $this->get_setting_with_default( 'apikey', $default_params );
307-
$default_params['debug'] = $this->get_setting_with_default( 'debug', $default_params );
308-
$default_params['orderdo'] = ( empty( $default_params['orderdo'] ) ) ? monei_get_settings( 'orderdo' ) : $default_params['orderdo'];
297+
public function monei_settings_by_default( $default_params ) {
298+
$default_params['testmode'] = $this->get_setting_with_default( 'testmode', $default_params );
299+
$default_params['apikey'] = $this->get_setting_with_default( 'apikey', $default_params );
300+
$default_params['debug'] = $this->get_setting_with_default( 'debug', $default_params );
301+
$default_params['orderdo'] = ( empty( $default_params['orderdo'] ) ) ? monei_get_settings( 'orderdo' ) : $default_params['orderdo'];
309302

310-
return $default_params;
311-
}
303+
return $default_params;
304+
}
312305

313-
private function get_setting_with_default( $key, $params ) {
314-
if ( ! empty( $params[ $key ] ) ) {
315-
return $params[ $key ];
316-
}
306+
private function get_setting_with_default( $key, $params ) {
307+
if ( ! empty( $params[ $key ] ) ) {
308+
return $params[ $key ];
309+
}
317310

318-
$option_value = get_option( "monei_$key" );
319-
if ( ! empty( $option_value ) ) {
320-
return $option_value;
321-
}
311+
$option_value = get_option( "monei_$key" );
312+
if ( ! empty( $option_value ) ) {
313+
return $option_value;
314+
}
322315

323-
$monei_setting_value = monei_get_settings( $key );
324-
if ( ! empty( $monei_setting_value ) ) {
325-
return $monei_setting_value;
326-
}
316+
$monei_setting_value = monei_get_settings( $key );
317+
if ( ! empty( $monei_setting_value ) ) {
318+
return $monei_setting_value;
319+
}
327320

328-
return '';
329-
}
321+
return '';
322+
}
330323

331324
/**
332325
* Hooks when plugin_loaded
@@ -343,17 +336,17 @@ public function plugins_loaded() {
343336
* @return array
344337
*/
345338
public function add_gateways( $methods ) {
346-
$container = \Monei\Core\ContainerProvider::getContainer();
347-
348-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiCC');
349-
if (!is_admin()) {
350-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiAppleGoogle');
351-
}
352-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiCofidis');
353-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiBizum');
354-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiPaypal');
355-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiMultibanco');
356-
$methods[] = $container->get('Monei\Gateways\PaymentMethods\WCGatewayMoneiMBWay');
339+
$container = \Monei\Core\ContainerProvider::getContainer();
340+
341+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiCC' );
342+
if ( ! is_admin() ) {
343+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiAppleGoogle' );
344+
}
345+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiCofidis' );
346+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiBizum' );
347+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiPaypal' );
348+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiMultibanco' );
349+
$methods[] = $container->get( 'Monei\Gateways\PaymentMethods\WCGatewayMoneiMBWay' );
357350
return $methods;
358351
}
359352

@@ -424,4 +417,3 @@ public function ajax_url() {
424417
}
425418

426419
endif;
427-

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
3+
use Monei\Services\ApiKeyService;
4+
use Monei\Services\payment\MoneiPaymentServices;
5+
use Monei\Services\sdk\MoneiSdkClientFactory;
6+
27
if ( ! defined( 'ABSPATH' ) ) {
38
exit; // Exit if accessed directly
49
}
@@ -16,12 +21,18 @@ class WC_Monei_Addons_Redirect_Hooks {
1621
*/
1722
use WC_Monei_Subscriptions_Trait;
1823

24+
private MoneiPaymentServices $moneiPaymentServices;
25+
1926
/**
2027
* Hooks on redirects.
2128
*/
2229
public function __construct() {
2330
add_action( 'template_redirect', array( $this, 'subscriptions_save_sequence_id' ) );
2431
add_action( 'template_redirect', array( $this, 'subscriptions_save_sequence_id_on_payment_method_change' ) );
32+
//TODO use the container
33+
$apiKeyService = new ApiKeyService();
34+
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
35+
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
2536
}
2637

2738
/**
@@ -57,7 +68,7 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
5768
/**
5869
* We need to update parent from subscription, where sequence id is stored.
5970
*/
60-
$payment = WC_Monei_API::get_payment( $payment_id );
71+
$payment = $this->moneiPaymentServices->get_payment( $payment_id );
6172
$subscription = new WC_Subscription( $order_id );
6273

6374
$subscription->update_meta_data( '_monei_sequence_id', $payment->getSequenceId() );
@@ -100,7 +111,7 @@ public function subscriptions_save_sequence_id() {
100111
return;
101112
}
102113

103-
$payment = WC_Monei_API::get_payment( $payment_id );
114+
$payment = $this->moneiPaymentServices->get_payment( $payment_id );
104115
/**
105116
* Iterate all subscriptions contained in the order, and add sequence id and cc data individually.
106117
*/

0 commit comments

Comments
 (0)