Passed
Push — master ( df4ed5...14d15a )
by Remco
21:23 queued 11:46
created

Gateway::create_invoice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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