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 26, 2022
2 parents 41e2dfa + edec9a1 commit 594f560
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 165 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C
## [Unreleased][unreleased]
-

## [4.2.0] - 2022-09-26
- Updated payment methods registration.

## [4.1.2] - 2022-08-15
- Fixed parsing gateways response message ([pronamic/wp-pronamic-pay-multisafepay#3](https://github.com/pronamic/wp-pronamic-pay-multisafepay/issues/3)).

Expand Down Expand Up @@ -89,7 +92,8 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C
## 1.0.0 - 2015-01-19
- First release.

[unreleased]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.1.2...HEAD
[unreleased]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.2.0...HEAD
[4.2.0]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.1.2...4.2.0
[4.1.2]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.1.1...4.1.2
[4.1.1]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.1.0...4.1.1
[4.1.0]: https://github.com/pronamic/wp-pronamic-pay-multisafepay/compare/4.0.1...4.1.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"scripts": {
"coveralls": "vendor/bin/php-coveralls -v",
"phpcbf": "vendor/bin/phpcbf",
"phpcbf": "XDEBUG_MODE=off vendor/bin/phpcbf",
"phpcs": "XDEBUG_MODE=off vendor/bin/phpcs -s -v",
"phplint": "vendor/bin/phplint",
"phpmd": "vendor/bin/phpmd src,tests text phpmd.ruleset.xml --suffixes php",
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": "multisafepay",
"version": "4.1.2",
"version": "4.2.0",
"description": "MultiSafepay driver for the WordPress payment processing library.",
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<exclude-pattern>tests/bootstrap.php</exclude-pattern>
<exclude-pattern>tests/wp-config.php</exclude-pattern>

<rule ref="Generic.CodeAnalysis.EmptyStatement.DetectedCatch">
<exclude-pattern>src/Gateway.php</exclude-pattern>
</rule>

<rule ref="PronamicWP">
<exclude name="Squiz.Commenting" />

Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ private function request( $message ) {
$result = Core_Util::remote_get_body(
$this->api_url,
200,
array(
[
'method' => 'POST',
'body' => (string) $message,
)
]
);

if ( is_wp_error( $result ) ) {
Expand Down
209 changes: 126 additions & 83 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
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\Server;
use Pronamic\WordPress\Pay\Fields\CachedCallbackOptions;
use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField;
use Pronamic\WordPress\Pay\Fields\SelectField;
use Pronamic\WordPress\Pay\Fields\SelectFieldOption;
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage;
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
Expand All @@ -27,140 +33,190 @@ class Gateway extends Core_Gateway {
*
* @var Client
*/
protected $client;
protected Client $client;

/**
* Config
*
* @var Config
*/
protected $config;
protected Config $config;

/**
* Constructs and initializes an MultiSafepay Connect gateway
*
* @param Config $config Config.
*/
public function __construct( Config $config ) {
parent::__construct( $config );
parent::__construct();

$this->set_method( self::METHOD_HTTP_REDIRECT );

$this->config = $config;

// Supported features.
$this->supports = array(
$this->supports = [
'payment_status_request',
];

// Payment method iDEAL.
$ideal_payment_method = new PaymentMethod( PaymentMethods::IDEAL );

$ideal_issuer_field = new IDealIssuerSelectField( 'pronamic_pay_multisafepay_ideal_issuer' );

$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 );

// Payment method credit card.
$credit_card_payment_method = new PaymentMethod( PaymentMethods::CREDIT_CARD );

$credit_card_issuer_field = new SelectField( 'pronamic_pay_multisafepay_credit_card_issuer' );

$credit_card_issuer_field->set_label( __( 'Card Brand', 'pronamic_ideal' ) );

$credit_card_issuer_field->meta_key = 'issuer';

$credit_card_issuer_field->set_options(
new CachedCallbackOptions(
function() {
return $this->get_credit_card_issuers();
},
'pronamic_pay_credit_card_issuers_' . \md5( \wp_json_encode( $config ) )
)
);

$credit_card_payment_method->add_field( $credit_card_issuer_field );

// Payment methods.
$this->register_payment_method( new PaymentMethod( PaymentMethods::ALIPAY ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::BANCONTACT ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::BANK_TRANSFER ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::BELFIUS ) );
$this->register_payment_method( $credit_card_payment_method );
$this->register_payment_method( new PaymentMethod( PaymentMethods::DIRECT_DEBIT ) );
$this->register_payment_method( $ideal_payment_method );
$this->register_payment_method( new PaymentMethod( PaymentMethods::IDEALQR ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::IN3 ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::GIROPAY ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::KBC ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::PAYPAL ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::SANTANDER ) );
$this->register_payment_method( new PaymentMethod( PaymentMethods::SOFORT ) );

// Client.
$this->client = new Client();

$this->client->api_url = $config->get_api_url();
}

/**
* Get iDEAL issuers
* Get iDEAL issuers.
*
* @see Core_Gateway::get_issuers()
* @return array<array<string,array>>
* @since 1.2.0
*/
public function get_issuers() {
$groups = array();
private function get_ideal_issuers() : array {
$merchant = new Merchant();

// Merchant.
$merchant = new Merchant();
$merchant->account = $this->config->account_id;
$merchant->site_id = $this->config->site_id;
$merchant->site_secure_code = $this->config->site_code;

$result = $this->client->get_ideal_issuers( $merchant );

if ( $result ) {
$groups[] = array(
'options' => $result,
);
if ( false === $result ) {
return [];
}

$options = [];

foreach ( $result as $key => $value ) {
$options[] = new SelectFieldOption( $key, $value );
}

return $groups;
return $options;
}

/**
* Get credit card issuers
*
* @see Core_Gateway::get_credit_card_issuers()
* @return array<array<string,array>>
*/
public function get_credit_card_issuers() {
private function get_credit_card_issuers() : array {
// Get active card issuers.
$issuers = \array_intersect_key( $this->get_gateways(), Methods::get_cards() );

sort( $issuers );
asort( $issuers );

$groups = array(
array(
'options' => $issuers,
),
);
$options = [];

foreach ( $issuers as $key => $value ) {
$options[] = new SelectFieldOption( $key, $value );
}

return $options;
}

/**
* Get payment methods.
*
* @param array $args Query arguments.
* @return PaymentMethodsCollection
*/
public function get_payment_methods( array $args = [] ) : PaymentMethodsCollection {
try {
$this->maybe_enrich_payment_methods();
} catch ( \Exception $e ) {
// No problem.
}

return $groups;
return parent::get_payment_methods( $args );
}

/**
* Get payment methods
* Maybe enrich payment methods.
*
* @see Core_Gateway::get_payment_methods()
* @return array<string>
* @return void
*/
public function get_available_payment_methods() {
$payment_methods = array();
private function maybe_enrich_payment_methods() {
$cache_key = 'pronamic_pay_multisafepay_payment_methods_' . \md5( \wp_json_encode( $this->config ) );

$multisafepay_payment_methods = \get_transient( $cache_key );

if ( false === $multisafepay_payment_methods ) {
$multisafepay_payment_methods = $this->get_gateways();

$gateways = $this->get_gateways();
\set_transient( $cache_key, $multisafepay_payment_methods, \DAY_IN_SECONDS );
}

foreach ( $gateways as $method => $title ) {
$payment_method = Methods::transform_gateway_method( $method );
foreach ( $multisafepay_payment_methods as $method => $title ) {
$core_payment_method_id = Methods::transform_gateway_method( $method );

// Handle cards, as no general method for credit cards is returned by gateway.
if ( null === $payment_method && \array_key_exists( $method, Methods::get_cards() ) ) {
$payment_method = PaymentMethods::CREDIT_CARD;
if ( null === $core_payment_method_id && \array_key_exists( $method, Methods::get_cards() ) ) {
$core_payment_method_id = PaymentMethods::CREDIT_CARD;
}

// Check valid payment method.
if ( null === $payment_method ) {
continue;
}
$core_payment_method = $this->get_payment_method( $core_payment_method_id );

// Add available payment method.
$payment_methods[] = $payment_method;
if ( null !== $core_payment_method ) {
$core_payment_method->set_status( 'active' );
}
}

return $payment_methods;
}

/**
* Get supported payment methods
*
* @see Core_Gateway::get_supported_payment_methods()
* @return array<string>
*/
public function get_supported_payment_methods() {
return array(
PaymentMethods::ALIPAY,
PaymentMethods::BANCONTACT,
PaymentMethods::BANK_TRANSFER,
PaymentMethods::BELFIUS,
PaymentMethods::CREDIT_CARD,
PaymentMethods::DIRECT_DEBIT,
PaymentMethods::IDEAL,
PaymentMethods::IDEALQR,
PaymentMethods::IN3,
PaymentMethods::GIROPAY,
PaymentMethods::KBC,
PaymentMethods::PAYPAL,
PaymentMethods::SANTANDER,
PaymentMethods::SOFORT,
);
foreach ( $this->payment_methods as $payment_method ) {
if ( '' === $payment_method->get_status() ) {
$payment_method->set_status( 'inactive' );
}
}
}

/**
Expand Down Expand Up @@ -210,11 +266,10 @@ public function start( Payment $payment ) {
$transaction->currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
$transaction->amount = $payment->get_total_amount()->get_minor_units()->format( 0, '', '' );
$transaction->description = $transaction_description;
$transaction->gateway = Methods::transform( $payment_method );

switch ( $payment_method ) {
case PaymentMethods::IDEAL:
$transaction->gateway = Methods::IDEAL;

$issuer = $payment->get_meta( 'issuer' );

if ( empty( $issuer ) ) {
Expand All @@ -229,28 +284,16 @@ public function start( Payment $payment ) {

break;
case PaymentMethods::CREDIT_CARD:
$gateway = Methods::transform( $payment_method );

$issuer = $payment->get_meta( 'issuer' );

if ( empty( $issuer ) ) {
if ( $gateway ) {
$transaction->gateway = $gateway;
}
} else {
if ( ! empty( $issuer ) ) {
$transaction->gateway = $issuer;
}

$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );

break;
default:
$gateway = Methods::transform( $payment_method );

if ( $gateway ) {
$transaction->gateway = $gateway;
}

if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
// Leap of faith if the WordPress payment method could not transform to a Mollie method?
$transaction->gateway = $payment_method;
Expand Down Expand Up @@ -328,7 +371,7 @@ public function update_status( Payment $payment ) {
*
* @return array<string, string>
*/
private function get_gateways() {
private function get_gateways() : array {
// Merchant.
$merchant = new Merchant();
$merchant->account = $this->config->account_id;
Expand All @@ -339,7 +382,7 @@ private function get_gateways() {
$customer = new Customer();

// Get gateways.
$gateways = array();
$gateways = [];

$result = $this->client->get_gateways( $merchant, $customer );

Expand Down
Loading

0 comments on commit 594f560

Please sign in to comment.