PluginProbe
PayPlug for WooCommerce (Official) / 1.2.1
PayPlug for WooCommerce (Official) v1.2.1
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugGateway.php

PayplugGateway.php in PayPlug for WooCommerce (Official) 1.2.1, at src/Gateway/PayplugGateway.php

1,442 lines 51.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Gateway;
4
5 // Exit if accessed directly
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 use Payplug\Authentication;
11 use Payplug\Exception\ConfigurationException;
12 use Payplug\Exception\HttpException;
13 use Payplug\Payplug;
14 use Payplug\PayplugWoocommerce\Admin\Ajax;
15 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
16 use Payplug\Resource\Payment as PaymentResource;
17 use Payplug\Resource\Refund as RefundResource;
18 use WC_Payment_Gateway_CC;
19 use WC_Payment_Tokens;
20
21 /**
22 * PayPlug WooCommerce Gateway.
23 *
24 * @package Payplug\PayplugWoocommerce\Gateway
25 */
26 class PayplugGateway extends WC_Payment_Gateway_CC
27 {
28
29 /**
30 * @var PayplugGatewayRequirements
31 */
32 private $requirements;
33
34 /**
35 * @var PayplugPermissions
36 */
37 private $permissions;
38
39 /**
40 * @var PayplugResponse
41 */
42 public $response;
43
44 /**
45 * @var PayplugApi
46 */
47 public $api;
48
49 /**
50 * @var \WC_Logger
51 */
52 protected static $log;
53
54 /**
55 * @var bool
56 */
57 protected static $log_enabled;
58
59 /**
60 * @var float
61 */
62 const MIN_AMOUNT = 0.99;
63
64 /**
65 * @var float
66 */
67 const MAX_AMOUNT = 20000;
68
69 /**
70 * Logging method.
71 *
72 * @param string $message Log message.
73 * @param string $level Optional. Default 'info'.
74 * emergency|alert|critical|error|warning|notice|info|debug
75 */
76 public static function log($message, $level = 'info')
77 {
78 if (!self::$log_enabled) {
79 return;
80 }
81
82 if (empty(self::$log)) {
83 self::$log = PayplugWoocommerceHelper::is_pre_30() ? new \WC_Logger() : wc_get_logger();
84 }
85
86 PayplugWoocommerceHelper::is_pre_30()
87 ? self::$log->add('payplug_gateway', $message)
88 : self::$log->log($level, $message, array('source' => 'payplug_gateway'));
89 }
90
91 public function __construct()
92 {
93 $this->id = 'payplug';
94 $this->icon = '';
95 $this->has_fields = false;
96 $this->method_title = _x('PayPlug', 'Gateway method title', 'payplug');
97 $this->method_description = __('Enable PayPlug for your customers.', 'payplug');
98 $this->supports = array(
99 'products',
100 'refunds',
101 'tokenization',
102 );
103 $this->new_method_label = __('Pay with another credit card', 'payplug');
104
105 $this->init_settings();
106 $this->requirements = new PayplugGatewayRequirements($this);
107 if ($this->user_logged_in()) {
108 $this->init_payplug();
109 }
110 $this->init_form_fields();
111
112 $this->title = $this->get_option('title');
113 $this->description = $this->get_option('description');
114 $this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
115 $this->debug = 'yes' === $this->get_option('debug', 'no');
116 $this->email = $this->get_option('email');
117 $this->payment_method = $this->get_option('payment_method');
118 $this->oneclick = 'yes' === $this->get_option('oneclick', 'no');
119
120 add_filter('woocommerce_get_customer_payment_tokens', [$this, 'filter_tokens'], 10, 3);
121
122 self::$log_enabled = $this->debug;
123
124 // Ensure the description is not empty to correctly display users's save cards
125 if (empty($this->description) && 0 !== count($this->get_tokens())) {
126 $this->description = ' ';
127 }
128
129 if ('test' === $this->mode) {
130 $this->description .= " \n";
131
132 $this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug');
133 $this->description = trim($this->description);
134 }
135
136 add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
137 add_action('wp_enqueue_scripts', [$this, 'scripts']);
138 add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
139 add_action('the_post', [$this, 'validate_payment']);
140 add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
141 }
142
143 /**
144 * Customize gateway title in emails.
145 *
146 * @param array $total_rows
147 * @param \WC_Order $order
148 *
149 * @return array
150 *
151 * @author Clément Boirie
152 */
153 public function customize_gateway_title($total_rows, $order)
154 {
155
156 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
157 if (
158 $this->id !== $payment_method
159 || !isset($total_rows['payment_method'])
160 ) {
161 return $total_rows;
162 }
163
164 $total_rows['payment_method']['value'] = __('Credit card', 'payplug');
165
166 return $total_rows;
167 }
168
169 /**
170 * Validate order payment when the user is redirected to the success confirmation page.
171 *
172 * @throws \WC_Data_Exception
173 */
174 public function validate_payment()
175 {
176 if (!is_wc_endpoint_url('order-received') || empty($_GET['key'])) {
177 return;
178 }
179
180 $order_id = wc_get_order_id_by_order_key(wc_clean($_GET['key']));
181 if (empty($order_id)) {
182 return;
183 }
184
185 $order = wc_get_order($order_id);
186 if (!$order instanceof \WC_Order) {
187 return;
188 }
189
190 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
191 if ('payplug' !== $payment_method) {
192 return;
193 }
194
195 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
196 if (empty($transaction_id)) {
197 PayplugGateway::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
198
199 return;
200 }
201
202 try {
203 $payment = $this->api->payment_retrieve($transaction_id);
204 } catch (\Exception $e) {
205 PayplugGateway::log(
206 sprintf(
207 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
208 $order_id,
209 $e->getMessage()
210 )
211 );
212
213 return;
214 }
215
216 $this->response->process_payment($payment);
217 }
218
219 /**
220 * Get payment icons.
221 *
222 * @return string
223 */
224 public function get_icon()
225 {
226
227 $src = ('it_IT' === get_locale())
228 ? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_PostePay.svg'
229 : PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_CB.svg';
230
231 $icons = apply_filters('payplug_payment_icons', [
232 'payplug' => sprintf('<img src="%s" alt="Visa & Mastercard" class="payplug-payment-icon" />', esc_url($src)),
233 ]);
234
235 $icons_str = '';
236 foreach ($icons as $icon) {
237 $icons_str .= $icon;
238 }
239
240 return $icons_str;
241 }
242
243 /**
244 * Check if this gateway is enabled
245 */
246 public function is_available()
247 {
248 if ('yes' === $this->enabled) {
249 return $this->requirements->satisfy_requirements() && !empty($this->get_api_key($this->get_current_mode()));
250 }
251
252 return parent::is_available();
253 }
254
255 /**
256 * Load gateway settings.
257 */
258 public function init_settings()
259 {
260 parent::init_settings();
261 $this->enabled = !empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
262 }
263
264 /**
265 * Register gateway settings.
266 */
267 public function init_form_fields()
268 {
269 $fields = [
270 'enabled' => [
271 'title' => __('Enable/Disable', 'payplug'),
272 'type' => 'checkbox',
273 'label' => __('Enable PayPlug', 'payplug'),
274 'description' => __('Only Euro payments can be processed with PayPlug.', 'payplug'),
275 'default' => 'no',
276 ],
277 'title' => [
278 'title' => __('Title', 'payplug'),
279 'type' => 'text',
280 'description' => __('The payment solution title displayed during checkout.', 'payplug'),
281 'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
282 'desc_tip' => true,
283 ],
284 'description' => [
285 'title' => __('Description', 'payplug'),
286 'type' => 'text',
287 'description' => __('The payment solution description displayed during checkout.', 'payplug'),
288 'default' => '',
289 'desc_tip' => true,
290 ],
291 'title_connexion' => [
292 'title' => __('Connection', 'payplug'),
293 'type' => 'title',
294 ],
295 'email' => [
296 'type' => 'hidden',
297 'default' => '',
298 ],
299 'login' => [
300 'type' => 'login',
301 'default' => '',
302 ],
303 'payplug_test_key' => [
304 'type' => 'hidden',
305 'default' => '',
306 ],
307 'payplug_live_key' => [
308 'type' => 'hidden',
309 'default' => '',
310 ],
311 'payplug_merchant_id' => [
312 'type' => 'hidden',
313 'default' => '',
314 ],
315 'title_testmode' => [
316 'title' => __('Mode', 'payplug'),
317 'type' => 'title',
318 ],
319 'mode' => [
320 'title' => '',
321 'label' => '',
322 'type' => 'yes_no',
323 'yes' => 'Live',
324 'no' => 'Test',
325 'description' => __('In TEST mode, all payments will be simulations and will not generate real transactions.', 'payplug'),
326 'default' => 'no',
327 'hide_label' => true,
328 ],
329 'title_settings' => [
330 'title' => __('Settings', 'payplug'),
331 'type' => 'title',
332 ],
333 'payment_method' => [
334 'title' => __('Payment page', 'payplug'),
335 'type' => 'radio',
336 'description' => __('Customers will be redirected to a PayPlug payment page to finalize the transaction, or payments will be performed in an embeddable payment form on your website.', 'payplug'),
337 'default' => 'redirect',
338 'desc_tip' => true,
339 'options' => array(
340 'redirect' => __('Redirect', 'payplug'),
341 'embedded' => __('Integrated', 'payplug'),
342 ),
343 ],
344 'debug' => [
345 'title' => __('Debug', 'payplug'),
346 'type' => 'checkbox',
347 'description' => __('Debug mode saves additional information on your server for each operation done via the PayPlug plugin (Developer setting).', 'payplug'),
348 'label' => __('Activate debug mode', 'payplug'),
349 'default' => 'yes',
350 'desc_tip' => true,
351 ],
352 'title_advanced_settings' => [
353 'title' => __('Advanced Settings', 'payplug'),
354 'description' => __(
355 'Your current offer does not allow this option. Try it on TEST mode. More information <a href="https://www.payplug.com/pricing" target="_blank">here.</a>',
356 'payplug'
357 ),
358 'type' => 'title',
359 ],
360 'oneclick' => [
361 'title' => __('One Click Payment', 'payplug'),
362 'type' => 'checkbox',
363 'label' => __('Activate', 'payplug'),
364 'description' => __('Allow your customers to save their credit card information for later purchases.', 'payplug'),
365 'default' => 'no',
366 'desc_tip' => true
367 ],
368 ];
369
370 if ($this->user_logged_in() && !$this->permissions->has_permissions(PayplugPermissions::SAVE_CARD) && 'live' === $this->get_current_mode()) {
371 $fields['oneclick']['disabled'] = true;
372 }
373
374 /**
375 * Filter PayPlug gateway settings.
376 *
377 * @param array $fields
378 */
379 $fields = apply_filters('payplug_gateway_settings', $fields);
380 $this->form_fields = $fields;
381 }
382
383 /**
384 * Set global configuration for PayPlug instance.
385 */
386 public function init_payplug()
387 {
388 $this->api = new PayplugApi($this);
389 $this->api->init();
390
391 $this->permissions = new PayplugPermissions($this);
392 $this->response = new PayplugResponse($this);
393
394 // Register IPN handler
395 new PayplugIpnResponse($this);
396 }
397
398 /**
399 * Embedded payment form scripts.
400 *
401 * Register scripts and additionnal data needed for the
402 * embedded payment form.
403 */
404 public function scripts()
405 {
406 if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) {
407 return;
408 }
409
410 // If PayPlug is not enabled bail.
411 if ('no' === $this->enabled) {
412 return;
413 }
414
415 // If keys are not set bail.
416 if (empty($this->get_api_key($this->mode))) {
417 PayplugGateway::log('Keys are not set correctly.');
418
419 return;
420 }
421
422 // Register checkout styles.
423 wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION);
424 wp_enqueue_style('payplug-checkout');
425
426 wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
427 wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [
428 'jquery',
429 'payplug'
430 ], PAYPLUG_GATEWAY_VERSION, true);
431 wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
432 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
433 'nonce' => [
434 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
435 ],
436 'is_embedded' => 'redirect' !== $this->payment_method
437 ]);
438 wp_enqueue_script('payplug-checkout');
439 }
440
441 /**
442 * Filter saved tokens for the gateway.
443 *
444 * A token will be removed if :
445 * - it doesn't match the current merchant logged in,
446 * - or it doesn't match the current gateway mode,
447 * - or it is expired.
448 *
449 * @param array $tokens
450 * @param int $user_id
451 * @param string $gateway_id
452 *
453 * @return array
454 */
455 public function filter_tokens($tokens, $user_id, $gateway_id)
456 {
457
458 if (!is_user_logged_in() || !class_exists('WC_Payment_Gateway_CC')) {
459 return $tokens;
460 }
461
462 /* @var \WC_Payment_Token_CC $token */
463 foreach ($tokens as $k => $token) {
464
465 if ($this->id !== $token->get_gateway_id()) {
466 continue;
467 }
468
469 // check if token is associated with a merchant id and if it match the current one
470 $token_merchant_id = $token->get_meta('payplug_account', true);
471 if (empty($token_merchant_id) || $this->get_merchant_id() !== $token_merchant_id) {
472 unset($tokens[$k]);
473 continue;
474 }
475
476 // check if token is available for the current gateway mode
477 if ($this->mode !== $token->get_meta('mode', true)) {
478 unset($tokens[$k]);
479 continue;
480 }
481
482 // check if token is not expired
483 $current_month = \absint(date('n'));
484 $current_year = \absint(date('Y'));
485 if ($current_year > (int) $token->get_expiry_year()) {
486 unset($tokens[$k]);
487 continue;
488 }
489
490 if ($current_year === (int) $token->get_expiry_year() && $current_month >= (int) $token->get_expiry_month()) {
491 unset($tokens[$k]);
492 continue;
493 }
494 }
495
496 return $tokens;
497 }
498
499 public function payment_fields()
500 {
501 $description = $this->get_description();
502 if (!empty($description)) {
503 echo wpautop(wptexturize($description));
504 }
505
506 if ($this->oneclick_available()) {
507 $this->tokenization_script();
508 $this->saved_payment_methods();
509 }
510 }
511
512 /**
513 * Handle admin display.
514 */
515 public function admin_options()
516 {
517 wp_enqueue_style(
518 'payplug-gateway-style',
519 PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/app.css',
520 [],
521 PAYPLUG_GATEWAY_VERSION
522 );
523
524 wp_enqueue_script(
525 'payplug-gateway-admin',
526 PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin.js',
527 ['jquery-ui-dialog'],
528 PAYPLUG_GATEWAY_VERSION
529 );
530
531 wp_localize_script('payplug-gateway-admin', 'payplug_admin_config', array(
532 'ajax_url' => admin_url('admin-ajax.php'),
533 'has_live_key' => (false === $this->has_api_key('live')) ? false : true,
534 'btn_ok' => _x('Ok', 'modal', 'payplug'),
535 'btn_label' => _x('Cancel', 'modal', 'payplug'),
536 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'),
537 ));
538
539 if ($this->user_logged_in() && false === $this->has_api_key('live')) {
540
541 add_action('admin_footer', function () {
542 $email = $this->get_option('email');
543 ?>
544 <div id="payplug-refresh-keys-modal" title="<?php echo esc_attr_x('Mode LIVE', 'modal', 'payplug'); ?>">
545 <form id="payplug-refresh-keys-modal__form">
546 <p id="dialog-msg"></p>
547 <p><?php echo esc_html_x('Please enter your PayPlug account password', 'modal', 'payplug'); ?></p>
548 <input type="password" name="password" required title="<?php echo esc_attr_x('Enter your PayPlug account password', 'modal', 'payplug'); ?>" />
549 <input type="hidden" name="email" value="<?php echo esc_attr($email); ?>">
550 <input type="hidden" name="action" value="<?php echo esc_attr(Ajax::REFRESH_KEY_ACTION); ?>">
551 <?php wp_nonce_field(sprintf('%s_%s', $email, Ajax::REFRESH_KEY_ACTION)); ?>
552 <input class="ui-dialog-sronly" type="submit" tabindex="-1">
553 </form>
554 </div>
555 <?php
556 });
557 }
558
559 $payplug_requirements = new PayplugGatewayRequirements($this); ?>
560
561 <h2 class="title--logo"><?php esc_html($this->get_method_title()) ?></h2>
562 <p><?php _e(sprintf('Version %s', PAYPLUG_GATEWAY_VERSION)); ?></p>
563 <div class="payplug-requirements">
564 <?php echo $payplug_requirements->curl_requirement(); ?>
565 <?php echo $payplug_requirements->php_requirement(); ?>
566 <?php echo $payplug_requirements->openssl_requirement(); ?>
567 <?php echo $payplug_requirements->account_requirement(); ?>
568 <?php echo $payplug_requirements->currency_requirement(); ?>
569 </div>
570 <?php echo wp_kses_post(wpautop($this->get_method_description())); ?>
571
572 <?php if ($this->user_logged_in()) : ?>
573 <table class="form-table">
574 <?php $this->generate_settings_html($this->get_form_fields()); ?>
575 </table>
576 <?php else :
577 $GLOBALS['hide_save_button'] = true; ?>
578 <h3 class="wc-settings-sub-title"><?php _e('Connection', 'payplug'); ?></h3>
579 <table class="form-table">
580 <tbody>
581 <tr valign="top">
582 <th scope="row" class="titledesc">
583 <label for="payplug_email"><?php _e('Email', 'payplug'); ?></label>
584 </th>
585 <td class="forminp">
586 <fieldset>
587 <legend class="screen-reader-text"><span><?php _e('Email', 'payplug'); ?></span></legend>
588 <input class="input-text regular-input" type="text" name="payplug_email" id="payplug_email" value="" placeholder="<?php _e('your@email.com', 'payplug'); ?>" />
589 </fieldset>
590 </td>
591 </tr>
592 <tr valign="top">
593 <th scope="row" class="titledesc">
594 <label for="payplug_password"><?php _e('Password', 'payplug'); ?></label>
595 </th>
596 <td class="forminp">
597 <fieldset>
598 <legend class="screen-reader-text"><span><?php _e('Password', 'payplug'); ?></span>
599 </legend>
600 <input class="input-text regular-input" type="password" name="payplug_password" id="payplug_password" value="" />
601 </fieldset>
602 </td>
603 </tr>
604 <tr valign="top">
605 <td class="forminp">
606 <input class="button" type="submit" value="<?php _e('Login', 'payplug'); ?>">
607 <input type="hidden" name="save" value="login">
608 <?php wp_nonce_field('payplug_user_login', '_loginaction'); ?>
609 </td>
610 </tr>
611 </tbody>
612 </table>
613 <?php
614 endif;
615 }
616
617 /**
618 * Process admin options.
619 *
620 * @return bool
621 */
622 public function process_admin_options()
623 {
624 $data = $this->get_post_data();
625
626 // Handle logout process
627 if (
628 isset($data['submit_logout'])
629 && false !== check_admin_referer('payplug_user_logout', '_logoutaction')
630 ) {
631
632 if ($this->permissions) {
633 $this->permissions->clear_permissions();
634 }
635
636 $data = get_option($this->get_option_key());
637 $data['payplug_test_key'] = '';
638 $data['payplug_live_key'] = '';
639 $data['payplug_merchant_id'] = '';
640 $data['enabled'] = 'no';
641 $data['mode'] = 'no';
642 $data['oneclick'] = 'no';
643 update_option(
644 $this->get_option_key(),
645 apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
646 );
647 \WC_Admin_Settings::add_message(__('Successfully logged out.', 'payplug'));
648
649 return true;
650 }
651
652 // Handle login process
653 if (
654 isset($data['payplug_email'])
655 && false !== check_admin_referer('payplug_user_login', '_loginaction')
656 ) {
657 $email = $data['payplug_email'];
658 $password = wp_unslash($data['payplug_password']);
659 $response = $this->retrieve_user_api_keys($email, $password);
660 if (is_wp_error($response)) {
661 \WC_Admin_Settings::add_error($response->get_error_message());
662
663 return false;
664 }
665
666 // try to use the api keys to retrieve the merchant id
667 $merchant_id = isset($response['test']) ? $this->retrieve_merchant_id($response['test']) : '';
668
669 $this->init_form_fields();
670 $fields = $this->get_form_fields();
671 $data = [];
672
673 // Load existing values if the user is re-login.
674 foreach ($fields as $key => $field) {
675 if (in_array($field['type'], ['title', 'login'])) {
676 continue;
677 }
678
679 switch ($key) {
680 case 'enabled':
681 $val = 'yes';
682 break;
683 case 'mode':
684 $val = 'no';
685 break;
686 case 'payplug_test_key':
687 $val = esc_attr($response['test']);
688 break;
689 case 'payplug_live_key':
690 $val = esc_attr($response['live']);
691 break;
692 case 'payplug_merchant_id':
693 $val = esc_attr($merchant_id);
694 break;
695 case 'email':
696 $val = esc_html($email);
697 break;
698 default:
699 $val = $this->get_option($key);
700 }
701
702 $data[$key] = $val;
703 }
704
705 $this->set_post_data($data);
706 update_option(
707 $this->get_option_key(),
708 apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
709 );
710 \WC_Admin_Settings::add_message(__('Successfully logged in.', 'payplug'));
711
712 return true;
713 }
714
715 // Don't let user without live key leave TEST mode.
716 $mode_fieldkey = $this->get_field_key('mode');
717 $live_key_fieldkey = $this->get_field_key('payplug_live_key');
718 if (isset($data[$mode_fieldkey]) && '1' === $data[$mode_fieldkey] && empty($data[$live_key_fieldkey])) {
719 $data[$mode_fieldkey] = '0';
720 $this->set_post_data($data);
721 \WC_Admin_Settings::add_error(__('Your account does not support LIVE mode at the moment, it must be validated first. If your account has already been validated, please log out and log in again.', 'payplug'));
722 }
723
724 // Check user permissions before activating one-click feature.
725 $oneclick_fieldkey = $this->get_field_key('oneclick');
726 if (
727 isset($data[$oneclick_fieldkey])
728 && '1' === $data[$oneclick_fieldkey]
729 && (!$this->user_logged_in()
730 || false === $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD))
731 ) {
732 $data[$oneclick_fieldkey] = '0';
733 \WC_Admin_Settings::add_error(__('Only PREMIUM accounts can enable the One Click option in LIVE mode.', 'payplug'));
734 }
735
736 parent::process_admin_options();
737 }
738
739 /**
740 * Process payment.
741 *
742 * @param int $order_id
743 *
744 * @return array
745 * @throws \Exception
746 */
747 public function process_payment($order_id)
748 {
749
750 PayplugGateway::log(sprintf('Processing payment for order #%s', $order_id));
751
752 $order = wc_get_order($order_id);
753 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
754 $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total());
755 $amount = $this->validate_order_amount($amount);
756 if (is_wp_error($amount)) {
757 PayplugGateway::log(sprintf('Invalid amount %s for the order.', $order->get_total()), 'error');
758 throw new \Exception($amount->get_error_message());
759 }
760
761 $payment_token_id = (isset($_POST['wc-' . $this->id . '-payment-token']) && 'new' !== $_POST['wc-' . $this->id . '-payment-token'])
762 ? wc_clean($_POST['wc-' . $this->id . '-payment-token'])
763 : false;
764
765 if ($payment_token_id && $this->oneclick_available() && (int) $customer_id > 0) {
766 PayplugGateway::log(sprintf('Payment token found.', $amount));
767
768 return $this->process_payment_with_token($order, $amount, $customer_id, $payment_token_id);
769 }
770
771 return $this->process_standard_payment($order, $amount, $customer_id);
772 }
773
774 /**
775 * @param \WC_Order $order
776 * @param int $amount
777 * @param int $customer_id
778 *
779 * @return array
780 * @throws \Exception
781 */
782 public function process_standard_payment($order, $amount, $customer_id)
783 {
784
785 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
786
787 try {
788 $address_data = PayplugAddressData::from_order($order);
789
790 $payment_data = [
791 'amount' => $amount,
792 'currency' => get_woocommerce_currency(),
793 'allow_save_card' => $this->oneclick_available() && (int) $customer_id > 0,
794 'billing' => $address_data->get_billing(),
795 'shipping' => $address_data->get_shipping(),
796 'hosted_payment' => [
797 'return_url' => esc_url_raw($order->get_checkout_order_received_url()),
798 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
799 ],
800 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
801 'metadata' => [
802 'order_id' => $order_id,
803 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
804 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
805 ],
806 ];
807
808 /**
809 * Filter the payment data before it's used
810 *
811 * @param array $payment_data
812 * @param int $order_id
813 * @param array $customer_details
814 * @param PayplugAddressData $address_data
815 */
816 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
817 $payment = $this->api->payment_create($payment_data);
818
819 // Save transaction id for the order
820 PayplugWoocommerceHelper::is_pre_30()
821 ? update_post_meta($order_id, '_transaction_id', $payment->id)
822 : $order->set_transaction_id($payment->id);
823
824 if (is_callable([$order, 'save'])) {
825 $order->save();
826 }
827
828 /**
829 * Fires once a payment has been created.
830 *
831 * @param int $order_id Order ID
832 * @param PaymentResource $payment Payment resource
833 */
834 \do_action('payplug_gateway_payment_created', $order_id, $payment);
835
836 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
837 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
838
839 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
840
841 return [
842 'result' => 'success',
843 'redirect' => $payment->hosted_payment->payment_url,
844 'cancel' => $payment->hosted_payment->cancel_url,
845 ];
846 } catch (HttpException $e) {
847 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
848 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
849 } catch (\Exception $e) {
850 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
851 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
852 }
853 }
854
855 /**
856 * @param \WC_Order $order
857 * @param int $amount
858 * @param int $customer_id
859 * @param string $token_id
860 *
861 * @return array
862 * @throws \Exception
863 */
864 public function process_payment_with_token($order, $amount, $customer_id, $token_id)
865 {
866
867 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
868 $payment_token = WC_Payment_Tokens::get($token_id);
869 if (!$payment_token || (int) $customer_id !== (int) $payment_token->get_user_id()) {
870 PayplugGateway::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error');
871 throw new \Exception(__('Invalid payment method.', 'payplug'));
872 }
873
874 try {
875 $address_data = PayplugAddressData::from_order($order);
876
877 $payment_data = [
878 'amount' => $amount,
879 'currency' => get_woocommerce_currency(),
880 'payment_method' => $payment_token->get_token(),
881 'allow_save_card' => false,
882 'billing' => $address_data->get_billing(),
883 'shipping' => $address_data->get_shipping(),
884 'initiator' => 'PAYER',
885 'hosted_payment' => [
886 'return_url' => esc_url_raw($order->get_checkout_order_received_url()),
887 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
888 ],
889 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
890 'metadata' => [
891 'order_id' => $order_id,
892 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
893 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
894 ],
895 ];
896
897 /** This filter is documented in src/Gateway/PayplugGateway */
898 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
899 $payment = $this->api->payment_create($payment_data);
900
901 /** This action is documented in src/Gateway/PayplugGateway */
902 \do_action('payplug_gateway_payment_created', $order_id, $payment);
903
904 $this->response->process_payment($payment, true);
905
906 PayplugGateway::log(sprintf('Payment process complete for order #%s', $order_id));
907
908 return [
909 'result' => 'success',
910 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
911 'redirect' => ($payment->__get('is_paid')) ? $order->get_checkout_order_received_url() : $payment->__get('hosted_payment')->payment_url
912 ];
913 } catch (HttpException $e) {
914 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
915 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
916 } catch (\Exception $e) {
917 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
918 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
919 }
920 }
921
922 /**
923 * Process refund for an order paid with PayPlug gateway.
924 *
925 * @param int $order_id
926 * @param null $amount
927 * @param string $reason
928 *
929 * @return bool|\WP_Error
930 */
931 public function process_refund($order_id, $amount = null, $reason = '')
932 {
933 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
934
935 $order = wc_get_order($order_id);
936 if (!$order instanceof \WC_Order) {
937 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
938
939 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
940 }
941
942 if ($order->get_status() === "cancelled") {
943 PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
944
945 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
946 }
947
948 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
949 if (empty($transaction_id)) {
950 PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
951
952 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
953 }
954
955 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
956
957 $data = [
958 'metadata' => [
959 'order_id' => $order_id,
960 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
961 'refund_from' => 'woocommerce',
962 ]
963 ];
964
965 if (!is_null($amount)) {
966 $data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount);
967 }
968
969 if (!empty($reason)) {
970 $data['metadata']['reason'] = $reason;
971 }
972
973 /**
974 * Filter the refund data before it's used.
975 *
976 * @param array $data
977 * @param int $order_id
978 * @param string $transaction_id
979 */
980 $data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id);
981
982 try {
983 $refund = $this->api->refund_create($transaction_id, $data);
984
985 /**
986 * Fires once a refund has been created.
987 *
988 * @param int $order_id Order ID
989 * @param RefundResource $refund Refund resource
990 * @param string $transaction_id Transaction id
991 */
992 \do_action('payplug_gateway_refund_created', $order_id, $refund, $transaction_id);
993
994 $refund_meta_key = sprintf('_pr_%s', wc_clean($refund->id));
995 if (PayplugWoocommerceHelper::is_pre_30()) {
996 update_post_meta($order_id, $refund_meta_key, $refund->id);
997 } else {
998 $order->add_meta_data($refund_meta_key, $refund->id, true);
999 $order->save();
1000 }
1001
1002 $note = sprintf(__('Refund %s : Refunded %s', 'payplug'), wc_clean($refund->id), wc_price(((int) $refund->amount) / 100));
1003 if (!empty($refund->metadata['reason'])) {
1004 $note .= sprintf(' (%s)', esc_html($refund->metadata['reason']));
1005 }
1006 $order->add_order_note($note);
1007
1008 try {
1009 $payment = $this->api->payment_retrieve($transaction_id);
1010 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
1011 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
1012 } catch (\Exception $e) {
1013 }
1014
1015 PayplugGateway::log('Refund process complete for the order.');
1016
1017 return true;
1018 } catch (HttpException $e) {
1019 PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1020
1021 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1022 } catch (\Exception $e) {
1023 PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
1024
1025 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1026 }
1027 }
1028
1029 /**
1030 * Check the order amount to ensure it's on the allowed range.
1031 *
1032 * @param int $amount
1033 *
1034 * @return int|\WP_Error
1035 */
1036 public function validate_order_amount($amount)
1037 {
1038 if (
1039 $amount < PayplugWoocommerceHelper::get_minimum_amount()
1040 || $amount > PayplugWoocommerceHelper::get_maximum_amount()
1041 ) {
1042 return new \WP_Error(
1043 'invalid order amount',
1044 sprintf(__('Payments for this amount (%s) are not authorised with this payment gateway.', 'payplug'), \wc_price($amount / 100))
1045 );
1046 }
1047
1048 return $amount;
1049 }
1050
1051 /**
1052 * Limit string length.
1053 *
1054 * @param string $value
1055 * @param int $maxlength
1056 *
1057 * @return string
1058 */
1059 public function limit_length($value, $maxlength = 100)
1060 {
1061 return (strlen($value) > $maxlength) ? substr($value, 0, $maxlength) : $value;
1062 }
1063
1064 /**
1065 * Get user's keys.
1066 *
1067 * @param string $email
1068 * @param string $password
1069 *
1070 * @return array|\WP_Error
1071 */
1072 public function retrieve_user_api_keys($email, $password)
1073 {
1074 if (empty($email) || empty($password)) {
1075 return new \WP_Error('missing_login_data', __('Please fill all login fields', 'payplug'));
1076 }
1077
1078 try {
1079 $response = Authentication::getKeysByLogin($email, $password);
1080 if (empty($response) || !isset($response['httpResponse'])) {
1081 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1082 }
1083
1084 return $response['httpResponse']['secret_keys'];
1085 } catch (HttpException $e) {
1086 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1087 }
1088 }
1089
1090 /**
1091 * Get user merchant id.
1092 *
1093 * This method might be called during the login process before the global PayPlug
1094 * configuration is set. In that case you can pass a valid token to make the request.
1095 *
1096 * @param string|null $key
1097 *
1098 * @return string
1099 */
1100 public function retrieve_merchant_id($key = null)
1101 {
1102 try {
1103 $response = !is_null($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1104 $merchant_id = isset($response['httpResponse']['id']) ? $response['httpResponse']['id'] : '';
1105 } catch (ConfigurationException $e) {
1106 PayplugGateway::log(sprintf('Missing API key for PayPlug client : %s', wc_print_r($e->getMessage(), true)), 'error');
1107
1108 $merchant_id = '';
1109 } catch (HttpException $e) {
1110 PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error');
1111
1112 $merchant_id = '';
1113 } catch (\Exception $e) {
1114 PayplugGateway::log(sprintf('Account request error : %s', wc_clean($e->getMessage())), 'error');
1115
1116 $merchant_id = '';
1117 }
1118
1119 return $merchant_id;
1120 }
1121
1122 /**
1123 * Generate Hidden HTML.
1124 *
1125 * @param string $key
1126 * @param array $data
1127 *
1128 * @return string
1129 */
1130 public function generate_hidden_html($key, $data)
1131 {
1132 $field_key = $this->get_field_key($key);
1133 $defaults = array(
1134 'title' => '',
1135 'disabled' => false,
1136 'class' => '',
1137 'css' => '',
1138 'placeholder' => '',
1139 'type' => 'text',
1140 'desc_tip' => false,
1141 'description' => '',
1142 'custom_attributes' => array(),
1143 );
1144
1145 $data = wp_parse_args($data, $defaults);
1146
1147 ob_start();
1148 ?>
1149 <input type="<?php echo esc_attr($data['type']); ?>" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>" value="<?php echo esc_attr($this->get_option($key)); ?>" />
1150 <?php
1151
1152 return ob_get_clean();
1153 }
1154
1155 /**
1156 * Generate Yes/No Input HTML.
1157 *
1158 * @param string $key
1159 * @param array $data
1160 *
1161 * @return string
1162 */
1163 public function generate_yes_no_html($key, $data)
1164 {
1165 $field_key = $this->get_field_key($key);
1166 $defaults = array(
1167 'title' => '',
1168 'no' => 'No',
1169 'yes' => 'Yes',
1170 'disabled' => false,
1171 'class' => '',
1172 'css' => '',
1173 'placeholder' => '',
1174 'type' => 'text',
1175 'desc_tip' => false,
1176 'description' => '',
1177 'custom_attributes' => [],
1178 'hide_label' => false,
1179 );
1180
1181 $data = wp_parse_args($data, $defaults);
1182 $checked = 'yes' === $this->get_option($key) ? '1' : '0';
1183
1184 ob_start();
1185 ?>
1186 <tr valign="top">
1187 <?php if (!$data['hide_label']) : ?>
1188 <th scope="row" class="titledesc">
1189 <label for="<?php echo esc_attr($field_key); ?>">
1190 <?php echo wp_kses_post($data['title']); ?>
1191 <?php echo $this->get_tooltip_html($data); ?>
1192 </label>
1193 </th>
1194 <?php endif; ?>
1195 <td class="forminp">
1196 <fieldset>
1197 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1198 </legend>
1199 <div class="radio--custom">
1200 <input class="radio radio-yes <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-yes" value="1" <?php checked('1', $checked); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1201 <label for="<?php echo esc_attr($field_key); ?>-yes"><?php echo esc_html($data['yes']); ?></label>
1202 </div>
1203 <div class="radio--custom">
1204 <input class="radio radio-no <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-no" value="0" <?php checked('0', $checked); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1205 <label for="<?php echo esc_attr($field_key); ?>-no"><?php echo esc_html($data['no']); ?></label>
1206 </div>
1207 <?php echo $this->get_description_html($data); ?>
1208 </fieldset>
1209 </td>
1210 </tr>
1211 <?php
1212
1213 return ob_get_clean();
1214 }
1215
1216 /**
1217 * Generate Radio Input HTML.
1218 *
1219 * @param string $key
1220 * @param array $data
1221 *
1222 * @return string
1223 */
1224 public function generate_radio_html($key, $data)
1225 {
1226 $field_key = $this->get_field_key($key);
1227 $defaults = array(
1228 'title' => '',
1229 'disabled' => false,
1230 'class' => '',
1231 'css' => '',
1232 'placeholder' => '',
1233 'type' => 'text',
1234 'desc_tip' => false,
1235 'description' => '',
1236 'custom_attributes' => [],
1237 'options' => [],
1238 );
1239
1240 $data = wp_parse_args($data, $defaults);
1241
1242 ob_start();
1243 ?>
1244 <tr valign="top">
1245 <th scope="row" class="titledesc">
1246 <label for="<?php echo esc_attr($field_key); ?>">
1247 <?php echo wp_kses_post($data['title']); ?>
1248 <?php echo $this->get_tooltip_html($data); ?>
1249 </label>
1250 </th>
1251 <td class="forminp">
1252 <fieldset>
1253 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1254 </legend>
1255 <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1256 <input class="radio <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" value="<?php echo esc_attr($option_key); ?>" <?php checked($option_key, $this->get_option($key)); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1257 <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value); ?></label>
1258 <?php endforeach; ?>
1259 </fieldset>
1260 </td>
1261 </tr>
1262 <?php
1263
1264 return ob_get_clean();
1265 }
1266
1267 /**
1268 * Generate Login HTML.
1269 *
1270 * @param string $key
1271 * @param array $data
1272 *
1273 * @return string
1274 */
1275 public function generate_login_html($key, $data)
1276 {
1277 $field_key = $this->get_field_key($key);
1278 $defaults = [];
1279
1280 $data = wp_parse_args($data, $defaults);
1281
1282 ob_start();
1283 ?>
1284 <tr valign="top">
1285 <td class="forminp">
1286 <p><?php echo $this->get_option('email'); ?></p>
1287 <p>
1288 <input type="submit" name="submit_logout" value="<?php _e('Logout', 'payplug'); ?>">
1289 <input type="hidden" name="save" value="logout">
1290 <?php wp_nonce_field('payplug_user_logout', '_logoutaction'); ?>
1291 |
1292 <a href="https://portal.payplug.com" target="_blank"><?php _e('Go to your PayPlug Portal', 'payplug'); ?></a>
1293 </p>
1294 </td>
1295 </tr>
1296 <?php
1297
1298 return ob_get_clean();
1299 }
1300
1301 /**
1302 * Validate Radio Field.
1303 *
1304 * Make sure the data is escaped correctly, etc.
1305 *
1306 * @param string $key
1307 * @param string|null $value Posted Value
1308 *
1309 * @return string
1310 */
1311 public function validate_radio_field($key, $value)
1312 {
1313 $value = is_null($value) ? '' : $value;
1314
1315 return wc_clean(stripslashes($value));
1316 }
1317
1318 /**
1319 * Validate Yes/No Field.
1320 *
1321 * @param string $key
1322 * @param string $value Posted Value
1323 *
1324 * @return string
1325 */
1326 public function validate_yes_no_field($key, $value)
1327 {
1328 return ('1' === (string) $value) ? 'yes' : 'no';
1329 }
1330
1331 /**
1332 * Get PayPlug gateway mode.
1333 *
1334 * @return string
1335 */
1336 public function get_current_mode()
1337 {
1338 return ('yes' === $this->get_option('mode')) ? 'live' : 'test';
1339 }
1340
1341 /**
1342 * Get user API key.
1343 *
1344 * @param string $mode
1345 *
1346 * @return string
1347 */
1348 public function get_api_key($mode = 'test')
1349 {
1350
1351 switch ($mode) {
1352 case 'test':
1353 $key = $this->get_option('payplug_test_key');
1354 break;
1355 case 'live':
1356 $key = $this->get_option('payplug_live_key');
1357 break;
1358 default:
1359 $key = '';
1360 break;
1361 }
1362
1363 return $key;
1364 }
1365
1366 /**
1367 * Check if an api key exist for a mode.
1368 *
1369 * @param string $mode
1370 *
1371 * @return bool
1372 */
1373 public function has_api_key($mode = 'test')
1374 {
1375 $key = $this->get_api_key($mode);
1376 $key = trim($key);
1377
1378 return !empty($key);
1379 }
1380
1381 /**
1382 * Get current merchant id.
1383 *
1384 * @return string
1385 */
1386 public function get_merchant_id()
1387 {
1388 return $this->get_option('payplug_merchant_id', '');
1389 }
1390
1391 /**
1392 * Check if user is logged in and we have an API key for TEST mode.
1393 *
1394 * @return bool
1395 */
1396 public function user_logged_in()
1397 {
1398 return !empty($this->get_option('payplug_test_key'));
1399 }
1400
1401 /**
1402 * Check if oneclick payment is activated and merchant can use it.
1403 *
1404 * @return bool
1405 */
1406 public function oneclick_available()
1407 {
1408 return $this->user_logged_in()
1409 && $this->oneclick
1410 && $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD);
1411 }
1412
1413 /**
1414 * Check if the gatteway is allowed for the order amount
1415 *
1416 * @param array
1417 * @return array
1418 */
1419 public function check_gateway($gateways)
1420 {
1421 if (isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1422 $order_amount = $this->get_order_total();
1423 if ($order_amount < self::MIN_AMOUNT || $order_amount > self::MAX_AMOUNT) {
1424 unset($gateways[$this->id]);
1425 }
1426 }
1427 return $gateways;
1428 }
1429
1430 /**
1431 * Can the order be refunded via this gateway?
1432 *
1433 *
1434 * @param WC_Order $order Order object.
1435 * @return bool If false, the automatic refund button is hidden in the UI.
1436 */
1437 public function can_refund_order($order)
1438 {
1439 return $order && $this->supports('refunds') && $order->get_status() !== "cancelled";
1440 }
1441 }
1442