Test Failed
Push — develop ( fa274f...d6a372 )
by Reüel
12:06
created

src/Gateway.php (3 issues)

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;
0 ignored issues
show
The type Pronamic\WordPress\Pay\Payments\PaymentStatus was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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

209
				->setAmount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_cents() )
Loading history...
210
				->setCountry( $country )
211
				->setLanguage( $language )
212
				->setReference( $payment->get_order_id() )
213
				->setDescription( $payment->get_description() )
214
				->setCurrency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
215
				->setIssuer( $payment->get_issuer() )
216
				->setOrderID( $payment->format_string( $this->config->order_id ) );
217
218
			/*
219
			 * Payment method
220
			 * @since 1.2.0
221
			 */
222
			$icepay_method = null;
223
224
			switch ( $payment->get_method() ) {
225
				case PaymentMethods::CREDIT_CARD:
226
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/creditcard.php
227
					$icepay_method = new Icepay_Paymentmethod_Creditcard();
228
229
					break;
230
				case PaymentMethods::DIRECT_DEBIT:
231
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ddebit.php
232
					$icepay_method = new Icepay_Paymentmethod_Ddebit();
233
234
					break;
235
				case PaymentMethods::IDEAL:
236
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ideal.php
237
					$icepay_method = new Icepay_Paymentmethod_Ideal();
238
239
					break;
240
				case PaymentMethods::BANCONTACT:
241
				case PaymentMethods::MISTER_CASH:
242
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/mistercash.php
243
					$icepay_method = new Icepay_Paymentmethod_Mistercash();
244
245
					break;
246
				case PaymentMethods::SOFORT:
247
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/directebank.php
248
					$icepay_method = new Icepay_Paymentmethod_Directebank();
249
250
					// Set issuer.
251
					$issuer = 'DIGITAL';
252
253
					$lines = $payment->get_lines();
254
255
					if ( null !== $lines ) {
256
						foreach ( $lines as $line ) {
257
							$issuer = DirectebankIssuers::transform( $line->get_type() );
258
259
							break;
260
						}
261
					}
262
263
					$payment_object->setIssuer( $issuer );
264
			}
265
266
			if ( isset( $icepay_method ) ) {
267
				// @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353
268
				$payment_object->setPaymentMethod( $icepay_method->getCode() );
269
270
				// Force language 'NL' for unsupported languages (i.e. 'EN' for iDEAL).
271
				if ( ! in_array( $language, $icepay_method->getSupportedLanguages(), true ) ) {
272
					$payment_object->setLanguage( 'NL' );
273
				}
274
			}
275
276
			// Protocol.
277
			$protocol = is_ssl() ? 'https' : 'http';
278
279
			// Basic mode.
280
			$basicmode = Icepay_Basicmode::getInstance();
281
			$basicmode
282
				->setMerchantID( $this->config->merchant_id )
283
				->setSecretCode( $this->config->secret_code )
284
				->setProtocol( $protocol )
285
				->setSuccessURL( $payment->get_return_url() )
286
				->setErrorURL( $payment->get_return_url() )
287
				->validatePayment( $payment_object );
288
289
			// Action URL.
290
			$payment->set_action_url( $basicmode->getURL() );
291
		} catch ( Exception $e ) {
292
			throw new \Pronamic\WordPress\Pay\GatewayException( 'icepay', $e->getMessage(), $e );
0 ignored issues
show
The type Pronamic\WordPress\Pay\GatewayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
293
		}
294
	}
295
296
	/**
297
	 * Update the status of the specified payment
298
	 *
299
	 * @param Payment $payment Payment.
300
	 *
301
	 * @throws Exception
302
	 */
303
	public function update_status( Payment $payment ) {
304
		// Get the Icepay Result and set the required fields.
305
		$result = new Icepay_Result();
306
		$result
307
			->setMerchantID( $this->config->merchant_id )
308
			->setSecretCode( $this->config->secret_code );
309
310
		try {
311
			// Determine if the result can be validated.
312
			if ( $result->validate() ) {
313
				// What was the status response.
314
				switch ( $result->getStatus() ) {
315
					case Icepay_StatusCode::SUCCESS:
316
						$payment->set_status( PaymentStatus::SUCCESS );
317
318
						break;
319
					case Icepay_StatusCode::OPEN:
320
						$payment->set_status( PaymentStatus::OPEN );
321
322
						break;
323
					case Icepay_StatusCode::ERROR:
324
						$payment->set_status( PaymentStatus::FAILURE );
325
326
						break;
327
				}
328
			}
329
		} catch ( Exception $e ) {
330
			throw new \Pronamic\WordPress\Pay\GatewayException( 'icepay', $e->getMessage(), $e );
331
		}
332
	}
333
}
334