Test Failed
Push — develop ( 6098b1...78195a )
by Reüel
02:46
created

src/Connect/Gateway.php (20 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
	public function __construct( Config $config ) {
51
		parent::__construct( $config );
52
53
		$this->supports = array(
54
			'payment_status_request',
55
		);
56
57
		$this->set_method( self::METHOD_HTTP_REDIRECT );
58
		$this->set_slug( self::SLUG );
59
60
		$this->client = new Client();
61
62
		$this->client->api_url = $config->api_url;
63
	}
64
65
	/**
66
	 * Get iDEAL issuers
67
	 *
68
	 * @see Core_Gateway::get_issuers()
69
	 * @since 1.2.0
70
	 */
71
	public function get_issuers() {
72
		$groups = array();
73
74
		// Merchant.
75
		$merchant                   = new Merchant();
76
		$merchant->account          = $this->config->account_id;
77
		$merchant->site_id          = $this->config->site_id;
78
		$merchant->site_secure_code = $this->config->site_code;
79
80
		$result = $this->client->get_ideal_issuers( $merchant );
81
82
		if ( $result ) {
83
			$groups[] = array(
84
				'options' => $result,
85
			);
86
		}
87
88
		return $groups;
89
	}
90
91
	/**
92
	 * Get credit card issuers
93
	 *
94
	 * @see Core_Gateway::get_credit_card_issuers()
95
	 */
96
	public function get_credit_card_issuers() {
97
		$groups[] = array(
98
			'options' => array(
99
				Methods::AMEX       => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
0 ignored issues
show
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

99
				Methods::AMEX       => /** @scrutinizer ignore-call */ _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
Loading history...
100
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
101
				Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ),
102
				Methods::VISA       => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ),
103
			),
104
		);
105
106
		return $groups;
107
	}
108
109
	/**
110
	 * Get payment methods
111
	 *
112
	 * @see Pronamic_WP_Pay_Gateway::get_payment_methods()
113
	 */
114
	public function get_available_payment_methods() {
115
		$payment_methods = array();
116
117
		// Merchant.
118
		$merchant                   = new Merchant();
119
		$merchant->account          = $this->config->account_id;
120
		$merchant->site_id          = $this->config->site_id;
121
		$merchant->site_secure_code = $this->config->site_code;
122
123
		// Customer.
124
		$customer = new Customer();
125
126
		// Get gateways.
127
		$result = $this->client->get_gateways( $merchant, $customer );
128
129
		if ( ! $result ) {
130
			$this->error = $this->client->get_error();
131
132
			return $payment_methods;
133
		}
134
135
		foreach ( $result as $method => $title ) {
136
			$payment_method = Methods::transform_gateway_method( $method );
137
138
			if ( $payment_method ) {
139
				$payment_methods[] = $payment_method;
140
			}
141
		}
142
143
		return $payment_methods;
144
	}
145
146
	/**
147
	 * Get supported payment methods
148
	 *
149
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
150
	 */
151
	public function get_supported_payment_methods() {
152
		return array(
153
			PaymentMethods::ALIPAY,
154
			PaymentMethods::BANCONTACT,
155
			PaymentMethods::BANK_TRANSFER,
156
			PaymentMethods::BELFIUS,
157
			PaymentMethods::CREDIT_CARD,
158
			PaymentMethods::DIRECT_DEBIT,
159
			PaymentMethods::IDEAL,
160
			PaymentMethods::IDEALQR,
161
			PaymentMethods::GIROPAY,
162
			PaymentMethods::KBC,
163
			PaymentMethods::PAYPAL,
164
			PaymentMethods::SOFORT,
165
		);
166
	}
167
168
	/**
169
	 * Start payment.
170
	 *
171
	 * @param Payment $payment Payment object.
172
	 */
