Failed Conditions
Push — develop ( 8ef8d5...d68dcb )
by Reüel
02:56
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
45
	/**
46
	 * Filter iDEAL
47
	 *
48
	 * @param array $method Payment method.
49
	 *
50
	 * @return bool
51
	 */
52
	private function filter_ideal( $method ) {
53
		return is_array( $method ) && isset( $method['PaymentMethodCode'] ) && 'IDEAL' === $method['PaymentMethodCode'];
54
	}
55
56
	/**
57
	 * Get issuers
58
	 *
59
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
60
	 */
61
	public function get_issuers() {
62
		$groups  = array();
63
		$issuers = array();
64
65
		try {
66
			$methods = Icepay_Api_Webservice::getInstance()
67
						->paymentmethodService()
68
						->setMerchantID( $this->config->merchant_id )
69
						->setSecretCode( $this->config->secret_code )
70
						->retrieveAllPaymentmethods()
71
						->asArray();
72
		} catch ( Exception $e ) {
73
			return $groups;
74
		}
75
76
		$ideal_methods = array_filter( $methods, array( $this, 'filter_ideal' ) );
77
78
		if ( ! empty( $ideal_methods ) ) {
79
			$issuers = Icepay_Api_Webservice::getInstance()->singleMethod()
80
						->loadFromArray( $methods )
81
						->selectPaymentMethodByCode( 'IDEAL' )
82
						->getIssuers();
83
		}
84
85
		if ( $issuers ) {
86
			$options = array();
87
88
			foreach ( $issuers as $issuer ) {
89
				$options[ $issuer['IssuerKeyword'] ] = $issuer['Description'];
90
			}
91
92
			$groups[] = array(
93
				'options' => $options,
94
			);
95
		}
96
97
		return $groups;
98
	}
99
100
	/**
101
	 * Get issuers
102
	 *
103
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
104
	 */
105
	public function get_credit_card_issuers() {
106
		$groups  = array();
107
		$issuers = array();
108
109
		$method = new Icepay_Paymentmethod_Creditcard();
110
111
		if ( isset( $method->_issuer ) ) {
112
			$issuers = $method->_issuer;
113
		}
114
115
		if ( $issuers ) {
116
			$options = array();
117
118
			foreach ( $issuers as $issuer ) {
119
				switch ( $issuer ) {
120
					case 'AMEX':
121
						$name = _x( 'AMEX', 'Payment method name', 'pronamic_ideal' );
122
123
						break;
124
					case 'MASTER':
125
						$name = _x( 'MASTER', 'Payment method name', 'pronamic_ideal' );
126
127
						break;
128
					case 'VISA':
129
						$name = _x( 'VISA', 'Payment method name', 'pronamic_ideal' );
130
131
						break;
132
					default:
133
						$name = $issuer;
134
135
						break;
136
				}
137
138
				$options[ $issuer ] = $name;
139
			}
140
141
			$groups[] = array(
142
				'options' => $options,
143
			);
144
		}
145
146
		return $groups;
147
	}
148
149
	/**
150
	 * Get supported payment methods
151
	 *
152
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
153
	 */
154
	public function get_supported_payment_methods() {
155
		return array(
156
			PaymentMethods::IDEAL,
157
			PaymentMethods::CREDIT_CARD,
158
			PaymentMethods::DIRECT_DEBIT,
159
			PaymentMethods::BANCONTACT,
160
		);
161
	}
162
163
	/**
164
	 * Start an transaction
165
	 *
166
	 * @see Core_Gateway::start()
167
	 *
168
	 * @param Payment $payment Payment.
169
	 */
