add('payplug_gateway', $message)
: self::$log->log($level, $message, array('source' => 'payplug_gateway'));
}
/**
* Construct method
*
* @return void
*/
public function __construct()
{
$this->id = 'payplug';
$this->icon = '';
$this->has_fields = false;
$this->method_title = _x('PayPlug', 'Gateway method title', 'payplug');
$this->method_description = __('Enable PayPlug for your customers.', 'payplug');
$this->supports = array(
'products',
'refunds',
'tokenization',
);
$this->new_method_label = __('Pay with another credit card', 'payplug');
$this->init_settings();
$this->requirements = new PayplugGatewayRequirements($this);
if ($this->user_logged_in()) {
$this->init_payplug();
}else{
delete_option('woocommerce_payplug_settings');
set_transient( PayplugWoocommerceHelper::get_transient_key(get_option('woocommerce_payplug_settings', [])), null );
}
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
$this->debug = 'yes' === $this->get_option('debug', 'no');
$this->email = $this->get_option('email');
$this->payment_method = $this->get_option('payment_method');
$this->oneclick = 'yes' === $this->get_option('oneclick', 'no');
$this->oney_type = $this->get_option('oney_type', 'with_fees');
$oney_range = PayplugWoocommerceHelper::get_min_max_oney();
$this->min_oney_price = (isset($oney_range['min'])) ? intval($oney_range['min']) : 100;
$this->max_oney_price = (isset($oney_range['max'])) ? intval($oney_range['max']) : 3000;
$this->oney_thresholds_min = $this->get_option('oney_thresholds_min', $this->min_oney_price );
$this->oney_thresholds_max = $this->get_option('oney_thresholds_max', $this->max_oney_price );
$this->init_form_fields();
$this->payplug_merchant_country = PayplugWoocommerceHelper::get_payplug_merchant_country();
$this->oney_product_animation = $this->get_option('oney_product_animation');
add_filter('woocommerce_get_customer_payment_tokens', [$this, 'filter_tokens'], 10, 3);
self::$log_enabled = $this->debug;
// Ensure the description is not empty to correctly display users's save cards
if (empty($this->description) && 0 !== count($this->get_tokens())) {
$this->description = ' ';
}
if ('test' === $this->mode) {
$this->description .= " \n";
$this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug');
$this->description = trim($this->description);
}
add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
add_action('wp_enqueue_scripts', [$this, 'scripts']);
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
add_action('the_post', [$this, 'validate_payment']);
add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
}
/**
* Customize gateway title in emails.
*
* @param array $total_rows
* @param \WC_Order $order
*
* @return array
*
* @author Clément Boirie
*/
public function customize_gateway_title($total_rows, $order)
{
$get_payment_method = $this->id;
if( method_exists($order, "get_payment_method") ) {
$get_payment_method = $order->get_payment_method();
}
$payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $get_payment_method;
if (
$this->id !== $payment_method
|| !isset($total_rows['payment_method'])
) {
return $total_rows;
}
$total_rows['payment_method']['value'] = __('Credit card', 'payplug');
return $total_rows;
}
/**
* Validate order payment when the user is redirected to the success confirmation page.
*
* @throws \WC_Data_Exception
*/
public function validate_payment()
{
if (!is_wc_endpoint_url('order-received') || empty($_GET['key'])) {
return;
}
$order_id = wc_get_order_id_by_order_key(wc_clean($_GET['key']));
if (empty($order_id)) {
return;
}
$order = wc_get_order($order_id);
if (!$order instanceof \WC_Order) {
return;
}
$payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
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'])) {
return;
}
$transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
if (empty($transaction_id)) {
PayplugGateway::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
return;
}
try {
$payment = $this->api->payment_retrieve($transaction_id);
} catch (\Exception $e) {
PayplugGateway::log(
sprintf(
'Order #%s : An error occurred while retrieving the payment data with the message : %s',
$order_id,
$e->getMessage()
)
);
return;
}
//FIXME:: this is being runned 1 time for each gateway,
// this comparisson is only needed to only run the process_method one time
if($payment_method != $this->id){
return;
}
$this->response->process_payment($payment);
}
/**
* Get payment icons.
*
* @return string
*/
public function get_icon()
{
$src = ('it_IT' === get_locale())
? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_PostePay.svg'
: PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_CB.svg';
$icons = apply_filters('payplug_payment_icons', [
'payplug' => sprintf('', esc_url($src)),
]);
$icons_str = '';
foreach ($icons as $icon) {
$icons_str .= $icon;
}
return $icons_str;
}
/**
* Check if this gateway is enabled
*/
public function is_available()
{
if ('yes' === $this->enabled) {
return $this->requirements->satisfy_requirements() && !empty($this->get_api_key($this->get_current_mode()));
}
return parent::is_available();
}
/**
* Load gateway settings.
*/
public function init_settings()
{
parent::init_settings();
$this->enabled = !empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
}
/**
* Register gateway settings.
*/
public function init_form_fields()
{
$anchor = esc_html_x( __("More informations", 'payplug'), 'modal', 'payplug' );
$domain = __( 'support.payplug.com/hc/fr/articles/4408142346002', 'payplug' );
$link = sprintf( ' %s', $domain, $anchor );
$anchor_bancontact = esc_html_x( __("payplug_bancontact_activation_request", 'payplug'), 'modal', 'payplug' );
$domain_bancontact = __( 'payplug_bancontact_activation_url', 'payplug' );
$bancontact_call_to_action = sprintf( ' %s', $domain_bancontact, $anchor_bancontact );
$fields = [
'enabled' => [
'title' => __('Enable/Disable', 'payplug'),
'type' => 'checkbox',
'label' => __('Enable PayPlug', 'payplug'),
'description' => __('Only Euro payments can be processed with PayPlug.', 'payplug'),
'default' => 'no',
],
'title' => [
'title' => __('Title', 'payplug'),
'type' => 'text',
'description' => __('The payment solution title displayed to your customers during checkout', 'payplug'),
'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
'desc_tip' => false,
],
'description' => [
'title' => __('Description', 'payplug'),
'type' => 'text',
'description' => __('The payment solution description displayed to your customers during checkout', 'payplug'),
'default' => '',
'desc_tip' => false,
],
'title_connexion' => [
'title' => __('Connection', 'payplug'),
'type' => 'title',
],
'email' => [
'type' => 'hidden',
'default' => '',
],
'login' => [
'type' => 'login',
'default' => '',
],
'payplug_test_key' => [
'type' => 'hidden',
'default' => '',
],
'payplug_live_key' => [
'type' => 'hidden',
'default' => '',
],
'payplug_merchant_id' => [
'type' => 'hidden',
'default' => '',
],
'title_testmode' => [
'title' => __('Mode', 'payplug'),
'type' => 'title',
],
'mode' => [
'title' => '',
'label' => '',
'type' => 'yes_no',
'yes' => 'Live',
'no' => 'Test',
'description' => __('In TEST mode, all payments will be simulations and will not generate real transactions.', 'payplug'),
'default' => 'no',
'hide_label' => true,
],
'title_settings' => [
'title' => __('Settings', 'payplug'),
'type' => 'title',
],
'payment_method' => [
'title' => __('Payment page', 'payplug'),
'type' => 'radio',
'options' => array(
'redirect' => __('Redirect', 'payplug'),
'embedded' => __('Integrated', 'payplug'),
),
'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'),
'default' => 'redirect',
'desc_tip' => false
],
'debug' => [
'title' => __('Debug', 'payplug'),
'type' => 'checkbox',
'description' => __('Debug mode saves additional information on your server for each operation done via the PayPlug plugin (Developer setting).', 'payplug'),
'label' => __('Activate debug mode', 'payplug'),
'default' => 'yes',
'desc_tip' => false
],
'title_advanced_settings' => [
'title' => __('Advanced Settings', 'payplug'),
'description' => __(
'Your current offer does not allow this option. Try it on TEST mode. More information here.',
'payplug'
),
'type' => 'title',
],
'oneclick' => [
'title' => __('One Click Payment', 'payplug'),
'type' => 'checkbox',
'label' => __('Activate', 'payplug'),
'description' => __('Allow your customers to save their credit card information for later purchases.', 'payplug'),
'default' => 'no',
'desc_tip' => false
],
'bancontact' => [
'title' => __('payplug_bancontact_activate_title', 'payplug'),
'type' => 'checkbox',
'label' => __('Activate', 'payplug'),
'description' => '
'. __('payplug_bancontact_testmode_description', 'payplug') .'
' . ''. __('payplug_bancontact_livemode_description_disabled', 'payplug') .'
' . $bancontact_call_to_action, 'default' => 'no', ], 'apple_pay' => [ 'title' => __('payplug_apple_pay_activate_title', 'payplug'), 'type' => 'checkbox', 'label' => __('Activate', 'payplug'), 'description' => ''. __('payplug_apple_pay_testmode_description', 'payplug') .'
' . ''. __('payplug_apple_pay_livemode_description', 'payplug') .'
' , 'default' => 'no', ], 'american_express' => [ 'title' => __('payplug_amex_title', 'payplug'), 'type' => 'checkbox', 'label' => __('payplug_amex_activate', 'payplug'), 'description' => ''. __('payplug_amex_testmode_description', 'payplug') .'
' . ''. __('payplug_amex_livemode_description', 'payplug') .'
' , 'default' => 'no', ], 'oney' => [ 'title' => __('3x 4x Oney payments', 'payplug'), 'type' => 'checkbox', 'label' => __('Activate', 'payplug'), // TRAD '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, 'default' => 'no', 'desc_tip' => false ], 'oney_type' => [ 'title' => '', 'type' => 'oney_type', 'options' => array( 'with_fees' => __('Oney with fees', 'payplug'), 'without_fees' => __('Oney without fees', 'payplug'), ), 'descriptions' => array( 'with_fees' => __('The fees are split between you and your customers', 'payplug'), 'without_fees' => __('You pay the fees', 'payplug'), ), 'description' => '', 'default' => 'with_fees', 'desc_tip' => false ], 'oney_thresholds' => [ 'title' => '', 'type' => 'oney_thresholds', 'description' => sprintf(__('I would like to offer guaranteed payment in installments for amounts between %s€ and %s€.', 'payplug'), '' . $this->oney_thresholds_min . '', '' . $this->oney_thresholds_max . ''), 'desc_tip' => false ], 'oney_thresholds_min' => [ 'title' => '', 'type' => 'hidden', 'label' => '', 'description' => '', 'default' => 'no', ], 'oney_thresholds_max' => [ 'title' => '', 'type' => 'hidden', 'label' => '', 'description' => '', 'default' => 'no', ], 'oney_product_animation' => [ 'title' => __('oney_installments_pop_up', 'payplug'), 'description' => __('display_the_oney_installments_pop_up_on_the_product_page', 'payplug'), 'label' => __('Activate', 'payplug'), 'default' => 'no', 'desc_tip' => false, 'type' => 'oney_product_animation' ], ]; if ($this->user_logged_in()) { if ($this->permissions->has_permissions(PayplugPermissions::SAVE_CARD)) { unset($fields['title_advanced_settings']); } else if ('live' === $this->get_current_mode()){ $fields['oneclick']['disabled'] = true; } } /** * Filter PayPlug gateway settings. * * @param array $fields */ $fields = apply_filters('payplug_gateway_settings', $fields); $this->form_fields = $fields; } /** * Set global configuration for PayPlug instance. */ public function init_payplug() { $this->api = new PayplugApi($this); $this->api->init(); $this->permissions = new PayplugPermissions($this); $this->response = new PayplugResponse($this); // Register IPN handler new PayplugIpnResponse($this); } /** * Embedded payment form scripts. * * Register scripts and additionnal data needed for the * embedded payment form. */ public function scripts() { if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) { return; } // If PayPlug is not enabled bail. if ('no' === $this->enabled) { return; } // If keys are not set bail. if (empty($this->get_api_key($this->mode))) { PayplugGateway::log('Keys are not set correctly.'); return; } // Register checkout styles. wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION); wp_enqueue_style('payplug-checkout'); wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true); wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [ 'jquery', 'payplug' ], PAYPLUG_GATEWAY_VERSION, true); wp_localize_script('payplug-checkout', 'payplug_checkout_params', [ 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'), 'nonce' => [ 'checkout' => wp_create_nonce('woocommerce-process_checkout'), ], 'is_embedded' => 'redirect' !== $this->payment_method ]); wp_enqueue_script('payplug-checkout'); } /** * Filter saved tokens for the gateway. * * A token will be removed if : * - it doesn't match the current merchant logged in, * - or it doesn't match the current gateway mode, * - or it is expired. * * @param array $tokens * @param int $user_id * @param string $gateway_id * * @return array */ public function filter_tokens($tokens, $user_id, $gateway_id) { if (!is_user_logged_in() || !class_exists('WC_Payment_Gateway_CC')) { return $tokens; } /* @var \WC_Payment_Token_CC $token */ foreach ($tokens as $k => $token) { if ($this->id !== $token->get_gateway_id()) { continue; } // check if token is associated with a merchant id and if it match the current one $token_merchant_id = $token->get_meta('payplug_account', true); if (empty($token_merchant_id) || $this->get_merchant_id() !== $token_merchant_id) { unset($tokens[$k]); continue; } // check if token is available for the current gateway mode if ($this->mode !== $token->get_meta('mode', true)) { unset($tokens[$k]); continue; } // check if token is not expired $current_month = \absint(date('n')); $current_year = \absint(date('Y')); if ($current_year > (int) $token->get_expiry_year()) { unset($tokens[$k]); continue; } if ($current_year === (int) $token->get_expiry_year() && $current_month > (int) $token->get_expiry_month()) { unset($tokens[$k]); continue; } } return $tokens; } public function payment_fields() { $description = $this->get_description(); if (!empty($description)) { echo wpautop(wptexturize($description)); } if ($this->oneclick_available()) { $this->tokenization_script(); $this->saved_payment_methods(); } } /** * Handle admin display. */ public function admin_options() { wp_enqueue_style( 'payplug-gateway-style', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/app.css', [], PAYPLUG_GATEWAY_VERSION ); wp_enqueue_script( 'payplug-gateway-admin', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin.js', ['jquery-ui-dialog'], PAYPLUG_GATEWAY_VERSION ); wp_enqueue_script( 'payplug-gateway-admin-bancontact', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-bancontact.js', ['jquery-ui-dialog'], PAYPLUG_GATEWAY_VERSION ); wp_enqueue_script( 'payplug-gateway-admin-applepay', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-applepay.js', ['jquery-ui-dialog'], PAYPLUG_GATEWAY_VERSION ); wp_enqueue_script( 'payplug-gateway-admin-amex', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-amex.js', [], PAYPLUG_GATEWAY_VERSION ); wp_localize_script('payplug-gateway-admin-bancontact', 'payplug_admin_config', array( 'ajax_url' => admin_url('admin-ajax.php'), 'has_live_key' => (false === $this->has_api_key('live')) ? false : true, 'btn_ok' => _x('Ok', 'modal', 'payplug'), 'btn_label' => _x('Cancel', 'modal', 'payplug'), 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'), )); wp_localize_script('payplug-gateway-admin', 'payplug_admin_config', array( 'ajax_url' => admin_url('admin-ajax.php'), 'has_live_key' => (false === $this->has_api_key('live')) ? false : true, 'btn_ok' => _x('Ok', 'modal', 'payplug'), 'btn_label' => _x('Cancel', 'modal', 'payplug'), 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'), )); wp_enqueue_script( 'payplug-gateway-admin-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-oney.js', ['jquery-ui-dialog'], PAYPLUG_GATEWAY_VERSION ); wp_localize_script('payplug-gateway-admin-oney', 'payplug_admin_config', array( 'ajax_url' => admin_url('admin-ajax.php'), 'btn_ok' => _x('Ok', 'modal', 'payplug'), 'has_live_key' => (false === $this->has_api_key('live')) ? false : true, 'min_oney_price' => $this->min_oney_price, 'max_oney_price' => $this->max_oney_price, 'oney_thresholds_min' =>$this->oney_thresholds_min, 'oney_thresholds_max' => $this->oney_thresholds_max, )); if ($this->user_logged_in() && false === $this->has_api_key('live')) { add_action('admin_footer', function () { $email = $this->get_option('email'); ?>get_option('email'); ?>