add('payplug_gateway', $message)
: self::$log->log($level, $message, array('source' => 'payplug_gateway'));
}
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();
}
$this->init_form_fields();
$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');
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)
{
$payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->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'])) {
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;
}
$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()
{
$oney_range = PayplugWoocommerceHelper::get_min_max_oney();
$min_oney_price = (isset($oney_range['min'])) ? $oney_range['min'] : 100;
$max_oney_price = (isset($oney_range['max'])) ? $oney_range['max'] : 3000;
$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 during checkout.', 'payplug'),
'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
'desc_tip' => true,
],
'description' => [
'title' => __('Description', 'payplug'),
'type' => 'text',
'description' => __('The payment solution description displayed during checkout.', 'payplug'),
'default' => '',
'desc_tip' => true,
],
'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',
'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' => true,
'options' => array(
'redirect' => __('Redirect', 'payplug'),
'embedded' => __('Integrated', 'payplug'),
),
],
'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' => true,
],
'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' => true
],
'oney' => [
'title' => __('Split Oney Payment', 'payplug'),
'type' => 'checkbox',
'label' => __('Activate', 'payplug'),
// TRAD
'description' => sprintf(__('Allow customers to spread out payments over 3 or 4 installments from %s€ to %s€.', 'payplug'), $min_oney_price, $max_oney_price),
'default' => 'no',
'desc_tip' => true
]
];
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_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'),
));
if ($this->user_logged_in() && false === $this->has_api_key('live')) {
add_action('admin_footer', function () {
$email = $this->get_option('email');
?>
get_option('email'); ?>