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 = new Amount( |
||
93 | $payment->get_total_amount()->get_currency()->get_alphabetic_code(), |
||
94 | $payment->get_total_amount()->get_minor_units() |
||
95 | ); |
||
96 | |||
97 | // Payment method. Take leap of faith for unknown payment methods. |
||
98 | $type = PaymentMethodType::transform( |
||
99 | $payment->get_method(), |
||
100 | $payment->get_method() |
||
101 | ); |
||
102 | |||
103 | $payment_method = new PaymentMethod( $type ); |
||
104 | |||
105 | switch ( $payment->get_method() ) { |
||
106 | case PaymentMethods::IDEAL: |
||
107 | $payment_method->issuer = $payment->get_issuer(); |
||
108 | |||
109 | break; |
||
110 | } |
||
111 | |||
112 | // Country. |
||
113 | $locale = get_locale(); |
||
114 | |||
115 | if ( null !== $payment->get_customer() ) { |
||
116 | $locale = $payment->get_customer()->get_locale(); |
||
117 | } |
||
118 | |||
119 | $locale = explode( '_', $locale ); |
||
120 | |||
121 | $country_code = strtoupper( substr( $locale[1], 0, 2 ) ); |
||
122 | |||
123 | // Create payment or payment session request. |
||
124 | switch ( $payment->get_method() ) { |
||
125 | case PaymentMethods::IDEAL: |
||
126 | case PaymentMethods::SOFORT: |
||
127 | // API integration. |
||
128 | $request = new PaymentRequest( |
||
129 | $amount, |
||
130 | $this->config->merchant_account, |
||
131 | $payment->get_id(), |
||
132 | $payment->get_return_url(), |
||
133 | $payment_method |
||
134 | ); |
||
135 | |||
136 | $request->set_country_code( $country_code ); |
||
137 | |||
138 | break; |
||
139 | default: |
||
140 | // Web SDK integration. |
||
141 | $request = new PaymentSessionRequest( |
||
142 | $amount, |
||
143 | $this->config->merchant_account, |
||
144 | $payment->get_id(), |
||
145 | $payment->get_return_url(), |
||
146 | $country_code |
||
147 | ); |
||
148 | |||
149 | $request->set_origin( home_url() ); |
||
150 | $request->set_sdk_version( self::SDK_VERSION ); |
||
151 | |||
152 | // Set allowed payment methods. |
||
153 | $allowed_methods = array( $type ); |
||
154 | |||
155 | // Add all available payment methods if no payment method is given. |
||
156 | if ( empty( $type ) ) { |
||
157 | $allowed_methods = array(); |
||
158 | |||
159 | foreach ( $this->get_available_payment_methods() as $method ) { |
||
160 | $allowed_methods[] = PaymentMethodType::transform( $method ); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | $request->set_allowed_payment_methods( $allowed_methods ); |
||
165 | } |
||
166 | |||
167 | $request = PaymentRequestTransformer::transform( $payment, $request ); |
||
168 | |||
169 | // Create payment or payment session. |
||
170 | if ( $request instanceof PaymentRequest ) { |
||
171 | $result = $this->client->create_payment( $request ); |
||
172 | } else { |
||
173 | $result = $this->client->create_payment_session( $request ); |
||
174 | } |
||
175 | |||
176 | // Handle errors. |
||
177 | if ( ! $result ) { |
||
178 | $this->error = $this->client->get_error(); |
||
179 | |||
180 | return; |
||
181 | } |
||
182 | |||
183 | // Set checkout meta for Web SDK redirect. |
||
184 | if ( isset( $result->paymentSession ) ) { |
||
185 | $payment->set_meta( |
||
186 | 'adyen_checkout', |
||
187 | array( |
||
188 | 'sdk_version' => self::SDK_VERSION, |
||
189 | 'payload' => $result->paymentSession, |
||
190 | ) |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | // Set transaction ID. |
||
195 | if ( isset( $result->pspReference ) ) { |
||
196 | $payment->set_transaction_id( $result->pspReference ); |
||
197 | } |
||
198 | |||
199 | // Set action URL. |
||
200 | $action_url = $payment->get_pay_redirect_url(); |
||
201 | |||
202 | if ( isset( $result->redirect->url ) ) { |
||
203 | $action_url = $result->redirect->url; |
||
204 | } |
||
205 | |||
206 | $payment->set_action_url( $action_url ); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Payment redirect. |
||
211 | * |
||
212 | * @param Payment $payment Payment. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function payment_redirect( Payment $payment ) { |
||
217 | $checkout = $payment->get_meta( 'adyen_checkout' ); |
||
218 | |||
219 | if ( empty( $checkout ) ) { |
||
220 | return; |
||
221 | } |
||
222 | |||
223 | $url = sprintf( |
||
224 | 'https://checkoutshopper-%s.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.%s.min.js', |
||
225 | ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ), |
||
226 | $checkout['sdk_version'] |
||
227 | ); |
||
228 | |||
229 | wp_register_script( |
||
230 | 'pronamic-pay-adyen-checkout', |
||
231 | $url, |
||
232 | array(), |
||
233 | $checkout['sdk_version'], |
||
234 | false |
||
235 | ); |
||
236 | |||
237 | // No cache. |
||
238 | Util::no_cache(); |
||
239 | |||
240 | $context = ( self::MODE_TEST === $payment->get_mode() ? 'test' : 'live' ); |
||
241 | |||
242 | require __DIR__ . '/../views/checkout.php'; |
||
243 | |||
244 | exit; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Update status of the specified payment. |
||
249 | * |
||
250 | * @param Payment $payment Payment. |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | public function update_status( Payment $payment ) { |
||
255 | // Process payload on return. |
||
256 | if ( ! filter_has_var( INPUT_GET, 'payload' ) ) { |
||
257 | return; |
||
258 | } |
||
259 | |||
260 | $status = null; |
||
261 | |||
262 | $payload = filter_input( INPUT_GET, 'payload', FILTER_SANITIZE_STRING ); |
||
263 | |||
264 | switch ( $payment->get_method() ) { |
||
265 | case PaymentMethods::IDEAL: |
||
266 | case PaymentMethods::SOFORT: |
||
267 | $result = $this->client->get_payment_details( $payload ); |
||
268 | |||
269 | break; |
||
270 | default: |
||
271 | $result = $this->client->get_payment_result( $payload ); |
||
272 | } |
||
273 | |||
274 | if ( $result ) { |
||
275 | $status = ResultCode::transform( $result->resultCode ); |
||
276 | |||
277 | $psp_reference = $result->pspReference; |
||
278 | } |
||
279 | |||
280 | // Handle errors. |
||
281 | if ( empty( $status ) ) { |
||
282 | $payment->set_status( Core_Statuses::FAILURE ); |
||
283 | |||
284 | $this->error = $this->client->get_error(); |
||
0 ignored issues
–
show
|
|||
285 | |||
286 | return; |
||
287 | } |
||
288 | |||
289 | // Update status. |
||
290 | $payment->set_status( $status ); |
||
291 | |||
292 | // Update transaction ID. |
||
293 | if ( isset( $psp_reference ) ) { |
||
294 | $payment->set_transaction_id( $psp_reference ); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get available payment methods. |
||
300 | * |
||
301 | * @see Core_Gateway::get_available_payment_methods() |
||
302 | */ |
||
303 | public function get_available_payment_methods() { |
||
304 | $payment_methods = array(); |
||
305 | |||
306 | // Get active payment methods for Adyen account. |
||
307 | $methods = $this->client->get_payment_methods(); |
||
308 | |||
309 | if ( ! $methods ) { |
||
310 | $this->error = $this->client->get_error(); |
||
311 | |||
312 | return $payment_methods; |
||
313 | } |
||
314 | |||
315 | // Transform to WordPress payment methods. |
||
316 | foreach ( $methods as $method => $details ) { |
||
317 | $payment_method = PaymentMethodType::transform_gateway_method( $method ); |
||
318 | |||
319 | if ( $payment_method ) { |
||
320 | $payment_methods[] = $payment_method; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | $payment_methods = array_unique( $payment_methods ); |
||
325 | |||
326 | return $payment_methods; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Get issuers. |
||
331 | * |
||
332 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
333 | */ |
||
334 | public function get_issuers() { |
||
335 | $groups = array(); |
||
336 | |||
337 | $payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL ); |
||
338 | |||
339 | $result = $this->client->get_issuers( $payment_method ); |
||
340 | |||
341 | if ( ! $result ) { |
||
342 | $this->error = $this->client->get_error(); |
||
343 | |||
344 | return $groups; |
||
345 | } |
||
346 | |||
347 | $groups[] = array( |
||
348 | 'options' => $result, |
||
349 | ); |
||
350 | |||
351 | return $groups; |
||
352 | } |
||
353 | } |
||
354 |
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.