Test Failed
Push — develop ( 4865b8...e29635 )
by Reüel
06:10
created

src/Gateway.php (1 issue)

Labels
Severity
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
	 * @since 1.2.0
65
	 */
66 1
	public function get_issuers() {
67 1
		$groups = array();
68
69
		// Merchant.
70 1
		$merchant                   = new Merchant();
71 1
		$merchant->account          = $this->config->account_id;
72 1
		$merchant->site_id          = $this->config->site_id;
73 1
		$merchant->site_secure_code = $this->config->site_code;
74
75 1
		$result = $this->client->get_ideal_issuers( $merchant );
76
77 1
		if ( $result ) {
78 1
			$groups[] = array(
79 1
				'options' => $result,
80
			);
81
		}
82
83 1
		return $groups;
84
	}
85
86
	/**
87
	 * Get credit card issuers
88
	 *
89
	 * @see Core_Gateway::get_credit_card_issuers()
90
	 */
91
	public function get_credit_card_issuers() {
92
		$groups[] = array(
93
			'options' => array(
94
				Methods::AMEX       => _x( 'American Express', 'Payment method name', 'pronamic_ideal' ),
95
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
96
				Methods::MASTERCARD => _x( 'Mastercard', 'Payment method name', 'pronamic_ideal' ),
97
				Methods::VISA       => _x( 'Visa', 'Payment method name', 'pronamic_ideal' ),
98
			),
99
		);
100
101
		return $groups;
102
	}
103
104
	/**
105
	 * Get payment methods
106
	 *
107
	 * @see Core_Gateway::get_payment_methods()
108
	 */
109
	public function get_available_payment_methods() {
110
		$payment_methods = array();
111
112
		// Merchant.
113
		$merchant                   = new Merchant();
114
		$merchant->account          = $this->config->account_id;
115
		$merchant->site_id          = $this->config->site_id;
116
		$merchant->site_secure_code = $this->config->site_code;
117
118
		// Customer.
119
		$customer = new Customer();
120
121
		// Get gateways.
122
		try {
123
			$result = $this->client->get_gateways( $merchant, $customer );
124
		} catch ( \Exception $e ) {
125
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
126
127
			$this->set_error( $error );
128
129
			return $payment_methods;
130
		}
131
132
		if ( false === $result ) {
133
			return $payment_methods;
134
		}
135
136
		$card_brands = array(
137
			Methods::AMEX,
138
			Methods::MAESTRO,
139
			Methods::MASTERCARD,
140
			Methods::VISA,
141
		);
142
143
		foreach ( $result as $method => $title ) {
144
			$payment_method = Methods::transform_gateway_method( $method );
145
146
			// Check credit card brands, as no general method for credit cards is returned.
147
			if ( null === $payment_method && \in_array( $method, $card_brands, true ) ) {
148
				$payment_method = PaymentMethods::CREDIT_CARD;
149
			}
150
151
			if ( $payment_method ) {
152
				$payment_methods[] = $payment_method;
153
			}
154
		}
155
156
		return $payment_methods;
157
	}
158
159
	/**
160
	 * Get supported payment methods
161
	 *
162
	 * @see Core_Gateway::get_supported_payment_methods()
163
	 */
164
	public function get_supported_payment_methods() {
165
		return array(
166
			PaymentMethods::ALIPAY,
167
			PaymentMethods::BANCONTACT,
168
			PaymentMethods::BANK_TRANSFER,
169
			PaymentMethods::BELFIUS,
170
			PaymentMethods::CREDIT_CARD,
171
			PaymentMethods::DIRECT_DEBIT,
172
			PaymentMethods::IDEAL,
173
			PaymentMethods::IDEALQR,
174
			PaymentMethods::IN3,
175
			PaymentMethods::GIROPAY,
176
			PaymentMethods::KBC,
177
			PaymentMethods::PAYPAL,
178
			PaymentMethods::SANTANDER,
179
			PaymentMethods::SOFORT,
180
		);
181
	}
182
183
	/**
184
	 * Start payment.
185
	 *
186
	 * @param Payment $payment Payment object.
187
	 */
