PluginProbe
PayPlug for WooCommerce (Official) / 1.0.17
PayPlug for WooCommerce (Official) v1.0.17
3.1.0 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 All 102 releases
payplug / src / Gateway / PayplugGateway.php

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

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