Failed Conditions
Push — develop ( 9705a9...c29d28 )
by Reüel
03:41
created

src/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay;
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\XML\DirectTransactionRequestMessage;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
11
use Pronamic\WordPress\Pay\Payments\Payment;
12
13
/**
14
 * Title: MultiSafepay Connect gateay
15
 * Description:
16
 * Copyright: 2005-2020 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
	 * Client.
26
	 *
27
	 * @var Client
28
	 */
29
	protected $client;
30
31
	/**
32
	 * Config
33
	 *
34
	 * @var Config
35
	 */
36
	protected $config;
37
38
	/**
39
	 * Constructs and initializes an MultiSafepay Connect gateway
40
	 *
41
	 * @param Config $config Config.
42
	 */
43 1
	public function __construct( Config $config ) {
44 1
		parent::__construct( $config );
45
46 1
		$this->set_method( self::METHOD_HTTP_REDIRECT );
47
48
		// Supported features.
49 1
		$this->supports = array(
50
			'payment_status_request',
51
		);
52
53
		// Client.
54 1
		$this->client = new Client();
55
56 1
		$this->client->api_url = $config->api_url;
57 1
	}
58
59
	/**
60
	 * Get iDEAL issuers
61
	 *
62
	 * @see Core_Gateway::get_issuers()
63
	 * @since 1.2.0
64
	 */
65 1
	public function get_issuers() {
66 1
		$groups = array();
67
68
		// Merchant.
69 1
		$merchant                   = new Merchant();
70 1
		$merchant->account          = $this->config->account_id;
71 1
		$merchant->site_id          = $this->config->site_id;
72 1
		$merchant->site_secure_code = $this->config->site_code;
73
74 1
		$result = $this->client->get_ideal_issuers( $merchant );
75
76 1
		if ( $result ) {
77 1
			$groups[] = array(
78 1
				'options' => $result,
79
			);
80
		}
81
82 1
		return $groups;
83
	}
84
85
	/**
86
	 * Get credit card issuers
87
	 *
88
	 * @see Core_Gateway::get_credit_card_issuers()
89
	 */
90
	public function get_credit_card_issuers() {
91
		$groups[] = array(
92
			'options' => array(
93
				Methods::AMEX       => _x( 'AMEX', 'Payment method name', 'pronamic_ideal' ),
94
				Methods::MAESTRO    => _x( 'Maestro', 'Payment method name', 'pronamic_ideal' ),
95
				Methods::MASTERCARD => _x( 'MASTER', 'Payment method name', 'pronamic_ideal' ),
96
				Methods::VISA       => _x( 'VISA', 'Payment method name', 'pronamic_ideal' ),
97
			),
98
		);
99
100
		return $groups;
101
	}
102
103
	/**
104
	 * Get payment methods
105
	 *
106
	 * @see Pronamic_WP_Pay_Gateway::get_payment_methods()
107
	 */
108
	public function get_available_payment_methods() {
109
		$payment_methods = array();
110
111
		// Merchant.
112
		$merchant                   = new Merchant();
113
		$merchant->account          = $this->config->account_id;
114
		$merchant->site_id          = $this->config->site_id;
115
		$merchant->site_secure_code = $this->config->site_code;
116
117
		// Customer.
118
		$customer = new Customer();
119
120
		// Get gateways.
121
		$result = $this->client->get_gateways( $merchant, $customer );
122
123
		if ( ! $result ) {
124
			$this->error = $this->client->get_error();
0 ignored issues
show
The method get_error() does not exist on Pronamic\WordPress\Pay\G...ays\MultiSafepay\Client. ( Ignorable by Annotation )

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

124
			/** @scrutinizer ignore-call */ 
125
   $this->error = $this->client->get_error();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
126
			return $payment_methods;
127
		}
128
129
		foreach ( $result as $method => $title ) {
130
			$payment_method = Methods::transform_gateway_method( $method );
131
132
			if ( $payment_method ) {
133
				$payment_methods[] = $payment_method;
134
			}
135
		}
136
137
		return $payment_methods;
138
	}
139
140
	/**
141
	 * Get supported payment methods
142
	 *
143
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
144
	 */
145
	public function get_supported_payment_methods() {
146
		return array(
147
			PaymentMethods::ALIPAY,
148
			PaymentMethods::BANCONTACT,
149
			PaymentMethods::BANK_TRANSFER,
150
			PaymentMethods::BELFIUS,
151
			PaymentMethods::CREDIT_CARD,
152
			PaymentMethods::DIRECT_DEBIT,
153
			PaymentMethods::IDEAL,
154
			PaymentMethods::IDEALQR,
155
			PaymentMethods::GIROPAY,
156
			PaymentMethods::KBC,
157
			PaymentMethods::PAYPAL,
158
			PaymentMethods::SOFORT,
159
		);
160
	}
