Failed Conditions
Push — master ( cc3467...84f509 )
by Reüel
09:31 queued 01:25
created

AbstractGateway   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 119
ccs 6
cts 33
cp 0.1818
rs 10
wmc 15

3 Methods

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