Skip to content

Commit

Permalink
Working on payment methods refactor.
Browse files Browse the repository at this point in the history
See #71.
  • Loading branch information
remcotolsma committed Aug 25, 2022
1 parent 4e2c148 commit 073e24b
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 41 deletions.
54 changes: 54 additions & 0 deletions src/Admin/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
}
}

/**
Expand Down Expand Up @@ -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(
'<select id="%s" name="%s">',
\esc_attr( $name ),
\esc_attr( $name )
);

foreach ( $statuses as $status => $label ) {
\printf(
'<option value="%s" %s>%s</option>',
\esc_attr( $status ),
\selected( $status, $selected, false ),
\esc_html( $label )
);
}

echo '</select>';
}
}
42 changes: 6 additions & 36 deletions src/Core/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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 );;
}

/**
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Core/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
98 changes: 98 additions & 0 deletions src/Core/PaymentMethodsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Payment methods collection
*
* @author Pronamic <[email protected]>
* @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<string, PaymentMethod>
*/
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() );
}
}
70 changes: 70 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}

/**
Expand Down

0 comments on commit 073e24b

Please sign in to comment.