Failed Conditions
Push — feature/webhook-status ( e8032c )
by Reüel
05:37
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Payments\Payment;
8
9
/**
10
 * Title: iDEAL Advanced v3+ gateway
11
 * Description:
12
 * Copyright: 2005-2019 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.2
17
 * @since   1.0.0
18
 */
19
class Gateway extends Core_Gateway {
20
	/**
21
	 * Client.
22
	 *
23
	 * @var Client
24
	 */
25
	protected $client;
26
27
	/**
28
	 * Constructs and initializes an iDEAL Advanced v3 gateway
29
	 *
30
	 * @param Config $config Config.
31
	 */
32
	public function __construct( Config $config ) {
33
		parent::__construct( $config );
34
35
		$this->set_method( self::METHOD_HTTP_REDIRECT );
36
37
		// Supported features.
38
		$this->supports = self::get_supported_features();
39
40
		// Client.
41
		$client = new Client();
42
43
		$client->set_acquirer_url( $config->get_payment_server_url() );
44
45
		$client->merchant_id          = $config->merchant_id;
46
		$client->sub_id               = $config->sub_id;
47
		$client->private_key          = $config->private_key;
48
		$client->private_key_password = $config->private_key_password;
49
		$client->private_certificate  = $config->private_certificate;
50
51
		$this->client = $client;
52
	}
53
54
	/**
55
	 * Get supported features.
56
	 *
57
	 * @return array
58
	 */
59
	public static function get_supported_features() {
60
		return array(
61
			'payment_status_request',
62
		);
63
	}
64
65
	/**
66
	 * Get issuers
67
	 *
68
	 * @see Core_Gateway::get_issuers()
69
	 *
70
	 * @return array
71
	 */
72
	public function get_issuers() {
73
		$directory = $this->client->get_directory();
74
75
		if ( ! $directory ) {
0 ignored issues
show
introduced by
$directory is of type Pronamic\WordPress\Pay\G...ealAdvancedV3\Directory, thus it always evaluated to true.
Loading history...
76
			$this->error = $this->client->get_error();
77
78
			return array();
79
		}
80
81
		$groups = array();
82
83
		foreach ( $directory->get_countries() as $country ) {
84
			$issuers = array();
85
86
			foreach ( $country->get_issuers() as $issuer ) {
87
				$issuers[ $issuer->get_id() ] = $issuer->get_name();
88
			}
89
90
			$groups[] = array(
91
				'name'    => $country->get_name(),
92
				'options' => $issuers,
93
			);
94
		}
95
96
		return $groups;
97
	}
98
99
	/**
100
	 * Get supported payment methods
101
	 *
102
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
103
	 */
104
	public function get_supported_payment_methods() {
105
		return array(
106
			PaymentMethods::IDEAL,
107
		);
108
	}
109
110
	/**
111
	 * Is payment method required to start transaction?
112
	 *
113
	 * @see   Core_Gateway::payment_method_is_required()
114
	 * @since 1.1.5
115
	 */
116
	public function payment_method_is_required() {
117
		return true;
118
	}
119
120
	/**
121
	 * Start
122
	 *
123
	 * @see Pronamic_WP_Pay_Gateway::start()
124
	 *
125
	 * @param Payment $payment Payment.
126
	 */
127
	public function start( Payment $payment ) {
128
		// Purchase ID.
129
		$purchase_id = $payment->format_string( $this->config->purchase_id );
130
131
		$payment->set_meta( 'purchase_id', $purchase_id );
132
133
		// Transaction.
134
		$transaction = new Transaction();
135
		$transaction->set_purchase_id( $purchase_id );
136
		$transaction->set_amount( $payment->get_total_amount()->get_value() );
137
		$transaction->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
138
		$transaction->set_expiration_period( 'PT30M' );
139
		$transaction->set_description( $payment->get_description() );
140
		$transaction->set_entrance_code( $payment->get_entrance_code() );
141
142
		if ( null !== $payment->get_customer() ) {
143
			$transaction->set_language( $payment->get_customer()->get_language() );
144
		}
145
146
		// Create transaction.
147
		$result = $this->client->create_transaction( $transaction, $payment->get_return_url(), $payment->get_issuer() );
148
149
		$error = $this->client->get_error();
150
151
		if ( is_wp_error( $error ) ) {
152
			$this->set_error( $error );
153
154
			return;
155
		}
156
157
		$payment->set_action_url( $result->issuer->get_authentication_url() );
158
		$payment->set_transaction_id( $result->transaction->get_id() );
159
	}
160
161
	/**
162
	 * Update status of the specified payment
163
	 *
164
	 * @param Payment $payment Payment.
165
	 */
166
	public function update_status( Payment $payment ) {
167
		$result = $this->client->get_status( $payment->get_transaction_id() );
168
169
		$error = $this->client->get_error();
170
171
		if ( is_wp_error( $error ) ) {
172
			$this->set_error( $error );
173
		} else {
174
			$transaction = $result->transaction;
175
176
			$payment->set_status( $transaction->get_status() );
177
			$payment->set_consumer_name( $transaction->get_consumer_name() );
178
			$payment->set_consumer_iban( $transaction->get_consumer_iban() );
179
			$payment->set_consumer_bic( $transaction->get_consumer_bic() );
180
		}
181
	}
182
}
183