| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Front; |
| 4 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x; |
| 5 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 |
|
| 7 |
// Exit if accessed directly |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class PayplugOneyDetail |
| 13 |
{ |
| 14 |
|
| 15 |
private $min_amount = "XXX"; |
| 16 |
private $max_amount = "XXX"; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
add_action( 'wp_ajax_simulate_oney_payment', [ $this, 'simulate_oney_payment' ]); |
| 21 |
add_action( 'wp_ajax_nopriv_simulate_oney_payment', [ $this, 'simulate_oney_payment' ]); |
| 22 |
add_action( 'template_redirect', [ $this, 'check_oney_frontend' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Check if Oney can add JS & CSS in Shop |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function check_oney_frontend() { |
| 31 |
if ( is_cart() || is_checkout()) { |
| 32 |
//if ( is_cart() || is_product() || is_checkout()) { |
| 33 |
if(PayplugWoocommerceHelper::is_oney_available()) { |
| 34 |
// Product page |
| 35 |
//add_action('woocommerce_single_product_summary', [$this, 'oney_simulate_payment_detail']); |
| 36 |
|
| 37 |
// Total cart |
| 38 |
add_action('woocommerce_cart_totals_after_order_total', [$this, 'oney_simulate_payment_detail']); |
| 39 |
|
| 40 |
// Add CSS |
| 41 |
add_action( 'wp_enqueue_scripts', [$this, 'add_oney_css'] ); |
| 42 |
|
| 43 |
// Add Js |
| 44 |
add_action( 'wp_enqueue_scripts', [$this, 'add_oney_js'] ); |
| 45 |
|
| 46 |
// Add Scripts |
| 47 |
add_action( 'wp_enqueue_scripts', [$this, 'add_oney_script'] ); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Add CSS |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function add_oney_script() { |
| 58 |
wp_localize_script('payplug-oney', 'payplug_config', array( |
| 59 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 60 |
'ajax_action' => 'simulate_oney_payment' |
| 61 |
)); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add CSS |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function add_oney_css() { |
| 70 |
wp_enqueue_style('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-oney.css', [], PAYPLUG_GATEWAY_VERSION); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add JS |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function add_oney_js() { |
| 79 |
wp_enqueue_script('payplug-oney-mobile', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-detect-mobile.js', [], PAYPLUG_GATEWAY_VERSION, true); |
| 80 |
wp_enqueue_script('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-oney.js', [ |
| 81 |
'jquery', |
| 82 |
'jquery-ui-position' |
| 83 |
], PAYPLUG_GATEWAY_VERSION, true); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Simulate Oney Payment |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function simulate_oney_payment() { |
| 92 |
$total_price = $_POST['price']; |
| 93 |
$oney_range = PayplugWoocommerceHelper::get_min_max_oney(); |
| 94 |
$this->min_amount = $oney_range['min']; |
| 95 |
$this->max_amount = $oney_range['max']; |
| 96 |
if ($total_price < $this->min_amount || $total_price > $this->max_amount) { |
| 97 |
$oney_response = false; |
| 98 |
} else { |
| 99 |
try { |
| 100 |
$api = new \Payplug\PayplugWoocommerce\Gateway\PayplugApi(new PayplugGatewayOney3x()); |
| 101 |
$api->init(); |
| 102 |
$oney_response = $api->simulate_oney_payment($total_price); |
| 103 |
} catch (\Exception $e) { |
| 104 |
PayplugGatewayOney3x::log("Simulate Oney Payment, " . $e->getMessage() ); |
| 105 |
$oney_response = null; |
| 106 |
} |
| 107 |
} |
| 108 |
$result = $this->get_simulate_oney_payment_popup($oney_response); |
| 109 |
wp_send_json_success( |
| 110 |
array( |
| 111 |
'popup' => $result |
| 112 |
) |
| 113 |
); |
| 114 |
wp_die(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Show oney simulation details |
| 119 |
* |
| 120 |
* @param $oney_response |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public function get_simulate_oney_payment_popup($oney_response) { |
| 124 |
|
| 125 |
$cgv = sprintf(__("Offre de financement avec apport obligatoire, |
| 126 |
réservée aux particuliers et valable pour tout achat de %s€ à %s€. |
| 127 |
Sous réserve d'acceptation par Oney Bank. Vous disposez d'un délai de 14 jours pour renoncer à votre crédit. |
| 128 |
Oney Bank - SA au capital de 51 286 585€ - 34 Avenue de Flandre 59170 Croix - 546 380 197 RCS Lille Métropole - n° Orias 07 023 261 www.orias.fr |
| 129 |
Correspondance : CS 60 006 - 59895 Lille Cedex - www.oney.fr", "payplug"), $this->min_amount, $this->max_amount); |
| 130 |
$f = function($fn) { return $fn; }; |
| 131 |
|
| 132 |
if($oney_response) { |
| 133 |
if(is_array($oney_response)) { |
| 134 |
$financing_cost_3x = intval($oney_response['x3_with_fees']['total_cost']) / 100; |
| 135 |
$financing_cost_4x = intval($oney_response['x4_with_fees']['total_cost']) / 100; |
| 136 |
$popup_content = " |
| 137 |
<div id='oney-popup-close'> |
| 138 |
<div class='oney-popup-close-mdiv'> |
| 139 |
<div class='oney-popup-close-md'></div> |
| 140 |
</div> |
| 141 |
</div> |
| 142 |
<div class='oney-img oney-logo no-margin'></div> |
| 143 |
<div class='oney-title'> |
| 144 |
<p class='no-margin oney-color'>{$f(__('PAYMENT', 'payplug'))} </p> |
| 145 |
<p class='no-margin bold oney-color'>{$f(__('BY CREDIT CARD', 'payplug'))}</p> |
| 146 |
</div> |
| 147 |
<div class='oney-content oney-3x-content'> |
| 148 |
<div class='oney-img oney-3x no-margin'></div> |
| 149 |
<div class='oney-details'> |
| 150 |
<p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$oney_response['x3_with_fees']['down_payment_amount']} €</p> |
| 151 |
<p class='bold no-margin'>+2 {$f(__('monthly payment of', 'payplug'))} : {$oney_response['x3_with_fees']['installments'][0]['amount']} € </p> |
| 152 |
<p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$financing_cost_3x} € </p> |
| 153 |
<p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$oney_response['x3_with_fees']['effective_annual_percentage_rate']} % </p> |
| 154 |
</div> |
| 155 |
</div> |
| 156 |
<div class='oney-separator'></div> |
| 157 |
<div class='oney-content oney-4x-content'> |
| 158 |
<div class='oney-img oney-4x no-margin'></div> |
| 159 |
<div class='oney-details'> |
| 160 |
<p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$oney_response['x4_with_fees']['down_payment_amount']} €</p> |
| 161 |
<p class='bold no-margin'>+3 {$f(__('monthly payment of', 'payplug'))} : {$oney_response['x4_with_fees']['installments'][0]['amount']} € </p> |
| 162 |
<p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$financing_cost_4x} € </p> |
| 163 |
<p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$oney_response['x4_with_fees']['effective_annual_percentage_rate']} % </p> |
| 164 |
</div> |
| 165 |
</div>"; |
| 166 |
} else { |
| 167 |
$popup_content = $oney_response; |
| 168 |
} |
| 169 |
|
| 170 |
$popup = <<<HTML |
| 171 |
$popup_content |
| 172 |
<div class='oney-content oney-cgv-content'> |
| 173 |
$cgv |
| 174 |
</div> |
| 175 |
HTML; |
| 176 |
|
| 177 |
} else { |
| 178 |
$popup = " |
| 179 |
<div class='oney-content oney-cgv-content'> |
| 180 |
</div>"; |
| 181 |
} |
| 182 |
|
| 183 |
return $popup; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Button to show oney popup |
| 188 |
* |
| 189 |
* @return void |
| 190 |
*/ |
| 191 |
public function oney_simulate_payment_detail() |
| 192 |
{ |
| 193 |
global $product; |
| 194 |
$total_price = (is_cart()) ? floatval(WC()->cart->total) : (float) ($product->get_price()); |
| 195 |
|
| 196 |
$total_products = 1; |
| 197 |
if(is_cart()) { |
| 198 |
$total_products = 0; |
| 199 |
foreach(WC()->cart->cart_contents as $product) { |
| 200 |
$total_products += $product['quantity']; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
$oney_range = PayplugWoocommerceHelper::get_min_max_oney(); |
| 205 |
$this->min_amount = $oney_range['min']; |
| 206 |
$this->max_amount = $oney_range['max']; |
| 207 |
$disabled = ""; |
| 208 |
|
| 209 |
if ($total_price < $this->min_amount || $total_price > $this->max_amount || $total_products >= PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM) { |
| 210 |
$disabled = "disabled"; |
| 211 |
} |
| 212 |
?> |
| 213 |
<div class="payplug-oney <?php echo $disabled; ?>" |
| 214 |
data-is-cart="<?php echo is_cart() ? 1 : 0; ?>" |
| 215 |
data-total-products="<?php echo $total_products; ?>" |
| 216 |
data-price="<?php echo $total_price ?>" |
| 217 |
data-max-oney-qty="<?php echo PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM; ?>" |
| 218 |
data-min-oney="<?php echo $this->min_amount; ?>" |
| 219 |
data-max-oney="<?php echo $this->max_amount; ?>"> |
| 220 |
<?php echo __('OR PAY IN', 'payplug'); ?> |
| 221 |
<div class="payplug-oney-popup"> |
| 222 |
<div class="oney-img oney-3x4x"></div> |
| 223 |
<div id="oney-show-popup" class="bold oney-color">?</div> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
<div class="payplug-oney <?php echo $disabled; ?>" id="oney-popup"> |
| 227 |
<div class="payplug-lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div> |
| 228 |
<div id="oney-popup-error"> |
| 229 |
<div class="oney-error range"> |
| 230 |
<?php echo sprintf(__('The total amount of your order should be between %s€ and %s€ to pay with Oney.', 'payplug'), $this->min_amount, $this->max_amount);?> |
| 231 |
</div> |
| 232 |
<div class="oney-error qty"> |
| 233 |
<?php echo sprintf(__('The payment with Oney is unavailable because you have more than %s items in your cart.', 'payplug'), PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM);?> |
| 234 |
</div> |
| 235 |
</div> |
| 236 |
</div> |
| 237 |
<?php |
| 238 |
} |
| 239 |
} |
| 240 |
|