Failed Conditions
Push — develop ( ef0a62...79532f )
by Remco
03:16
created

src/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Core\Util;
17
use Pronamic\WordPress\Pay\Payments\Payment;
18
use Pronamic\WordPress\Pay\Plugin;
19
20
/**
21
 * Gateway
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 * @link    https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Slug of this gateway.
31
	 *
32
	 * @var string
33
	 */
34
	const SLUG = 'adyen';
35
36
	/**
37
	 * Web SDK version.
38
	 *
39
	 * @link https://docs.adyen.com/developers/checkout/web-sdk/release-notes-web-sdk
40
	 *
41
	 * @var string
42
	 */
43
	const SDK_VERSION = '1.9.2';
44
45
	/**
46
	 * Client.
47
	 *
48
	 * @var Client
49
	 */
50
	protected $client;
51
52
	/**
53
	 * Constructs and initializes an Adyen gateway.
54
	 *
55
	 * @param Config $config Config.
56
	 */
57
	public function __construct( Config $config ) {
58
		parent::__construct( $config );
59
60
		$this->set_method( self::METHOD_HTTP_REDIRECT );
61
		$this->set_slug( self::SLUG );
62
63
		$this->client = new Client( $config );
64
	}
65
66
	/**
67
	 * Get supported payment methods
68
	 *
69
	 * @see Core_Gateway::get_supported_payment_methods()
70
	 */
71
	public function get_supported_payment_methods() {
72
		return array(
73
			PaymentMethods::BANCONTACT,
74
			PaymentMethods::CREDIT_CARD,
75
			PaymentMethods::DIRECT_DEBIT,
76
			PaymentMethods::GIROPAY,
77
			PaymentMethods::IDEAL,
78
			PaymentMethods::MAESTRO,
79
			PaymentMethods::SOFORT,
80
		);
81
	}
82
83
	/**
84
	 * Start.
85
	 *
86
	 * @param Payment $payment Payment.
87
	 *
88
	 * @see Plugin::start()
89
	 */
90
	public function start( Payment $payment ) {
91
		// Amount.
92
		$amount = AmountTransformer::transform( $payment->get_total_amount() );
93
94
		// Payment method. Take leap of faith for unknown payment methods.
95
		$payment_method_type = PaymentMethodType::transform( $payment->get_method() );
96
97
		// Country.
98
		$locale = get_locale();
99
100
		if ( null !== $payment->get_customer() ) {
101
			$locale = $payment->get_customer()->get_locale();
102
		}
103
104
		$locale = explode( '_', $locale );
105
106
		$country_code = strtoupper( substr( $locale[1], 0, 2 ) );
107
108
		// Create payment or payment session request.
109
		switch ( $payment->get_method() ) {
110
			case PaymentMethods::IDEAL:
111
			case PaymentMethods::SOFORT:
112
				$payment_method = new PaymentMethod( $payment_method_type );
113
114
				switch ( $payment->get_method() ) {
115
					case PaymentMethods::IDEAL:
116
						$payment_method->issuer = $payment->get_issuer();
117
118
						break;
119
				}
120
121
				// API integration.
122
				$payment_request = new PaymentRequest(
123
					$amount,
124
					$this->config->get_merchant_account(),
125
					$payment->get_id(),
126
					$payment->get_return_url(),
127
					$payment_method
128
				);
129
130
				$payment_request->set_country_code( $country_code );
131
132
				$payment_request = PaymentRequestTransformer::transform( $payment, $payment_request );
133
134
				$payment_response = $this->client->create_payment( $payment_request );
135
136
				$payment->set_transaction_id( $payment_response->get_psp_reference() );
137
138
				$redirect = $payment_response->get_redirect();
139
140
				if ( null !== $redirect ) {
141
					$payment->set_action_url( $redirect->get_url() );
142
				}
143
144
				break;
145
			default:
146
				// Web SDK integration.
147
				$payment_session_request = new PaymentSessionRequest(
148
					$amount,
149
					$this->config->get_merchant_account(),
150
					$payment->get_id(),
151
					$payment->get_return_url(),
152
					$country_code
153
				);
154
155
				$payment_session_request = PaymentRequestTransformer::transform( $payment, $payment_session_request );
156
157
				$payment_session_request->set_origin( home_url() );
158
				$payment_session_request->set_sdk_version( self::SDK_VERSION );
159
160
				if ( null !== $payment_method_type ) {
161
					$payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) );
162
				}
163
164
				$payment_session_response = $this->client->create_payment_session( $payment_session_request );