170
	public function start( Payment $payment ) {
171
		try {
172
			/*
173
			 * Order ID
174
			 * Your unique order number.
175
			 * This can be auto incremental number from your payments table
176
			 *
177
			 * Data type  = String
178
			 * Max length = 10
179
			 * Required   = Yes
180
			 */
181
182
			// Locale, country and language.
183
			$locale   = get_locale();
184
			$language = substr( $locale, 0, 2 );
185
186
			if ( null !== $payment->get_customer() ) {
187
				$locale = $payment->get_customer()->get_locale();
188
189
				$language = strtoupper( $payment->get_customer()->get_language() );
190
			}
191
192
			$country = strtoupper( substr( $locale, 3, 2 ) );
193
194
			// Set country from billing address.
195
			if ( null !== $payment->get_billing_address() ) {
196
				$country_code = $payment->get_billing_address()->get_country_code();
197
198
				if ( ! empty( $country_code ) ) {
199
					$country = $country_code;
200
				}
201
			}
202
203
			// Payment object.
204
			$payment_object = new Icepay_PaymentObject();
205
			$payment_object
206
				->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

206
				->setAmount( /** @scrutinizer ignore-type */ $payment->get_total_amount()->get_cents() )
Loading history...
207
				->setCountry( $country )
208
				->setLanguage( $language )
209
				->setReference( $payment->get_order_id() )
210
				->setDescription( $payment->get_description() )
211
				->setCurrency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
212
				->setIssuer( $payment->get_issuer() )
213
				->setOrderID( $payment->format_string( $this->config->order_id ) );
214
215
			/*
216
			 * Payment method
217
			 * @since 1.2.0
218
			 */
219
			$icepay_method = null;
220
221
			switch ( $payment->get_method() ) {
222
				case PaymentMethods::CREDIT_CARD:
223
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/creditcard.php
224
					$icepay_method = new Icepay_Paymentmethod_Creditcard();
225
226
					break;
227
				case PaymentMethods::DIRECT_DEBIT:
228
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ddebit.php
229
					$icepay_method = new Icepay_Paymentmethod_Ddebit();
230
231
					break;
232
				case PaymentMethods::IDEAL:
233
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/ideal.php
234
					$icepay_method = new Icepay_Paymentmethod_Ideal();
235
236
					break;
237
				case PaymentMethods::BANCONTACT:
238
				case PaymentMethods::MISTER_CASH:
239
					// @link https://github.com/icepay/icepay/blob/2.4.0/api/paymentmethods/mistercash.php
240
					$icepay_method = new Icepay_Paymentmethod_Mistercash();
241
242
					break;
243
			}
244
245
			if ( isset( $icepay_method ) ) {
246
				// @link https://github.com/icepay/icepay/blob/2.4.0/api/icepay_api_base.php#L342-L353
247
				$payment_object->setPaymentMethod( $icepay_method->getCode() );
248
			}
249
250
			// Protocol.
251
			$protocol = is_ssl() ? 'https' : 'http';
252
253
			// Basic mode.
254
			$basicmode = Icepay_Basicmode::getInstance();
255
			$basicmode
256
				->setMerchantID( $this->config->merchant_id )
257
				->setSecretCode( $this->config->secret_code )
258
				->setProtocol( $protocol )
259
				->setSuccessURL( $payment->get_return_url() )
260
				->setErrorURL( $payment->get_return_url() )
261
				->validatePayment( $payment_object );
262
263
			// Action URL.
264
			$payment->set_action_url( $basicmode->getURL() );
265
		} catch ( Exception $exception ) {
266
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
267
		}
268
	}
269
270
	/**
271
	 * Update the status of the specified payment
272
	 *
273
	 * @param Payment $payment Payment.
274
	 *
275
	 * @throws Exception
276
	 */
277
	public function update_status( Payment $payment ) {
278
		// Get the Icepay Result and set the required fields.
279
		$result = new Icepay_Result();
280
		$result
281
			->setMerchantID( $this->config->merchant_id )
282
			->setSecretCode( $this->config->secret_code );
283
284
		try {
285
			// Determine if the result can be validated.
286
			if ( $result->validate() ) {
287
				// What was the status response.
288
				switch ( $result->getStatus() ) {
289
					case Icepay_StatusCode::SUCCESS:
290
						$payment->set_status( Statuses::SUCCESS );
291
292
						break;
293
					case Icepay_StatusCode::OPEN:
294
						$payment->set_status( Statuses::OPEN );
295
296
						break;
297
					case Icepay_StatusCode::ERROR:
298
						$payment->set_status( Statuses::FAILURE );
299
300
						break;
301
				}
302
			}
303
		} catch ( Exception $exception ) {
304
			$this->error = new WP_Error( 'icepay_error', $exception->getMessage(), $exception );
305
		}
306
	}
307
}
308