Failed Conditions
Push — develop ( 22c93a...5b958e )
by Remco
04:02 queued 10s
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 = self::get_supported_features();
58
59
		// Client.
60 1
		$this->client = new Client();
61
62 1
		$this->client->api_url = $config->api_url;
63 1
	}
64
65
	/**
66
	 * Get supported features.
67
	 *
68
	 * @return array
69
	 */
70 1
	public static function get_supported_features() {
71
		return array(
72 1
			'payment_status_request',
73
		);
74
	}
75
76
	/**
77
	 * Get iDEAL issuers
78
	 *
79
	 * @see Core_Gateway::get_issuers()
80
	 * @since 1.2.0
81
	 */
82 1
	public function get_issuers() {
83 1
		$groups = array();
84
85
		// Merchant.
86 1
		$merchant                   = new Merchant();
87 1
		$merchant->account          = $this->config->account_id;
88 1
		$merchant->site_id          = $this->config->site_id;
89 1
		$merchant->site_secure_code = $this->config->site_code;
90
91 1
		$result = $this->client->get_ideal_issuers( $merchant );
92
93 1
		if ( $result ) {
94 1
			$groups[] = array(
95 1
				'options' => $result,
96
			);
97
		}
98
99 1
		return $groups;
100
	}
101
102
	/**
103
	 * Get credit card issuers
104
	 *
105
	 * @see Core_Gateway::get_credit_card_issuers()
106
	 */
107
	public function get_credit_card_issuers() {
108
		$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...
109
			'options' => array(
110
				Methods::AMEX       => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
111
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
112
				Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ),
113
				Methods::VISA       => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ),
114
			),
115
		);
116
117
		return $groups;
118
	}
119
120
	/**
121
	 * Get payment methods
122
	 *
123
	 * @see Pronamic_WP_Pay_Gateway::get_payment_methods()
124
	 */
125
	public function get_available_payment_methods() {
126
		$payment_methods = array();
127
128
		// Merchant.
129
		$merchant                   = new Merchant();
130
		$merchant->account          = $this->config->account_id;
131
		$merchant->site_id          = $this->config->site_id;
132
		$merchant->site_secure_code = $this->config->site_code;
133
134
		// Customer.
135
		$customer = new Customer();
136
137
		// Get gateways.
138
		$result = $this->client->get_gateways( $merchant, $customer );
139
140
		if ( ! $result ) {
141
			$this->error = $this->client->get_error();
142
143
			return $payment_methods;
144
		}
145
146
		foreach ( $result as $method => $title ) {
147
			$payment_method = Methods::transform_gateway_method( $method );
148
149
			if ( $payment_method ) {
150
				$payment_methods[] = $payment_method;
151
			}
152
		}
153
154
		return $payment_methods;
155
	}
156
157
	/**
158
	 * Get supported payment methods
159
	 *
160
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
161
	 */
162
	public function get_supported_payment_methods() {
163
		return array(
164
			PaymentMethods::ALIPAY,
165
			PaymentMethods::BANCONTACT,
166
			PaymentMethods::BANK_TRANSFER,
167
			PaymentMethods::BELFIUS,
168
			PaymentMethods::CREDIT_CARD,
169
			PaymentMethods::DIRECT_DEBIT,
170
			PaymentMethods::IDEAL,
171
			PaymentMethods::IDEALQR,
172
			PaymentMethods::GIROPAY,
173
			PaymentMethods::KBC,
174
			PaymentMethods::PAYPAL,
175
			PaymentMethods::SOFORT,
176
		);
177
	}
178
179
	/**
180
	 * Start payment.
181
	 *
182
	 * @param Payment $payment Payment object.
183
	 */
