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

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