Gateway::get_supported_payment_methods()   A
last analyzed

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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Gateway.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
12
13
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Payments\Payment;
17
18
/**
19
 * Title: iDEAL Advanced v3+ gateway
20
 * Description:
21
 * Copyright: 2005-2021 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.5
26
 * @since   1.0.0
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Client.
31
	 *
32
	 * @var Client
33
	 */
34
	protected $client;
35
36
	/**
37
	 * Constructs and initializes an iDEAL Advanced v3 gateway
38
	 *
39
	 * @param Config $config Config.
40
	 */
41
	public function __construct( Config $config ) {
42
		parent::__construct( $config );
43
44
		$this->set_method( self::METHOD_HTTP_REDIRECT );
45
46
		// Supported features.
47
		$this->supports = array(
48
			'payment_status_request',
49
		);
50
51
		// Client.
52
		$client = new Client();
53
54
		$client->set_acquirer_url( (string) $config->get_payment_server_url() );
55
56
		$client->merchant_id          = (string) $config->get_merchant_id();
57
		$client->sub_id               = (string) $config->get_sub_id();
58
		$client->private_key          = (string) $config->get_private_key();
59
		$client->private_key_password = (string) $config->get_private_key_password();
60
		$client->private_certificate  = (string) $config->get_private_certificate();
61
62
		$this->client = $client;
63
	}
64
65
	/**
66
	 * Get issuers
67
	 *
68
	 * @see Core_Gateway::get_issuers()
69
	 * @return array<int, array<string, array<string, string>|string>>
70
	 */
71
	public function get_issuers() {
72
		$groups = array();
73
74
		try {
75
			$directory = $this->client->get_directory();
76
		} catch ( \Exception $e ) {
77
			$this->error = new \WP_Error( 'ideal_advanced_v3_error', $e->getMessage() );
78
79
			return $groups;
80
		}
81
82
		if ( null === $directory ) {
83
			return $groups;
84
		}
85
86
		foreach ( $directory->get_countries() as $country ) {
87
			$issuers = array();
88
89
			foreach ( $country->get_issuers() as $issuer ) {
90
				$id   = $issuer->get_id();
91
				$name = $issuer->get_name();
92
93
				if ( null === $id || null === $name ) {
94
					continue;
95
				}
96
97
				$issuers[ $id ] = $name;
98
			}
99
100
			$groups[] = array(
101
				'name'    => $country->get_name(),
102
				'options' => $issuers,
103
			);
104
		}
105
106
		return $groups;
107
	}
108
109
	/**
110
	 * Get supported payment methods
111
	 *
112
	 * @see Core_Gateway::get_supported_payment_methods()
113
	 * @return array<int, string>
114
	 */
115
	public function get_supported_payment_methods() {
116
		return array(
117
			PaymentMethods::IDEAL,
118
		);
119
	}
120
121
	/**
122
	 * Is payment method required to start transaction?
123
	 *
124
	 * @see   Core_Gateway::payment_method_is_required()
125
	 * @since 1.1.5
126
	 */
127
	public function payment_method_is_required() {
128
		return true;
129
	}
130
131
	/**
132
	 * Start
133
	 *
134
	 * @see Core_Gateway::start()
135
	 *
136
	 * @param Payment $payment Payment.
137
	 */
138
	public function start( Payment $payment ) {
139
		// Purchase ID.
140
		$purchase_id = $payment->format_string( $this->config->get_purchase_id() );
141
142
		$payment->set_meta( 'purchase_id', $purchase_id );
143
144
		/**
145
		 * The Transaction.entranceCode is an 'authentication identifier' to
146
		 * facilitate continuation of the session between Merchant and Consumer,
147
		 * even if the existing session has been lost. It enables the Merchant to
148
		 * recognise the Consumer associated with a (completed) transaction.
149
		 * The Transaction.entranceCode is sent to the Merchant in the Redirect.
150
		 * The Transaction.entranceCode must have a minimum variation of 1
151
		 * million and should comprise letters and/or figures (maximum 40
152
		 * positions).
153
		 * The Transaction.entranceCode is created by the Merchant and passed
154
		 * to the Issuer.
155
		 *
156
		 * @link https://www.pronamic.eu/wp-content/uploads/sites/2/2016/06/Merchant-Integration-Guide-v3-3-1-ENG-February-2015.pdf
157
		 */
158
		$entrance_code = \wp_generate_password( 40, false );
159
160
		$payment->set_meta( 'entrance_code', $entrance_code );
161
162
		// Transaction.
163
		$transaction = new Transaction();
164
		$transaction->set_purchase_id( $purchase_id );
165
		$transaction->set_amount( $payment->get_total_amount()->number_format( null, '.', '' ) );
166
		$transaction->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() );
167
		$transaction->set_expiration_period( 'PT30M' );
168
		$transaction->set_description( $payment->get_description() );
169
		$transaction->set_entrance_code( $entrance_code );
170
171
		$customer = $payment->get_customer();
172
173
		if ( null !== $customer ) {
174
			$transaction->set_language( $customer->get_language() );
175
		}
176
177
		// Create transaction.
178
		$result = $this->client->create_transaction( $transaction, $payment->get_return_url(), (string) $payment->get_issuer() );
179
180
		if ( null !== $result->issuer ) {
181
			$authentication_url = $result->issuer->get_authentication_url();
182
183
			if ( null !== $authentication_url ) {
184
				$payment->set_action_url( $authentication_url );
185
			}
186
		}
187
188
		if ( null !== $result->transaction ) {
189
			$payment->set_transaction_id( $result->transaction->get_id() );
190
		}
191
	}
192
193
	/**
194
	 * Update status of the specified payment
195
	 *
196
	 * @param Payment $payment Payment.
197
	 */
198
	public function update_status( Payment $payment ) {
199
		$transaction_id = (string) $payment->get_transaction_id();
200
201
		// Try to retrieve payment status.
202
		try {
203
			$result = $this->client->get_status( $transaction_id );
204
		} catch ( \Exception $e ) {
205
			$note = sprintf(
206
				/* translators: %s: exception message */
207
				__( 'Error getting payment status: %s', 'pronamic_ideal' ),
208
				$e->getMessage()
209
			);
210
211
			$payment->add_note( $note );
212
213
			return;
214
		}
215
216
		// Check transaction result.
217
		if ( null === $result->transaction ) {
218
			return;
219
		}
220
221
		// Update payment with transaction data.
222
		$transaction = $result->transaction;
223
224
		$payment->set_status( $transaction->get_status() );
225
226
		$consumer_bank_details = $payment->get_consumer_bank_details();
227
228
		if ( null === $consumer_bank_details ) {
229
			$consumer_bank_details = new BankAccountDetails();
230
231
			$payment->set_consumer_bank_details( $consumer_bank_details );
232
		}
233
234
		$consumer_bank_details->set_name( $transaction->get_consumer_name() );
235
		$consumer_bank_details->set_iban( $transaction->get_consumer_iban() );
236
		$consumer_bank_details->set_bic( $transaction->get_consumer_bic() );
237
	}
238
}
239