PluginProbe
PayPlug for WooCommerce (Official) / 2.8.2
PayPlug for WooCommerce (Official) v2.8.2
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) 2.8.2, at src/Gateway/PayplugGateway.php

1,742 lines 60.9 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\Exception\ForbiddenException;
14 use Payplug\Payplug;
15 use Payplug\PayplugWoocommerce\Admin\Ajax;
16 use Payplug\PayplugWoocommerce\Controller\IntegratedPayment;
17 use Payplug\PayplugWoocommerce\Helper\Lock;
18 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
19 use Payplug\Resource\Payment as PaymentResource;
20 use Payplug\Resource\Refund as RefundResource;
21 use WC_Payment_Gateway_CC;
22 use WC_Payment_Tokens;
23
24 /**
25 * PayPlug WooCommerce Gateway.
26 *
27 * @package Payplug\PayplugWoocommerce\Gateway
28 */
29 class PayplugGateway extends WC_Payment_Gateway_CC
30 {
31 const OPTION_NAME = "payplug_config";
32
33 /**
34 * @var PayplugGatewayRequirements
35 */
36 private $requirements;
37
38 /**
39 * @var PayplugPermissions
40 */
41 private $permissions;
42
43 /**
44 * @var PayplugResponse
45 */
46 public $response;
47
48 /**
49 * @var PayplugApi
50 */
51 public $api;
52
53 /**
54 * @var \WC_Logger
55 */
56 protected static $log;
57
58 /**
59 * @var bool
60 */
61 protected static $log_enabled;
62
63 /**
64 * @var float
65 */
66 const MIN_AMOUNT = 0.99;
67
68 /**
69 * @var float
70 */
71 const MAX_AMOUNT = 20000;
72
73 /**
74 * @var string
75 */
76 private $payplug_merchant_country = 'FR';
77
78 protected $oney_response;
79 public $min_oney_price, $oney_thresholds_min;
80 public $max_oney_price, $oney_thresholds_max;
81
82 const ENABLE_ON_TEST_MODE = true;
83
84
85 /**
86 * Logging method.
87 *
88 * @param string $message Log message.
89 * @param string $level Optional. Default 'info'.
90 * emergency|alert|critical|error|warning|notice|info|debug
91 */
92 public static function log($message, $level = 'info')
93 {
94 if (!self::$log_enabled) {
95 return;
96 }
97
98 if (empty(self::$log)) {
99 self::$log = PayplugWoocommerceHelper::is_pre_30() ? new \WC_Logger() : wc_get_logger();
100 }
101
102 PayplugWoocommerceHelper::is_pre_30()
103 ? self::$log->add('payplug_gateway', $message)
104 : self::$log->log($level, $message, array('source' => 'payplug_gateway'));
105 }
106
107 /**
108 * Construct method
109 *
110 * @return void
111 */
112 public function __construct()
113 {
114 $payplug_gateways = array('payplug', 'american_express', 'apple_pay', 'bancontact', 'oney_x3_with_fees', 'oney_x3_without_fees', 'oney_x4_with_fees', 'oney_x4_without_fees', 'satispay', 'sofort', 'ideal', 'mybank');
115
116 if ((!empty($_GET['section'])) && (in_array($_GET['section'], $payplug_gateways))) {
117 $GLOBALS['hide_save_button'] = true;
118 }
119
120 //TODO: this should be properties of the class and implemented an interface on all classes (set values)
121 $this->id = 'payplug';
122 $this->icon = '';
123 $this->has_fields = false;
124 $this->method_title = _x('PayPlug', 'Gateway method title', 'payplug');
125 $this->method_description = __('Enable PayPlug for your customers.', 'payplug');
126 $this->supports = array(
127 'products',
128 'refunds',
129 'tokenization',
130 );
131 $this->new_method_label = __('Pay with another credit card', 'payplug');
132
133 $this->init_settings();
134 $this->requirements = new PayplugGatewayRequirements($this);
135 if ($this->user_logged_in()) {
136 $this->init_payplug();
137 }else{
138 delete_option('woocommerce_payplug_settings');
139 set_transient( PayplugWoocommerceHelper::get_transient_key(get_option('woocommerce_payplug_settings', [])), null );
140 }
141
142 $this->title = $this->get_option('title');
143 $this->description = $this->get_option('description');
144 $this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
145 $this->debug = 'yes' === $this->get_option('debug', 'no');
146 $this->email = $this->get_option('email');
147 $this->payment_method = $this->get_option('payment_method');
148 $this->oneclick = (('yes' === $this->get_option('oneclick', 'no')) && (is_user_logged_in()));
149 $this->oney_type = $this->get_option('oney_type', 'with_fees');
150 $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
151
152 //TODO:: remove this properties from here and add them on Oney classes
153 $this->min_oney_price = (isset($oney_range['min'])) ? intval($oney_range['min']) : 100;
154 $this->max_oney_price = (isset($oney_range['max'])) ? intval($oney_range['max']) : 3000;
155 $this->oney_thresholds_min = $this->get_option('oney_thresholds_min', $this->min_oney_price );
156 $this->oney_thresholds_max = $this->get_option('oney_thresholds_max', $this->max_oney_price );
157 $this->init_form_fields();
158 $this->payplug_merchant_country = PayplugWoocommerceHelper::get_payplug_merchant_country();
159 $this->oney_product_animation = $this->get_option('oney_product_animation');
160
161 add_filter('woocommerce_get_customer_payment_tokens', [$this, 'filter_tokens'], 10, 3);
162
163 self::$log_enabled = $this->debug;
164
165
166 // Ensure the description is not empty to correctly display users's save cards
167 if (empty($this->description) && 0 !== count($this->get_tokens()) && $this->oneclick_available()) {
168 $this->description = ' ';
169 }
170
171
172 if ('test' === $this->mode) {
173 $this->description .= " \n";
174 $this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug');
175 $this->description = trim($this->description);
176 }
177
178 //add fields of IP to the description
179 if($this->payment_method === 'integrated'){
180 $this->has_fields = true;
181 }
182
183 add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
184 add_action('wp_enqueue_scripts', [$this, 'scripts']);
185 add_action('the_post', [$this, 'validate_payment']);
186 add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
187 }
188
189 /**
190 * @param $option
191 * @param $value
192 * @return bool|void
193 */
194 public function update_option($option, $value = ''){
195 if ( $this->needs_setup() ) {
196 wp_send_json_error( 'needs_setup' );
197 wp_die();
198 }
199
200 parent::update_option($option, $value);
201 }
202
203 /**
204 * this payment gateway cannot be updated on the wooco payment settings
205 * @return bool
206 */
207 public function needs_setup()
208 {
209 return true;
210 }
211
212 /**
213 * Customize gateway title in emails.
214 *
215 * @param array $total_rows
216 * @param \WC_Order $order
217 *
218 * @return array
219 *
220 * @author Clément Boirie
221 */
222 public function customize_gateway_title($total_rows, $order)
223 {
224
225 $get_payment_method = $this->id;
226 if( method_exists($order, "get_payment_method") ) {
227 $get_payment_method = $order->get_payment_method();
228 }
229
230 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $get_payment_method;
231 if (
232 $this->id !== $payment_method
233 || !isset($total_rows['payment_method'])
234 ) {
235 return $total_rows;
236 }
237
238 $total_rows['payment_method']['value'] = __('Credit card', 'payplug');
239
240 return $total_rows;
241 }
242
243 /**
244 * Validate order payment when the user is redirected to the success confirmation page.
245 *
246 * @throws \WC_Data_Exception
247 */
248 public function validate_payment($id = null, $save_request = true)
249 {
250 if (!is_wc_endpoint_url('order-received') || (empty($_GET['key']) && empty($id)) ) {
251 return;
252 }
253
254 $order_id = wc_get_order_id_by_order_key(wc_clean( (!empty($_GET['key']) ? $_GET['key'] : $id) ) );
255 if (empty($order_id)) {
256 return;
257 }
258
259 $order = wc_get_order($order_id);
260 if (!$order instanceof \WC_Order) {
261 return;
262 }
263
264 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
265 if (!in_array($payment_method, ['payplug', 'oney_x3_with_fees', 'oney_x4_with_fees', 'oney_x3_without_fees', 'oney_x4_without_fees','bancontact', 'apple_pay', 'american_express', "satispay","sofort","mybank","ideal" ])) {
266 return;
267 }
268
269
270 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
271 if (empty($transaction_id)) {
272 PayplugGateway::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
273 return;
274 }
275
276 if($payment_method === $this->id) {
277
278 $lock_id = Lock::handle_insert($save_request, $transaction_id);
279 if(!$lock_id){
280 return;
281 }
282
283 try {
284 $payment = $this->api->payment_retrieve($transaction_id);
285 } catch (\Exception $e) {
286 PayplugGateway::log(
287 sprintf(
288 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
289 $order_id,
290 $e->getMessage()
291 )
292 );
293
294 return;
295 }
296
297 $this->response->process_payment($payment);
298
299 \Payplug\PayplugWoocommerce\Model\Lock::delete_lock($lock_id);
300 $waiting_requests = \Payplug\PayplugWoocommerce\Model\Lock::get_lock_by_payment_id($transaction_id);
301
302 if($waiting_requests){
303 $this->validate_payment($order_id, false);
304 \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_by_payment_id($transaction_id);
305 };
306 }
307 }
308
309 /**
310 * Get payment icons.
311 *
312 * @return string
313 */
314 public function get_icon()
315 {
316
317 $src = ('it_IT' === get_locale())
318 ? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_PostePay.svg'
319 : PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_CB.svg';
320
321 $icons = apply_filters('payplug_payment_icons', [
322 'payplug' => sprintf('<img src="%s" alt="Visa & Mastercard" class="payplug-payment-icon" />', esc_url($src)),
323 ]);
324
325 $icons_str = '';
326 foreach ($icons as $icon) {
327 $icons_str .= $icon;
328 }
329
330 return $icons_str;
331 }
332
333 /**
334 * Check if this gateway is enabled
335 */
336 public function is_available()
337 {
338 if ('yes' === $this->enabled) {
339 return $this->requirements->satisfy_requirements() && !empty($this->get_api_key($this->get_current_mode()));
340 }
341
342 return parent::is_available();
343 }
344
345 /**
346 * Load gateway settings.
347 */
348 public function init_settings()
349 {
350 parent::init_settings();
351 $this->enabled = !empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
352 }
353
354 /**
355 * Register gateway settings.
356 */
357 public function init_form_fields()
358 {
359
360 $anchor = esc_html_x( __("More informations", 'payplug'), 'modal', 'payplug' );
361 $domain = __( 'support.payplug.com/hc/fr/articles/4408142346002', 'payplug' );
362 $link = sprintf( ' <a href="https://%s" target="_blank">%s</a>', $domain, $anchor );
363
364
365 $anchor_bancontact = esc_html_x( __("payplug_bancontact_activation_request", 'payplug'), 'modal', 'payplug' );
366 $domain_bancontact = __( 'payplug_bancontact_activation_url', 'payplug' );
367 $bancontact_call_to_action = sprintf( ' <a id="bancontact_call_to_action" href="https://%s" target="_blank">%s</a>', $domain_bancontact, $anchor_bancontact );
368
369 $fields = [
370 'enabled' => [
371 'title' => __('Enable/Disable', 'payplug'),
372 'type' => 'checkbox',
373 'label' => __('Enable PayPlug', 'payplug'),
374 'description' => __('Only Euro payments can be processed with PayPlug.', 'payplug'),
375 'default' => 'no',
376 ],
377 'title' => [
378 'title' => __('Title', 'payplug'),
379 'type' => 'text',
380 'description' => __('The payment solution title displayed to your customers during checkout', 'payplug'),
381 'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
382 'desc_tip' => false,
383 ],
384 'description' => [
385 'title' => __('Description', 'payplug'),
386 'type' => 'text',
387 'description' => __('The payment solution description displayed to your customers during checkout', 'payplug'),
388 'default' => '',
389 'desc_tip' => false,
390 ],
391 'title_connexion' => [
392 'title' => __('Connection', 'payplug'),
393 'type' => 'title',
394 ],
395 'email' => [
396 'type' => 'hidden',
397 'default' => '',
398 ],
399 'login' => [
400 'type' => 'login',
401 'default' => '',
402 ],
403 'payplug_test_key' => [
404 'type' => 'hidden',
405 'default' => '',
406 ],
407 'payplug_live_key' => [
408 'type' => 'hidden',
409 'default' => '',
410 ],
411 'payplug_merchant_id' => [
412 'type' => 'hidden',
413 'default' => '',
414 ],
415 'title_testmode' => [
416 'title' => __('Mode', 'payplug'),
417 'type' => 'title',
418 ],
419 'mode' => [
420 'title' => '',
421 'label' => '',
422 'type' => 'yes_no',
423 'yes' => 'Live',
424 'no' => 'Test',
425 'description' => __('In TEST mode, all payments will be simulations and will not generate real transactions.', 'payplug'),
426 'default' => 'no',
427 'hide_label' => true,
428 ],
429 'title_settings' => [
430 'title' => __('Settings', 'payplug'),
431 'type' => 'title',
432 ],
433 'payment_method' => [
434 'title' => __('Payment page', 'payplug'),
435 'type' => 'radio',
436 'options' => array(
437 'redirect' => __('Redirect', 'payplug'),
438 'embedded' => __('Integrated', 'payplug'),
439 ),
440 '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'),
441 'default' => 'redirect',
442 'desc_tip' => false
443 ],
444 'debug' => [
445 'title' => __('Debug', 'payplug'),
446 'type' => 'checkbox',
447 'description' => __('Debug mode saves additional information on your server for each operation done via the PayPlug plugin (Developer setting).', 'payplug'),
448 'label' => __('Activate debug mode', 'payplug'),
449 'default' => 'yes',
450 'desc_tip' => false
451 ],
452 'title_advanced_settings' => [
453 'title' => __('payplug_advanced_settings', 'payplug'),
454 'description' => __(
455 '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>',
456 'payplug'
457 ),
458 'type' => 'title',
459 ],
460 'oneclick' => [
461 'title' => __('One Click Payment', 'payplug'),
462 'type' => 'checkbox',
463 'label' => __('Activate', 'payplug'),
464 'description' => __('Allow your customers to save their credit card information for later purchases.', 'payplug'),
465 'default' => 'no',
466 'desc_tip' => false
467 ],
468 'bancontact' => [
469 'title' => __('payplug_bancontact_activate_title', 'payplug'),
470 'type' => 'checkbox',
471 'label' => __('Activate', 'payplug'),
472 'description' => '<p class="description" id="bancontact_test_mode_description"> '. __('payplug_bancontact_testmode_description', 'payplug') .' </p>' .
473 '<p class="description" id="bancontact_live_mode_description_disabled"> '. __('payplug_bancontact_livemode_description_disabled', 'payplug') .' </p>' .
474 $bancontact_call_to_action,
475 'default' => 'no',
476 ],
477 'apple_pay' => [
478 'title' => __('payplug_apple_pay_activate_title', 'payplug'),
479 'type' => 'checkbox',
480 'label' => __('Activate', 'payplug'),
481 'description' => '<p class="description" id="apple_pay_test_mode_description"> '. __('payplug_apple_pay_testmode_description', 'payplug') .' </p>' .
482 '<p class="description" id="apple_pay_live_mode_description"> '. __('payplug_apple_pay_livemode_description', 'payplug') .' </p>' ,
483 'default' => 'no',
484 ],
485 'american_express' => [
486 'title' => __('payplug_amex_title', 'payplug'),
487 'type' => 'checkbox',
488 'label' => __('payplug_amex_activate', 'payplug'),
489 'description' => '<p class="description" id="amex_test_mode_description"> '. __('payplug_amex_testmode_description', 'payplug') .' </p>' .
490 '<p class="description" id="amex_live_mode_description"> '. __('payplug_amex_livemode_description', 'payplug') .' </p>' ,
491 'default' => 'no',
492 ],
493 'oney' => [
494 'title' => __('3x 4x Oney payments', 'payplug'),
495 'type' => 'checkbox',
496 'label' => __('Activate', 'payplug'),
497 // TRAD
498 'description' => sprintf(__('Allow your customers to split payments into 3 or 4 installments, for orders between %s€ and %s€', 'payplug'), $this->min_oney_price, $this->max_oney_price) . $link,
499 'default' => 'no',
500 'desc_tip' => false
501 ],
502 'oney_type' => [
503 'title' => '',
504 'type' => 'oney_type',
505 'options' => array(
506 'with_fees' => __('Oney with fees', 'payplug'),
507 'without_fees' => __('Oney without fees', 'payplug'),
508 ),
509 'descriptions' => array(
510 'with_fees' => __('The fees are split between you and your customers', 'payplug'),
511 'without_fees' => __('You pay the fees', 'payplug'),
512 ),
513 'description' => '',
514 'default' => 'with_fees',
515 'desc_tip' => false
516 ],
517 'oney_thresholds' => [
518 'title' => '',
519 'type' => 'oney_thresholds',
520 'description' => sprintf(__('I would like to offer guaranteed payment in installments for amounts between %s€ and %s€.', 'payplug'),
521 '<b class="min">' . $this->oney_thresholds_min . '</b>', '<b class="max">' . $this->oney_thresholds_max . '</b>'),
522 'desc_tip' => false
523 ],
524 'oney_thresholds_min' => [
525 'title' => '',
526 'type' => 'hidden',
527 'label' => '',
528 'description' => '',
529 'default' => 'no',
530 ],
531 'oney_thresholds_max' => [
532 'title' => '',
533 'type' => 'hidden',
534 'label' => '',
535 'description' => '',
536 'default' => 'no',
537 ],
538 'oney_product_animation' => [
539 'title' => __('oney_installments_pop_up', 'payplug'),
540 'description' => __('display_the_oney_installments_pop_up_on_the_product_page', 'payplug'),
541 'label' => __('Activate', 'payplug'),
542 'default' => 'no',
543 'desc_tip' => false,
544 'type' => 'oney_product_animation'
545 ],
546 ];
547
548 if ($this->user_logged_in()) {
549 if ($this->permissions->has_permissions(PayplugPermissions::SAVE_CARD)) {
550 unset($fields['title_advanced_settings']);
551 } else if ('live' === $this->get_current_mode()){
552 $fields['oneclick']['disabled'] = true;
553 }
554 }
555
556 /**
557 * Filter PayPlug gateway settings.
558 *
559 * @param array $fields
560 */
561 $fields = apply_filters('payplug_gateway_settings', $fields);
562 $this->form_fields = $fields;
563 }
564
565 /**
566 * Set global configuration for PayPlug instance.
567 */
568 public function init_payplug()
569 {
570 $this->api = new PayplugApi($this);
571 $this->api->init();
572
573 $this->permissions = new PayplugPermissions($this);
574 $this->response = new PayplugResponse($this);
575
576 // Register IPN handler
577 new PayplugIpnResponse($this);
578
579 }
580
581 public function integrated_payments_scripts(){
582
583 $translations = array(
584 "cardholder" => __('payplug_integrated_payment_cardholder', 'payplug'),
585 "your_card" => __('payplug_integrated_payment_your_card', 'payplug'),
586 "card_number" => __('payplug_integrated_payment_card_number', 'payplug'),
587 "expiration_date" => __('payplug_integrated_payment_expiration_date', 'payplug'),
588 "cvv" => __('payplug_integrated_payment_cvv', 'payplug'),
589 "one_click" => __('payplug_integrated_payment_oneClick', 'payplug'),
590 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
591 'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'),
592 'nonce' => wp_create_nonce('woocommerce-process_checkout'),
593 'mode' => PayplugWoocommerceHelper::check_mode(), // true for TEST, false for LIVE
594 'check_payment_url' => \WC_AJAX::get_endpoint('payplug_check_payment')
595 );
596
597 //TODO:: if integrated payment is active please active form and comment the one above
598 /**x
599 * Integrated payments scripts
600 */
601 wp_enqueue_style('payplugIP', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-integrated-payments.css', [], PAYPLUG_GATEWAY_VERSION);
602
603 wp_register_script('payplug-integrated-payments-api', 'https://cdn.payplug.com/js/integrated-payment/v1@1/index.js', [], 'v1.1', true);
604 wp_enqueue_script('payplug-integrated-payments-api');
605
606 wp_register_script( 'jquery-bind-first', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/jquery.bind-first-0.2.3.min.js', array( 'jquery' ), '1.0.0', true );
607 wp_enqueue_script('jquery-bind-first');
608
609 wp_register_script('payplug-integrated-payments', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-integrated-payments.js', ['jquery', 'jquery-bind-first', 'payplug-integrated-payments-api'], 'v1.1', true);
610 wp_enqueue_script('payplug-integrated-payments');
611
612 wp_localize_script( 'payplug-integrated-payments', 'payplug_integrated_payment_params', $translations);
613 }
614
615 /**
616 * Embedded payment form scripts.
617 *
618 * Register scripts and additionnal data needed for the
619 * embedded payment form.
620 */
621 public function scripts()
622 {
623 if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) {
624 return;
625 }
626
627 // If PayPlug is not enabled bail.
628 if ('no' === $this->enabled) {
629 return;
630 }
631
632 // If keys are not set bail.
633 if (empty($this->get_api_key($this->mode))) {
634 PayplugGateway::log('Keys are not set correctly.');
635
636 return;
637 }
638
639 // Register checkout styles.
640 wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION);
641 wp_enqueue_style('payplug-checkout');
642
643 if ($this->payment_method == "integrated" ) {
644 $this->integrated_payments_scripts();
645 }
646
647 if (($this->payment_method == "popup" ) && ($this->id === "payplug")) {
648
649 //load popup features
650 //TODO:: if integrated payment is not active please active this and comment the one bellow
651 wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
652 wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [
653 'jquery',
654 'payplug'
655 ], PAYPLUG_GATEWAY_VERSION, true);
656 wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
657 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
658 'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'),
659 'nonce' => [
660 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
661 ],
662 'is_embedded' => 'redirect' !== $this->payment_method
663 ]);
664
665 wp_enqueue_script('payplug-checkout');
666 }
667
668 }
669
670 /**
671 * Filter saved tokens for the gateway.
672 *
673 * A token will be removed if :
674 * - it doesn't match the current merchant logged in,
675 * - or it doesn't match the current gateway mode,
676 * - or it is expired.
677 *
678 * @param array $tokens
679 * @param int $user_id
680 * @param string $gateway_id
681 *
682 * @return array
683 */
684 public function filter_tokens($tokens, $user_id, $gateway_id)
685 {
686
687 if (!is_user_logged_in() || !class_exists('WC_Payment_Gateway_CC')) {
688 return $tokens;
689 }
690
691 /* @var \WC_Payment_Token_CC $token */
692 foreach ($tokens as $k => $token) {
693
694 if ($this->id !== $token->get_gateway_id()) {
695 continue;
696 }
697
698 // check if token is associated with a merchant id and if it match the current one
699 $token_merchant_id = $token->get_meta('payplug_account', true);
700 if (empty($token_merchant_id) || $this->get_merchant_id() !== $token_merchant_id) {
701 unset($tokens[$k]);
702 continue;
703 }
704
705 // check if token is available for the current gateway mode
706 if ($this->mode !== $token->get_meta('mode', true)) {
707 unset($tokens[$k]);
708 continue;
709 }
710
711 // check if token is not expired
712 $current_month = \absint(date('n'));
713 $current_year = \absint(date('Y'));
714 if ($current_year > (int) $token->get_expiry_year()) {
715 unset($tokens[$k]);
716 continue;
717 }
718
719 if ($current_year === (int) $token->get_expiry_year() && $current_month > (int) $token->get_expiry_month()) {
720 unset($tokens[$k]);
721 continue;
722 }
723 }
724
725 return $tokens;
726 }
727
728 public function payment_fields()
729 {
730 $description = $this->get_description();
731
732 if (!empty($description)) {
733 echo wpautop(wptexturize($description));
734 }
735
736 if(($this->payment_method === 'integrated') && ($this->id == 'payplug')){
737 echo IntegratedPayment::template_form($this->oneclick);
738 }
739
740 if ($this->oneclick_available()) {
741 $this->tokenization_script();
742 $this->saved_payment_methods();
743 }
744 }
745
746 /**
747 * Handle admin display.
748 */
749 public function admin_options()
750 {
751 /************ VUE Code *************/
752 wp_enqueue_script('chunk-vendors.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/chunk-vendors-1.7.2.js', [], PAYPLUG_GATEWAY_VERSION);
753 wp_enqueue_script('app.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/app-1.7.2.js', [], PAYPLUG_GATEWAY_VERSION);
754 wp_enqueue_style('app.css', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/css/app.css', [], PAYPLUG_GATEWAY_VERSION);
755 wp_localize_script('app.js', 'payplug_admin_config',
756 array(
757 "img_path" => esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/'),
758 'ajax_url' => get_home_url()
759 ));
760
761 ?>
762 <script>window.get_data_url = "<?php echo rest_url('payplug/data'); ?>"</script>
763 <script>window.set_data_url = "<?php echo rest_url('payplug/save_data'); ?>"</script>
764 <div id="payplug_admin"></div>
765
766 <?php
767
768 /*********** End VUE Code ***********/
769
770 }
771
772 /**
773 * Process payment.
774 *
775 * @param int $order_id
776 *
777 * @return array
778 * @throws \Exception
779 */
780 public function process_payment($order_id)
781 {
782
783 PayplugGateway::log(sprintf('Processing payment for order #%s', $order_id));
784
785 $order = wc_get_order($order_id);
786 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
787 $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total());
788 $amount = $this->validate_order_amount($amount);
789
790 if (is_wp_error($amount)) {
791 PayplugGateway::log(sprintf('Invalid amount %s for the order.', $order->get_total()), 'error');
792 throw new \Exception($amount->get_error_message());
793 }
794
795 $payment_token_id = (isset($_POST['wc-' . $this->id . '-payment-token']) && 'new' !== $_POST['wc-' . $this->id . '-payment-token'])
796 ? wc_clean($_POST['wc-' . $this->id . '-payment-token'])
797 : false;
798
799 if ($payment_token_id && $this->oneclick_available() && (int) $customer_id > 0) {
800 PayplugGateway::log(sprintf('Payment token found.', $amount));
801
802 return $this->process_payment_with_token($order, $amount, $customer_id, $payment_token_id);
803 }
804
805 return $this->process_standard_payment($order, $amount, $customer_id);
806 }
807
808 /**
809 * @param \WC_Order $order
810 * @param int $amount
811 * @param int $customer_id
812 *
813 * @return array
814 * @throws \Exception
815 */
816 public function process_standard_payment($order, $amount, $customer_id)
817 {
818
819 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
820
821 try {
822 $address_data = PayplugAddressData::from_order($order);
823
824 $return_url = esc_url_raw($order->get_checkout_order_received_url());
825
826 if (!(substr( $return_url, 0, 4 ) === "http")) {
827 $return_url = get_site_url().$return_url;
828 }
829
830 $payment_data = [
831 'amount' => $amount,
832 'currency' => get_woocommerce_currency(),
833 'allow_save_card' => $this->oneclick_available() && (int) $customer_id > 0,
834 'billing' => $address_data->get_billing(),
835 'shipping' => $address_data->get_shipping(),
836 'hosted_payment' => [
837 'return_url' => $return_url,
838 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
839 ],
840 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
841 'metadata' => [
842 'order_id' => $order_id,
843 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
844 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
845 ],
846 ];
847
848 if($this->payment_method === 'integrated'){
849 $payment_data['initiator'] = 'PAYER';
850 $payment_data['integration'] = 'INTEGRATED_PAYMENT';
851 unset($payment_data['hosted_payment']['cancel_url']);
852 }
853
854 /**
855 * Filter the payment data before it's used
856 *
857 * @param array $payment_data
858 * @param int $order_id
859 * @param array $customer_details
860 * @param PayplugAddressData $address_data
861 */
862 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
863 $payment = $this->api->payment_create($payment_data);
864
865 // Save transaction id for the order
866 PayplugWoocommerceHelper::is_pre_30()
867 ? update_post_meta($order_id, '_transaction_id', $payment->id)
868 : $order->set_transaction_id($payment->id);
869
870 if (is_callable([$order, 'save'])) {
871 $order->save();
872 }
873
874 /**
875 * Fires once a payment has been created.
876 *
877 * @param int $order_id Order ID
878 * @param PaymentResource $payment Payment resource
879 */
880 \do_action('payplug_gateway_payment_created', $order_id, $payment);
881
882 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
883 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
884
885 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
886
887 if(ob_get_length() > 0){
888 ob_clean();
889 }
890
891 return array(
892 'payment_id' => $payment->id,
893 'result' => 'success',
894 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
895 'cancel' => $payment->hosted_payment->cancel_url
896 );
897
898 } catch (HttpException $e) {
899 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
900 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
901 } catch (\Exception $e) {
902 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
903 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
904 }
905 }
906
907 /**
908 * @param \WC_Order $order
909 * @param int $amount
910 * @param int $customer_id
911 * @param string $token_id
912 *
913 * @return array
914 * @throws \Exception
915 */
916 public function process_payment_with_token($order, $amount, $customer_id, $token_id)
917 {
918
919 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
920 $payment_token = WC_Payment_Tokens::get($token_id);
921 if (!$payment_token || (int) $customer_id !== (int) $payment_token->get_user_id()) {
922 PayplugGateway::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error');
923 throw new \Exception(__('Invalid payment method.', 'payplug'));
924 }
925
926 try {
927 $address_data = PayplugAddressData::from_order($order);
928
929 $return_url = esc_url_raw($order->get_checkout_order_received_url());
930
931 if (!(substr( $return_url, 0, 4 ) === "http")) {
932 $return_url = get_site_url().$return_url;
933 }
934
935 $payment_data = [
936 'amount' => $amount,
937 'currency' => get_woocommerce_currency(),
938 'payment_method' => $payment_token->get_token(),
939 'allow_save_card' => false,
940 'billing' => $address_data->get_billing(),
941 'shipping' => $address_data->get_shipping(),
942 'initiator' => 'PAYER',
943 'hosted_payment' => [
944 'return_url' => $return_url,
945 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
946 ],
947 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
948 'metadata' => [
949 'order_id' => $order_id,
950 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
951 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
952 ],
953 ];
954
955 /** This filter is documented in src/Gateway/PayplugGateway */
956 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
957 $payment = $this->api->payment_create($payment_data);
958
959 /** This action is documented in src/Gateway/PayplugGateway */
960 \do_action('payplug_gateway_payment_created', $order_id, $payment);
961
962 $this->response->process_payment($payment, true);
963
964 PayplugGateway::log(sprintf('Payment process complete for order #%s', $order_id));
965
966 if(($payment->__get('is_paid'))){
967 $redirect = $order->get_checkout_order_received_url();
968 }else if(isset($payment->__get('hosted_payment')->payment_url)){
969 $redirect = $payment->__get('hosted_payment')->payment_url;
970 }else{
971 $redirect = $return_url;
972 }
973
974 return [
975 'payment_id' => $payment->id,
976 'result' => 'success',
977 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
978 'redirect' => $redirect
979 ];
980 } catch (HttpException $e) {
981 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
982 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
983 } catch (\Exception $e) {
984 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
985 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
986 }
987 }
988
989 /**
990 * Process refund for an order paid with PayPlug gateway.
991 *
992 * @param int $order_id
993 * @param null $amount
994 * @param string $reason
995 *
996 * @return bool|\WP_Error
997 */
998 public function process_refund($order_id, $amount = null, $reason = '')
999 {
1000 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
1001
1002 if( !$this->user_logged_in()){
1003 PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
1004 return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
1005 }
1006
1007 $order = wc_get_order($order_id);
1008 if (!$order instanceof \WC_Order) {
1009 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
1010
1011 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
1012 }
1013
1014 if ($order->get_status() === "cancelled") {
1015 PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
1016
1017 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
1018 }
1019
1020 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
1021 if (empty($transaction_id)) {
1022 PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
1023
1024 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
1025 }
1026
1027 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
1028
1029 $data = [
1030 'metadata' => [
1031 'order_id' => $order_id,
1032 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
1033 'refund_from' => 'woocommerce',
1034 ]
1035 ];
1036
1037 if (!is_null($amount)) {
1038 $data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount);
1039 }
1040
1041 if (!empty($reason)) {
1042 $data['metadata']['reason'] = $reason;
1043 }
1044
1045 /**
1046 * Filter the refund data before it's used.
1047 *
1048 * @param array $data
1049 * @param int $order_id
1050 * @param string $transaction_id
1051 */
1052 $data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id);
1053
1054 try {
1055 $refund = $this->api->refund_create($transaction_id, $data);
1056
1057 /**
1058 * Fires once a refund has been created.
1059 *
1060 * @param int $order_id Order ID
1061 * @param RefundResource $refund Refund resource
1062 * @param string $transaction_id Transaction id
1063 */
1064 \do_action('payplug_gateway_refund_created', $order_id, $refund, $transaction_id);
1065
1066 $refund_meta_key = sprintf('_pr_%s', wc_clean($refund->id));
1067 if (PayplugWoocommerceHelper::is_pre_30()) {
1068 update_post_meta($order_id, $refund_meta_key, $refund->id);
1069 } else {
1070 $order->add_meta_data($refund_meta_key, $refund->id, true);
1071 $order->save();
1072 }
1073
1074 $note = sprintf(__('Refund %s : Refunded %s', 'payplug'), wc_clean($refund->id), wc_price(((int) $refund->amount) / 100));
1075 if (!empty($refund->metadata['reason'])) {
1076 $note .= sprintf(' (%s)', esc_html($refund->metadata['reason']));
1077 }
1078 $order->add_order_note($note);
1079
1080 try {
1081 $payment = $this->api->payment_retrieve($transaction_id);
1082 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
1083 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
1084 } catch (\Exception $e) {
1085 }
1086
1087 PayplugGateway::log('Refund process complete for the order.');
1088
1089 return true;
1090 } catch (HttpException $e) {
1091 PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1092
1093 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1094 } catch (\Exception $e) {
1095 PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
1096
1097 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1098 }
1099 }
1100
1101 /**
1102 * Check the order amount to ensure it's on the allowed range.
1103 *
1104 * @param int $amount
1105 *
1106 * @return int|\WP_Error
1107 */
1108 public function validate_order_amount($amount)
1109 {
1110 if (
1111 $amount < PayplugWoocommerceHelper::get_minimum_amount()
1112 || $amount > PayplugWoocommerceHelper::get_maximum_amount()
1113 ) {
1114 return new \WP_Error(
1115 'invalid order amount',
1116 sprintf(__('Payments for this amount (%s) are not authorised with this payment gateway.', 'payplug'), \wc_price($amount / 100))
1117 );
1118 }
1119
1120 return $amount;
1121 }
1122
1123 /**
1124 * Limit string length.
1125 *
1126 * @param string $value
1127 * @param int $maxlength
1128 *
1129 * @return string
1130 */
1131 public function limit_length($value, $maxlength = 100)
1132 {
1133 return (strlen($value) > $maxlength) ? substr($value, 0, $maxlength) : $value;
1134 }
1135
1136 /**
1137 * Get user's keys.
1138 *
1139 * @param string $email
1140 * @param string $password
1141 *
1142 * @return array|\WP_Error
1143 */
1144 public function retrieve_user_api_keys($email, $password)
1145 {
1146 if (empty($email) || empty($password)) {
1147 return new \WP_Error('missing_login_data', __('Please fill all login fields', 'payplug'));
1148 }
1149
1150 try {
1151 $response = Authentication::getKeysByLogin($email, $password);
1152 if (empty($response) || !isset($response['httpResponse']) && "payplug" === $this->id) {
1153 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1154 }
1155
1156 return $response['httpResponse']['secret_keys'];
1157 } catch (HttpException $e) {
1158 if("payplug" === $this->id) {
1159 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1160 }
1161 }
1162 }
1163
1164 /**
1165 * Get user merchant id.
1166 *
1167 * This method might be called during the login process before the global PayPlug
1168 * configuration is set. In that case you can pass a valid token to make the request.
1169 *
1170 * @param string|null $key
1171 *
1172 * @return string
1173 */
1174 public function retrieve_merchant_id($key = null)
1175 {
1176 try {
1177 $response = !is_null($key) && !empty($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1178 PayplugWoocommerceHelper::set_transient_data($response);
1179 $merchant_id = isset($response['httpResponse']['id']) ? $response['httpResponse']['id'] : '';
1180 } catch (ConfigurationException $e) {
1181 PayplugGateway::log(sprintf('Missing API key for PayPlug client : %s', wc_print_r($e->getMessage(), true)), 'error');
1182
1183 $merchant_id = '';
1184 } catch (HttpException $e) {
1185 PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error');
1186
1187 $merchant_id = '';
1188 } catch (\Exception $e) {
1189 PayplugGateway::log(sprintf('Account request error : %s', wc_clean($e->getMessage())), 'error');
1190
1191 $merchant_id = '';
1192 }
1193
1194 return $merchant_id;
1195 }
1196
1197 /**
1198 * Generate Hidden HTML.
1199 *
1200 * @param string $key
1201 * @param array $data
1202 *
1203 * @return string
1204 */
1205 public function generate_hidden_html($key, $data)
1206 {
1207 $field_key = $this->get_field_key($key);
1208 $defaults = array(
1209 'title' => '',
1210 'disabled' => false,
1211 'class' => '',
1212 'css' => '',
1213 'placeholder' => '',
1214 'type' => 'text',
1215 'desc_tip' => false,
1216 'description' => '',
1217 'custom_attributes' => array(),
1218 );
1219
1220 $data = wp_parse_args($data, $defaults);
1221
1222 ob_start();
1223 ?>
1224 <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)); ?>" />
1225 <?php
1226
1227 return ob_get_clean();
1228 }
1229
1230 /**
1231 * Generate Yes/No Input HTML.
1232 *
1233 * @param string $key
1234 * @param array $data
1235 *
1236 * @return string
1237 */
1238 public function generate_yes_no_html($key, $data)
1239 {
1240 $field_key = $this->get_field_key($key);
1241 $defaults = array(
1242 'title' => '',
1243 'no' => 'No',
1244 'yes' => 'Yes',
1245 'disabled' => false,
1246 'class' => '',
1247 'css' => '',
1248 'placeholder' => '',
1249 'type' => 'text',
1250 'desc_tip' => false,
1251 'description' => '',
1252 'custom_attributes' => [],
1253 'hide_label' => false,
1254 );
1255
1256 $data = wp_parse_args($data, $defaults);
1257 $checked = 'yes' === $this->get_option($key) ? '1' : '0';
1258
1259 ob_start();
1260 ?>
1261 <tr valign="top">
1262 <?php if (!$data['hide_label']) : ?>
1263 <th scope="row" class="titledesc">
1264 <label for="<?php echo esc_attr($field_key); ?>">
1265 <?php echo wp_kses_post($data['title']); ?>
1266 <?php echo $this->get_tooltip_html($data); ?>
1267 </label>
1268 </th>
1269 <?php endif; ?>
1270 <td class="forminp">
1271 <fieldset>
1272 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1273 </legend>
1274 <div class="radio--custom">
1275 <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); ?>>
1276 <label for="<?php echo esc_attr($field_key); ?>-yes"><?php echo esc_html($data['yes']); ?></label>
1277 </div>
1278 <div class="radio--custom">
1279 <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); ?>>
1280 <label for="<?php echo esc_attr($field_key); ?>-no"><?php echo esc_html($data['no']); ?></label>
1281 </div>
1282 <div id="live-mode-test-p"><?php echo $this->get_description_html($data); ?></div>
1283 </fieldset>
1284 </td>
1285 </tr>
1286 <?php
1287
1288 return ob_get_clean();
1289 }
1290
1291 /**
1292 * Generate Radio Input HTML.
1293 *
1294 * @param string $key
1295 * @param array $data
1296 *
1297 * @return string
1298 */
1299 public function generate_radio_html($key, $data)
1300 {
1301 $field_key = $this->get_field_key($key);
1302 $defaults = array(
1303 'title' => '',
1304 'disabled' => false,
1305 'class' => '',
1306 'css' => '',
1307 'placeholder' => '',
1308 'type' => 'text',
1309 'desc_tip' => false,
1310 'description' => '',
1311 'custom_attributes' => [],
1312 'options' => [],
1313 );
1314
1315 $data = wp_parse_args($data, $defaults);
1316
1317 ob_start();
1318 ?>
1319 <tr valign="top">
1320 <th scope="row" class="titledesc">
1321 <label for="<?php echo esc_attr($field_key); ?>">
1322 <?php echo wp_kses_post($data['title']); ?>
1323 <?php echo $this->get_tooltip_html($data); ?>
1324 </label>
1325 </th>
1326 <td class="forminp">
1327 <fieldset>
1328 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1329 </legend>
1330 <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1331 <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); ?>>
1332 <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value); ?></label>
1333 <?php endforeach; ?>
1334 <?php echo $this->get_description_html($data); ?>
1335 </fieldset>
1336 </td>
1337 </tr>
1338 <?php
1339
1340 return ob_get_clean();
1341 }
1342
1343 /**
1344 * Generate Login HTML.
1345 *
1346 * @param string $key
1347 * @param array $data
1348 *
1349 * @return string
1350 */
1351 public function generate_login_html($key, $data)
1352 {
1353 $field_key = $this->get_field_key($key);
1354 $defaults = [];
1355
1356 $data = wp_parse_args($data, $defaults);
1357
1358 ob_start();
1359 ?>
1360 <tr valign="top">
1361 <td class="forminp">
1362 <p><?php echo $this->get_option('email'); ?></p>
1363 <p>
1364 <input id="payplug-logout" type="submit" name="submit_logout" value="<?php _e('Logout', 'payplug'); ?>">
1365 <input type="hidden" name="save" value="logout">
1366 <?php wp_nonce_field('payplug_user_logout', '_logoutaction'); ?>
1367 |
1368 <a href="https://portal.payplug.com" target="_blank"><?php _e('Go to your PayPlug Portal', 'payplug'); ?></a>
1369 </p>
1370 </td>
1371 </tr>
1372 <?php
1373
1374 return ob_get_clean();
1375 }
1376
1377
1378 /**
1379 * Generate Oney popup option HTML.
1380 *
1381 * @param string $key
1382 * @param array $data
1383 *
1384 * @return string
1385 */
1386 public function generate_oney_product_animation_html($key, $data)
1387 {
1388 $field_key = $this->get_field_key($key);
1389
1390 $defaults = array(
1391 'title' => '',
1392 'disabled' => false,
1393 'class' => '',
1394 'css' => '',
1395 'placeholder' => '',
1396 'type' => 'checkbox',
1397 'desc_tip' => false,
1398 'description' => '',
1399 'custom_attributes' => [],
1400 'options' => [],
1401 );
1402
1403 $data = wp_parse_args($data, $defaults);
1404
1405 ob_start();
1406 ?>
1407 <tr valign="top" id="oney_installments_pop_up">
1408 <th scope="row" class="titledesc">
1409 <label for="<?php echo esc_attr($field_key); ?>">
1410 <?php echo wp_kses_post($data['title']); ?>
1411 <?php echo $this->get_tooltip_html($data); ?>
1412 </label>
1413 </th>
1414 <td class="forminp">
1415 <fieldset>
1416 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span></legend>
1417 <label for="woocommerce_payplug_oney_product_animation">
1418 <input class="" type="checkbox" name="woocommerce_payplug_oney_product_animation" id="woocommerce_payplug_oney_product_animation" style="" <?php echo (($this->oney_product_animation == 'yes') ? "checked" : ''); ?>> <?php echo wp_kses_post($data['label']); ?></label><br>
1419 <p class="description"> <?php echo wp_kses_post($data['description']); ?></p>
1420 </fieldset>
1421 </td>
1422 </tr>
1423 <?php
1424
1425 return ob_get_clean();
1426 }
1427
1428 /**
1429 * Generate Oney Type Input HTML.
1430 *
1431 * @param string $key
1432 * @param array $data
1433 *
1434 * @return string
1435 */
1436 public function generate_oney_type_html($key, $data)
1437 {
1438 $field_key = $this->get_field_key($key);
1439 $defaults = array(
1440 'title' => '',
1441 'disabled' => false,
1442 'class' => '',
1443 'css' => '',
1444 'placeholder' => '',
1445 'type' => 'text',
1446 'desc_tip' => false,
1447 'description' => '',
1448 'custom_attributes' => [],
1449 'options' => [],
1450 );
1451
1452 $data = wp_parse_args($data, $defaults);
1453
1454 ob_start();
1455 ?>
1456 <tr valign="top" id="woocommerce_payplug_oney_type">
1457 <th scope="row" class="titledesc" style="padding-top: 0px;">
1458 <label for="<?php echo esc_attr($field_key); ?>">
1459 <?php echo wp_kses_post($data['title']); ?>
1460 <?php echo $this->get_tooltip_html($data); ?>
1461 </label>
1462 </th>
1463 <td class="forminp" style="padding-top: 0px;">
1464 <fieldset>
1465 <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1466 </legend>
1467 <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1468 <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); ?>>
1469 <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" style="margin-right: 20px !important;">
1470 <span style="font-weight: 500;"><?php echo esc_html($option_value); ?></span>
1471 <span style="color:#646970;"> : <?php echo $data['descriptions'][$option_key] ;?></span>
1472 </label>
1473 <?php endforeach; ?>
1474 <?php echo $this->get_description_html($data); ?>
1475 </fieldset>
1476 </td>
1477 </tr>
1478 <?php
1479
1480 return ob_get_clean();
1481 }
1482
1483 /**
1484 * Generate Oney Thresholds Input HTML.
1485 *
1486 * @param string $key
1487 * @param array $data
1488 *
1489 * @return string
1490 */
1491 public function generate_oney_thresholds_html($key, $data)
1492 {
1493 $field_key = $this->get_field_key($key);
1494 $defaults = array(
1495 'title' => '',
1496 'disabled' => false,
1497 'class' => '',
1498 'css' => '',
1499 'placeholder' => '',
1500 'type' => 'text',
1501 'desc_tip' => false,
1502 'description' => '',
1503 'custom_attributes' => [],
1504 'options' => [],
1505 );
1506
1507 $data = wp_parse_args($data, $defaults);
1508
1509 ob_start();
1510 ?>
1511 <tr valign="top" id="woocommerce_payplug_oney_thresholds">
1512 <th scope="row" class="titledesc" style="padding-top: 0px;">
1513 <label for="<?php echo esc_attr($field_key); ?>">
1514 <?php echo wp_kses_post($data['title']); ?>
1515 <?php echo $this->get_tooltip_html($data); ?>
1516 </label>
1517 </th>
1518 <td class="forminp" style="padding-top: 0px;">
1519 <fieldset>
1520 <div id="oney_thresholds_description"><?php echo $this->get_description_html($data); ?></div>
1521 <input type="number" id="payplug_oney_thresholds_min" min="<?php echo $this->min_oney_price;?>" max="<?php echo $this->max_oney_price;?>" class="payplug-admin-oney-threshold-input">
1522 <b class="d-inline-block">€</b>
1523 <div class="d-inline-block" id="slider-range"></div>
1524 <input type="number" id="payplug_oney_thresholds_max" min="<?php echo $this->min_oney_price;?>" max="<?php echo $this->max_oney_price;?>" class="payplug-admin-oney-threshold-input">
1525 <b class="d-inline-block">€</b>
1526 </fieldset>
1527 </td>
1528 </tr>
1529 <?php
1530
1531 return ob_get_clean();
1532 }
1533
1534 /**
1535 * Validate Radio Field.
1536 *
1537 * Make sure the data is escaped correctly, etc.
1538 *
1539 * @param string $key
1540 * @param string|null $value Posted Value
1541 *
1542 * @return string
1543 */
1544 public function validate_radio_field($key, $value)
1545 {
1546 $value = is_null($value) ? '' : $value;
1547
1548 return wc_clean(stripslashes($value));
1549 }
1550
1551 /**
1552 * Validate Yes/No Field.
1553 *
1554 * @param string $key
1555 * @param string $value Posted Value
1556 *
1557 * @return string
1558 */
1559 public function validate_yes_no_field($key, $value)
1560 {
1561 return ('1' === (string) $value) ? 'yes' : 'no';
1562 }
1563
1564 /**
1565 * Validate Yes/No Field.
1566 *
1567 * @param string $key
1568 * @param string $value Posted Value
1569 *
1570 * @return string
1571 */
1572 public function validate_oney_product_animation_field($key, $value)
1573 {
1574 return ('on' === (string) $value) ? 'yes' : 'no';
1575 }
1576
1577 /**
1578 * Validate Oney Type Field.
1579 *
1580 * Make sure the data is escaped correctly, etc.
1581 *
1582 * @param string $key
1583 * @param string|null $value Posted Value
1584 *
1585 * @return string
1586 */
1587 public function validate_oney_type_field($key, $value)
1588 {
1589 $value = is_null($value) ? 'with_fees' : $value;
1590
1591 return wc_clean(stripslashes($value));
1592 }
1593
1594 /**
1595 * Validate Oney Thresholds Field.
1596 *
1597 * Make sure the data is escaped correctly, etc.
1598 *
1599 * @param string $key
1600 * @param string|null $value Posted Value
1601 *
1602 * @return string
1603 */
1604 public function validate_oney_thresholds_field($key, $value)
1605 {
1606 $value = is_null($value) ? [100, 3000] : $value;
1607
1608 return wc_clean($value);
1609 }
1610
1611 /**
1612 * Get PayPlug gateway mode.
1613 *
1614 * @return string
1615 */
1616 public function get_current_mode()
1617 {
1618 return ('yes' === $this->get_option('mode')) ? 'live' : 'test';
1619 }
1620
1621 /**
1622 * Get user API key.
1623 *
1624 * @param string $mode
1625 *
1626 * @return string
1627 */
1628 public function get_api_key($mode = 'test')
1629 {
1630
1631 switch ($mode) {
1632 case 'test':
1633 $key = $this->get_option('payplug_test_key');
1634 break;
1635 case 'live':
1636 $key = $this->get_option('payplug_live_key');
1637 break;
1638 default:
1639 $key = '';
1640 break;
1641 }
1642
1643 return $key;
1644 }
1645
1646 /**
1647 * Check if an api key exist for a mode.
1648 *
1649 * @param string $mode
1650 *
1651 * @return bool
1652 */
1653 public function has_api_key($mode = 'test')
1654 {
1655 $key = $this->get_api_key($mode);
1656 $key = trim($key);
1657
1658 return !empty($key);
1659 }
1660
1661 /**
1662 * Get current merchant id.
1663 *
1664 * @return string
1665 */
1666 public function get_merchant_id()
1667 {
1668 return $this->get_option('payplug_merchant_id', '');
1669 }
1670
1671 /**
1672 * Check if user is logged in and we have an API key for TEST mode.
1673 *
1674 * @return bool
1675 */
1676 public function user_logged_in()
1677 {
1678 return !empty($this->get_option('payplug_test_key'));
1679 }
1680
1681 /**
1682 * Check if oneclick payment is activated and merchant can use it.
1683 *
1684 * @return bool
1685 */
1686 public function oneclick_available()
1687 {
1688 return $this->user_logged_in()
1689 && $this->oneclick
1690 && $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD);
1691 }
1692
1693 /**
1694 * Check if the gatteway is allowed for the order amount
1695 *
1696 * @param array
1697 * @return array
1698 */
1699 public function check_gateway($gateways)
1700 {
1701 if ( !empty( WC()->cart ) && isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1702 $order_amount = $this->get_order_total();
1703 if ($order_amount < self::MIN_AMOUNT || $order_amount > self::MAX_AMOUNT) {
1704 unset($gateways[$this->id]);
1705 }
1706 }
1707
1708 //FIXME:: refactoring to remove this - we should see this on the oney classes and not here
1709 if($this->oney_type == 'with_fees'){
1710 unset($gateways['oney_x3_without_fees']);
1711 unset($gateways['oney_x4_without_fees']);
1712 } else{
1713 unset($gateways['oney_x3_with_fees']);
1714 unset($gateways['oney_x4_with_fees']);
1715 }
1716
1717 return $gateways;
1718 }
1719
1720 /**
1721 * Can the order be refunded via this gateway?
1722 *
1723 *
1724 * @param WC_Order $order Order object.
1725 * @return bool If false, the automatic refund button is hidden in the UI.
1726 */
1727 public function can_refund_order($order)
1728 {
1729 $status = $order->get_status();
1730 return $order && $this->supports('refunds') && $status !== "cancelled" && $status !== "failed";
1731 }
1732
1733 public function getPayplugMerchantCountry(){
1734 return $this->payplug_merchant_country;
1735 }
1736
1737 public function setPayplugMerchantCountry($country){
1738 $this->payplug_merchant_country = $country;
1739 }
1740
1741 }
1742