184
	public function start( Payment $payment ) {
185
		$payment_method = $payment->get_method();
186
187
		$transaction_description = $payment->get_description();
188
189
		if ( empty( $transaction_description ) ) {
190
			$transaction_description = $payment->get_id();
191
		}
192
193
		// Merchant.
194
		$merchant                   = new Merchant();
195
		$merchant->account          = $this->config->account_id;
196
		$merchant->site_id          = $this->config->site_id;
197
		$merchant->site_secure_code = $this->config->site_code;
198
		$merchant->notification_url = $payment->get_return_url();
199
		$merchant->redirect_url     = $payment->get_return_url();
200
		$merchant->cancel_url       = $payment->get_return_url();
201
		$merchant->close_window     = 'false';
202
203
		// Customer.
204
		$customer               = new Customer();
205
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
206
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
207
208
		if ( null !== $payment->get_customer() ) {
209
			$name = $payment->get_customer()->get_name();
210
211
			if ( null !== $name ) {
212
				$customer->first_name = $name->get_first_name();
213
				$customer->last_name  = $name->get_last_name();
214
			}
215
216
			$customer->locale = $payment->get_customer()->get_locale();
217
			$customer->email  = $payment->get_customer()->get_email();
218
		}
219
220
		// Transaction.
221
		$transaction              = new Transaction();
222
		$transaction->id          = uniqid();
223
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
224
		$transaction->amount      = $payment->get_total_amount()->get_cents();
225
		$transaction->description = $transaction_description;
226
227
		switch ( $payment_method ) {
228
			case PaymentMethods::IDEAL:
229
				$transaction->gateway = Methods::IDEAL;
230
231
				$issuer = $payment->get_issuer();
232
233
				if ( empty( $issuer ) ) {
234
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
235
				} else {
236
					$gateway_info = new GatewayInfo();
237
238
					$gateway_info->issuer_id = $issuer;
239
240
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
241
				}
242
243
				break;
244
			case PaymentMethods::CREDIT_CARD:
245
				$gateway = Methods::transform( $payment_method );
246
247
				$issuer = $payment->get_issuer();
248
249
				if ( empty( $issuer ) ) {
250
					if ( $gateway ) {
251
						$transaction->gateway = $gateway;
252
					}
253
				} else {
254
					$transaction->gateway = $issuer;
255
				}
256
257
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
258
259
				break;
260
			default:
261
				$gateway = Methods::transform( $payment_method );
262
263
				if ( $gateway ) {
264
					$transaction->gateway = $gateway;
265
				}
266
267
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
268
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
269
					$transaction->gateway = $payment_method;
270
				}
271
272
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
273
		}
274
275
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
276
277
		$message->signature = $signature;
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
Bug introduced by
The property signature does not seem to exist on Pronamic\WordPress\Pay\G...ansactionRequestMessage.
Loading history...
278
279
		$response = $this->client->start_transaction( $message );
0 ignored issues
show
Bug introduced by
$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

279
		$response = $this->client->start_transaction( /** @scrutinizer ignore-type */ $message );
Loading history...
280
281
		if ( $response ) {
282
			$transaction = $response->transaction;
283
284
			$payment->set_transaction_id( $transaction->id );
0 ignored issues
show
Bug introduced by
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
Bug introduced by
The property gateway_info does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
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
		} else {
294
			$this->error = $this->client->get_error();
295
		}
296
	}
297
298
	/**
299
	 * Update status.
300
	 *
301
	 * @param Payment $payment Payment.
302
	 */
303
	public function update_status( Payment $payment ) {
304
		$merchant = new Merchant();
305
306
		$merchant->account          = $this->config->account_id;
307
		$merchant->site_id          = $this->config->site_id;
308
		$merchant->site_secure_code = $this->config->site_code;
309
310
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
311
312
		$result = $this->client->get_status( $message );
0 ignored issues
show
Bug introduced by
$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

312
		$result = $this->client->get_status( /** @scrutinizer ignore-type */ $message );
Loading history...
313
314
		if ( $result ) {
315
			$status = Statuses::transform( $result->ewallet->status );
0 ignored issues
show
Bug introduced by
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property ewallet does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
316
317
			$payment->set_status( $status );
318
			$payment->set_consumer_name( $result->payment_details->account_holder_name );
0 ignored issues
show
Bug introduced by
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property payment_details does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
319
			$payment->set_consumer_iban( $result->payment_details->account_iban );
320
			$payment->set_consumer_bic( $result->payment_details->account_bic );
321
			$payment->set_consumer_account_number( $result->payment_details->account_id );
322
		} else {
323
			$this->error = $this->client->get_error();
324
		}
325
	}
326
}
327