Test Failed
Push — develop ( ee5b51...397f1c )
by Reüel
05:20
created

AbstractGateway::get_available_payment_methods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Exception;
14
use InvalidArgumentException;
15
use Locale;
16
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
17
use Pronamic\WordPress\Pay\Core\PaymentMethods;
18
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
use Pronamic\WordPress\Pay\Plugin;
21
use WP_Error;
22
23
/**
24
 * Gateway
25
 *
26
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
27
 *
28
 * @author  Remco Tolsma
29
 * @version 1.0.5
30
 * @since   1.0.0
31
 */
32
abstract class AbstractGateway extends Core_Gateway {
33
	/**
34
	 * Client.
35
	 *
36
	 * @var Client
37
	 */
38
	public $client;
39
40
	/**
41
	 * Constructs and initializes an Adyen gateway.
42
	 *
43
	 * @param Config $config Config.
44
	 */
45
	public function __construct( Config $config ) {
46
		parent::__construct( $config );
47
48
		$this->set_method( self::METHOD_HTTP_REDIRECT );
49
50
		// Supported features.
51
		$this->supports = array();
52
53
		// Client.
54
		$this->client = new Client( $config );
55
	}
56
57
	/**
58
	 * Get available payment methods.
59
	 *
60
	 * @return array<int, string>
61
	 * @see Core_Gateway::get_available_payment_methods()
62
	 *
63
	 */
64
	public function get_available_payment_methods() {
65
		$core_payment_methods = array();
66
67
		try {
68
			$payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) );
69
		} catch ( Exception $e ) {
70
			$this->error = new WP_Error( 'adyen_error', $e->getMessage() );
71
72
			return $core_payment_methods;
73
		}
74
75
		foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) {
76
			$core_payment_method = PaymentMethodType::to_wp( $payment_method->get_type() );
77
78
			$core_payment_methods[] = $core_payment_method;
79
		}
80
81
		$core_payment_methods = array_filter( $core_payment_methods );
82
		$core_payment_methods = array_unique( $core_payment_methods );
83
84
		return $core_payment_methods;
85
	}
86
87
	/**
88
	 * Get issuers.
89
	 *
90
	 * @return array<int, array<string, array<string, string>>>
91
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
92
	 */
93
	public function get_issuers() {
94
		$issuers = array();
95
96
		try {
97
			$payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) );
98
		} catch ( Exception $e ) {
99
			$this->error = new WP_Error( 'adyen_error', $e->getMessage() );
100
101
			return $issuers;
102
		}
103
104
		$payment_methods = $payment_methods_response->get_payment_methods();
105
106
		// Limit to iDEAL payment methods.
107
		$payment_methods = array_filter(
108
			$payment_methods,
109
			/**
110
			 * Check if payment method is iDEAL.
111
			 *
112
			 * @param PaymentMethod $payment_method Payment method.
113
			 *
114
			 * @return boolean True if payment method is iDEAL, false otherwise.
115
			 */
116
			function( $payment_method ) {
117
				return ( PaymentMethodType::IDEAL === $payment_method->get_type() );
118
			}
119
		);
120
121
		foreach ( $payment_methods as $payment_method ) {
122
			$details = $payment_method->get_details();
123
124
			if ( is_array( $details ) ) {
125
				foreach ( $details as $detail ) {
126
					if ( ! isset( $detail->key, $detail->type, $detail->items ) ) {
127
						continue;
128
					}
129
130
					if ( 'issuer' === $detail->key && 'select' === $detail->type ) {
131
						foreach ( $detail->items as $item ) {
132
							$issuers[ \strval( $item->id ) ] = \strval( $item->name );
133
						}
134
					}
135
				}
136
			}
137
		}
138
139
		if ( empty( $issuers ) ) {
140
			return $issuers;
141
		}
142
143
		return array(
144
			array(
145
				'options' => $issuers,
146
			),
147
		);
148
	}
149
}
150