Failed Conditions
Push — develop ( 468d8c...84789b )
by Reüel
07:53
created

Gateway::get_credit_card_issuers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
cc 1
nc 1
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::SOFORT,
166
		);
167
	}
168
169
	/**
170
	 * Start payment.
171
	 *
172
	 * @param Payment $payment Payment object.
173
	 */
174
	public function start( Payment $payment ) {
175
		$payment_method = $payment->get_method();
176
177
		$transaction_description = $payment->get_description();
178
179
		if ( empty( $transaction_description ) ) {
180
			$transaction_description = $payment->get_id();
181
		}
182
183
		// Merchant.
184
		$merchant                   = new Merchant();
185
		$merchant->account          = $this->config->account_id;
186
		$merchant->site_id          = $this->config->site_id;
187
		$merchant->site_secure_code = $this->config->site_code;
188
		$merchant->notification_url = $payment->get_return_url();
189
		$merchant->redirect_url     = $payment->get_return_url();
190
		$merchant->cancel_url       = $payment->get_return_url();
191
		$merchant->close_window     = 'false';
192
193
		// Customer.
194
		$customer               = new Customer();
195
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
196
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
197
198
		if ( null !== $payment->get_customer() ) {
199
			$name = $payment->get_customer()->get_name();
200
201
			if ( null !== $name ) {
202
				$customer->first_name = $name->get_first_name();
203
				$customer->last_name  = $name->get_last_name();
204
			}
205
206
			$customer->locale = $payment->get_customer()->get_locale();
207
			$customer->email  = $payment->get_customer()->get_email();
208
		}
209
210
		// Transaction.
211
		$transaction              = new Transaction();
212
		$transaction->id          = uniqid();
213
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
214
		$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

214
		$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...
215
		$transaction->description = $transaction_description;
216
217
		switch ( $payment_method ) {
218
			case PaymentMethods::IDEAL:
219
				$transaction->gateway = Methods::IDEAL;
220
221
				$issuer = $payment->get_issuer();
222
223
				if ( empty( $issuer ) ) {
224
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
225
				} else {
226
					$gateway_info = new GatewayInfo();
227
228
					$gateway_info->issuer_id = $issuer;
229
230
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
231
				}
232
233
				break;
234
			case PaymentMethods::CREDIT_CARD:
235
				$gateway = Methods::transform( $payment_method );
236
237
				$issuer = $payment->get_issuer();
238
239
				if ( empty( $issuer ) ) {
240
					if ( $gateway ) {
241
						$transaction->gateway = $gateway;
242
					}
243
				} else {
244
					$transaction->gateway = $issuer;
245
				}
246
247
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
248
249
				break;
250
			default:
251
				$gateway = Methods::transform( $payment_method );
252
253
				if ( $gateway ) {
254
					$transaction->gateway = $gateway;
255
				}
256
257
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
258
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
259
					$transaction->gateway = $payment_method;
260
				}
261
262
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
263
		}
264
265
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
266
267
		$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...
268
269
		try {
270
			$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

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

309
			$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
310
		} catch ( \Exception $e ) {
311
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
312
313
			return;
314
		}
315
316
		if ( false === $result ) {
317
			return;
318
		}
319
320
		// Status.
321
		$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...
322
323
		$payment->set_status( $status );
324
325
		// Consumer bank details.
326
		$consumer_bank_details = $payment->get_consumer_bank_details();
327
328
		if ( null === $consumer_bank_details ) {
329
			$consumer_bank_details = new BankAccountDetails();
330
331
			$payment->set_consumer_bank_details( $consumer_bank_details );
332
		}
333
334
		$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...
335
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
336
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
337
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
338
	}
339
}
340