Failed Conditions
Push — develop ( 9705a9...c29d28 )
by Reüel
03:41
created

Gateway::start()   F

Complexity

Conditions 16
Paths 324

Size

Total Lines 116
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 70
c 2
b 0
f 0
dl 0
loc 116
ccs 0
cts 65
cp 0
rs 3.1833
cc 16
nc 324
nop 1
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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

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

308
			$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
309
		} catch ( \Exception $e ) {
310
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
311
312
			return;
313
		}
314
315
		if ( false !== $result ) {
316
			$status = Statuses::transform( $result->ewallet->status );
0 ignored issues
show
Bug introduced by Remco Tolsma
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by Remco Tolsma
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
317
318
			$payment->set_status( $status );
319
			$payment->set_consumer_name( $result->payment_details->account_holder_name );
0 ignored issues
show
Deprecated Code introduced by Remco Tolsma
The function Pronamic\WordPress\Pay\P...fo::set_consumer_name() has been deprecated: 2.2.6 Use Payment::set_consumer_bank_details()->set_name() instead. ( Ignorable by Annotation )

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

319
			/** @scrutinizer ignore-deprecated */ $payment->set_consumer_name( $result->payment_details->account_holder_name );

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...
Bug introduced by Remco Tolsma
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by Remco Tolsma
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
320
			$payment->set_consumer_iban( $result->payment_details->account_iban );
0 ignored issues
show
Deprecated Code introduced by Remco Tolsma
The function Pronamic\WordPress\Pay\P...fo::set_consumer_iban() has been deprecated: 2.2.6 Use Payment::set_consumer_bank_details()->set_iban() instead. ( Ignorable by Annotation )

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

320
			/** @scrutinizer ignore-deprecated */ $payment->set_consumer_iban( $result->payment_details->account_iban );

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...
321
			$payment->set_consumer_bic( $result->payment_details->account_bic );
0 ignored issues
show
Deprecated Code introduced by Remco Tolsma
The function Pronamic\WordPress\Pay\P...nfo::set_consumer_bic() has been deprecated: 2.2.6 Use Payment::set_consumer_bank_details()->set_bic() instead. ( Ignorable by Annotation )

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

321
			/** @scrutinizer ignore-deprecated */ $payment->set_consumer_bic( $result->payment_details->account_bic );

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...
322
			$payment->set_consumer_account_number( $result->payment_details->account_id );
0 ignored issues
show
Deprecated Code introduced by Remco Tolsma
The function Pronamic\WordPress\Pay\P...nsumer_account_number() has been deprecated: 2.2.6 Use Payment::set_consumer_bank_details()->set_account_number() instead. ( Ignorable by Annotation )

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

322
			/** @scrutinizer ignore-deprecated */ $payment->set_consumer_account_number( $result->payment_details->account_id );

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...
323
		}
324
	}
325
}
326