Failed Conditions
Push — develop ( 84789b...288de2 )
by Reüel
06:59
created

Gateway::get_issuers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9332
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay;
4
5
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
6
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
7
use Pronamic\WordPress\Pay\Core\PaymentMethods;
8
use Pronamic\WordPress\Pay\Core\Server;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
11
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
12
use Pronamic\WordPress\Pay\Payments\Payment;
13
14
/**
15
 * Title: MultiSafepay Connect gateay
16
 * Description:
17
 * Copyright: 2005-2020 Pronamic
18
 * Company: Pronamic
19
 *
20
 * @author  Remco Tolsma
21
 * @version 2.1.1
22
 * @since   1.0.1
23
 */
24
class Gateway extends Core_Gateway {
25
	/**
26
	 * Client.
27
	 *
28
	 * @var Client
29
	 */
30
	protected $client;
31
32
	/**
33
	 * Config
34
	 *
35
	 * @var Config
36
	 */
37
	protected $config;
38
39
	/**
40
	 * Constructs and initializes an MultiSafepay Connect gateway
41
	 *
42
	 * @param Config $config Config.
43
	 */
44 1
	public function __construct( Config $config ) {
45 1
		parent::__construct( $config );
46
47 1
		$this->set_method( self::METHOD_HTTP_REDIRECT );
48
49
		// Supported features.
50 1
		$this->supports = array(
51
			'payment_status_request',
52
		);
53
54
		// Client.
55 1
		$this->client = new Client();
56
57 1
		$this->client->api_url = $config->api_url;
58 1
	}
59
60
	/**
61
	 * Get iDEAL issuers
62
	 *
63
	 * @see Core_Gateway::get_issuers()
64
	 * @since 1.2.0
65
	 */
66 1
	public function get_issuers() {
67 1
		$groups = array();
68
69
		// Merchant.
70 1
		$merchant                   = new Merchant();
71 1
		$merchant->account          = $this->config->account_id;
72 1
		$merchant->site_id          = $this->config->site_id;
73 1
		$merchant->site_secure_code = $this->config->site_code;
74
75 1
		$result = $this->client->get_ideal_issuers( $merchant );
76
77 1
		if ( $result ) {
78 1
			$groups[] = array(
79 1
				'options' => $result,
80
			);
81
		}
82
83 1
		return $groups;
84
	}
85
86
	/**
87
	 * Get credit card issuers
88
	 *
89
	 * @see Core_Gateway::get_credit_card_issuers()
90
	 */
91
	public function get_credit_card_issuers() {
92
		$groups[] = array(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$groups was never initialized. Although not strictly required by PHP, it is generally a good practice to add $groups = array(); before regardless.
Loading history...
93
			'options' => array(
94
				Methods::AMEX       => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
95
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
96
				Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ),
97
				Methods::VISA       => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ),
98
			),
99
		);
100
101
		return $groups;
102
	}
103
104
	/**
105
	 * Get payment methods
106
	 *
107
	 * @see Pronamic_WP_Pay_Gateway::get_payment_methods()
108
	 */
109
	public function get_available_payment_methods() {
110
		$payment_methods = array();
111
112
		// Merchant.
113
		$merchant                   = new Merchant();
114
		$merchant->account          = $this->config->account_id;
115
		$merchant->site_id          = $this->config->site_id;
116
		$merchant->site_secure_code = $this->config->site_code;
117
118
		// Customer.
119
		$customer = new Customer();
120
121
		// Get gateways.
122
		try {
123
			$result = $this->client->get_gateways( $merchant, $customer );
124
		} catch ( \Exception $e ) {
125
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
126
127
			$this->set_error( $error );
128
129
			return $payment_methods;
130
		}
131
132
		if ( false === $result ) {
133
			return $payment_methods;
134
		}
135
136
		foreach ( $result as $method => $title ) {
137
			$payment_method = Methods::transform_gateway_method( $method );
138
139
			if ( $payment_method ) {
140
				$payment_methods[] = $payment_method;
141
			}
142
		}
143
144
		return $payment_methods;
145
	}
146
147
	/**
148
	 * Get supported payment methods
149
	 *
150
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
151
	 */
152
	public function get_supported_payment_methods() {
153
		return array(
154
			PaymentMethods::ALIPAY,
155
			PaymentMethods::BANCONTACT,
156
			PaymentMethods::BANK_TRANSFER,
157
			PaymentMethods::BELFIUS,
158
			PaymentMethods::CREDIT_CARD,
159
			PaymentMethods::DIRECT_DEBIT,
160
			PaymentMethods::IDEAL,
161
			PaymentMethods::IDEALQR,
162
			PaymentMethods::GIROPAY,
163
			PaymentMethods::KBC,
164
			PaymentMethods::PAYPAL,
165
			PaymentMethods::SANTANDER,
0 ignored issues
show
Bug introduced by
The constant Pronamic\WordPress\Pay\C...ymentMethods::SANTANDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
166
			PaymentMethods::SOFORT,
167
		);
168
	}
169
170
	/**
171
	 * Start payment.
172
	 *
173
	 * @param Payment $payment Payment object.
174
	 */
