PluginProbe
PayPlug for WooCommerce (Official) / 1.2.3
PayPlug for WooCommerce (Official) v1.2.3
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 / Front / PayplugOneyDetail.php

PayplugOneyDetail.php in PayPlug for WooCommerce (Official) 1.2.3, at src/Front/PayplugOneyDetail.php

221 lines 8.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\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_product() || is_checkout()) {
32 if(PayplugWoocommerceHelper::is_oney_available()) {
33 // Product page
34 add_action('woocommerce_single_product_summary', [$this, 'oney_simulate_payment_detail']);
35
36 // Total cart
37 add_action('woocommerce_cart_totals_after_order_total', [$this, 'oney_simulate_payment_detail']);
38
39 // Add CSS
40 add_action( 'wp_enqueue_scripts', [$this, 'add_oney_css'] );
41 // Add Js
42 add_action( 'wp_enqueue_scripts', [$this, 'add_oney_js'] );
43 // Add Scripts
44 add_action( 'wp_enqueue_scripts', [$this, 'add_oney_script'] );
45 }
46 }
47 }
48
49 /**
50 * Add CSS
51 *
52 * @return void
53 */
54 public function add_oney_script() {
55 wp_localize_script('payplug-oney', 'payplug_config', array(
56 'ajax_url' => admin_url('admin-ajax.php'),
57 'ajax_action' => 'simulate_oney_payment'
58 ));
59 }
60
61 /**
62 * Add CSS
63 *
64 * @return void
65 */
66 public function add_oney_css() {
67 wp_enqueue_style('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-oney.css', [], PAYPLUG_GATEWAY_VERSION);
68 }
69
70 /**
71 * Add JS
72 *
73 * @return void
74 */
75 public function add_oney_js() {
76 wp_enqueue_script('payplug-oney-mobile', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-detect-mobile.js', [], PAYPLUG_GATEWAY_VERSION, true);
77 wp_enqueue_script('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-oney.js', [
78 'jquery',
79 'jquery-ui-position'
80 ], PAYPLUG_GATEWAY_VERSION, true);
81 }
82
83 /**
84 * Simulate Oney Payment
85 *
86 * @return void
87 */
88 public function simulate_oney_payment() {
89 $total_price = $_POST['price'];
90 $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
91 $this->min_amount = $oney_range['min'];
92 $this->max_amount = $oney_range['max'];
93 if ($total_price < $this->min_amount || $total_price > $this->max_amount) {
94 $oney_response = false;
95 } else {
96 try {
97 $api = new \Payplug\PayplugWoocommerce\Gateway\PayplugApi(new PayplugGatewayOney3x());
98 $api->init();
99 $oney_response = $api->simulate_oney_payment($total_price);
100 } catch (\Exception $e) {
101 PayplugGatewayOney3x::log("Simulate Oney Payment, " . $e->getMessage() );
102 $oney_response = null;
103 }
104 }
105 $result = $this->get_simulate_oney_payment_popup($oney_response);
106 wp_send_json_success(
107 array(
108 'popup' => $result
109 )
110 );
111 wp_die();
112 }
113
114 /**
115 * Show oney simulation details
116 *
117 * @param $oney_response
118 * @return string
119 */
120 public function get_simulate_oney_payment_popup($oney_response) {
121
122 $cgv = sprintf(__("Offre de financement avec apport obligatoire,
123 réservée aux particuliers et valable pour tout achat de %s€ à %s€.
124 Sous réserve d'acceptation par Oney Bank. Vous disposez d'un délai de 14 jours pour renoncer à votre crédit.
125 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
126 Correspondance : CS 60 006 - 59895 Lille Cedex - www.oney.fr", "payplug"), $this->min_amount, $this->max_amount);
127 $f = function($fn) { return $fn; };
128
129 if($oney_response) {
130 $popup = "
131 <div class='oney-img oney-logo no-margin'></div>
132 <div class='oney-title'>
133 <p class='no-margin oney-color'>{$f(__('PAYMENT', 'payplug'))} </p>
134 <p class='no-margin bold oney-color'>{$f(__('BY CREDIT CARD', 'payplug'))}</p>
135 </div>
136 <div class='oney-content oney-3x-content'>
137 <div class='oney-img oney-3x no-margin'></div>
138 <div class='oney-details'>
139 <p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$oney_response['x3_with_fees']['down_payment_amount']} €</p>
140 <p class='bold no-margin'>+2 {$f(__('monthly payment of', 'payplug'))} : {$oney_response['x3_with_fees']['installments'][0]['amount']} € </p>
141 <p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$oney_response['x3_with_fees']['total_cost']} € </p>
142 <p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$oney_response['x3_with_fees']['effective_annual_percentage_rate']} % </p>
143 </div>
144 </div>
145 <div class='oney-separator'></div>
146 <div class='oney-content oney-4x-content'>
147 <div class='oney-img oney-4x no-margin'></div>
148 <div class='oney-details'>
149 <p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$oney_response['x4_with_fees']['down_payment_amount']} €</p>
150 <p class='bold no-margin'>+3 {$f(__('monthly payment of', 'payplug'))} : {$oney_response['x4_with_fees']['installments'][0]['amount']} € </p>
151 <p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$oney_response['x4_with_fees']['total_cost']} € </p>
152 <p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$oney_response['x4_with_fees']['effective_annual_percentage_rate']} % </p>
153 </div>
154 </div>
155 <div class='oney-content oney-cgv-content'>
156 {$cgv}
157 </div>";
158 } else {
159 $popup = "
160 <div id='oney-popup-arrow' class='triangle-left'></div>
161 <div class='oney-content oney-cgv-content'>
162 </div>";
163 }
164
165 return $popup;
166 }
167
168 /**
169 * Button to show oney popup
170 *
171 * @return void
172 */
173 public function oney_simulate_payment_detail()
174 {
175 global $product;
176 $total_price = (is_cart()) ? floatval(WC()->cart->total) : (float) ($product->get_price());
177 $total_products = 0;
178 if(is_cart()) {
179 foreach(WC()->cart->cart_contents as $product) {
180 $total_products += $product['quantity'];
181 }
182 }
183
184 $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
185 $this->min_amount = $oney_range['min'];
186 $this->max_amount = $oney_range['max'];
187 $disabled = "";
188 if ($total_price < $this->min_amount || $total_price > $this->max_amount || $total_products >= PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM) {
189 $disabled = "disabled";
190 }
191
192 ?>
193 <div class="payplug-oney <?php echo $disabled; ?>"
194 data-is-cart="<?php echo is_cart() ? 1 : 0; ?>"
195 data-total-products="<?php echo $total_products; ?>"
196 data-price="<?php echo $total_price ?>"
197 data-max-oney-qty="<?php echo PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM; ?>"
198 data-min-oney="<?php echo $this->min_amount; ?>"
199 data-max-oney="<?php echo $this->max_amount; ?>">
200 <?php echo __('OR PAY IN', 'payplug'); ?>
201 <div class="payplug-oney-popup">
202 <div class="oney-img oney-3x4x"></div>
203 <div id="oney-show-popup" class="bold oney-color">?</div>
204 </div>
205 </div>
206 <div class="payplug-oney <?php echo $disabled; ?>" id="oney-popup">
207 <div id='oney-popup-arrow' class='triangle-left'></div>
208 <div class="payplug-lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
209 <div id="oney-popup-error">
210 <div class="oney-error range">
211 <?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);?>
212 </div>
213 <div class="oney-error qty">
214 <?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);?>
215 </div>
216 </div>
217 </div>
218 <?php
219 }
220 }
221