Failed Conditions
Push — develop ( 45c176...d31088 )
by Reüel
05:42 queued 10s
created

src/Gateway.php (11 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 gateay
16
 * Description:
17
 * Copyright: 2005-2020 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( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
95
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
96
				Methods::MASTERCARD => _x( 'MASTER', '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 Pronamic_WP_Pay_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
		foreach ( $result as $method => $title ) {
137
			$payment_method = Methods::transform_gateway_method( $method );
138
139
			if ( $payment_method ) {
140
				$payment_methods[] = $payment_method;
141
			}
142
		}
143
144
		return $payment_methods;
145
	}
146
147
	/**
148
	 * Get supported payment methods
149
	 *
150
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
151
	 */
152
	public function get_supported_payment_methods() {
153
		return array(
154
			PaymentMethods::ALIPAY,
155
			PaymentMethods::BANCONTACT,
156
			PaymentMethods::BANK_TRANSFER,
157
			PaymentMethods::BELFIUS,
158
			PaymentMethods::CREDIT_CARD,
159
			PaymentMethods::DIRECT_DEBIT,
160
			PaymentMethods::IDEAL,
161
			PaymentMethods::IDEALQR,
162
			PaymentMethods::IN3,
163
			PaymentMethods::GIROPAY,
164
			PaymentMethods::KBC,
165
			PaymentMethods::PAYPAL,
166
			PaymentMethods::SANTANDER,
167
			PaymentMethods::SOFORT,
168
		);
169
	}
170
171
	/**
172
	 * Start payment.
173
	 *
174
	 * @param Payment $payment Payment object.
175
	 */
176
	public function start( Payment $payment ) {
177
		$payment_method = $payment->get_method();
178
179
		$transaction_description = $payment->get_description();
180
181
		if ( empty( $transaction_description ) ) {
182
			$transaction_description = $payment->get_id();
183
		}
184
185
		// Merchant.
186
		$merchant                   = new Merchant();
187
		$merchant->account          = $this->config->account_id;
188
		$merchant->site_id          = $this->config->site_id;
189
		$merchant->site_secure_code = $this->config->site_code;
190
		$merchant->notification_url = $payment->get_return_url();
191
		$merchant->redirect_url     = $payment->get_return_url();
192
		$merchant->cancel_url       = $payment->get_return_url();
193
		$merchant->close_window     = 'false';
194
195
		// Customer.
196
		$customer               = new Customer();
197
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
198
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
199
200
		if ( null !== $payment->get_customer() ) {
201
			$name = $payment->get_customer()->get_name();
202
203
			if ( null !== $name ) {
204
				$customer->first_name = $name->get_first_name();
205
				$customer->last_name  = $name->get_last_name();
206
			}
207
208
			$customer->locale = $payment->get_customer()->get_locale();
209
			$customer->email  = $payment->get_customer()->get_email();
210
		}
211
212
		// Transaction.
213
		$transaction              = new Transaction();
214
		$transaction->id          = uniqid();
215
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
216
		$transaction->amount      = $payment->get_total_amount()->get_cents();
217
		$transaction->description = $transaction_description;
218
219
		switch ( $payment_method ) {
220
			case PaymentMethods::IDEAL:
221
				$transaction->gateway = Methods::IDEAL;
222
223
				$issuer = $payment->get_issuer();
224
225
				if ( empty( $issuer ) ) {
226
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
227
				} else {
228
					$gateway_info = new GatewayInfo();
229
230
					$gateway_info->issuer_id = $issuer;
231
232
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
233
				}
234
235
				break;
236
			case PaymentMethods::CREDIT_CARD:
237
				$gateway = Methods::transform( $payment_method );
238
239
				$issuer = $payment->get_issuer();
240
241
				if ( empty( $issuer ) ) {
242
					if ( $gateway ) {
243
						$transaction->gateway = $gateway;
244
					}
245
				} else {
246
					$transaction->gateway = $issuer;
247
				}
248
249
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
250
251
				break;
252
			default:
253
				$gateway = Methods::transform( $payment_method );
254
255
				if ( $gateway ) {
256
					$transaction->gateway = $gateway;
257
				}
258
259
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
260
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
261
					$transaction->gateway = $payment_method;
262
				}
263
264
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
265
		}
266
267
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
268
269
		$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...
270
271
		try {
272
			$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

272
			$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
273
		} catch ( \Exception $e ) {
274
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
275
276
			$this->set_error( $error );
277
278
			return;
279
		}
280
281
		if ( false !== $response ) {
282
			$transaction = $response->transaction;
283
284
			$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...
285
286
			if ( isset( $transaction->payment_url ) ) {
287
				$payment->set_action_url( $transaction->payment_url );
288
			}
289
290
			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...nsactionResponseMessage.
Loading history...
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
291
				$payment->set_action_url( $response->gateway_info->redirect_url );
292
			}
293
		}
294
	}
295
296
	/**
297
	 * Update status.
298
	 *
299
	 * @param Payment $payment Payment.
300
	 */
301
	public function update_status( Payment $payment ) {
302
		$merchant = new Merchant();
303
304
		$merchant->account          = $this->config->account_id;
305
		$merchant->site_id          = $this->config->site_id;
306
		$merchant->site_secure_code = $this->config->site_code;
307
308
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
309
310
		try {
311
			$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

311
			$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
312
		} catch ( \Exception $e ) {
313
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
314
315
			return;
316
		}
317
318
		if ( false === $result ) {
319
			return;
320
		}
321
322
		// Status.
323
		$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...
324
325
		$payment->set_status( $status );
326
327
		// Consumer bank details.
328
		$consumer_bank_details = $payment->get_consumer_bank_details();
329
330
		if ( null === $consumer_bank_details ) {
331
			$consumer_bank_details = new BankAccountDetails();
332
333
			$payment->set_consumer_bank_details( $consumer_bank_details );
334
		}
335
336
		$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...
337
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
338
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
339
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
340
	}
341
}
342