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 | * Client. |
||
38 | * |
||
39 | * @var Client |
||
40 | */ |
||
41 | protected $client; |
||
42 | |||
43 | /** |
||
44 | * Constructs and initializes an Adyen gateway. |
||
45 | * |
||
46 | * @param Config $config Config. |
||
47 | */ |
||
48 | public function __construct( Config $config ) { |
||
49 | parent::__construct( $config ); |
||
50 | |||
51 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
52 | $this->set_slug( self::SLUG ); |
||
53 | |||
54 | $this->client = new Client( $config->api_key, $config->api_live_url_prefix ); |
||
55 | $this->client->set_merchant_account( $config->merchant_account ); |
||
56 | $this->client->set_mode( $config->mode ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get supported payment methods |
||
61 | * |
||
62 | * @see Core_Gateway::get_supported_payment_methods() |
||
63 | */ |
||
64 | public function get_supported_payment_methods() { |
||
65 | return array( |
||
66 | PaymentMethods::BANCONTACT, |
||
67 | PaymentMethods::CREDIT_CARD, |
||
68 | PaymentMethods::DIRECT_DEBIT, |
||
69 | PaymentMethods::GIROPAY, |
||
70 | PaymentMethods::IDEAL, |
||
71 | PaymentMethods::MAESTRO, |
||
72 | PaymentMethods::SOFORT, |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Start. |
||
78 | * |
||
79 | * @param Payment $payment Payment. |
||
80 | * |
||
81 | * @see Plugin::start() |
||
82 | */ |
||
83 | public function start( Payment $payment ) { |
||
84 | // Payment request. |
||
85 | $request = new PaymentRequest(); |
||
86 | |||
87 | $request->merchant_account = $this->config->merchant_account; |
||
88 | $request->return_url = $payment->get_return_url(); |
||
89 | $request->reference = $payment->get_id(); |
||
90 | $request->origin_url = home_url(); |
||
91 | $request->sdk_version = '1.6.3'; |
||
92 | $request->channel = 'Web'; |
||
93 | |||
94 | // Amount. |
||
95 | $request->currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code(); |
||
96 | $request->amount_value = $payment->get_total_amount()->get_cents(); // @todo Money +get_minor_units(). |
||
97 | |||
98 | // Payment method. Take leap of faith for unknown payment method types. |
||
99 | $adyen_method = PaymentMethodType::transform( $payment->get_method(), $payment->get_method() ); |
||
100 | |||
101 | $request->payment_method = array( |
||
102 | 'type' => $adyen_method, |
||
103 | ); |
||
104 | |||
105 | switch ( $payment->get_method() ) { |
||
106 | case PaymentMethods::IDEAL: |
||
107 | $request->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 | $request->country_code = strtoupper( substr( $locale[1], 0, 2 ) ); |
||
122 | |||
123 | // Shopper. |
||
124 | $request->shopper_statement = $payment->get_description(); |
||
125 | |||
126 | if ( null !== $payment->get_customer() ) { |
||
127 | $request->shopper_ip = $payment->get_customer()->get_ip_address(); |
||
128 | $request->shopper_gender = $payment->get_customer()->get_gender(); |
||
129 | $request->shopper_locale = $payment->get_customer()->get_locale(); |
||
130 | $request->shopper_reference = $payment->get_customer()->get_user_id(); |
||
131 | $request->shopper_telephone_number = $payment->get_customer()->get_phone(); |
||
132 | |||
133 | if ( null !== $payment->get_customer()->get_name() ) { |
||
134 | $request->shopper_first_name = $payment->get_customer()->get_name()->get_first_name(); |
||
135 | $request->shopper_name_infix = $payment->get_customer()->get_name()->get_middle_name(); |
||
136 | $request->shopper_last_name = $payment->get_customer()->get_name()->get_last_name(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | // Create payment or payment session. |
||
141 | switch ( $payment->get_method() ) { |
||
142 | case PaymentMethods::IDEAL: |
||
143 | case PaymentMethods::SOFORT: |
||
144 | // API integration. |
||
145 | $result = $this->client->create_payment( $request ); |
||
146 | |||
147 | break; |
||
148 | default: |
||
149 | // Web SDK integration. |
||
150 | $allowed_methods = array( $adyen_method ); |
||
151 | |||
152 | // Add all available payment methods if no payment method is given. |
||
153 | if ( empty( $adyen_method ) ) { |
||
154 | $allowed_methods = array(); |
||
155 | |||
156 | foreach ( $this->get_available_payment_methods() as $method ) { |
||
157 | $allowed_methods[] = PaymentMethodType::transform( $method ); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Set allowed payment methods. |
||
162 | $request->allowed_payment_methods = $allowed_methods; |
||
163 | |||
164 | // Create payment session. |
||
165 | $result = $this->client->create_payment_session( $request ); |
||
166 | } |
||
167 | |||
168 | if ( ! $result ) { |
||
169 | $this->error = $this->client->get_error(); |
||
170 | |||
171 | return; |
||
172 | } |
||
173 | |||
174 | if ( isset( $result->paymentSession ) ) { |
||
175 | // No cache. |
||
176 | Util::no_cache(); |
||
177 | |||
178 | $redirect_message = '<div id="pronamic-pay-checkout"></div><div style="clear:both;"></div>'; |
||
179 | |||
180 | include Plugin::$dirname . '/views/redirect-message.php'; |
||
181 | |||
182 | ?> |
||
183 | |||
184 | <script type="text/javascript" src="https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.6.3.min.js"></script> |
||
185 | |||
186 | <script type="text/javascript"> |
||
187 | // Initiate the Adyen Checkout form. |
||
188 | var checkout = chckt.checkout( |
||
189 | '<?php echo $result->paymentSession; ?>', |
||
190 | '#pronamic-pay-checkout', |
||
191 | { context: '<?php echo( self::MODE_TEST === $this->config->mode ? 'test' : 'live' ); ?>' } |
||
192 | ); |
||
193 | |||
194 | // Redirect once payment completes. |
||
195 | chckt.hooks.beforeComplete = function ( node, paymentData ) { |
||
196 | if ( "undefined" !== paymentData.payload ) { |
||
197 | console.log( paymentData ); |
||
198 | |||
199 | window.location.href = '<?php echo $payment->get_return_url(); ?>&payload=' + encodeURIComponent( paymentData.payload ); |
||
200 | |||
201 | return false; |
||
202 | } |
||
203 | }; |
||
204 | </script> |
||
205 | |||
206 | <?php |
||
207 | |||
208 | exit; |
||
209 | } |
||
210 | |||
211 | // Set transaction ID. |
||
212 | if ( isset( $result->pspReference ) ) { |
||
213 | $payment->set_transaction_id( $result->pspReference ); |
||
214 | } |
||
215 | |||
216 | // Set redirect URL. |
||
217 | if ( isset( $result->redirect->url ) ) { |
||
218 | $payment->set_action_url( $result->redirect->url ); |
||
219 | } |
||
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 | $status = null; |
||
231 | |||
232 | if ( filter_has_var( INPUT_GET, 'payload' ) ) { |
||
233 | $payload = filter_input( INPUT_GET, 'payload' ); |
||
234 | |||
235 | switch ( $payment->get_method() ) { |
||
236 | case PaymentMethods::IDEAL: |
||
237 | case PaymentMethods::SOFORT: |
||
238 | $result = $this->client->get_payment_details( $payload ); |
||
239 | |||
240 | break; |
||
241 | default: |
||
242 | $result = $this->client->get_payment_result( $payload ); |
||
243 | } |
||
244 | |||
245 | if ( $result ) { |
||
246 | $status = Statuses::transform( $result->resultCode ); |
||
247 | |||
248 | $psp_reference = $result->pspReference; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | if ( empty( $status ) ) { |
||
253 | $payment->set_status( Core_Statuses::FAILURE ); |
||
254 | |||
255 | $this->error = $this->client->get_error(); |
||
256 | |||
257 | return; |
||
258 | } |
||
259 | |||
260 | $payment->set_status( $status ); |
||
261 | |||
262 | if ( isset( $psp_reference ) ) { |
||
263 | $payment->set_transaction_id( $psp_reference ); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | if ( isset( $payment_details->pspReference ) ) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
268 | $payment->set_transaction_id( $payment_details->pspReference ); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Get available payment methods. |
||
274 | * |
||
275 | * @see Core_Gateway::get_available_payment_methods() |
||
276 | */ |
||
277 | public function get_available_payment_methods() { |
||
278 | $payment_methods = array(); |
||
279 | |||
280 | // Get active payment methods for Adyen account. |
||
281 | $methods = $this->client->get_payment_methods(); |
||
282 | |||
283 | if ( ! $methods ) { |
||
284 | $this->error = $this->client->get_error(); |
||
285 | |||
286 | return $payment_methods; |
||
287 | } |
||
288 | |||
289 | // Transform to WordPress payment methods. |
||
290 | foreach ( $methods as $method => $details ) { |
||
291 | $payment_method = PaymentMethodType::transform_gateway_method( $method ); |
||
292 | |||
293 | if ( $payment_method ) { |
||
294 | $payment_methods[] = $payment_method; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | $payment_methods = array_unique( $payment_methods ); |
||
299 | |||
300 | return $payment_methods; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Get issuers. |
||
305 | * |
||
306 | * @see Pronamic_WP_Pay_Gateway::get_issuers() |
||
307 | */ |
||
308 | public function get_issuers() { |
||
309 | $groups = array(); |
||
310 | |||
311 | $payment_method = PaymentMethodType::transform( PaymentMethods::IDEAL ); |
||
312 | |||
313 | $result = $this->client->get_issuers( $payment_method ); |
||
314 | |||
315 | if ( ! $result ) { |
||
316 | $this->error = $this->client->get_error(); |
||
317 | |||
318 | return $groups; |
||
319 | } |
||
320 | |||
321 | $groups[] = array( |
||
322 | 'options' => $result, |
||
323 | ); |
||
324 | |||
325 | return $groups; |
||
326 | } |
||
327 | } |
||
328 |