188
	public function start( Payment $payment ) {
189
		$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

189
		/** @scrutinizer ignore-call */ 
190
  $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...
190
191
		$transaction_description = $payment->get_description();
192
193
		if ( empty( $transaction_description ) ) {
194
			$transaction_description = $payment->get_id();
195
		}
196
197
		// Merchant.
198
		$merchant                   = new Merchant();
199
		$merchant->account          = $this->config->account_id;
200
		$merchant->site_id          = $this->config->site_id;
201
		$merchant->site_secure_code = $this->config->site_code;
202
		$merchant->notification_url = $payment->get_return_url();
203
		$merchant->redirect_url     = $payment->get_return_url();
204
		$merchant->cancel_url       = $payment->get_return_url();
205
		$merchant->close_window     = 'false';
206
207
		// Customer.
208
		$customer               = new Customer();
209
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
210
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
211
212
		if ( null !== $payment->get_customer() ) {
213
			$name = $payment->get_customer()->get_name();
214
215
			if ( null !== $name ) {
216
				$customer->first_name = $name->get_first_name();
217
				$customer->last_name  = $name->get_last_name();
218
			}
219
220
			$customer->locale = $payment->get_customer()->get_locale();
221
			$customer->email  = $payment->get_customer()->get_email();
222
		}
223
224
		// Transaction.
225
		$transaction              = new Transaction();
226
		$transaction->id          = uniqid();
227
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
228
		$transaction->amount      = $payment->get_total_amount()->get_minor_units()->format( 0, '', '' );
229
		$transaction->description = $transaction_description;
230
231
		switch ( $payment_method ) {
232
			case PaymentMethods::IDEAL:
233
				$transaction->gateway = Methods::IDEAL;
234
235
				$issuer = $payment->get_meta( 'issuer' );
236
237
				if ( empty( $issuer ) ) {
238
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
239
				} else {
240
					$gateway_info = new GatewayInfo();
241
242
					$gateway_info->issuer_id = $issuer;
243
244
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
245
				}
246
247
				break;
248
			case PaymentMethods::CREDIT_CARD:
249
				$gateway = Methods::transform( $payment_method );
250
251
				$issuer = $payment->get_meta( 'issuer' );
252
253
				if ( empty( $issuer ) ) {
254
					if ( $gateway ) {
255
						$transaction->gateway = $gateway;
256
					}
257
				} else {
258
					$transaction->gateway = $issuer;
259
				}
260
261
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
262
263
				break;
264
			default:
265
				$gateway = Methods::transform( $payment_method );
266
267
				if ( $gateway ) {
268
					$transaction->gateway = $gateway;
269
				}
270
271
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
272
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
273
					$transaction->gateway = $payment_method;
274
				}
275
276
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
277
		}
278
279
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
280
281
		$message->signature = $signature;
282
283
		try {
284
			$response = $this->client->start_transaction( $message );
285
		} catch ( \Exception $e ) {
286
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
287
288
			$this->set_error( $error );
289
290
			return;
291
		}
292
293
		if ( false !== $response ) {
294
			$transaction = $response->transaction;
295
296
			$payment->set_transaction_id( $transaction->id );
297
298
			if ( isset( $transaction->payment_url ) ) {
299
				$payment->set_action_url( $transaction->payment_url );
300
			}
301
302
			if ( isset( $response->gateway_info->redirect_url ) ) {
303
				$payment->set_action_url( $response->gateway_info->redirect_url );
304
			}
305
		}
306
	}
307
308
	/**
309
	 * Update status.
310
	 *
311
	 * @param Payment $payment Payment.
312
	 */
313
	public function update_status( Payment $payment ) {
314
		$merchant = new Merchant();
315
316
		$merchant->account          = $this->config->account_id;
317
		$merchant->site_id          = $this->config->site_id;
318
		$merchant->site_secure_code = $this->config->site_code;
319
320
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
321
322
		try {
323
			$result = $this->client->get_status( $message );
324
		} catch ( \Exception $e ) {
325
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
326
327
			return;
328
		}
329
330
		if ( false === $result ) {
331
			return;
332
		}
333
334
		// Status.
335
		$status = Statuses::transform( $result->ewallet->status );
336
337
		$payment->set_status( $status );
338
339
		// Consumer bank details.
340
		$consumer_bank_details = $payment->get_consumer_bank_details();
341
342
		if ( null === $consumer_bank_details ) {
343
			$consumer_bank_details = new BankAccountDetails();
344
345
			$payment->set_consumer_bank_details( $consumer_bank_details );
346
		}
347
348
		$consumer_bank_details->set_name( $result->payment_details->account_holder_name );
349
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
350
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
351
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
352
	}
353
}
354