173
	public function start( Payment $payment ) {
174
		$payment_method = $payment->get_method();
175
176
		$transaction_description = $payment->get_description();
177
178
		if ( empty( $transaction_description ) ) {
179
			$transaction_description = $payment->get_id();
180
		}
181
182
		// Merchant.
183
		$merchant                   = new Merchant();
184
		$merchant->account          = $this->config->account_id;
185
		$merchant->site_id          = $this->config->site_id;
186
		$merchant->site_secure_code = $this->config->site_code;
187
		$merchant->notification_url = $payment->get_return_url();
188
		$merchant->redirect_url     = $payment->get_return_url();
189
		$merchant->cancel_url       = $payment->get_return_url();
190
		$merchant->close_window     = 'false';
191
192
		// Customer.
193
		$customer               = new Customer();
194
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
195
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
196
197
		if ( null !== $payment->get_customer() ) {
198
			$name = $payment->get_customer()->get_name();
199
200
			if ( null !== $name ) {
201
				$customer->first_name = $name->get_first_name();
202
				$customer->last_name  = $name->get_last_name();
203
			}
204
205
			$customer->locale = $payment->get_customer()->get_locale();
206
			$customer->email  = $payment->get_customer()->get_email();
207
		}
208
209
		// Transaction.
210
		$transaction              = new Transaction();
211
		$transaction->id          = uniqid();
212
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
213
		$transaction->amount      = $payment->get_total_amount()->get_cents();
214
		$transaction->description = $transaction_description;
215
216
		switch ( $payment_method ) {
217
			case PaymentMethods::IDEAL:
218
				$transaction->gateway = Methods::IDEAL;
219
220
				$issuer = $payment->get_issuer();
221
222
				if ( empty( $issuer ) ) {
223
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
224
				} else {
225
					$gateway_info = new GatewayInfo();
226
227
					$gateway_info->issuer_id = $issuer;
228
229
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
230
				}
231
232
				break;
233
			case PaymentMethods::CREDIT_CARD:
234
				$gateway = Methods::transform( $payment_method );
235
236
				$issuer = $payment->get_issuer();
237
238
				if ( empty( $issuer ) ) {
239
					if ( $gateway ) {
240
						$transaction->gateway = $gateway;
241
					}
242
				} else {
243
					$transaction->gateway = $issuer;
244
				}
245
246
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
247
248
				break;
249
			default:
250
				$gateway = Methods::transform( $payment_method );
251
252
				if ( $gateway ) {
253
					$transaction->gateway = $gateway;
254
				}
255
256
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
257
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
258
					$transaction->gateway = $payment_method;
259
				}
260
261
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
262
		}
263
264
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
265
266
		$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...
267
268
		$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

268
		$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
269
270
		if ( $response ) {
271
			$transaction = $response->transaction;
0 ignored issues
show
The property transaction does not seem to exist on Pronamic\WordPress\Pay\G...lIssuersResponseMessage.
Loading history...
The property transaction does not seem to exist on Pronamic\WordPress\Pay\G...GatewaysResponseMessage.
Loading history...
272
273
			$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...
274
275
			if ( isset( $transaction->payment_url ) ) {
276
				$payment->set_action_url( $transaction->payment_url );
277
			}
278
279
			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...
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...lIssuersResponseMessage.
Loading history...
The property gateway_info does not exist on Pronamic\WordPress\Pay\G...GatewaysResponseMessage. Did you mean gateways?
Loading history...
280
				$payment->set_action_url( $response->gateway_info->redirect_url );
281
			}
282
		} else {
283
			$this->error = $this->client->get_error();
284
		}
285
	}
286
287
	/**
288
	 * Update status.
289
	 *
290
	 * @param Payment $payment Payment.
291
	 */
292
	public function update_status( Payment $payment ) {
293
		$merchant = new Merchant();
294
295
		$merchant->account          = $this->config->account_id;
296
		$merchant->site_id          = $this->config->site_id;
297
		$merchant->site_secure_code = $this->config->site_code;
298
299
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
300
301
		$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

301
		$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
302
303
		if ( $result ) {
304
			$status = Statuses::transform( $result->ewallet->status );
0 ignored issues
show
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...GatewaysResponseMessage.
Loading history...
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...lIssuersResponseMessage.
Loading history...
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...
305
306
			$payment->set_status( $status );
307
			$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...lIssuersResponseMessage.
Loading history...
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...GatewaysResponseMessage.
Loading history...
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
308
			$payment->set_consumer_iban( $result->payment_details->account_iban );
309
			$payment->set_consumer_bic( $result->payment_details->account_bic );
310
			$payment->set_consumer_account_number( $result->payment_details->account_id );
311
		} else {
312
			$this->error = $this->client->get_error();
313
		}
314
	}
315
}
316