Failed Conditions
Push — feature/post-pay ( 83b78e...bc5b35 )
by Reüel
04:30
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
					'locale'    => $customer->get_locale(),
165
				)
166
			);
167
168
			if ( null !== $customer->get_birth_date() ) {
169
				$request->set_parameter( 'birth_date', $customer->get_birth_date()->format( 'ddmmYYYY' ) );
170
			}
171
		}
172
173
		// Billing address.
174
		if ( null !== $payment->get_billing_address() ) {
175
			$address = $payment->get_billing_address();
176
177
			if ( null !== $address->get_name() ) {
178
				$name = $address->get_name();
179
180
				$request->merge_parameters(
181
					array(
182
						'billing_firstname' => $name->get_first_name(),
183
						'billing_lastname'  => $name->get_first_name(),
184
					)
185
				);
186
			}
187
188
			$request->merge_parameters(
189
				array(
190
					'billing_mail'        => $address->get_email(),
191
					'billing_company'     => $address->get_company_name(),
192
					'billing_coc'         => $address->get_kvk_number(),
193
					'billing_address1'    => $address->get_line_1(),
194
					'billing_address2'    => $address->get_line_2(),
195
					'billing_zip'         => $address->get_postal_code(),
196
					'billing_city'        => $address->get_city(),
197
					'billing_country'     => $address->get_country_name(),
198
					'billing_countrycode' => $address->get_country_code(),
199
					'billing_phone'       => $address->get_phone(),
200
				)
201
			);
202
		}
203
204
		// Shipping address.
205
		if ( null !== $payment->get_shipping_address() ) {
206
			$address = $payment->get_shipping_address();
207
208
			if ( null !== $address->get_name() ) {
209
				$name = $address->get_name();
210
211
				$request->merge_parameters(
212
					array(
213
						'shipping_firstname' => $name->get_first_name(),
214
						'shipping_lastname'  => $name->get_first_name(),
215
					)
216
				);
217
			}
218
219
			$request->merge_parameters(
220
				array(
221
					'shipping_mail'        => $address->get_email(),
222
					'shipping_company'     => $address->get_company_name(),
223
					'shipping_address1'    => $address->get_line_1(),
224
					'shipping_address2'    => $address->get_line_2(),
225
					'shipping_zip'         => $address->get_postal_code(),
226
					'shipping_city'        => $address->get_city(),
227
					'shipping_country'     => $address->get_country_name(),
228
					'shipping_countrycode' => $address->get_country_code(),
229
					'shipping_phone'       => $address->get_phone(),
230
				)
231
			);
232
		}
233
234
		// Lines.
235
		if ( null !== $payment->get_lines() ) {
236
			$lines = $payment->get_lines();
237
238
			$x = 1;
239
240
			foreach ( $lines as $line ) {
241
				$net_price = ( null === $line->get_unit_price() ) ? null : $line->get_unit_price()->get_cents();
242
				$total     = ( null === $line->get_total_amount() ) ? null : $line->get_total_amount()->get_cents();
243
				$tax       = ( null === $line->get_tax_amount() ) ? null : $line->get_tax_amount()->get_cents();
244
				$net_total = ( $total - $tax );
245
246
				$product_id = $line->get_id();
247
248
				if ( PaymentLineType::SHIPPING === $line->get_type() ) {
249
					$product_id = 'shipping';
250
				} 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...
251
					$product_id = 'paymentfee';
252
				}
253
254
				$request->merge_parameters(
255
					array(
256
						'product_id_' . $x          => $product_id,
257
						'product_description_' . $x => $line->get_description(),
258
						'product_quantity_' . $x    => $line->get_quantity(),
259
						'product_netprice_' . $x    => $net_price,
260
						'product_total_' . $x       => $total,
261
						'product_nettotal_' . $x    => $net_total,
262
						'product_tax_' . $x         => $tax,
263
						'product_taxrate_' . $x     => $line->get_tax_percentage() * 100,
264
					)
265
				);
266
267
				$x++;
268
			}
269
		}
270
271
		// Create transaction.
272
		$result = $this->client->create_transaction( $request );
273
274
		if ( false !== $result ) {
275
			$payment->set_transaction_id( $result->id );
276
			$payment->set_action_url( $result->issuer_url );
277
		} else {
278
			$this->error = $this->client->get_error();
279
		}
280
	}
281
282
	/**
283
	 * Update status of the specified payment
284
	 *
285
	 * @param Payment $payment Payment.
286
	 */
287
	public function update_status( Payment $payment ) {
288
		$request = new StatusRequest(
289
			$payment->get_transaction_id(),
290
			$this->config->merchant_id,
291
			$this->config->shop_id
292
		);
293
294
		$result = $this->client->get_status( $request );
295
296
		if ( false === $result ) {
297
			$this->error = $this->client->get_error();
298
299
			return;
300
		}
301
302
		$transaction = $result;
303
304
		$payment->set_status( $transaction->status );
305
		$payment->set_consumer_name( $transaction->consumer_name );
306
		$payment->set_consumer_account_number( $transaction->consumer_account );
307
		$payment->set_consumer_city( $transaction->consumer_city );
308
	}
309
}
310