Passed
Push — feature/webhook-status ( f80546 )
by Reüel
07:12
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Sisow;
12
13
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses;
16
use Pronamic\WordPress\Pay\Payments\Payment;
17
use Pronamic\WordPress\Pay\Payments\PaymentLineType;
18
19
/**
20
 * Title: Sisow gateway
21
 * Description:
22
 * Copyright: 2005-2019 Pronamic
23
 * Company: Pronamic
24
 *
25
 * @author  Remco Tolsma
26
 * @version 2.0.0
27
 * @since   1.0.0
28
 */
29
class Gateway extends Core_Gateway {
30
	/**
31
	 * Client.
32
	 *
33
	 * @var Client
34
	 */
35
	protected $client;
36
37
	/**
38
	 * Constructs and initialize an Sisow gateway
39
	 *
40
	 * @param Config $config Config.
41
	 */
42
	public function __construct( Config $config ) {
43
		parent::__construct( $config );
44
45
		$this->set_method( self::METHOD_HTTP_REDIRECT );
46
47
		// Supported features.
48
		$this->supports = self::get_supported_features();
49
50
		// Client.
51
		$this->client = new Client( $config->merchant_id, $config->merchant_key );
52
		$this->client->set_test_mode( self::MODE_TEST === $config->mode );
53
	}
54
55
	/**
56
	 * Get supported features.
57
	 *
58
	 * @return array
59
	 */
60
	public static function get_supported_features() {
61
		return array(
62
			'payment_status_request',
63
			'reservation_payments',
64
		);
65
	}
66
67
	/**
68
	 * Get issuers
69
	 *
70
	 * @see Core_Gateway::get_issuers()
71
	 */
72
	public function get_issuers() {
73
		$groups = array();
74
75
		$result = $this->client->get_directory();
76
77
		if ( $result ) {
78
			$groups[] = array(
79
				'options' => $result,
80
			);
81
		} else {
82
			$this->error = $this->client->get_error();
83
		}
84
85
		return $groups;
86
	}
87
88
	/**
89
	 * Get available payment methods.
90
	 *
91
	 * @see Core_Gateway::get_available_payment_methods()
92
	 */
93
	public function get_available_payment_methods() {
94
		if ( self::MODE_TEST === $this->config->mode ) {
95
			return $this->get_supported_payment_methods();
96
		}
97
98
		$payment_methods = array();
99
100
		// Merchant request.
101
		$request = new MerchantRequest( $this->config->merchant_id );
102
103
		// Get merchant.
104
		$result = $this->client->get_merchant( $request );
105
106
		// Handle errors.
107
		if ( false === $result ) {
108
			$this->error = $this->client->get_error();
109
110
			return $payment_methods;
111
		}
112
113
		foreach ( $result->payments as $method ) {
114
			// Transform to WordPress payment methods.
115
			$payment_method = Methods::transform_gateway_method( $method );
116
117
			if ( $payment_method ) {
118
				$payment_methods[] = $payment_method;
119
			}
120
		}
121
122
		return $payment_methods;
123
	}
124
125
	/**
126
	 * Get supported payment methods
127
	 *
128
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
129
	 */
130
	public function get_supported_payment_methods() {
131
		return array(
132
			PaymentMethods::AFTERPAY,
133
			PaymentMethods::BANK_TRANSFER,
134
			PaymentMethods::BANCONTACT,
135
			PaymentMethods::BELFIUS,
136
			PaymentMethods::BILLINK,
137
			PaymentMethods::BUNQ,
138
			PaymentMethods::CAPAYABLE,
139
			PaymentMethods::IN3,
140
			PaymentMethods::CREDIT_CARD,
141
			PaymentMethods::FOCUM,
142
			PaymentMethods::GIROPAY,
143
			PaymentMethods::IDEAL,
144
			PaymentMethods::IDEALQR,
145
			PaymentMethods::KLARNA_PAY_LATER,
146
			PaymentMethods::PAYPAL,
147
			PaymentMethods::SOFORT,
148
		);
149
	}
150
151
	/**
152
	 * Is payment method required to start transaction?
153
	 *
154
	 * @see Core_Gateway::payment_method_is_required()
155
	 */
156
	public function payment_method_is_required() {
157
		return true;
158
	}
159
160
	/**
161
	 * Start
162
	 *
163
	 * @see Core_Gateway::start()
164
	 *
165
	 * @param Payment $payment Payment.
166
	 */
167
	public function start( Payment $payment ) {
168
		// Order and purchase ID.
169
		$order_id    = $payment->get_order_id();
170
		$purchase_id = strval( empty( $order_id ) ? $payment->get_id() : $order_id );
171
172
		// Maximum length for purchase ID is 16 characters, otherwise an error will occur:
173
		// ideal_sisow_error - purchaseid too long (16).
174
		$purchase_id = substr( $purchase_id, 0, 16 );
175
176
		// New transaction request.
177
		$request = new TransactionRequest(
178
			$this->config->merchant_id,
179
			$this->config->shop_id
180
		);
181
182
		$request->merge_parameters(
183
			array(
184
				'payment'      => Methods::transform( $payment->get_method(), $payment->get_method() ),
185
				'purchaseid'   => substr( $purchase_id, 0, 16 ),
186
				'entrancecode' => $payment->get_entrance_code(),
187
				'amount'       => $payment->get_total_amount()->get_cents(),
188
				'description'  => substr( $payment->get_description(), 0, 32 ),
189
				'testmode'     => ( self::MODE_TEST === $this->config->mode ) ? 'true' : 'false',
190
				'returnurl'    => $payment->get_return_url(),
191
				'cancelurl'    => $payment->get_return_url(),
192
				'notifyurl'    => $payment->get_return_url(),
193
				'callbackurl'  => $payment->get_return_url(),
194
				// Other parameters.
195
				'issuerid'     => $payment->get_issuer(),
196
				'billing_mail' => $payment->get_email(),
197
			)
198
		);
199
200
		// Payment method.
201
		$this->set_payment_method( null === $payment->get_method() ? PaymentMethods::IDEAL : $payment->get_method() );
202
203
		// Additional parameters for payment method.
204
		if ( PaymentMethods::IDEALQR === $payment->get_method() ) {
205
			$request->set_parameter( 'qrcode', 'true' );
206
		}
207
208
		// Customer.
209
		if ( null !== $payment->get_customer() ) {
210
			$customer = $payment->get_customer();
211
212
			$request->merge_parameters(
213
				array(
214
					'ipaddress' => $customer->get_ip_address(),
215
					'gender'    => $customer->get_gender(),
216
				)
217
			);
218
219
			if ( null !== $customer->get_locale() ) {
220
				/*
221
				 * @link https://github.com/wp-pay-gateways/sisow/tree/feature/post-pay/documentation#parameter-locale
222
				 */
223
				$sisow_locale = strtoupper( substr( $customer->get_locale(), -2 ) );
224
225
				$request->set_parameter( 'locale', $sisow_locale );
226
			}
227
228
			if ( null !== $customer->get_birth_date() ) {
229
				$request->set_parameter( 'birthdate', $customer->get_birth_date()->format( 'dmY' ) );
230
			}
231
		}
232
233
		// Billing address.
234
		if ( null !== $payment->get_billing_address() ) {
235
			$address = $payment->get_billing_address();
236
237
			if ( null !== $address->get_name() ) {
238
				$name = $address->get_name();
239
240
				$request->merge_parameters(
241
					array(
242
						'billing_firstname' => $name->get_first_name(),
243
						'billing_lastname'  => $name->get_last_name(),
244
					)
245
				);
246
247
				// Remove accents from first name for AfterPay.
248
				if ( PaymentMethods::AFTERPAY === $payment->get_method() ) {
249
					$request->set_parameter( 'billing_firstname', remove_accents( $name->get_first_name() ) );
250
				}
251
			}
252
253
			$request->merge_parameters(
254
				array(
255
					'billing_mail'        => $address->get_email(),
256
					'billing_company'     => $address->get_company_name(),
257
					'billing_coc'         => $address->get_coc_number(),
258
					'billing_address1'    => $address->get_line_1(),
259
					'billing_address2'    => $address->get_line_2(),
260
					'billing_zip'         => $address->get_postal_code(),
261
					'billing_city'        => $address->get_city(),
262
					'billing_country'     => $address->get_country_name(),
263
					'billing_countrycode' => $address->get_country_code(),
264
					'billing_phone'       => $address->get_phone(),
265
				)
266
			);
267
		}
268
269
		// Shipping address.
270
		if ( null !== $payment->get_shipping_address() ) {
271
			$address = $payment->get_shipping_address();
272
273
			if ( null !== $address->get_name() ) {
274
				$name = $address->get_name();
275
276
				$request->merge_parameters(
277
					array(
278
						'shipping_firstname' => $name->get_first_name(),
279
						'shipping_lastname'  => $name->get_last_name(),
280
					)
281
				);
282
			}
283
284
			$request->merge_parameters(
285
				array(
286
					'shipping_mail'        => $address->get_email(),
287
					'shipping_company'     => $address->get_company_name(),
288
					'shipping_address1'    => $address->get_line_1(),
289
					'shipping_address2'    => $address->get_line_2(),
290
					'shipping_zip'         => $address->get_postal_code(),
291
					'shipping_city'        => $address->get_city(),
292
					'shipping_country'     => $address->get_country_name(),
293
					'shipping_countrycode' => $address->get_country_code(),
294
					'shipping_phone'       => $address->get_phone(),
295
				)
296
			);
297
		}
298
299
		// Lines.
300
		if ( null !== $payment->get_lines() ) {
301
			$lines = $payment->get_lines();
302
303
			$x = 1;
304
305
			foreach ( $lines as $line ) {
306
				// Product ID.
307
				$product_id = $line->get_id();
308
309
				switch ( $line->get_type() ) {
310
					case PaymentLineType::SHIPPING:
311
						$product_id = 'shipping';
312
313
						break;
314
					case PaymentLineType::FEE:
315
						$product_id = 'paymentfee';
316
317
						break;
318
				}
319
320
				// Price.
321
				$unit_price = null;
322
323
				if ( null !== $line->get_unit_price() ) {
324
					$unit_price = $line->get_unit_price()->get_excluding_tax()->get_cents();
325
				}
326
327
				// Request parameters.
328
				$request->merge_parameters(
329
					array(
330
						'product_id_' . $x          => $product_id,
331
						'product_description_' . $x => $line->get_name(),
332
						'product_quantity_' . $x    => $line->get_quantity(),
333
						'product_netprice_' . $x    => $unit_price,
334
						'product_total_' . $x       => $line->get_total_amount()->get_including_tax()->get_cents(),
335
						'product_nettotal_' . $x    => $line->get_total_amount()->get_excluding_tax()->get_cents(),
336
						'product_tax_' . $x         => $line->get_tax_amount()->get_cents(),
337
						'product_taxrate_' . $x     => $line->get_total_amount()->get_tax_percentage() * 100,
338
					)
339
				);
340
341
				$x++;
342
			}
343
		}
344
345
		// Create transaction.
346
		$result = $this->client->create_transaction( $request );
347
348
		if ( false !== $result ) {
349
			$payment->set_transaction_id( $result->id );
350
			$payment->set_action_url( $result->issuer_url );
351
		} else {
352
			$this->error = $this->client->get_error();
353
354
			return false;
355
		}
356
	}
357
358
	/**
359
	 * Update status of the specified payment
360
	 *
361
	 * @param Payment $payment Payment.
362
	 */
363
	public function update_status( Payment $payment ) {
364
		$request = new StatusRequest(
365
			$payment->get_transaction_id(),
366
			$this->config->merchant_id,
367
			$this->config->shop_id
368
		);
369
370
		$result = $this->client->get_status( $request );
371
372
		if ( false === $result ) {
373
			$this->error = $this->client->get_error();
374
375
			return;
376
		}
377
378
		$transaction = $result;
379
380
		$payment->set_status( Statuses::transform( $transaction->status ) );
381
		$payment->set_consumer_name( $transaction->consumer_name );
382
		$payment->set_consumer_account_number( $transaction->consumer_account );
383
		$payment->set_consumer_city( $transaction->consumer_city );
384
	}
385
386
	/**
387
	 * Create invoice.
388
	 *
389
	 * @param Payment $payment Payment.
390
	 *
391
	 * @return bool|Invoice
392
	 */
393
	public function create_invoice( $payment ) {
394
		$transaction_id = $payment->get_transaction_id();
395
396
		if ( empty( $transaction_id ) ) {
397
			return false;
398
		}
399
400
		// Invoice request.
401
		$request = new InvoiceRequest(
402
			$this->config->merchant_id,
403
			$this->config->shop_id
404
		);
405
406
		$request->set_parameter( 'trxid', $transaction_id );
407
408
		// Create invoice.
409
		$result = $this->client->create_invoice( $request );
410
411
		// Handle errors.
412
		if ( false === $result ) {
413
			$this->error = $this->client->get_error();
414
415
			return false;
416
		}
417
418
		$payment->set_status( Core_Statuses::SUCCESS );
419
420
		$payment->save();
421
422
		return $result;
423
	}
424
425
	/**
426
	 * Cancel reservation.
427
	 *
428
	 * @param Payment $payment Payment.
429
	 *
430
	 * @return bool|Reservation
431
	 */
432
	public function cancel_reservation( $payment ) {
433
		$transaction_id = $payment->get_transaction_id();
434
435
		if ( empty( $transaction_id ) ) {
436
			return false;
437
		}
438
439
		// Cancel reservation request.
440
		$request = new CancelReservationRequest(
441
			$this->config->merchant_id,
442
			$this->config->shop_id
443
		);
444
445
		$request->set_parameter( 'trxid', $transaction_id );
446
447
		// Cancel reservation.
448
		$result = $this->client->cancel_reservation( $request );
449
450
		// Handle errors.
451
		if ( false === $result ) {
452
			$this->error = $this->client->get_error();
453
454
			return false;
455
		}
456
457
		if ( isset( $result->status ) ) {
458
			$payment->set_status( Statuses::transform( $result->status ) );
459
460
			$payment->save();
461
		}
462
463
		return $result;
464
	}
465
}
466