diff --git a/CHANGELOG.md b/CHANGELOG.md index 42bd56f..e5708d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased][unreleased] +## [4.2.0] - 2022-09-25 +- Updated payment methods registration. + ## [4.1.0] - 2022-07-01 ### Changed - Added WordPress network ID and blog ID to merchant reference ([#1](https://github.com/pronamic/wp-pronamic-pay-adyen/issues/1)). @@ -150,8 +153,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 1.0.0 - 2019-03-28 - First release. -[unreleased]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/4.1.0...HEAD -[4.0.0]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/4.0.0...4.1.0 +[unreleased]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/4.2.0...HEAD +[4.2.0]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/4.1.0...4.2.0 +[4.1.0]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/4.0.0...4.1.0 [4.0.0]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/3.1.1...4.0.0 [3.1.1]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/3.1.0...3.1.1 [3.1.0]: https://github.com/pronamic/wp-pronamic-pay-adyen/compare/3.0.1...3.1.0 diff --git a/package.json b/package.json index e90aa01..be50922 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adyen", - "version": "4.1.0", + "version": "4.2.0", "description": "Adyen driver for the WordPress payment processing library.", "repository": { "type": "git", diff --git a/src/Config.php b/src/Config.php index c1671e8..4036f60 100644 --- a/src/Config.php +++ b/src/Config.php @@ -10,13 +10,13 @@ namespace Pronamic\WordPress\Pay\Gateways\Adyen; -use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; +use JsonSerializable; use Pronamic\WordPress\Pay\Core\GatewayConfig; /** * Config class */ -class Config extends GatewayConfig { +class Config extends GatewayConfig implements JsonSerializable { /** * Environment. * @@ -85,4 +85,20 @@ public function get_merchant_account() { public function get_merchant_order_reference() { return (string) $this->merchant_order_reference; } + + /** + * Serialize to JSON. + * + * @link https://www.w3.org/TR/json-ld11/#specifying-the-type + * @return mixed|void + */ + public function jsonSerialize() { + return [ + '@type' => __CLASS__, + 'environment' => $this->environment, + 'merchant_account' => (string) $this->merchant_account, + 'api_key' => (string) $this->api_key, + 'client_key' => (string) $this->client_key, + ]; + } } diff --git a/src/Gateway.php b/src/Gateway.php index f592e18..923f9f9 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -11,8 +11,14 @@ namespace Pronamic\WordPress\Pay\Gateways\Adyen; use Pronamic\WordPress\Pay\Core\Gateway as 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\Fields\CachedCallbackOptions; +use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField; +use Pronamic\WordPress\Pay\Fields\SelectFieldOption; +use Pronamic\WordPress\Pay\Fields\SelectFieldOptionGroup; use Pronamic\WordPress\Pay\Payments\Payment; use Pronamic\WordPress\Pay\Payments\PaymentStatus; use Pronamic\WordPress\Pay\Plugin; @@ -50,6 +56,8 @@ class Gateway extends Core_Gateway { * @param Config $config Config. */ public function __construct( Config $config ) { + parent::__construct(); + $this->config = $config; $this->set_method( self::METHOD_HTTP_REDIRECT ); @@ -60,72 +68,103 @@ public function __construct( Config $config ) { ]; $this->client = new Client( $config ); + + // Methods. + $ideal_payment_method = new PaymentMethod( PaymentMethods::IDEAL ); + + $ideal_issuer_field = new IDealIssuerSelectField( 'ideal-issuer' ); + + $ideal_issuer_field->set_required( true ); + + $ideal_issuer_field->set_options( + new CachedCallbackOptions( + function() { + return $this->get_ideal_issuers(); + }, + 'pronamic_pay_ideal_issuers_' . \md5( \wp_json_encode( $config ) ) + ) + ); + + $ideal_payment_method->add_field( $ideal_issuer_field ); + + $this->register_payment_method( new PaymentMethod( PaymentMethods::AFTERPAY_COM ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::ALIPAY ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::APPLE_PAY ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::BANCONTACT ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::BLIK ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::CREDIT_CARD ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::DIRECT_DEBIT ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::EPS ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::GIROPAY ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::GOOGLE_PAY ) ); + $this->register_payment_method( $ideal_payment_method ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::KLARNA_PAY_LATER ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::KLARNA_PAY_NOW ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::KLARNA_PAY_OVER_TIME ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::MB_WAY ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::SOFORT ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::SWISH ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::TWINT ) ); + $this->register_payment_method( new PaymentMethod( PaymentMethods::VIPPS ) ); } /** - * Get supported payment methods + * Get payment methods. * - * @return array - * @see Core_Gateway::get_supported_payment_methods() + * @param array $args Query arguments. + * @return PaymentMethodsCollection */ - public function get_supported_payment_methods() { - return [ - PaymentMethods::AFTERPAY_COM, - PaymentMethods::ALIPAY, - PaymentMethods::APPLE_PAY, - PaymentMethods::BANCONTACT, - PaymentMethods::BLIK, - PaymentMethods::CREDIT_CARD, - PaymentMethods::DIRECT_DEBIT, - PaymentMethods::EPS, - PaymentMethods::GIROPAY, - PaymentMethods::GOOGLE_PAY, - PaymentMethods::IDEAL, - PaymentMethods::KLARNA_PAY_LATER, - PaymentMethods::KLARNA_PAY_NOW, - PaymentMethods::KLARNA_PAY_OVER_TIME, - PaymentMethods::MB_WAY, - PaymentMethods::SOFORT, - PaymentMethods::SWISH, - PaymentMethods::TWINT, - PaymentMethods::VIPPS, - ]; + public function get_payment_methods( array $args = [] ) : PaymentMethodsCollection { + try { + $this->maybe_enrich_payment_methods(); + } catch ( \Exception $e ) { + // No problem. + } + + return parent::get_payment_methods( $args ); } /** - * Get available payment methods. + * Maybe enrich payment methods. * - * @return array - * @see Core_Gateway::get_available_payment_methods() + * @return void */ - public function get_available_payment_methods() { - $core_payment_methods = []; + private function maybe_enrich_payment_methods() { + $cache_key = 'pronamic_pay_adyen_payment_methods_' . \md5( \wp_json_encode( $this->config ) ); - $payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) ); + $adyen_payment_methods = \get_transient( $cache_key ); - foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) { - $type = $payment_method->get_type(); + if ( false === $adyen_payment_methods ) { + $payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) ); - if ( null === $type ) { - continue; - } + $adyen_payment_methods = $payment_methods_response->get_payment_methods(); - $core_payment_methods[] = PaymentMethodType::to_wp( $type ); + \set_transient( $cache_key, $adyen_payment_methods, \DAY_IN_SECONDS ); } - $core_payment_methods = array_filter( $core_payment_methods ); - $core_payment_methods = array_unique( $core_payment_methods ); + foreach ( $adyen_payment_methods as $adyen_payment_method ) { + $core_payment_method_id = PaymentMethodType::to_wp( $adyen_payment_method->get_type() ); + + $core_payment_method = $this->get_payment_method( $core_payment_method_id ); + + if ( null !== $core_payment_method ) { + $core_payment_method->set_status( 'active' ); + } + } - return $core_payment_methods; + foreach ( $this->payment_methods as $payment_method ) { + if ( '' === $payment_method->get_status() ) { + $payment_method->set_status( 'inactive' ); + } + } } /** - * Get issuers. + * Get iDEAL issuers. * - * @return array|array>> - * @see Core_Gateway::get_issuers() + * @return iterable */ - public function get_issuers() { + private function get_ideal_issuers() { $issuers = []; $payment_methods_request = new PaymentMethodsRequest( $this->config->get_merchant_account() ); @@ -144,20 +183,12 @@ public function get_issuers() { $id = $payment_method_issuer->get_id(); $name = $payment_method_issuer->get_name(); - $issuers[ $id ] = $name; + $issuers[] = new SelectFieldOption( $id, $name ); } } } - if ( empty( $issuers ) ) { - return $issuers; - } - - return [ - [ - 'options' => $issuers, - ], - ]; + return $issuers; } /** diff --git a/tests/src/DropInGatewayTest.php b/tests/src/DropInGatewayTest.php index 44ced37..f9cc8a5 100644 --- a/tests/src/DropInGatewayTest.php +++ b/tests/src/DropInGatewayTest.php @@ -36,23 +36,8 @@ public function test_gateway() { $gateway = new DropInGateway( $config ); - $this->assertEquals( - [ - PaymentMethods::ALIPAY, - PaymentMethods::APPLE_PAY, - PaymentMethods::BANCONTACT, - PaymentMethods::CREDIT_CARD, - PaymentMethods::DIRECT_DEBIT, - PaymentMethods::EPS, - PaymentMethods::GIROPAY, - PaymentMethods::GOOGLE_PAY, - PaymentMethods::IDEAL, - PaymentMethods::KLARNA_PAY_LATER, - PaymentMethods::SOFORT, - PaymentMethods::SWISH, - PaymentMethods::VIPPS, - ], - $gateway->get_supported_payment_methods() - ); + $payment_methods = $gateway->get_payment_methods(); + + $this->assertCount( 12, $payment_methods ); } }