Failed Conditions
Push — develop ( 87302b...e08f1e )
by Reüel
05:32
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-2020 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 Pronamic_WP_Pay_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 Pronamic_WP_Pay_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
		);
91
	}
92
93
	/**
94
	 * Start.
95
	 *
96
	 * @see Core_Gateway::start()
97
	 *
98
	 * @param Payment $payment Payment.
99
	 */
100
	public function start( Payment $payment ) {
101
		$payment_method = $payment->get_method();
102
103
		/*
104
		 * New transaction request.
105
		 * @link https://www.pay.nl/docs/developers.php#transactions
106
		 */
107
		$customer         = $payment->get_customer();
108
		$billing_address  = $payment->get_billing_address();
109
		$shipping_address = $payment->get_shipping_address();
110
111
		// Payment lines.
112
		$order_data = array();
113
114
		if ( null !== $payment->get_lines() ) {
115
			foreach ( $payment->get_lines() as $line ) {
116
				$price = null;
117
118
				if ( null !== $line->get_unit_price() ) {
119
					$price = $line->get_unit_price()->get_including_tax()->get_cents();
120
				}
121
122
				$order_data[] = array(
123
					'productId'   => $line->get_id(),
124
					'productType' => ProductTypes::transform( $line->get_type() ),
125
					'description' => $line->get_name(),
126
					'price'       => $price,
127
					'quantity'    => $line->get_quantity(),
128
				);
129
			}
130
		}
131
132
		// End user.
133
		$end_user = array();
134
135
		// End user - Address.
136
		if ( null !== $shipping_address ) {
137
			$end_user['address'] = array(
138
				'streetName'            => $shipping_address->get_street_name(),
139
				'streetNumber'          => $shipping_address->get_house_number_base(),
140
				'streetNumberExtension' => $shipping_address->get_house_number_addition(),
141
				'zipCode'               => $shipping_address->get_postal_code(),
142
				'city'                  => $shipping_address->get_city(),
143
				'countryCode'           => $shipping_address->get_country_code(),
144
			);
145
		}
146
147
		// End user - Invoice address.
148
		if ( null !== $billing_address ) {
149
			$end_user['invoiceAddress'] = array(
150
				'streetName'            => $billing_address->get_street_name(),
151
				'streetNumber'          => $billing_address->get_house_number_base(),
152
				'streetNumberExtension' => $billing_address->get_house_number_addition(),
153
				'zipCode'               => $billing_address->get_postal_code(),
154
				'city'                  => $billing_address->get_city(),
155
				'countryCode'           => $billing_address->get_country_code(),
156
			);
157
		}
158
159
		// Request.
160
		$request = array(
161
			// Transaction.
162
			'transaction' => array(
163
				'currency'    => $payment->get_total_amount()->get_currency()->get_alphabetic_code(),
164
				'description' => $payment->get_description(),
165
			),
166
167
			// End user.
168
			'enduser'     => $end_user,
169
170
			// Sale data.
171
			'saleData'    => array(
172
				'invoiceDate'  => $payment->get_date()->format( 'd-m-Y' ),
173
				'deliveryDate' => $payment->get_date()->format( 'd-m-Y' ),
174
				'orderData'    => $order_data,
175
			),
176
		);
177
178
		// Payment method.
179
		$method = Methods::transform( $payment_method );
180
181
		if ( null !== $method ) {
182
			$request['paymentOptionId'] = $method;
183
		}
184
185
		if ( null !== $payment->get_customer() ) {
186
			$enduser = array(
187
				'gender'       => $customer->get_gender(),
188
				'phoneNumber'  => $customer->get_phone(),
189
				'emailAddress' => $customer->get_email(),
190
				'language'     => $customer->get_language(),
191
			);
192
193
			$invoice_address = array(
194
				'gender' => $customer->get_gender(),
195
			);
196
197
			// Set name from customer.
198
			if ( null !== $customer->get_name() ) {
199
				$first_name = \substr( (string) $customer->get_name()->get_first_name(), 0, 32 );
200
				$last_name  = \substr( (string) $customer->get_name()->get_last_name(), 0, 32 );
201
202
				$enduser['initials'] = $first_name;
203
				$enduser['lastName'] = $last_name;
204
205
				$invoice_address['initials'] = $first_name;
206
				$invoice_address['lastName'] = $last_name;
207
			}
208
209
			// Set date of birth.
210
			if ( $customer->get_birth_date() instanceof \DateTime ) {
211
				$enduser['dob'] = $customer->get_birth_date()->format( 'dmY' );
212
			}
213
214
			$request['enduser'] = array_merge( $request['enduser'], $enduser );
215
216
			$request['enduser']['invoiceAddress'] = array_merge( $request['enduser']['invoiceAddress'], $invoice_address );
217
		}
218
219
		// Check payment method.
220
		if ( null === $request['paymentOptionId'] && ! empty( $payment_method ) ) {
221
			// Leap of faith if the WordPress payment method could not transform to a Pay.nl method?
222
			$request['paymentOptionId'] = $payment_method;
223
		}
224
225
		// Set payment method specific parameters.
226
		if ( PaymentMethods::IDEAL === $payment_method ) {
227
			$request['paymentOptionSubId'] = $payment->get_issuer();
228
		}
229
230
		// Start transaction.
231
		$result = $this->client->transaction_start(
0 ignored issues
show
Are you sure the assignment to $result is correct as $this->client->transacti...return_url(), $request) targeting Pronamic\WordPress\Pay\G...nt::transaction_start() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
232
			$payment->get_total_amount()->get_minor_units(),
233
			Util::get_ip_address(),
234
			$payment->get_return_url(),
235
			$request
236
		);
237
238
		// Update gateway results in payment.
239
		$payment->set_transaction_id( $result->transaction->transactionId );
240
		$payment->set_action_url( $result->transaction->paymentURL );
241
	}
242
243
	/**
244
	 * Update status of the specified payment.
245
	 *
246
	 * @param Payment $payment Payment.
247
	 */
248
	public function update_status( Payment $payment ) {
249
		try {
250
			// Get transaction info.
251
			$result = $this->client->transaction_info( $payment->get_transaction_id() );
252
		} catch ( \Exception $e ) {
253
			return;
254
		}
255
256
		if ( is_object( $result ) && isset( $result->paymentDetails ) ) {
257
			$status = Statuses::transform( $result->paymentDetails->state );
258
259
			// Update payment status.
260
			$payment->set_status( $status );
261
		}
262
	}
263
}
264