165
166
				$payment->set_action_url( $payment->get_pay_redirect_url() );
167
168
				$payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION );
169
				$payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() );
170
		}
171
	}
172
173
	/**
174
	 * Payment redirect.
175
	 *
176
	 * @param Payment $payment Payment.
177
	 *
178
	 * @return void
179
	 */
180
	public function payment_redirect( Payment $payment ) {
181
		$sdk_version     = $payment->get_meta( 'adyen_sdk_version' );
182
		$payment_session = $payment->get_meta( 'adyen_payment_session' );
183
184
		if ( empty( $sdk_version ) || empty( $payment_session ) ) {
185
			return;
186
		}
187
188
		$url = sprintf(
189
			'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js',
190
			( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
191
			$sdk_version
192
		);
193
194
		wp_register_script(
195
			'pronamic-pay-adyen-checkout',
196
			$url,
197
			array(),
198
			$sdk_version,
199
			false
200
		);
201
202
		wp_localize_script(
203
			'pronamic-pay-adyen-checkout',
204
			'pronamicPayAdyenCheckout',
205
			array(
206
				'paymentReturnUrl' => $payment->get_return_url(),
207
				'paymentSession'   => $payment_session,
208
				'configObject'     => array(
209
					'context' => ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ),
210
				),
211
			)
212
		);
213
214
		// No cache.
215
		Util::no_cache();
216
217
		require __DIR__ . '/../views/checkout.php';
218
219
		exit;
220
	}
221
222
	/**
223
	 * Update status of the specified payment.
224
	 *
225
	 * @param Payment $payment Payment.
226
	 *
227
	 * @return void
228
	 */
229
	public function update_status( Payment $payment ) {
230
		// Process payload on return.
231
		if ( ! filter_has_var( INPUT_GET, 'payload' ) ) {
232
			return;
233
		}
234
235
		$status = null;
236
237
		$payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING );
238
239
		switch ( $payment->get_method() ) {
240
			case PaymentMethods::IDEAL:
241
			case PaymentMethods::SOFORT:
242
				$result = $this->client->get_payment_details( $payload );
243
244
				break;
245
			default:
246
				$result = $this->client->get_payment_result( $payload );
247
		}
248
249
		if ( $result ) {
250
			$status = ResultCode::transform( $result->resultCode );
251
252
			$psp_reference = $result->pspReference;
253
		}
254
255
		// Handle errors.
256
		if ( empty( $status ) ) {
257
			$payment->set_status( Core_Statuses::FAILURE );
258
259
			$this->error = $this->client->get_error();
0 ignored issues
show
The method get_error() does not exist on Pronamic\WordPress\Pay\Gateways\Adyen\Client. ( Ignorable by Annotation )

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

259
			/** @scrutinizer ignore-call */ 
260
   $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...
260
261
			return;
262
		}
263
264
		// Update status.
265
		$payment->set_status( $status );
266
267
		// Update transaction ID.
268
		if ( isset( $psp_reference ) ) {
269
			$payment->set_transaction_id( $psp_reference );
270
		}
271
	}
272
273
	/**
274
	 * Get available payment methods.
275
	 *
276
	 * @see Core_Gateway::get_available_payment_methods()
277
	 */
278
	public function get_available_payment_methods() {
279
		$payment_methods = array();
280
281
		// Get active payment methods for Adyen account.
282
		$methods = $this->client->get_payment_methods();
283
284
		if ( ! $methods ) {
285
			$this->error = $this->client->get_error();
286
287
			return $payment_methods;
288
		}
289
290
		// Transform to WordPress payment methods.
291
		foreach ( $methods as $method => $details ) {
292
			$payment_method = PaymentMethodType::transform_gateway_method( $method );
293
294
			if ( $payment_method ) {
295
				$payment_methods[] = $payment_method;
296
			}
297
		}
298
299
		$payment_methods = array_unique( $payment_methods );
300
301
		return $payment_methods;
302
	}
303
304
	/**
305
	 * Get issuers.
306
	 *
307
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
308
	 */
309
	public function get_issuers() {
310
		$groups = array();
311
312
		$payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL );
313
314
		$result = $this->client->get_issuers( $payment_method );
315
316
		if ( ! $result ) {
317
			$this->error = $this->client->get_error();
318
319
			return $groups;
320
		}
321
322
		$groups[] = array(
323
			'options' => $result,
324
		);
325
326
		return $groups;
327
	}
328
}
329