Failed Conditions
Push — develop ( b99fe0...34673b )
by Remco
05:28
created

src/Gateway.php (3 issues)

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,
0 ignored issues
show
The constant Pronamic\WordPress\Pay\C...PaymentMethods::BILLINK was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
			PaymentMethods::BUNQ,
90
			PaymentMethods::CAPAYABLE,
0 ignored issues
show
The constant Pronamic\WordPress\Pay\C...ymentMethods::CAPAYABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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(),
0 ignored issues
show
The method get_total_amount() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
				'amount'       => $payment->/** @scrutinizer ignore-call */ get_total_amount()->get_cents(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
200
			$request->merge_parameters(
201
				array(
202
					'billing_mail'        => $address->get_email(),
203
					'billing_company'     => $address->get_company_name(),
204
					'billing_coc'         => $address->get_kvk_number(),
205
					'billing_address1'    => $address->get_line_1(),
206
					'billing_address2'    => $address->get_line_2(),
207
					'billing_zip'         => $address->get_postal_code(),
208
					'billing_city'        => $address->get_city(),
209
					'billing_country'     => $address->get_country_name(),
210
					'billing_countrycode' => $address->get_country_code(),
211
					'billing_phone'       => $address->get_phone(),
212
				)
213
			);
214
		}
215
216
		// Shipping address.
217
		if ( null !== $payment->get_shipping_address() ) {
218
			$address = $payment->get_shipping_address();
219
220
			if ( null !== $address->get_name() ) {
221
				$name = $address->get_name();
222
223
				$request->merge_parameters(
224
					array(
225
						'shipping_firstname' => $name->get_first_name(),
226
						'shipping_lastname'  => $name->get_last_name(),
227
					)
228
				);
229
			}
230
231
			$request->merge_parameters(
232
				array(
233
					'shipping_mail'        => $address->get_email(),
234
					'shipping_company'     => $address->get_company_name(),
235
					'shipping_address1'    => $address->get_line_1(),
236
					'shipping_address2'    => $address->get_line_2(),
237
					'shipping_zip'         => $address->get_postal_code(),
238
					'shipping_city'        => $address->get_city(),
239
					'shipping_country'     => $address->get_country_name(),
240
					'shipping_countrycode' => $address->get_country_code(),
241
					'shipping_phone'       => $address->get_phone(),
242
				)
243
			);
244
		}
245
246
		// Lines.
247
		if ( null !== $payment->get_lines() ) {
248
			$lines = $payment->get_lines();
249
250
			$x = 1;
251
252
			foreach ( $lines as $line ) {
253
				$net_price = ( null === $line->get_unit_price_excluding_tax() ) ? null : $line->get_unit_price_excluding_tax()->get_cents();
254
				$total     = ( null === $line->get_total_amount() ) ? null : $line->get_total_amount()->get_cents();
255
				$net_total = ( null === $line->get_total_amount_excluding_tax() ) ? null : $line->get_total_amount_excluding_tax()->get_cents();
256
				$tax       = ( null === $line->get_tax_amount() ) ? null : $line->get_tax_amount()->get_cents();
257
258
				$product_id = $line->get_id();
259
260
				if ( PaymentLineType::SHIPPING === $line->get_type() ) {
261
					$product_id = 'shipping';
262
				} elseif ( PaymentLineType::FEE === $line->get_type() ) {
263
					$product_id = 'paymentfee';
264
				}
265
266
				$request->merge_parameters(
267
					array(
268
						'product_id_' . $x          => $product_id,
269
						'product_description_' . $x => $line->get_name(),
270
						'product_quantity_' . $x    => $line->get_quantity(),
271
						'product_netprice_' . $x    => $net_price,
272
						'product_total_' . $x       => $total,
273
						'product_nettotal_' . $x    => $net_total,
274
						'product_tax_' . $x         => $tax,
275
						'product_taxrate_' . $x     => $line->get_tax_percentage() * 100,
276
					)
277
				);
278
279
				$x++;
280
			}
281
		}
282
283
		// Create transaction.
284
		$result = $this->client->create_transaction( $request );
285
286
		if ( false !== $result ) {
287
			$payment->set_transaction_id( $result->id );
288
			$payment->set_action_url( $result->issuer_url );
289
		} else {
290
			$this->error = $this->client->get_error();
291
292
			return false;
293
		}
294
	}
295
296
	/**
297
	 * Update status of the specified payment
298
	 *
299
	 * @param Payment $payment Payment.
300
	 */
301
	public function update_status( Payment $payment ) {
302
		$request = new StatusRequest(
303
			$payment->get_transaction_id(),
304
			$this->config->merchant_id,
305
			$this->config->shop_id
306
		);
307
308
		$result = $this->client->get_status( $request );
309
310
		if ( false === $result ) {
311
			$this->error = $this->client->get_error();
312
313
			return;
314
		}
315
316
		$transaction = $result;
317
318
		$payment->set_status( Statuses::transform( $transaction->status ) );
319
		$payment->set_consumer_name( $transaction->consumer_name );
320
		$payment->set_consumer_account_number( $transaction->consumer_account );
321
		$payment->set_consumer_city( $transaction->consumer_city );
322
	}
323
324
	/**
325
	 * Create invoice.
326
	 *
327
	 * @param Payment $payment Payment.
328
	 *
329
	 * @return bool|Invoice
330
	 */
331
	public function create_invoice( $payment ) {
332
		$transaction_id = $payment->get_transaction_id();
333
334
		if ( empty( $transaction_id ) ) {
335
			return false;
336
		}
337
338
		// Invoice request.
339
		$request = new InvoiceRequest(
340
			$this->config->merchant_id,
341
			$this->config->shop_id
342
		);
343
344
		$request->set_parameter( 'trxid', $transaction_id );
345
346
		// Create invoice.
347
		$result = $this->client->create_invoice( $request );
348
349
		// Handle errors.
350
		if ( false === $result ) {
351
			$this->error = $this->client->get_error();
352
353
			return false;
354
		}
355
356
		$payment->set_status( Core_Statuses::SUCCESS );
357
358
		$payment->save();
359
360
		return $result;
361
	}
362
363
	/**
364
	 * Cancel reservation.
365
	 *
366
	 * @param Payment $payment Payment.
367
	 *
368
	 * @return bool|Reservation
369
	 */
370
	public function cancel_reservation( $payment ) {
371
		$transaction_id = $payment->get_transaction_id();
372
373
		if ( empty( $transaction_id ) ) {
374
			return false;
375
		}
376
377
		// Cancel reservation request.
378
		$request = new CancelReservationRequest(
379
			$this->config->merchant_id,
380
			$this->config->shop_id
381
		);
382
383
		$request->set_parameter( 'trxid', $transaction_id );
384
385
		// Cancel reservation.
386
		$result = $this->client->cancel_reservation( $request );
387
388
		// Handle errors.
389
		if ( false === $result ) {
390
			$this->error = $this->client->get_error();
391
392
			return false;
393
		}
394
395
		if ( isset( $result->status ) ) {
396
			$payment->set_status( Statuses::transform( $result->status ) );
397
398
			$payment->save();
399
		}
400
401
		return $result;
402
	}
403
}
404