Test Failed
Push — master ( 16d556...ba5f50 )
by Remco
22:26 queued 14:39
created

Gateway::update_status()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 4
b 0
f 0
nc 10
nop 1
dl 0
loc 28
ccs 0
cts 3
cp 0
crap 42
rs 9.0444
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Icepay;
4
5
use Exception;
6
use Icepay_Api_Webservice;
7
use Icepay_Basicmode;
8
use Icepay_Paymentmethod_Creditcard;
9
use Icepay_Paymentmethod_Ddebit;
10
use Icepay_Paymentmethod_Directebank;
11
use Icepay_Paymentmethod_Ideal;
12
use Icepay_Paymentmethod_Mistercash;
13
use Icepay_PaymentObject;
14
use Icepay_Result;
15
use Icepay_StatusCode;
16
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
17
use Pronamic\WordPress\Pay\Core\PaymentMethods;
18
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
use WP_Error;
21
22
/**
23
 * Title: ICEPAY gateway
24
 * Description:
25
 * Copyright: 2005-2019 Pronamic
26
 * Company: Pronamic
27
 *
28
 * @author Remco Tolsma
29
 * @version 2.0.5
30
 * @since 1.0.0
31
 */
32
class Gateway extends Core_Gateway {
33
	/**
34
	 * Constructs and intializes an ICEPAY gateway
35
	 *
36
	 * @param Config $config Config.
37
	 */
38
	public function __construct( Config $config ) {
39
		parent::__construct( $config );
40
41
		// Default properties for this gateway.
42
		$this->set_method( self::METHOD_HTTP_REDIRECT );
43
44
		// Supported features.
45
		$this->supports = array();
46
	}
47
48
	/**
49
	 * Filter iDEAL
50
	 *
51
	 * @param array $method Payment method.
52
	 *
53
	 * @return bool
54
	 */
55
	private function filter_ideal( $method ) {
56
		return is_array( $method ) && isset( $method['PaymentMethodCode'] ) && 'IDEAL' === $method['PaymentMethodCode'];
57
	}
58
59
	/**
60
	 * Get issuers
61
	 *
62
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
63
	 */
64
	public function get_issuers() {
65
		$groups  = array();
66
		$issuers = array();
67
68
		try {
69
			$methods = Icepay_Api_Webservice::getInstance()
70
						->paymentmethodService()
71
						->setMerchantID( $this->config->merchant_id )
72
						->setSecretCode( $this->config->secret_code )
73
						->retrieveAllPaymentmethods()
74
						->asArray();
75
		} catch ( Exception $e ) {
76
			return $groups;
77
		}
78
79
		$ideal_methods = array_filter( $methods, array( $this, 'filter_ideal' ) );
80
81
		if ( ! empty( $ideal_methods ) ) {
82
			$issuers = Icepay_Api_Webservice::getInstance()->singleMethod()
83
						->loadFromArray( $methods )
84
						->selectPaymentMethodByCode( 'IDEAL' )
85
						->getIssuers();
86
		}
87
88
		if ( $issuers ) {
89
			$options = array();
90
91
			foreach ( $issuers as $issuer ) {
92
				$options[ $issuer['IssuerKeyword'] ] = $issuer['Description'];
93
			}
94
95
			$groups[] = array(
96
				'options' => $options,
97
			);
98
		}
99
100
		return $groups;
101
	}
102
103
	/**
104
	 * Get issuers
105
	 *
106
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
107
	 */
108
	public function get_credit_card_issuers() {
109
		$groups  = array();
110
		$issuers = array();
111
112
		$method = new Icepay_Paymentmethod_Creditcard();
113
114
		if ( isset( $method->_issuer ) ) {
115
			$issuers = $method->_issuer;
116
		}
117
118
		if ( $issuers ) {
119
			$options = array();
120
121
			foreach ( $issuers as $issuer ) {
122
				switch ( $issuer ) {
123
					case 'AMEX':
124
						$name = _x( 'AMEX', 'Payment method name', 'pronamic_ideal' );
125
126
						break;
127
					case 'MASTER':
128
						$name = _x( 'MASTER', 'Payment method name', 'pronamic_ideal' );
129
130
						break;
131
					case 'VISA':
132
						$name = _x( 'VISA', 'Payment method name', 'pronamic_ideal' );
133
134
						break;
135
					default:
136
						$name = $issuer;
137
138
						break;
139
				}
140
141
				$options[ $issuer ] = $name;
142
			}
143
144
			$groups[] = array(
145
				'options' => $options,
146
			);
147
		}
148
149
		return $groups;
150
	}
151
152
	/**
153
	 * Get supported payment methods
154
	 *
155
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
156
	 */
157
	public function get_supported_payment_methods() {
158
		return array(
159
			PaymentMethods::IDEAL,
160
			PaymentMethods::CREDIT_CARD,
161
			PaymentMethods::DIRECT_DEBIT,
162
			PaymentMethods::BANCONTACT,
163
			PaymentMethods::SOFORT,
164
		);
165
	}
166
167
	/**
168
	 * Start an transaction
169
	 *
170
	 * @see Core_Gateway::start()
171
	 *
172
	 * @param Payment $payment Payment.
173
	 */
174
	public function start( Payment $payment ) {
175
		try {
176
			/*
177
			 * Order ID
178
			 * Your unique order number.
179
			 * This can be auto incremental number from your payments table
180
			 *
181
			 * Data type  = String
182
			 * Max length = 10
183
			 * Required   = Yes
184
			 */
185
186
			// Locale, country and language.
187
			$locale   = get_locale();
188
			$language = substr( $locale, 0, 2 );
189
190
			if ( null !== $payment->get_customer() ) {
191
				$locale = $payment->get_customer()->get_locale();
192
193
				$language = strtoupper( $payment->get_customer()->get_language() );
194
			}
195
196
			$country = strtoupper( substr( $locale, 3, 2 ) );
197
198
			// Set country from billing address.
199
			if ( null !== $payment->get_billing_address() ) {
200
				$country_code = $payment->get_billing_address()->get_country_code();
201
202
				if ( ! empty( $country_code ) ) {
203
					$country = $country_code;
204
				}
205
			}
206
207
			// Payment object.
208
			$payment_object = new Icepay_PaymentObject();
209
			$payment_object
210
				->setAmount( $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

210
				->setAmount( /** @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...
Bug introduced by
$payment->get_total_amount()->get_cents() of type double is incompatible with the type integer expected by parameter $amount of Icepay_PaymentObject::setAmount(). ( Ignorable by Annotation )

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

210
				->setAmount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_cents() )
Loading history...
211
				->setCountry( $country )
212
				->setLanguage( $language )
213
				->setReference( $payment->get_order_id() )
214
				->setDescription( $payment->get_description() )
215
				->setCurrency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
216
				->setIssuer( $payment->get_issuer() )
217
				->setOrderID( $payment->format_string( $this->config->order_id ) );
218
219
			/*
220
			 * Payment method
221
			 * @since 1.2.0
222
			 */
223
			$icepay_method = null;
224
225
			switch ( $payment->get_method() ) {
226
				case PaymentMethods::CREDIT_CARD:
227
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/creditcard.php
228
					$icepay_method = new Icepay_Paymentmethod_Creditcard();
229
230
					break;
231
				case PaymentMethods::DIRECT_DEBIT:
232
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ddebit.php
233
					$icepay_method = new Icepay_Paymentmethod_Ddebit();
234
235
					break;
236
				case PaymentMethods::IDEAL:
237
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ideal.php
238
					$icepay_method = new Icepay_Paymentmethod_Ideal();
239
240
					break;
241
				case PaymentMethods::BANCONTACT:
242
				case PaymentMethods::MISTER_CASH:
0 ignored issues
show
Deprecated Code introduced by
The constant Pronamic\WordPress\Pay\C...entMethods::MISTER_CASH has been deprecated: "Bancontact/Mister Cash" was renamed to just "Bancontact". ( Ignorable by Annotation )

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

242
				case /** @scrutinizer ignore-deprecated */ PaymentMethods::MISTER_CASH:

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

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

Loading history...
243
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/mistercash.php
244
					$icepay_method = new Icepay_Paymentmethod_Mistercash();
245
246
					break;
247
				case PaymentMethods::SOFORT:
248
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/directebank.php
249
					$icepay_method = new Icepay_Paymentmethod_Directebank();
250
251
					// Set issuer.
252
					$issuer = 'DIGITAL';
253
254
					$lines = $payment->get_lines();
255
256
					if ( null !== $lines ) {
257
						foreach ( $lines as $line ) {
258
							$issuer = DirectebankIssuers::transform( $line->get_type() );
259
260
							break;
261
						}
262
					}
263
264
					$payment_object->setIssuer( $issuer );
265
			}
266
267
			if ( isset( $icepay_method ) ) {
268
				// @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353
269
				$payment_object->setPaymentMethod( $icepay_method->getCode() );
270
271
				// Force language 'NL' for unsupported languages (i.e. 'EN' for iDEAL).
272
				if ( ! in_array( $language, $icepay_method->getSupportedLanguages(), true ) ) {
273
					$payment_object->setLanguage( 'NL' );
274
				}
275
			}
276
277
			// Protocol.
278
			$protocol = is_ssl() ? 'https' : 'http';
279
280
			// Basic mode.
281
			$basicmode = Icepay_Basicmode::getInstance();
282
			$basicmode
283
				->setMerchantID( $this->config->merchant_id )
284
				->setSecretCode( $this->config->secret_code )
285
				->setProtocol( $protocol )
286
				->setSuccessURL( $payment->get_return_url() )
287
				->setErrorURL( $payment->get_return_url() )
288
				->validatePayment( $payment_object );
289
290
			// Action URL.
291
			$payment->set_action_url( $basicmode->getURL() );
292
		} catch ( Exception $exception ) {
293
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
294
		}
295
	}
296
297
	/**
298
	 * Update the status of the specified payment
299
	 *
300
	 * @param Payment $payment Payment.
301
	 *
302
	 * @throws Exception
303
	 */
304
	public function update_status( Payment $payment ) {
305
		// Get the Icepay Result and set the required fields.
306
		$result = new Icepay_Result();
307
		$result
308
			->setMerchantID( $this->config->merchant_id )
309
			->setSecretCode( $this->config->secret_code );
310
311
		try {
312
			// Determine if the result can be validated.
313
			if ( $result->validate() ) {
314
				// What was the status response.
315
				switch ( $result->getStatus() ) {
316
					case Icepay_StatusCode::SUCCESS:
317
						$payment->set_status( PaymentStatus::SUCCESS );
318
319
						break;
320
					case Icepay_StatusCode::OPEN:
321
						$payment->set_status( PaymentStatus::OPEN );
322
323
						break;
324
					case Icepay_StatusCode::ERROR:
325
						$payment->set_status( PaymentStatus::FAILURE );
326
327
						break;
328
				}
329
			}
330
		} catch ( Exception $exception ) {
331
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
332
		}
333
	}
334
}
335