Passed
Push — develop ( 8b98d9...426279 )
by Reüel
06:39
created

Gateway::get_available_payment_methods()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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