Failed Conditions
Push — develop ( af8e84...9b83c1 )
by Reüel
26:05 queued 21:23
created

src/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\PayNL;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Payments\Payment;
8
9
/**
10
 * Title: Pay.nl gateway
11
 * Description:
12
 * Copyright: 2005-2021 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.4
17
 * @since   1.0.0
18
 */
19
class Gateway extends Core_Gateway {
20
	/**
21
	 * Client.
22
	 *
23
	 * @var Client
24
	 */
25
	protected $client;
26
27
	/**
28
	 * Constructs and initializes an Pay.nl gateway
29
	 *
30
	 * @param Config $config Config.
31
	 */
32
	public function __construct( Config $config ) {
33
		parent::__construct( $config );
34
35
		$this->set_method( self::METHOD_HTTP_REDIRECT );
36
37
		// Supported features.
38
		$this->supports = array(
39
			'payment_status_request',
40
		);
41
42
		// Client.
43
		$this->client = new Client( $config->token, $config->service_id );
44
	}
45
46
	/**
47
	 * Get issuers
48
	 *
49
	 * @see Core_Gateway::get_issuers()
50
	 */
51
	public function get_issuers() {
52
		$groups = array();
53
54
		try {
55
			$result = $this->client->get_issuers();
56
57
			if ( is_array( $result ) ) {
58
				$groups[] = array(
59
					'options' => $result,
60
				);
61
			}
62
		} catch ( \Exception $e ) {
63
			$this->error = new \WP_Error( 'pay_nl_error', $e->getMessage() );
64
65
			return $groups;
66
		}
67
68
		return $groups;
69
	}
70
71
	/**
72
	 * Get supported payment methods
73
	 *
74
	 * @see Core_Gateway::get_supported_payment_methods()
75
	 */
76
	public function get_supported_payment_methods() {
77
		return array(
78
			PaymentMethods::AFTERPAY,
79
			PaymentMethods::BANCONTACT,
80
			PaymentMethods::BANK_TRANSFER,
81
			PaymentMethods::CREDIT_CARD,
82
			PaymentMethods::FOCUM,
83
			PaymentMethods::GIROPAY,
84
			PaymentMethods::IDEAL,
85
			PaymentMethods::IN3,
86
			PaymentMethods::KLARNA_PAY_LATER,
87
			PaymentMethods::MAESTRO,
88
			PaymentMethods::PAYPAL,
89
			PaymentMethods::SOFORT,
90
			PaymentMethods::SPRAYPAY,
0 ignored issues
show
The constant Pronamic\WordPress\Pay\C...aymentMethods::SPRAYPAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
		);
92
	}
93
94
	/**
95
	 * Start.
96
	 *
97
	 * @see Core_Gateway::start()
98
	 *
99
	 * @param Payment $payment Payment.
100
	 */
101
	public function start( Payment $payment ) {
102
		$payment_method = $payment->get_method();
103
104
		/*
105
		 * New transaction request.
106
		 * @link https://www.pay.nl/docs/developers.php#transactions
107
		 */
108
		$customer         = $payment->get_customer();
109
		$billing_address  = $payment->get_billing_address();
110
		$shipping_address = $payment->get_shipping_address();
111
112
		// Payment lines.
113
		$order_data = array();
114
115
		if ( null !== $payment->get_lines() ) {
116
			foreach ( $payment->get_lines() as $line ) {
117
				$price = null;
118
119
				if ( null !== $line->get_unit_price() ) {
120
					$price = $line->get_unit_price()->get_including_tax()->get_minor_units()->to_int();
121
				}
122
123
				$order_data[] = array(
124
					'productId'   => $line->get_id(),
125
					'productType' => ProductTypes::transform( $line->get_type() ),
126
					'description' => $line->get_name(),
127
					'price'       => $price,
128
					'quantity'    => $line->get_quantity(),
129
				);
130
			}
131
		}
132
133
		// End user.
134
		$end_user = array();
135
136
		// End user - Address.
137
		if ( null !== $shipping_address ) {
138
			$end_user['address'] = array(
139
				'streetName'            => $shipping_address->get_street_name(),
140
				'streetNumber'          => $shipping_address->get_house_number_base(),
141
				'streetNumberExtension' => $shipping_address->get_house_number_addition(),
142
				'zipCode'               => $shipping_address->get_postal_code(),
143
				'city'                  => $shipping_address->get_city(),
144
				'countryCode'           => $shipping_address->get_country_code(),
145
			);
146
		}
147
148
		// End user - Invoice address.
149
		if ( null !== $billing_address ) {
150
			$end_user['invoiceAddress'] = array(
151
				'streetName'            => $billing_address->get_street_name(),
152
				'streetNumber'          => $billing_address->get_house_number_base(),
153
				'streetNumberExtension' => $billing_address->get_house_number_addition(),
154
				'zipCode'               => $billing_address->get_postal_code(),
155
				'city'                  => $billing_address->get_city(),
156
				'countryCode'           => $billing_address->get_country_code(),
157
			);
158
		}
159
160
		// Request.
161
		$request = array(
162
			// Transaction.
163
			'transaction' => array(
164
				'currency'    => $payment->get_total_amount()->get_currency()->get_alphabetic_code(),
165
				'description' => $payment->get_description(),
166
			),
167
168
			// End user.
169
			'enduser'     => $end_user,
170
171
			// Sale data.
172
			'saleData'    => array(
173
				'invoiceDate'  => $payment->get_date()->format( 'd-m-Y' ),
174
				'deliveryDate' => $payment->get_date()->format( 'd-m-Y' ),
175
				'orderData'    => $order_data,
176
			),
177
		);
178
179
		// Payment method.
180
		$method = Methods::transform( $payment_method );
181
182
		if ( null !== $method ) {
183
			$request['paymentOptionId'] = $method;
184
		}
185
186
		if ( null !== $payment->get_customer() ) {
187
			$enduser = array(
188
				'gender'       => $customer->get_gender(),
189
				'phoneNumber'  => $customer->get_phone(),
190
				'emailAddress' => $customer->get_email(),
191
				'language'     => $customer->get_language(),
192
			);
193
194
			$invoice_address = array(
195
				'gender' => $customer->get_gender(),
196
			);
197
198
			// Set name from customer.
199
			if ( null !== $customer->get_name() ) {
200
				$first_name = \substr( (string) $customer->get_name()->get_first_name(), 0, 32 );
201
				$last_name  = \substr( (string) $customer->get_name()->get_last_name(), 0, 32 );
202
203
				$enduser['initials'] = $first_name;
204
				$enduser['lastName'] = $last_name;
205
206
				$invoice_address['initials'] = $first_name;
207
				$invoice_address['lastName'] = $last_name;
208
			}
209
210
			// Set date of birth.
211
			if ( $customer->get_birth_date() instanceof \DateTime ) {
212
				$enduser['dob'] = $customer->get_birth_date()->format( 'dmY' );
213
			}
214
215
			$request['enduser'] = array_merge( $request['enduser'], $enduser );
216
217
			$request['enduser']['invoiceAddress'] = array_merge( $request['enduser']['invoiceAddress'], $invoice_address );
218
		}
219
220
		// Check payment method.
221
		if ( null === $request['paymentOptionId'] && ! empty( $payment_method ) ) {
222
			// Leap of faith if the WordPress payment method could not transform to a Pay.nl method?
223
			$request['paymentOptionId'] = $payment_method;
224
		}
225
226
		// Set payment method specific parameters.
227
		if ( PaymentMethods::IDEAL === $payment_method ) {
228
			$request['paymentOptionSubId'] = $payment->get_issuer();
229
		}
230
231
		// Start transaction.
232
		$result = $this->client->transaction_start(
233
			$payment->get_total_amount()->get_minor_units()->to_int(),
234
			Util::get_ip_address(),
235
			$payment->get_return_url(),
236
			$request
237
		);
238
239
		// Update gateway results in payment.
240
		$payment->set_transaction_id( $result->transaction->transactionId );
241
		$payment->set_action_url( $result->transaction->paymentURL );
242
	}
243
244
	/**
245
	 * Update status of the specified payment.
246
	 *
247
	 * @param Payment $payment Payment.
248
	 */
249
	public function update_status( Payment $payment ) {
250
		try {
251
			// Get transaction info.
252
			$result = $this->client->transaction_info( $payment->get_transaction_id() );
253
		} catch ( \Exception $e ) {
254
			return;
255
		}
256
257
		if ( is_object( $result ) && isset( $result->paymentDetails ) ) {
258
			$status = Statuses::transform( $result->paymentDetails->state );
259
260
			// Update payment status.
261
			$payment->set_status( $status );
262
		}
263
	}
264
}
265