Failed Conditions
Push — feature/post-pay ( 92fd8e...3640bb )
by Remco
04:42
created

src/Gateway.php (1 issue)

Labels
Severity
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\Payments\Payment;
16
use Pronamic\WordPress\Pay\Payments\PaymentLineType;
17
18
/**
19
 * Title: Sisow gateway
20
 * Description:
21
 * Copyright: Copyright (c) 2005 - 2018
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.0
26
 * @since   1.0.0
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Client.
31
	 *
32
	 * @var Client
33
	 */
34
	protected $client;
35
36
	/**
37
	 * Constructs and initialize an Sisow gateway
38
	 *
39
	 * @param Config $config Config.
40
	 */
41
	public function __construct( Config $config ) {
42
		parent::__construct( $config );
43
44
		$this->supports = array(
45
			'payment_status_request',
46
		);
47
48
		$this->set_method( self::METHOD_HTTP_REDIRECT );
49
50
		$this->client = new Client( $config->merchant_id, $config->merchant_key );
51
		$this->client->set_test_mode( self::MODE_TEST === $config->mode );
52
	}
53
54
	/**
55
	 * Get issuers
56
	 *
57
	 * @see Core_Gateway::get_issuers()
58
	 */
59
	public function get_issuers() {
60
		$groups = array();
61
62
		$result = $this->client->get_directory();
63
64
		if ( $result ) {
65
			$groups[] = array(
66
				'options' => $result,
67
			);
68
		} else {
69
			$this->error = $this->client->get_error();
70
		}
71
72
		return $groups;
73
	}
74
75
	/**
76
	 * Get supported payment methods
77
	 *
78
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
79
	 */
80
	public function get_supported_payment_methods() {
81
		return array(
82
			PaymentMethods::AFTERPAY,
83
			PaymentMethods::BANK_TRANSFER,
84
			PaymentMethods::BANCONTACT,
85
			PaymentMethods::BELFIUS,
86
			PaymentMethods::BUNQ,
87
			PaymentMethods::IN3,
88
			PaymentMethods::CREDIT_CARD,
89
			PaymentMethods::FOCUM,
90
			PaymentMethods::GIROPAY,
91
			PaymentMethods::IDEAL,
92
			PaymentMethods::IDEALQR,
93
			PaymentMethods::KLARNA_PAY_LATER,
94
			PaymentMethods::PAYPAL,
95
			PaymentMethods::SOFORT,
96
		);
97
	}
98
99
	/**
100
	 * Is payment method required to start transaction?
101
	 *
102
	 * @see Core_Gateway::payment_method_is_required()
103
	 */
104
	public function payment_method_is_required() {
105
		return true;
106
	}
107
108
	/**
109
	 * Start
110
	 *
111
	 * @see Core_Gateway::start()
112
	 *
113
	 * @param Payment $payment Payment.
114
	 */
