Failed Conditions
Push — develop ( 93bfb6...fd928b )
by Remco
03:31
created

src/Connect/Gateway.php (12 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Core\Server;
8
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\DirectTransactionRequestMessage;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\RedirectTransactionRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\StatusRequestMessage;
11
use Pronamic\WordPress\Pay\Payments\Payment;
12
13
/**
14
 * Title: MultiSafepay Connect gateay
15
 * Description:
16
 * Copyright: 2005-2019 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.0.3
21
 * @since   1.0.1
22
 */
23
class Gateway extends Core_Gateway {
24
	/**
25
	 * Slug of this gateway
26
	 *
27
	 * @var string
28
	 */
29
	const SLUG = 'multisafepay-connect';
30
31
	/**
32
	 * Client.
33
	 *
34
	 * @var Client
35
	 */
36
	protected $client;
37
38
	/**
39
	 * Config
40
	 *
41
	 * @var Config
42
	 */
43
	protected $config;
44
45
	/**
46
	 * Constructs and initializes an MultiSafepay Connect gateway
47
	 *
48
	 * @param Config $config Config.
49
	 */
50 1
	public function __construct( Config $config ) {
51 1
		parent::__construct( $config );
52
53 1
		$this->set_method( self::METHOD_HTTP_REDIRECT );
54 1
		$this->set_slug( self::SLUG );
55
56
		// Supported features.
57 1
		$this->supports = array(
58
			'payment_status_request',
59
		);
60
61
		// Client.
62 1
		$this->client = new Client();
63
64 1
		$this->client->api_url = $config->api_url;
65 1
	}
66
67
	/**
68
	 * Get iDEAL issuers
69
	 *
70
	 * @see Core_Gateway::get_issuers()
71
	 * @since 1.2.0
72
	 */
73 1
	public function get_issuers() {
74 1
		$groups = array();
75
76
		// Merchant.
77 1
		$merchant                   = new Merchant();
78 1
		$merchant->account          = $this->config->account_id;
79 1
		$merchant->site_id          = $this->config->site_id;
80 1
		$merchant->site_secure_code = $this->config->site_code;
81
82 1
		$result = $this->client->get_ideal_issuers( $merchant );
83
84 1
		if ( $result ) {
85 1
			$groups[] = array(
86 1
				'options' => $result,
87
			);
88
		}
89
90 1
		return $groups;
91
	}
92
93
	/**
94
	 * Get credit card issuers
95
	 *
96
	 * @see Core_Gateway::get_credit_card_issuers()
97
	 */
98
	public function get_credit_card_issuers() {
99
		$groups[] = array(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$groups was never initialized. Although not strictly required by PHP, it is generally a good practice to add $groups = array(); before regardless.
Loading history...
100
			'options' => array(
101
				Methods::AMEX       => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
102
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
103
				Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ),
104
				Methods::VISA       => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ),
105
			),
106
		);
107
108
		return $groups;
109
	}
110
111
	/**
112
	 * Get payment methods
113
	 *
114
	 * @see Pronamic_WP_Pay_Gateway::get_payment_methods()
115
	 */
116
	public function get_available_payment_methods() {
117
		$payment_methods = array();
118
119
		// Merchant.
120
		$merchant                   = new Merchant();
121
		$merchant->account          = $this->config->account_id;
122
		$merchant->site_id          = $this->config->site_id;
123
		$merchant->site_secure_code = $this->config->site_code;
124
125
		// Customer.
126
		$customer = new Customer();
127
128
		// Get gateways.
129
		$result = $this->client->get_gateways( $merchant, $customer );
130
131
		if ( ! $result ) {
132
			$this->error = $this->client->get_error();
133
134
			return $payment_methods;
135
		}
136
137
		foreach ( $result as $method => $title ) {
138
			$payment_method = Methods::transform_gateway_method( $method );
139
140
			if ( $payment_method ) {
141
				$payment_methods[] = $payment_method;
142
			}
143
		}
144
145
		return $payment_methods;
146
	}
147
148
	/**
149
	 * Get supported payment methods
150
	 *
151
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
152
	 */
153
	public function get_supported_payment_methods() {
154
		return array(
155
			PaymentMethods::ALIPAY,
156
			PaymentMethods::BANCONTACT,
157
			PaymentMethods::BANK_TRANSFER,
158
			PaymentMethods::BELFIUS,
159
			PaymentMethods::CREDIT_CARD,
160
			PaymentMethods::DIRECT_DEBIT,
161
			PaymentMethods::IDEAL,
162
			PaymentMethods::IDEALQR,
163
			PaymentMethods::GIROPAY,
164
			PaymentMethods::KBC,
165
			PaymentMethods::PAYPAL,
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;
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...
269
270
		$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

270
		$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
271
272
		if ( $response ) {
273
			$transaction = $response->transaction;
274
275
			$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...
276
277
			if ( isset( $transaction->payment_url ) ) {
278
				$payment->set_action_url( $transaction->payment_url );
279
			}
280
281
			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...
282
				$payment->set_action_url( $response->gateway_info->redirect_url );
283
			}
284
		} else {
285
			$this->error = $this->client->get_error();
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
		$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...ct\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

303
		$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
304
305
		if ( $result ) {
306
			$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...
307
308
			$payment->set_status( $status );
309
			$payment->set_consumer_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...
310
			$payment->set_consumer_iban( $result->payment_details->account_iban );
311
			$payment->set_consumer_bic( $result->payment_details->account_bic );
312
			$payment->set_consumer_account_number( $result->payment_details->account_id );
313
		} else {
314
			$this->error = $this->client->get_error();
315
		}
316
	}
317
}
318