wp-pay-core icon indicating copy to clipboard operation
wp-pay-core copied to clipboard

Refactor `PaymentMethods` class, `PaymentMethods::get_active_payment_methods()` and `PaymentMethods::is_active( $payment_method = null )` methods

Open remcotolsma opened this issue 2 years ago • 1 comments

In https://github.com/pronamic/wp-pronamic-pay/issues/334 I ran into a problem with the PaymentMethods::update_active_payment_methods() method.

Saving a gateway configuration will trigger the Pronamic\WordPress\Pay\Core\PaymentMethods::update_active_payment_methods( ) method, exceptions in this method are not handled, how should we proceed?

Originally posted by @remcotolsma in https://github.com/pronamic/wp-pronamic-pay/issues/334#issuecomment-1225819445


The PaymentMethods::update_active_payment_methods() is not very scalable with a 'nopaging' => true WP_Query. Currently we mainly use the PaymentMethods class to hide some less common payment methods in for example the WooCommerce checkout settings page and in the payment methods field of forms plugins.


We need a way to register payment methods globally in the plugin. I would like to suggest that we create a settings page to give each registered payment method a status.

Payment Methods

Payment Method Status
iDEAL 'active'
AfterPay ''
Credit Card 'inactive'

Payment methods with an undefined status ('') could be auto activated by a gateway. If a user disabled a payment method (inactive) the status would never be auto updated. On saving a gateway configuration we only have to activate the payment methods that have an undefined status ('' or null). We no longer need to loop through all the gateway configurations and request which payment methods are active. We also no longer have to save that information per gateway configuration.

To implement this we could introduce a PaymentMethodsContainer or PaymentMethodsCollection class. Both the Gateway class and the Plugin class could use this class.

class PaymentMethodsCollection {

}

class Gateway {
	public function get_payment_methods() : PaymentMethodsCollection {

	}
}

class Plugin {
	public function get_payment_methods() : PaymentMethodsCollection {

	}
}

remcotolsma avatar Aug 25 '22 08:08 remcotolsma

Currently, a payment method can be listed as 'Active' on the 'Payment methods' tab in the payment gateway configuration settings, while the method does not appear in e.g. WooCommerce. Updating the payment gateway configuration resolved the issue.

Internal Help Scout ticket: https://secure.helpscout.net/conversation/2023558897/24522/

rvdsteege avatar Sep 29 '22 13:09 rvdsteege