Issues (46)

src/Gateway.php (12 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay;
4
5
use Pronamic\WordPress\Pay\Banks\BankAccountDetails;
6
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
7
use Pronamic\WordPress\Pay\Core\PaymentMethods;
8
use Pronamic\WordPress\Pay\Core\Server;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
11
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
12
use Pronamic\WordPress\Pay\Payments\Payment;
13
14
/**
15
 * Title: MultiSafepay Connect gateway
16
 * Description:
17
 * Copyright: 2005-2021 Pronamic
18
 * Company: Pronamic
19
 *
20
 * @author  Remco Tolsma
21
 * @version 2.1.1
22
 * @since   1.0.1
23
 */
24
class Gateway extends Core_Gateway {
25
	/**
26
	 * Client.
27
	 *
28
	 * @var Client
29
	 */
30
	protected $client;
31
32
	/**
33
	 * Config
34
	 *
35
	 * @var Config
36
	 */
37
	protected $config;
38
39
	/**
40
	 * Constructs and initializes an MultiSafepay Connect gateway
41
	 *
42
	 * @param Config $config Config.
43
	 */
44 1
	public function __construct( Config $config ) {
45 1
		parent::__construct( $config );
46
47 1
		$this->set_method( self::METHOD_HTTP_REDIRECT );
48
49
		// Supported features.
50 1
		$this->supports = array(
51
			'payment_status_request',
52
		);
53
54
		// Client.
55 1
		$this->client = new Client();
56
57 1
		$this->client->api_url = $config->api_url;
58 1
	}
59
60
	/**
61
	 * Get iDEAL issuers
62
	 *
63
	 * @see Core_Gateway::get_issuers()
64
	 * @return array<array<string,array>>
65
	 * @since 1.2.0
66
	 */
67 1
	public function get_issuers() {
68 1
		$groups = array();
69
70
		// Merchant.
71 1
		$merchant                   = new Merchant();
72 1
		$merchant->account          = $this->config->account_id;
73 1
		$merchant->site_id          = $this->config->site_id;
74 1
		$merchant->site_secure_code = $this->config->site_code;
75
76 1
		$result = $this->client->get_ideal_issuers( $merchant );
77
78 1
		if ( $result ) {
79 1
			$groups[] = array(
80 1
				'options' => $result,
81
			);
82
		}
83
84 1
		return $groups;
85
	}
86
87
	/**
88
	 * Get credit card issuers
89
	 *
90
	 * @see Core_Gateway::get_credit_card_issuers()
91
	 * @return array<array<string,array>>
92
	 */
93
	public function get_credit_card_issuers() {
94
		// Get active card issuers.
95
		$issuers = \array_intersect_key( $this->get_gateways(), Methods::get_cards() );
96
97
		sort( $issuers );
98
99
		$groups = array(
100
			array(
101
				'options' => $issuers,
102
			),
103
		);
104
105
		return $groups;
106
	}
107
108
	/**
109
	 * Get payment methods
110
	 *
111
	 * @see Core_Gateway::get_payment_methods()
112
	 * @return array<string>
113
	 */
114
	public function get_available_payment_methods() {
115
		$payment_methods = array();
116
117
		$gateways = $this->get_gateways();
118
119
		foreach ( $gateways as $method => $title ) {
120
			$payment_method = Methods::transform_gateway_method( $method );
121
122
			// Handle cards, as no general method for credit cards is returned by gateway.
123
			if ( null === $payment_method && \array_key_exists( $method, Methods::get_cards() ) ) {
124
				$payment_method = PaymentMethods::CREDIT_CARD;
125
			}
126
127
			// Check valid payment method.
128
			if ( null === $payment_method ) {
129
				continue;
130
			}
131
132
			// Add available payment method.
133
			$payment_methods[] = $payment_method;
134
		}
135
136
		return $payment_methods;
137
	}
138
139
	/**
140
	 * Get supported payment methods
141
	 *
142
	 * @see Core_Gateway::get_supported_payment_methods()
143
	 * @return array<string>
144
	 */
145
	public function get_supported_payment_methods() {
146
		return array(
147
			PaymentMethods::ALIPAY,
148
			PaymentMethods::BANCONTACT,
149
			PaymentMethods::BANK_TRANSFER,
150
			PaymentMethods::BELFIUS,
151
			PaymentMethods::CREDIT_CARD,
152
			PaymentMethods::DIRECT_DEBIT,
153
			PaymentMethods::IDEAL,
154
			PaymentMethods::IDEALQR,
155
			PaymentMethods::IN3,
156
			PaymentMethods::GIROPAY,
157
			PaymentMethods::KBC,
158
			PaymentMethods::PAYPAL,
159
			PaymentMethods::SANTANDER,
160
			PaymentMethods::SOFORT,
161
		);
162
	}
163
164
	/**
165
	 * Start payment.
166
	 *
167
	 * @param Payment $payment Payment object.
168
	 */
169
	public function start( Payment $payment ) {
170
		$payment_method = $payment->get_payment_method();
0 ignored issues
show
The method get_payment_method() 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

170
		/** @scrutinizer ignore-call */ 
171
  $payment_method = $payment->get_payment_method();

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...
171
172
		$transaction_description = $payment->get_description();
173
174
		if ( empty( $transaction_description ) ) {
175
			$transaction_description = $payment->get_id();
176
		}
177
178
		// Merchant.
179
		$merchant                   = new Merchant();
180
		$merchant->account          = $this->config->account_id;
181
		$merchant->site_id          = $this->config->site_id;
182
		$merchant->site_secure_code = $this->config->site_code;
183
		$merchant->notification_url = $payment->get_return_url();
184
		$merchant->redirect_url     = $payment->get_return_url();
185
		$merchant->cancel_url       = $payment->get_return_url();
186
		$merchant->close_window     = 'false';
187
188
		// Customer.
189
		$customer               = new Customer();
190
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
191
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
192
193
		if ( null !== $payment->get_customer() ) {
194
			$name = $payment->get_customer()->get_name();
195
196
			if ( null !== $name ) {
197
				$customer->first_name = $name->get_first_name();
198
				$customer->last_name  = $name->get_last_name();
199
			}
200
201
			$customer->locale = $payment->get_customer()->get_locale();
202
			$customer->email  = $payment->get_customer()->get_email();
203
		}
204
205
		// Transaction.
206
		$transaction              = new Transaction();
207
		$transaction->id          = uniqid();
208
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
209
		$transaction->amount      = $payment->get_total_amount()->get_minor_units()->format( 0, '', '' );
210
		$transaction->description = $transaction_description;
211
212
		switch ( $payment_method ) {
213
			case PaymentMethods::IDEAL:
214
				$transaction->gateway = Methods::IDEAL;
215
216
				$issuer = $payment->get_meta( 'issuer' );
217
218
				if ( empty( $issuer ) ) {
219
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
220
				} else {
221
					$gateway_info = new GatewayInfo();
222
223
					$gateway_info->issuer_id = $issuer;
224
225
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
226
				}
227
228
				break;
229
			case PaymentMethods::CREDIT_CARD:
230
				$gateway = Methods::transform( $payment_method );
231
232
				$issuer = $payment->get_meta( 'issuer' );
233
234
				if ( empty( $issuer ) ) {
235
					if ( $gateway ) {
236
						$transaction->gateway = $gateway;
237
					}
238
				} else {
239
					$transaction->gateway = $issuer;
240
				}
241
242
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
243
244
				break;
245
			default:
246
				$gateway = Methods::transform( $payment_method );
247
248
				if ( $gateway ) {
249
					$transaction->gateway = $gateway;
250
				}
251
252
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
253
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
254
					$transaction->gateway = $payment_method;
255
				}
256
257
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
258
		}
259
260
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
261
262
		$message->signature = $signature;
0 ignored issues
show
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
263
264
		try {
265
			$response = $this->client->start_transaction( $message );
0 ignored issues
show
$message of type Pronamic\WordPress\Pay\G...ansactionRequestMessage is incompatible with the type array expected by parameter $message of Pronamic\WordPress\Pay\G...nt::start_transaction(). ( Ignorable by Annotation )

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

265
			$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
266
		} catch ( \Exception $e ) {
267
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
268
269
			$this->set_error( $error );
270
271
			return;
272
		}
273
274
		if ( false !== $response ) {
275
			$transaction = $response->transaction;
276
277
			$payment->set_transaction_id( $transaction->id );
0 ignored issues
show
The property id does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
278
279
			if ( isset( $transaction->payment_url ) ) {
280
				$payment->set_action_url( $transaction->payment_url );
281
			}
282
283
			if ( isset( $response->gateway_info->redirect_url ) ) {
0 ignored issues
show
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
284
				$payment->set_action_url( $response->gateway_info->redirect_url );
285
			}
286
		}
287
	}
288
289
	/**
290
	 * Update status.
291
	 *
292
	 * @param Payment $payment Payment.
293
	 */
294
	public function update_status( Payment $payment ) {
295
		$merchant = new Merchant();
296
297
		$merchant->account          = $this->config->account_id;
298
		$merchant->site_id          = $this->config->site_id;
299
		$merchant->site_secure_code = $this->config->site_code;
300
301
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
302
303
		try {
304
			$result = $this->client->get_status( $message );
0 ignored issues
show
$message of type Pronamic\WordPress\Pay\G...ML\StatusRequestMessage is incompatible with the type array expected by parameter $message of Pronamic\WordPress\Pay\G...ay\Client::get_status(). ( Ignorable by Annotation )

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

304
			$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
305
		} catch ( \Exception $e ) {
306
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
307
308
			return;
309
		}
310
311
		if ( false === $result ) {
312
			return;
313
		}
314
315
		// Status.
316
		$status = Statuses::transform( $result->ewallet->status );
0 ignored issues
show
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
317
318
		$payment->set_status( $status );
319
320
		// Consumer bank details.
321
		$consumer_bank_details = $payment->get_consumer_bank_details();
322
323
		if ( null === $consumer_bank_details ) {
324
			$consumer_bank_details = new BankAccountDetails();
325
326
			$payment->set_consumer_bank_details( $consumer_bank_details );
327
		}
328
329
		$consumer_bank_details->set_name( $result->payment_details->account_holder_name );
0 ignored issues
show
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
330
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
331
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
332
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
333
	}
334
335
	/**
336
	 * Get gateways.
337
	 *
338
	 * @return array<string, string>
339
	 */
340
	private function get_gateways() {
341
		// Merchant.
342
		$merchant                   = new Merchant();
343
		$merchant->account          = $this->config->account_id;
344
		$merchant->site_id          = $this->config->site_id;
345
		$merchant->site_secure_code = $this->config->site_code;
346
347
		// Customer.
348
		$customer = new Customer();
349
350
		// Get gateways.
351
		$gateways = array();
352
353
		try {
354
			$result = $this->client->get_gateways( $merchant, $customer );
355
356
			if ( false !== $result ) {
357
				$gateways = $result;
358
			}
359
		} catch ( \Exception $e ) {
360
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
361
362
			$this->set_error( $error );
363
		}
364
365
		return $gateways;
366
	}
367
}
368