161
162
	/**
163
	 * Start payment.
164
	 *
165
	 * @param Payment $payment Payment object.
166
	 */
167
	public function start( Payment $payment ) {
168
		$payment_method = $payment->get_method();
169
170
		$transaction_description = $payment->get_description();
171
172
		if ( empty( $transaction_description ) ) {
173
			$transaction_description = $payment->get_id();
174
		}
175
176
		// Merchant.
177
		$merchant                   = new Merchant();
178
		$merchant->account          = $this->config->account_id;
179
		$merchant->site_id          = $this->config->site_id;
180
		$merchant->site_secure_code = $this->config->site_code;
181
		$merchant->notification_url = $payment->get_return_url();
182
		$merchant->redirect_url     = $payment->get_return_url();
183
		$merchant->cancel_url       = $payment->get_return_url();
184
		$merchant->close_window     = 'false';
185
186
		// Customer.
187
		$customer               = new Customer();
188
		$customer->ip_address   = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP );
189
		$customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
190
191
		if ( null !== $payment->get_customer() ) {
192
			$name = $payment->get_customer()->get_name();
193
194
			if ( null !== $name ) {
195
				$customer->first_name = $name->get_first_name();
196
				$customer->last_name  = $name->get_last_name();
197
			}
198
199
			$customer->locale = $payment->get_customer()->get_locale();
200
			$customer->email  = $payment->get_customer()->get_email();
201
		}
202
203
		// Transaction.
204
		$transaction              = new Transaction();
205
		$transaction->id          = uniqid();
206
		$transaction->currency    = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
207
		$transaction->amount      = $payment->get_total_amount()->get_cents();
208
		$transaction->description = $transaction_description;
209
210
		switch ( $payment_method ) {
211
			case PaymentMethods::IDEAL:
212
				$transaction->gateway = Methods::IDEAL;
213
214
				$issuer = $payment->get_issuer();
215
216
				if ( empty( $issuer ) ) {
217
					$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
218
				} else {
219
					$gateway_info = new GatewayInfo();
220
221
					$gateway_info->issuer_id = $issuer;
222
223
					$message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info );
224
				}
225
226
				break;
227
			case PaymentMethods::CREDIT_CARD:
228
				$gateway = Methods::transform( $payment_method );
229
230
				$issuer = $payment->get_issuer();
231
232
				if ( empty( $issuer ) ) {
233
					if ( $gateway ) {
234
						$transaction->gateway = $gateway;
235
					}
236
				} else {
237
					$transaction->gateway = $issuer;
238
				}
239
240
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
241
242
				break;
243
			default:
244
				$gateway = Methods::transform( $payment_method );
245
246
				if ( $gateway ) {
247
					$transaction->gateway = $gateway;
248
				}
249
250
				if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) {
251
					// Leap of faith if the WordPress payment method could not transform to a Mollie method?
252
					$transaction->gateway = $payment_method;
253
				}
254
255
				$message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction );
256
		}
257
258
		$signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id );
259
260
		$message->signature = $signature;
261
262
		$response = $this->client->start_transaction( $message );
263
264
		if ( $response ) {
265
			$transaction = $response->transaction;
266
267
			$payment->set_transaction_id( $transaction->id );
268
269
			if ( isset( $transaction->payment_url ) ) {
270
				$payment->set_action_url( $transaction->payment_url );
271
			}
272
273
			if ( isset( $response->gateway_info->redirect_url ) ) {
274
				$payment->set_action_url( $response->gateway_info->redirect_url );
275
			}
276
		} else {
277
			$this->error = $this->client->get_error();
278
		}
279
	}
280
281
	/**
282
	 * Update status.
283
	 *
284
	 * @param Payment $payment Payment.
285
	 */
286
	public function update_status( Payment $payment ) {
287
		$merchant = new Merchant();
288
289
		$merchant->account          = $this->config->account_id;
290
		$merchant->site_id          = $this->config->site_id;
291
		$merchant->site_secure_code = $this->config->site_code;
292
293
		$message = new StatusRequestMessage( $merchant, $payment->get_transaction_id() );
294
295
		$result = $this->client->get_status( $message );
296
297
		if ( $result ) {
298
			$status = Statuses::transform( $result->ewallet->status );
299
300
			$payment->set_status( $status );
301
			$payment->set_consumer_name( $result->payment_details->account_holder_name );
302
			$payment->set_consumer_iban( $result->payment_details->account_iban );
303
			$payment->set_consumer_bic( $result->payment_details->account_bic );
304
			$payment->set_consumer_account_number( $result->payment_details->account_id );
305
		} else {
306
			$this->error = $this->client->get_error();
307
		}
308
	}
309
}
310