115
	public function start( Payment $payment ) {
116
		// Order and purchase ID.
117
		$order_id    = $payment->get_order_id();
118
		$purchase_id = strval( empty( $order_id ) ? $payment->get_id() : $order_id );
119
120
		// Maximum length for purchase ID is 16 characters, otherwise an error will occur:
121
		// ideal_sisow_error - purchaseid too long (16).
122
		$purchase_id = substr( $purchase_id, 0, 16 );
123
124
		// New transaction request.
125
		$request = new TransactionRequest(
126
			$this->config->merchant_id,
127
			$this->config->shop_id
128
		);
129
130
		$request->merge_parameters(
131
			array(
132
				'payment'      => Methods::transform( $payment->get_method(), $payment->get_method() ),
133
				'purchaseid'   => substr( $purchase_id, 0, 16 ),
134
				'entrancecode' => $payment->get_entrance_code(),
135
				'amount'       => $payment->get_amount()->get_cents(),
136
				'description'  => substr( $payment->get_description(), 0, 32 ),
137
				'testmode'     => ( self::MODE_TEST === $this->config->mode ) ? 'true' : 'false',
138
				'returnurl'    => $payment->get_return_url(),
139
				'cancelurl'    => $payment->get_return_url(),
140
				'notifyurl'    => $payment->get_return_url(),
141
				'callbackurl'  => $payment->get_return_url(),
142
				// Other parameters.
143
				'issuerid'     => $payment->get_issuer(),
144
				'billing_mail' => $payment->get_email(),
145
			)
146
		);
147
148
		// Payment method.
149
		$this->set_payment_method( null === $payment->get_method() ? PaymentMethods::IDEAL : $payment->get_method() );
150
151
		// Additional parameters for payment method.
152
		if ( PaymentMethods::IDEALQR === $payment->get_method() ) {
153
			$request->set_parameter( 'qrcode', 'true' );
154
		}
155
156
		// Customer.
157
		if ( null !== $payment->get_customer() ) {
158
			$customer = $payment->get_customer();
159
160
			$request->merge_parameters(
161
				array(
162
					'ipaddress' => $customer->get_ip_address(),
163
					'gender'    => $customer->get_gender(),
164
				)
165
			);
166
167
			if ( null !== $customer->get_locale() ) {
168
				/*
169
				 * @link https://github.com/wp-pay-gateways/sisow/tree/feature/post-pay/documentation#parameter-locale
170
				 */
171
				$sisow_locale = strtoupper( substr( $customer->get_locale(), -2 ) );
172
173
				$request->set_parameter( 'locale', $sisow_locale );
174
			}
175
176
			if ( null !== $customer->get_birth_date() ) {
177
				$request->set_parameter( 'birth_date', $customer->get_birth_date()->format( 'dmY' ) );
178
			}
179
		}
180
181
		// Billing address.
182
		if ( null !== $payment->get_billing_address() ) {
183
			$address = $payment->get_billing_address();
184
185
			if ( null !== $address->get_name() ) {
186
				$name = $address->get_name();
187
188
				$request->merge_parameters(
189
					array(
190
						'billing_firstname' => $name->get_first_name(),
191
						'billing_lastname'  => $name->get_first_name(),
192
					)
193
				);
194
			}
195
196
			$request->merge_parameters(
197
				array(
198
					'billing_mail'        => $address->get_email(),
199
					'billing_company'     => $address->get_company_name(),
200
					'billing_coc'         => $address->get_kvk_number(),
201
					'billing_address1'    => $address->get_line_1(),
202
					'billing_address2'    => $address->get_line_2(),
203
					'billing_zip'         => $address->get_postal_code(),
204
					'billing_city'        => $address->get_city(),
205
					'billing_country'     => $address->get_country_name(),
206
					'billing_countrycode' => $address->get_country_code(),
207
					'billing_phone'       => $address->get_phone(),
208
				)
209
			);
210
		}
211
212
		// Shipping address.
213
		if ( null !== $payment->get_shipping_address() ) {
214
			$address = $payment->get_shipping_address();
215
216
			if ( null !== $address->get_name() ) {
217
				$name = $address->get_name();
218
219
				$request->merge_parameters(
220
					array(
221
						'shipping_firstname' => $name->get_first_name(),
222
						'shipping_lastname'  => $name->get_first_name(),
223
					)
224
				);
225
			}
226
227
			$request->merge_parameters(
228
				array(
229
					'shipping_mail'        => $address->get_email(),
230
					'shipping_company'     => $address->get_company_name(),
231
					'shipping_address1'    => $address->get_line_1(),
232
					'shipping_address2'    => $address->get_line_2(),
233
					'shipping_zip'         => $address->get_postal_code(),
234
					'shipping_city'        => $address->get_city(),
235
					'shipping_country'     => $address->get_country_name(),
236
					'shipping_countrycode' => $address->get_country_code(),
237
					'shipping_phone'       => $address->get_phone(),
238
				)
239
			);
240
		}
241
242
		// Lines.
243
		if ( null !== $payment->get_lines() ) {
244
			$lines = $payment->get_lines();
245
246
			$x = 1;
247
248
			foreach ( $lines as $line ) {
249
				$net_price = ( null === $line->get_unit_price_excluding_tax() ) ? null : $line->get_unit_price_excluding_tax()->get_cents();
250
				$total     = ( null === $line->get_total_amount() ) ? null : $line->get_total_amount()->get_cents();
251
				$net_total = ( null === $line->get_total_amount_excluding_tax() ) ? null : $line->get_total_amount_excluding_tax()->get_cents();
252
				$tax       = ( null === $line->get_tax_amount() ) ? null : $line->get_tax_amount()->get_cents();
253
254
				$product_id = $line->get_id();
255
256
				if ( PaymentLineType::SHIPPING === $line->get_type() ) {
257
					$product_id = 'shipping';
258
				} elseif ( PaymentLineType::FEE === $line->get_type() ) {
0 ignored issues
show
The constant Pronamic\WordPress\Pay\P...ts\PaymentLineType::FEE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
259
					$product_id = 'paymentfee';
260
				}
261
262
				$request->merge_parameters(
263
					array(
264
						'product_id_' . $x          => $product_id,
265
						'product_description_' . $x => $line->get_description(),
266
						'product_quantity_' . $x    => $line->get_quantity(),
267
						'product_netprice_' . $x    => $net_price,
268
						'product_total_' . $x       => $total,
269
						'product_nettotal_' . $x    => $net_total,
270
						'product_tax_' . $x         => $tax,
271
						'product_taxrate_' . $x     => $line->get_tax_percentage() * 100,
272
					)
273
				);
274
275
				$x++;
276
			}
277
		}
278
279
		// Create transaction.
280
		$result = $this->client->create_transaction( $request );
281
282
		if ( false !== $result ) {
283
			$payment->set_transaction_id( $result->id );
284
			$payment->set_action_url( $result->issuer_url );
285
		} else {
286
			$this->error = $this->client->get_error();
287
		}
288
	}
289
290
	/**
291
	 * Update status of the specified payment
292
	 *
293
	 * @param Payment $payment Payment.
294
	 */
295
	public function update_status( Payment $payment ) {
296
		$request = new StatusRequest(
297
			$payment->get_transaction_id(),
298
			$this->config->merchant_id,
299
			$this->config->shop_id
300
		);
301
302
		$result = $this->client->get_status( $request );
303
304
		if ( false === $result ) {
305
			$this->error = $this->client->get_error();
306
307
			return;
308
		}
309
310
		$transaction = $result;
311
312
		$payment->set_status( $transaction->status );
313
		$payment->set_consumer_name( $transaction->consumer_name );
314
		$payment->set_consumer_account_number( $transaction->consumer_account );
315
		$payment->set_consumer_city( $transaction->consumer_city );
316
	}
317
}
318