Failed Conditions
Push — develop ( 84789b...288de2 )
by Reüel
06:59
created

src/Gateway.php (1 issue)

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::GIROPAY,
163
			PaymentMethods::KBC,
164
			PaymentMethods::PAYPAL,
165
			PaymentMethods::SANTANDER,
0 ignored issues
show
The constant Pronamic\WordPress\Pay\C...ymentMethods::SANTANDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
166
			PaymentMethods::SOFORT,
167
		);
168
	}
169
170
	/**
171
	 * Start payment.
172
	 *
173
	 * @param Payment $payment Payment object.
174
	 */
175
	public function start( Payment $payment ) {
176
		$payment_method = $payment->get_method();
177
178
		$transaction_description = $payment->get_description();
179
180
		if ( empty( $transaction_description ) ) {
181
			$transaction_description = $payment->get_id();
182
		}
183
184
		// Merchant.
185
		$merchant                   = new Merchant();
186
		$merchant->account          = $this->config->account_id;
187
		$merchant->site_id          = $this->config->site_id;
188
		$merchant->site_secure_code = $this->config->site_code;
189
		$merchant->notification_url = $payment->get_return_url();
190
		$merchant->redirect_url     = $payment->get_return_url();
191
		$merchant->cancel_url       = $payment->get_return_url();
192
		$merchant->close_window     = 'false';
193
194
		// Customer.
195
		$customer               = new Customer();
196
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
197
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
198
199
		if ( null !== $payment->get_customer() ) {
200
			$name = $payment->get_customer()->get_name();
201
202
			if ( null !== $name ) {
203
				$customer->first_name = $name->get_first_name();
204
				$customer->last_name  = $name->get_last_name();
205
			}
206
207
			$customer->locale = $payment->get_customer()->get_locale();
208
			$customer->email  = $payment->get_customer()->get_email();
209
		}
210
211
		// Transaction.
212
		$transaction              = new Transaction();
213
		$transaction->id          = uniqid();
214
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
215
		$transaction->amount      = $payment->get_total_amount()->get_cents();
216
		$transaction->description = $transaction_description;
217
218
		switch ( $payment_method ) {
219
			case PaymentMethods::IDEAL:
220
				$transaction->gateway = Methods::IDEAL;
221
222
				$issuer = $payment->get_issuer();
223
224
				if ( empty( $issuer ) ) {
225
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
226
				} else {
227
					$gateway_info = new GatewayInfo();
228
229
					$gateway_info->issuer_id = $issuer;
230
231
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
232
				}
233
234
				break;
235
			case PaymentMethods::CREDIT_CARD:
236
				$gateway = Methods::transform( $payment_method );
237
238
				$issuer = $payment->get_issuer();
239
240
				if ( empty( $issuer ) ) {
241
					if ( $gateway ) {
242
						$transaction->gateway = $gateway;
243
					}
244
				} else {
245
					$transaction->gateway = $issuer;
246
				}
247
248
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
249
250
				break;
251
			default:
252
				$gateway = Methods::transform( $payment_method );
253
254
				if ( $gateway ) {
255
					$transaction->gateway = $gateway;
256
				}
257
258
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
259
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
260
					$transaction->gateway = $payment_method;
261
				}
262
263
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
264
		}
265
266
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
267
268
		$message->signature = $signature;
269
270
		try {
271
			$response = $this->client->start_transaction( $message );
272
		} catch ( \Exception $e ) {
273
			$error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
274
275
			$this->set_error( $error );
276
277
			return;
278
		}
279
280
		if ( false !== $response ) {
281
			$transaction = $response->transaction;
282
283
			$payment->set_transaction_id( $transaction->id );
284
285
			if ( isset( $transaction->payment_url ) ) {
286
				$payment->set_action_url( $transaction->payment_url );
287
			}
288
289
			if ( isset( $response->gateway_info->redirect_url ) ) {
290
				$payment->set_action_url( $response->gateway_info->redirect_url );
291
			}
292
		}
293
	}
294
295
	/**
296
	 * Update status.
297
	 *
298
	 * @param Payment $payment Payment.
299
	 */
300
	public function update_status( Payment $payment ) {
301
		$merchant = new Merchant();
302
303
		$merchant->account          = $this->config->account_id;
304
		$merchant->site_id          = $this->config->site_id;
305
		$merchant->site_secure_code = $this->config->site_code;
306
307
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
308
309
		try {
310
			$result = $this->client->get_status( $message );
311
		} catch ( \Exception $e ) {
312
			$this->error = new \WP_Error( 'multisafepay_error', $e->getMessage() );
313
314
			return;
315
		}
316
317
		if ( false === $result ) {
318
			return;
319
		}
320
321
		// Status.
322
		$status = Statuses::transform( $result->ewallet->status );
323
324
		$payment->set_status( $status );
325
326
		// Consumer bank details.
327
		$consumer_bank_details = $payment->get_consumer_bank_details();
328
329
		if ( null === $consumer_bank_details ) {
330
			$consumer_bank_details = new BankAccountDetails();
331
332
			$payment->set_consumer_bank_details( $consumer_bank_details );
333
		}
334
335
		$consumer_bank_details->set_name( $result->payment_details->account_holder_name );
336
		$consumer_bank_details->set_iban( $result->payment_details->account_iban );
337
		$consumer_bank_details->set_bic( $result->payment_details->account_bic );
338
		$consumer_bank_details->set_account_number( $result->payment_details->account_id );
339
	}
340
}
341