From 073e24bf1bc01f319d27971f0f70b4888c4b8570 Mon Sep 17 00:00:00 2001
From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com>
Date: Thu, 25 Aug 2022 13:46:30 +0200
Subject: [PATCH] Working on payment methods refactor.
See https://github.com/pronamic/wp-pay-core/issues/71.
---
src/Admin/AdminSettings.php | 54 +++++++++++++++
src/Core/Gateway.php | 42 ++----------
src/Core/PaymentMethods.php | 9 ++-
src/Core/PaymentMethodsCollection.php | 98 +++++++++++++++++++++++++++
src/Plugin.php | 70 +++++++++++++++++++
src/Settings.php | 17 +++++
6 files changed, 249 insertions(+), 41 deletions(-)
create mode 100644 src/Core/PaymentMethodsCollection.php
diff --git a/src/Admin/AdminSettings.php b/src/Admin/AdminSettings.php
index 776b22fc..36e7a9dd 100644
--- a/src/Admin/AdminSettings.php
+++ b/src/Admin/AdminSettings.php
@@ -172,6 +172,31 @@ public function admin_init() {
]
);
}
+
+ if ( version_compare( $this->plugin->get_version(), '10', '>=' ) ) {
+ // Settings - Payment Methods.
+ \add_settings_section(
+ 'pronamic_pay_payment_methods',
+ \__( 'Payment Methods', 'pronamic_ideal' ),
+ [ $this, 'settings_section' ],
+ 'pronamic_pay'
+ );
+
+ foreach ( $this->plugin->get_payment_methods() as $payment_method ) {
+ $id = 'pronamic_pay_payment_method_' . $payment_method->get_id() . '_status';
+
+ add_settings_field(
+ $id,
+ $payment_method->get_name(),
+ [ $this, 'select_payment_method_status' ],
+ 'pronamic_pay',
+ 'pronamic_pay_payment_methods',
+ [
+ 'label_for' => $id,
+ ]
+ );
+ }
+ }
}
/**
@@ -338,4 +363,33 @@ public function input_page( $args ) {
'class' => 'regular-text',
) );
}
+
+ public function select_payment_method_status( $args ) {
+ $name = $args['label_for'];
+
+ $selected = get_option( $name, '' );
+
+ $statuses = [
+ '' => '',
+ 'active' => \__( 'Active', 'pronamic_ideal' ),
+ 'inactive' => \__( 'Inactive', 'pronamic_ideal' ),
+ ];
+
+ \printf(
+ '';
+ }
}
diff --git a/src/Core/Gateway.php b/src/Core/Gateway.php
index 0d401caf..6e4bc7e8 100644
--- a/src/Core/Gateway.php
+++ b/src/Core/Gateway.php
@@ -80,15 +80,15 @@ abstract class Gateway {
/**
* Payment methods.
*
- * @var PaymentMethod[]
+ * @var PaymentMethodsCollection
*/
- protected $payment_methods = [];
+ protected $payment_methods;
/**
* Construct gateway.
*/
public function __construct() {
-
+ $this->payment_methods = new PaymentMethodsCollection();
}
/**
@@ -98,9 +98,7 @@ public function __construct() {
* @return void
*/
protected function register_payment_method( PaymentMethod $payment_method ) {
- $id = $payment_method->get_id();
-
- $this->payment_methods[ $id ] = $payment_method;
+ $this->payment_methods->add( $payment_method );;
}
/**
@@ -110,11 +108,7 @@ protected function register_payment_method( PaymentMethod $payment_method ) {
* @return PaymentMethod|null
*/
public function get_payment_method( $id ) {
- if ( array_key_exists( $id, $this->payment_methods ) ) {
- return $this->payment_methods[ $id ];
- }
-
- return null;
+ return $this->payment_methods->get( $id );
}
/**
@@ -149,31 +143,7 @@ public function first_payment_method_field( $payment_method_id, $field_class ) {
* @return PaymentMethod[]
*/
public function get_payment_methods( $args = [] ) {
- $payment_methods = $this->payment_methods;
-
- if ( \array_key_exists( 'status', $args ) ) {
- $status_list = \wp_parse_list( $args['status'] );
-
- $payment_methods = array_filter(
- $payment_methods,
- function( $payment_method ) use ( $status_list ) {
- return \in_array( $payment_method->get_status(), $status_list, true );
- }
- );
- }
-
- if ( \array_key_exists( 'supports', $args ) ) {
- $feature = $args['supports'];
-
- $payment_methods = array_filter(
- $payment_methods,
- function( $payment_method ) use ( $feature ) {
- return $payment_method->supports( $feature );
- }
- );
- }
-
- return $payment_methods;
+ return $this->payment_methods->query( $args );
}
/**
diff --git a/src/Core/PaymentMethods.php b/src/Core/PaymentMethods.php
index cebccf18..dd065baf 100644
--- a/src/Core/PaymentMethods.php
+++ b/src/Core/PaymentMethods.php
@@ -577,11 +577,10 @@ public static function update_active_payment_methods() {
continue;
}
- $payment_methods = array_filter(
- $gateway->get_payment_methods(),
- function ( $payment_method ) {
- return \in_array( $payment_method->get_status(), [ '', 'active' ], true );
- }
+ $payment_methods = $gateway->get_payment_methods(
+ [
+ 'status' => [ '', 'active' ],
+ ]
);
foreach ( $payment_methods as $payment_method ) {
diff --git a/src/Core/PaymentMethodsCollection.php b/src/Core/PaymentMethodsCollection.php
new file mode 100644
index 00000000..35bc34d8
--- /dev/null
+++ b/src/Core/PaymentMethodsCollection.php
@@ -0,0 +1,98 @@
+
+ * @copyright 2005-2022 Pronamic
+ * @license GPL-3.0-or-later
+ * @package Pronamic\WordPress\Pay\Core
+ */
+
+namespace Pronamic\WordPress\Pay\Core;
+
+use ArrayIterator;
+use IteratorAggregate;
+
+/**
+ * Payment methods collection class
+ */
+class PaymentMethodsCollection implements IteratorAggregate {
+ private $items = [];
+
+ public function add( PaymentMethod $payment_method ) {
+ $id = $payment_method->get_id();
+
+ $this->items[ $id ] = $payment_method;
+ }
+
+ /**
+ * Get payment method by ID.
+ *
+ * @param string $id ID.
+ * @return PaymentMethod|null
+ */
+ public function get( $id ) {
+ if ( array_key_exists( $id, $this->items ) ) {
+ return $this->items[ $id ];
+ }
+
+ return null;
+ }
+
+ public function query( $args ) {
+ $items = $this->items;
+
+ if ( \array_key_exists( 'status', $args ) ) {
+ $status_list = \wp_parse_list( $args['status'] );
+
+ $items = array_filter(
+ $items,
+ function( $payment_method ) use ( $status_list ) {
+ return \in_array( $payment_method->get_status(), $status_list, true );
+ }
+ );
+ }
+
+ if ( \array_key_exists( 'supports', $args ) ) {
+ $feature = $args['supports'];
+
+ $items = array_filter(
+ $items,
+ function( $payment_method ) use ( $feature ) {
+ return $payment_method->supports( $feature );
+ }
+ );
+ }
+
+ $collection = new self();
+
+ $collection->items = $items;
+
+ return $collection;
+ }
+
+ /**
+ * Get iterator.
+ *
+ * @return ArrayIterator
+ */
+ public function getIterator() {
+ return new ArrayIterator( $this->items );
+ }
+
+ /**
+ * Is active.
+ *
+ * @param string $id Payment method ID.
+ * @return bool True if status is active, false otherwise.
+ */
+ public function is_active( $id ) {
+ $payment_method = $this->get( $id );
+
+ if ( null === $payment_method ) {
+ return false;
+ }
+
+ return ( 'active' === $payment_method->get_status() );
+ }
+}
diff --git a/src/Plugin.php b/src/Plugin.php
index 1e39a527..c530faf6 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -15,7 +15,9 @@
use Pronamic\WordPress\Pay\Admin\AdminModule;
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
use Pronamic\WordPress\Pay\Core\Gateway;
+use Pronamic\WordPress\Pay\Core\PaymentMethod;
use Pronamic\WordPress\Pay\Core\PaymentMethods;
+use Pronamic\WordPress\Pay\Core\PaymentMethodsCollection;
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
use Pronamic\WordPress\Pay\Gateways\GatewaysDataStoreCPT;
use Pronamic\WordPress\Pay\Payments\Payment;
@@ -241,6 +243,13 @@ public static function instance( $args = [] ) {
*/
private static $pronamic_service_url;
+ /**
+ * Payment methods.
+ *
+ * @var PaymentMethodsCollection
+ */
+ private $payment_methods;
+
/**
* Construct and initialize an Pronamic Pay plugin object.
*
@@ -321,6 +330,67 @@ public function __construct( $args = [] ) {
}
require_once $args['action_scheduler'];
+
+ /**
+ * Payment methods.
+ */
+ $this->payment_methods = new PaymentMethodsCollection();
+
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::AFTERPAY_NL ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::AFTERPAY_COM ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::ALIPAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::AMERICAN_EXPRESS ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::APPLE_PAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BANCONTACT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BANK_TRANSFER ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BELFIUS ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BILLINK ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BITCOIN ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BLIK ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::BUNQ ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::IN3 ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::CAPAYABLE ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::CREDIT_CARD ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::DIRECT_DEBIT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::DIRECT_DEBIT_BANCONTACT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::DIRECT_DEBIT_IDEAL ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::DIRECT_DEBIT_SOFORT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::EPS ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::FOCUM ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::IDEAL ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::IDEALQR ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::GIROPAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::GOOGLE_PAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::GULDEN ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::KBC ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::KLARNA_PAY_LATER ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::KLARNA_PAY_NOW ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::KLARNA_PAY_OVER_TIME ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::MAESTRO ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::MASTERCARD ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::MB_WAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::PAYCONIQ ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::PAYPAL ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::PRZELEWY24 ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::SANTANDER ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::SOFORT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::SPRAYPAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::SWISH ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::TWINT ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::V_PAY ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::VIPPS ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::VISA ) );
+ $this->payment_methods->add( new PaymentMethod( PaymentMethods::VOID ) );
+ }
+
+ /**
+ * Get payment methods.
+ *
+ * @param array $args Query arguments.
+ * @return PaymentMethodsCollection
+ */
+ public function get_payment_methods( $args = [] ) {
+ return $this->payment_methods->query( $args );
}
/**
diff --git a/src/Settings.php b/src/Settings.php
index bf8b3b83..3d5afbc1 100644
--- a/src/Settings.php
+++ b/src/Settings.php
@@ -118,6 +118,23 @@ public function init() {
]
);
}
+
+ // Payment Methods.
+ $payment_methods = $this->plugin->get_payment_methods();
+
+ foreach ( $payment_methods as $payment_method ) {
+ $id = 'pronamic_pay_payment_method_' . $payment_method->get_id() . '_status';
+
+ \register_setting(
+ 'pronamic_pay',
+ $id,
+ [
+ 'type' => 'string',
+ ]
+ );
+
+ $payment_method->set_status( (string) \get_option( $id ) );
+ }
}
/**