Skip to content

Commit

Permalink
Merge branch 'release/4.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Sep 25, 2022
2 parents 4c7a58a + 6e41e6b commit 3a48299
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 77 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 18 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
];
}
}
139 changes: 85 additions & 54 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -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<string>
* @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<int, string>
* @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<string, string>|array<int, array<string, array<string, string>>>
* @see Core_Gateway::get_issuers()
* @return iterable<SelectFieldOption|SelectFieldOptionGroup>
*/
public function get_issuers() {
private function get_ideal_issuers() {
$issuers = [];

$payment_methods_request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
Expand All @@ -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;
}

/**
Expand Down
21 changes: 3 additions & 18 deletions tests/src/DropInGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit 3a48299

Please sign in to comment.