175
	public function start( Payment $payment ) {
176
		$payment_method = $payment->get_method();
177
178
		$transaction_description = $payment->get_description();
179
180
		if ( empty( $transaction_description ) ) {
181
			$transaction_description = $payment->get_id();
182
		}
183
184
		// Merchant.
185
		$merchant                   = new Merchant();
186
		$merchant->account          = $this->config->account_id;
187
		$merchant->site_id          = $this->config->site_id;
188
		$merchant->site_secure_code = $this->config->site_code;
189
		$merchant->notification_url = $payment->get_return_url();
190
		$merchant->redirect_url     = $payment->get_return_url();
191
		$merchant->cancel_url       = $payment->get_return_url();
192
		$merchant->close_window     = 'false';
193
194
		// Customer.
195
		$customer               = new Customer();
196
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
197
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
198
199
		if ( null !== $payment->get_customer() ) {
200
			$name = $payment->get_customer()->get_name();
201
202
			if ( null !== $name ) {
203
				$customer->first_name = $name->get_first_name();
204
				$customer->last_name  = $name->get_last_name();
205
			}
206
207
			$customer->locale = $payment->get_customer()->get_locale();
208
			$customer->email  = $payment->get_customer()->get_email();
209
		}
210
211
		// Transaction.
212
		$transaction              = new Transaction();
213
		$transaction->id          = uniqid();
214
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
215
		$transaction->amount      = $payment->get_total_amount()->get_cents();
0 ignored issues
show
Deprecated Code introduced by
The function Pronamic\WordPress\Money\Money::get_cents() has been deprecated: 1.2.2 Use `Money::get_minor_units()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

215
		$transaction->amount      = /** @scrutinizer ignore-deprecated */ $payment->get_total_amount()->get_cents();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
216
		$transaction->description = $transaction_description;
217
218
		switch ( $payment_method ) {
219
			case PaymentMethods::IDEAL:
220
				$transaction->gateway = Methods::IDEAL;
221
222
				$issuer = $payment->get_issuer();
223
224
				if ( empty( $issuer ) ) {
225
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
226
				} else {
227
					$gateway_info = new GatewayInfo();
228
229
					$gateway_info->issuer_id = $issuer;
230
231
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
232
				}
233
234
				break;
235
			case PaymentMethods::CREDIT_CARD:
236
				$gateway = Methods::transform( $payment_method );
237
238
				$issuer = $payment->get_issuer();
239
240
				if ( empty( $issuer ) ) {
241
					if ( $gateway ) {
242
						$transaction->gateway = $gateway;
243
					}
244
				} else {
245
					$transaction->gateway = $issuer;
246
				}
247
248
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
249
250
				break;
251
			default:
252
				$gateway = Methods::transform( $payment_method );
253
254
				if ( $gateway ) {
255
					$transaction->gateway = $gateway;
256
				}
257
258
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
259
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
260
					$transaction->gateway = $payment_method;
261
				}
262
263
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
264
		}
265
266
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
267
268
		$message->signature = $signature;
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
Bug introduced by
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
269
270
		try {
271
			$response = $this->client->start_transaction( $message );
0 ignored issues
show
Bug introduced by
$message of type Pronamic\WordPress\Pay\G...ansactionRequestMessage is incompatible with the type array expected by parameter $message of Pronamic\WordPress\Pay\G...nt::start_transaction(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

271
			$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
272
		} catch ( \Exception $e ) {
273
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
274
275
			$this->set_error( $error );
276
277
			return;
278
		}
279
280
		if ( false !== $response ) {
281
			$transaction = $response->transaction;
282
283
			$payment->set_transaction_id( $transaction->id );
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
284
285
			if ( isset( $transaction->payment_url ) ) {
286
				$payment->set_action_url( $transaction->payment_url );
287
			}
288
289
			if ( isset( $response->gateway_info->redirect_url ) ) {
0 ignored issues
show
Bug introduced by
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
Bug introduced by
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
290
				$payment->set_action_url( $response->gateway_info->redirect_url );
291
			}
292
		}
293
	}
294
295
	/**
296
	 * Update status.
297
	 *
298
	 * @param Payment $payment Payment.
299
	 */
300
	public function update_status( Payment $payment ) {
301
		$merchant = new Merchant();
302
303
		$merchant->account          = $this->config->account_id;
304
		$merchant->site_id          = $this->config->site_id;
305
		$merchant->site_secure_code = $this->config->site_code;
306
307
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
308
309
		try {
310
			$result = $this->client->get_status( $message );
0 ignored issues
show
Bug introduced by
$message of type Pronamic\WordPress\Pay\G...ML\StatusRequestMessage is incompatible with the type array expected by parameter $message of Pronamic\WordPress\Pay\G...ay\Client::get_status(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

310
			$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
311
		} catch ( \Exception $e ) {
312
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
313
314
			return;
315
		}
316
317
		if ( false === $result ) {
318
			return;
319
		}
320
321
		// Status.
322
		$status = Statuses::transform( $result->ewallet->status );
0 ignored issues
show
Bug introduced by
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
323
324
		$payment->set_status( $status );
325
326
		// Consumer bank details.
327
		$consumer_bank_details = $payment->get_consumer_bank_details();
328
329
		if ( null === $consumer_bank_details ) {
330
			$consumer_bank_details = new BankAccountDetails();
331
332
			$payment->set_consumer_bank_details( $consumer_bank_details );
333
		}
334
335
		$consumer_bank_details->set_name( $result->payment_details->account_holder_name );
0 ignored issues
show
Bug introduced by
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
336
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
337
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
338
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
339
	}
340
}
341