Test Failed
Push — develop ( 2a0ef1...79c1e2 )
by Remco
03:39
created

src/Gateway.php (1 issue)

Labels
Severity
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_Ideal;
11
use Icepay_Paymentmethod_Mistercash;
12
use Icepay_PaymentObject;
13
use Icepay_Result;
14
use Icepay_StatusCode;
15
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
16
use Pronamic\WordPress\Pay\Core\PaymentMethods;
17
use Pronamic\WordPress\Pay\Core\Statuses;
18
use Pronamic\WordPress\Pay\Payments\Payment;
19
use WP_Error;
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
		$this->set_slug( 'icepay' );
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
		);
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
			}
247
248
			if ( isset( $icepay_method ) ) {
249
				// @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353
250
				$payment_object->setPaymentMethod( $icepay_method->getCode() );
251
252
				// Force language 'NL' for unsupported languages (i.e. 'EN' for iDEAL).
253
				if ( ! in_array( $language, $icepay_method->getSupportedLanguages(), true ) ) {
254
					$payment_object->setLanguage( 'NL' );
255
				}
256
			}
257
258
			// Protocol.
259
			$protocol = is_ssl() ? 'https' : 'http';
260
261
			// Basic mode.
262
			$basicmode = Icepay_Basicmode::getInstance();
263
			$basicmode
264
				->setMerchantID( $this->config->merchant_id )
265
				->setSecretCode( $this->config->secret_code )
266
				->setProtocol( $protocol )
267
				->setSuccessURL( $payment->get_return_url() )
268
				->setErrorURL( $payment->get_return_url() )
269
				->validatePayment( $payment_object );
270
271
			// Action URL.
272
			$payment->set_action_url( $basicmode->getURL() );
273
		} catch ( Exception $exception ) {
274
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
275
		}
276
	}
277
278
	/**
279
	 * Update the status of the specified payment
280
	 *
281
	 * @param Payment $payment Payment.
282
	 *
283
	 * @throws Exception
284
	 */
285
	public function update_status( Payment $payment ) {
286
		// Get the Icepay Result and set the required fields.
287
		$result = new Icepay_Result();
288
		$result
289
			->setMerchantID( $this->config->merchant_id )
290
			->setSecretCode( $this->config->secret_code );
291
292
		try {
293
			// Determine if the result can be validated.
294
			if ( $result->validate() ) {
295
				// What was the status response.
296
				switch ( $result->getStatus() ) {
297
					case Icepay_StatusCode::SUCCESS:
298
						$payment->set_status( Statuses::SUCCESS );
299
300
						break;
301
					case Icepay_StatusCode::OPEN:
302
						$payment->set_status( Statuses::OPEN );
303
304
						break;
305
					case Icepay_StatusCode::ERROR:
306
						$payment->set_status( Statuses::FAILURE );
307
308
						break;
309
				}
310
			}
311
		} catch ( Exception $exception ) {
312
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
313
		}
314
	}
315
}
316