PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.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 1.2.6 All 101 releases
← All changes | src/Gateway/PayplugGateway.php +730 -381 1.2.52.9.0 View file →
@@ -9,17 +9,20 @@
9 9
10 10 use Payplug\Authentication;
11 11 use Payplug\Exception\ConfigurationException;
12 12 use Payplug\Exception\HttpException;
13 +use Payplug\Exception\ForbiddenException;
13 14 use Payplug\Payplug;
14 15 use Payplug\PayplugWoocommerce\Admin\Ajax;
16 +use Payplug\PayplugWoocommerce\Controller\IntegratedPayment;
17 +use Payplug\PayplugWoocommerce\Helper\Lock;
15 18 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
16 19 use Payplug\Resource\Payment as PaymentResource;
17 20 use Payplug\Resource\Refund as RefundResource;
21 +use WC_Blocks_Utils;
18 22 use WC_Payment_Gateway_CC;
19 23 use WC_Payment_Tokens;
20 24
21 -
22 25 /**
23 26 * PayPlug WooCommerce Gateway.
24 27 *
25 28 * @package Payplug\PayplugWoocommerce\Gateway
@@ -25,10 +28,11 @@
25 28 * @package Payplug\PayplugWoocommerce\Gateway
26 29 */
27 30 class PayplugGateway extends WC_Payment_Gateway_CC
28 31 {
32 + const OPTION_NAME = "payplug_config";
29 33
30 - /**
34 + /**
31 35 * @var PayplugGatewayRequirements
32 36 */
33 37 private $requirements;
34 38
@@ -61,14 +65,26 @@
61 65 * @var float
62 66 */
63 67 const MIN_AMOUNT = 0.99;
64 68
65 - /**
66 - * @var float
67 - */
68 - const MAX_AMOUNT = 20000;
69 + /**
70 + * @var float
71 + */
72 + const MAX_AMOUNT = 20000;
69 73
70 - /**
74 + /**
75 + * @var string
76 + */
77 + private $payplug_merchant_country = 'FR';
78 +
79 + protected $oney_response;
80 + public $min_oney_price, $oney_thresholds_min;
81 + public $max_oney_price, $oney_thresholds_max;
82 +
83 + const ENABLE_ON_TEST_MODE = true;
84 +
85 +
86 + /**
71 87 * Logging method.
72 88 *
73 89 * @param string $message Log message.
74 90 * @param string $level Optional. Default 'info'.
@@ -88,10 +104,22 @@
88 104 ? self::$log->add('payplug_gateway', $message)
89 105 : self::$log->log($level, $message, array('source' => 'payplug_gateway'));
90 106 }
91 107
108 + /**
109 + * Construct method
110 + *
111 + * @return void
112 + */
92 113 public function __construct()
93 114 {
115 + $payplug_gateways = array('payplug', 'american_express', 'apple_pay', 'bancontact', 'oney_x3_with_fees', 'oney_x3_without_fees', 'oney_x4_with_fees', 'oney_x4_without_fees', 'satispay', 'sofort', 'ideal', 'mybank');
116 +
117 + if ((!empty($_GET['section'])) && (in_array($_GET['section'], $payplug_gateways))) {
118 + $GLOBALS['hide_save_button'] = true;
119 + }
120 +
121 + //TODO: this should be properties of the class and implemented an interface on all classes (set values)
94 122 $this->id = 'payplug';
95 123 $this->icon = '';
96 124 $this->has_fields = false;
97 125 $this->method_title = _x('PayPlug', 'Gateway method title', 'payplug');
@@ -106,10 +134,12 @@
106 134 $this->init_settings();
107 135 $this->requirements = new PayplugGatewayRequirements($this);
108 136 if ($this->user_logged_in()) {
109 137 $this->init_payplug();
110 - }
111 - $this->init_form_fields();
138 + }else{
139 + delete_option('woocommerce_payplug_settings');
140 + set_transient( PayplugWoocommerceHelper::get_transient_key(get_option('woocommerce_payplug_settings', [])), null );
141 + }
112 142
113 143 $this->title = $this->get_option('title');
114 144 $this->description = $this->get_option('description');
115 145 $this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
@@ -115,34 +145,73 @@
115 145 $this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
116 146 $this->debug = 'yes' === $this->get_option('debug', 'no');
117 147 $this->email = $this->get_option('email');
118 148 $this->payment_method = $this->get_option('payment_method');
119 - $this->oneclick = 'yes' === $this->get_option('oneclick', 'no');
149 + $this->oneclick = (('yes' === $this->get_option('oneclick', 'no')) && (is_user_logged_in()));
150 + $this->oney_type = $this->get_option('oney_type', 'with_fees');
151 + $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
120 152
153 + //TODO:: remove this properties from here and add them on Oney classes
154 + $this->min_oney_price = (isset($oney_range['min'])) ? intval($oney_range['min']) : 100;
155 + $this->max_oney_price = (isset($oney_range['max'])) ? intval($oney_range['max']) : 3000;
156 + $this->oney_thresholds_min = $this->get_option('oney_thresholds_min', $this->min_oney_price );
157 + $this->oney_thresholds_max = $this->get_option('oney_thresholds_max', $this->max_oney_price );
158 + $this->init_form_fields();
159 + $this->payplug_merchant_country = PayplugWoocommerceHelper::get_payplug_merchant_country();
160 + $this->oney_product_animation = $this->get_option('oney_product_animation');
161 +
121 162 add_filter('woocommerce_get_customer_payment_tokens', [$this, 'filter_tokens'], 10, 3);
122 163
123 164 self::$log_enabled = $this->debug;
124 165
166 +
125 167 // Ensure the description is not empty to correctly display users's save cards
126 - if (empty($this->description) && 0 !== count($this->get_tokens())) {
168 + if (empty($this->description) && 0 !== count($this->get_tokens()) && $this->oneclick_available()) {
127 169 $this->description = ' ';
128 170 }
129 171
172 +
130 173 if ('test' === $this->mode) {
131 174 $this->description .= " \n";
132 -
133 175 $this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug');
134 176 $this->description = trim($this->description);
135 177 }
136 178
179 + //add fields of IP to the description
180 + if($this->payment_method === 'integrated'){
181 + $this->has_fields = true;
182 + }
183 +
137 184 add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
138 185 add_action('wp_enqueue_scripts', [$this, 'scripts']);
139 - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
140 - add_action('the_post', [$this, 'validate_payment']);
186 + add_action('the_post', [$this, 'validate_payment']);
141 187 add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
142 - }
188 + }
143 189
144 - /**
190 + /**
191 + * @param $option
192 + * @param $value
193 + * @return bool|void
194 + */
195 + public function update_option($option, $value = ''){
196 + if ( $this->needs_setup() ) {
197 + wp_send_json_error( 'needs_setup' );
198 + wp_die();
199 + }
200 +
201 + parent::update_option($option, $value);
202 + }
203 +
204 + /**
205 + * this payment gateway cannot be updated on the wooco payment settings
206 + * @return bool
207 + */
208 + public function needs_setup()
209 + {
210 + return true;
211 + }
212 +
213 + /**
145 214 * Customize gateway title in emails.
146 215 *
147 216 * @param array $total_rows
148 217 * @param \WC_Order $order
@@ -152,9 +221,15 @@
152 221 * @author Clément Boirie
153 222 */
154 223 public function customize_gateway_title($total_rows, $order)
155 224 {
156 - $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
225 +
226 + $get_payment_method = $this->id;
227 + if( method_exists($order, "get_payment_method") ) {
228 + $get_payment_method = $order->get_payment_method();
229 + }
230 +
231 + $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $get_payment_method;
157 232 if (
158 233 $this->id !== $payment_method
159 234 || !isset($total_rows['payment_method'])
160 235 ) {
@@ -170,51 +245,79 @@
170 245 * Validate order payment when the user is redirected to the success confirmation page.
171 246 *
172 247 * @throws \WC_Data_Exception
173 248 */
174 - public function validate_payment()
249 + public function validate_payment($id = null, $save_request = true, $ipn = false)
175 250 {
176 - if (!is_wc_endpoint_url('order-received') || empty($_GET['key'])) {
177 - return;
178 - }
251 + if(!$ipn){
252 + if (!is_wc_endpoint_url('order-received') || (empty($_GET['key']) && empty($id)) ) {
253 + return;
254 + }
255 + }
179 256
180 - $order_id = wc_get_order_id_by_order_key(wc_clean($_GET['key']));
181 - if (empty($order_id)) {
182 - return;
183 - }
257 + if (!empty($_GET['order-received'])) {
258 + $order_id = (int) ($_GET['order-received']);
184 259
185 - $order = wc_get_order($order_id);
260 + } elseif (!empty($id) && !is_object($id)) {
261 + $order_id = (int) $id;
262 + }
263 +
264 + if (empty($order_id)) {
265 + $order_id = wc_get_order_id_by_order_key(wc_clean( (!empty($_GET['key']) ? $_GET['key'] : $id) ) );
266 + if (empty($order_id)) {
267 + return;
268 + }
269 + }
270 +
271 + $order = wc_get_order($order_id);
186 272 if (!$order instanceof \WC_Order) {
187 273 return;
188 274 }
189 275
190 276 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
191 - if ('payplug' !== $payment_method) {
277 + 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', "satispay","sofort","mybank","ideal" ])) {
192 278 return;
193 279 }
194 280
281 +
195 282 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
196 283 if (empty($transaction_id)) {
197 284 PayplugGateway::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
198 -
199 285 return;
200 286 }
201 287
202 - try {
203 - $payment = $this->api->payment_retrieve($transaction_id);
204 - } catch (\Exception $e) {
205 - PayplugGateway::log(
206 - sprintf(
207 - 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
208 - $order_id,
209 - $e->getMessage()
210 - )
211 - );
288 + if($payment_method === $this->id) {
212 289
213 - return;
214 - }
290 + $lock_id = Lock::handle_insert($save_request, $transaction_id);
291 + if(!$lock_id){
292 + return;
293 + }
215 294
216 - $this->response->process_payment($payment);
295 + try {
296 + $payment = $this->api->payment_retrieve($transaction_id);
297 +
298 + } catch (\Exception $e) {
299 + PayplugGateway::log(
300 + sprintf(
301 + 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
302 + $order_id,
303 + $e->getMessage()
304 + )
305 + );
306 +
307 + return;
308 + }
309 +
310 + $this->response->process_payment($payment);
311 +
312 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock($lock_id);
313 + $waiting_requests = \Payplug\PayplugWoocommerce\Model\Lock::get_lock_by_payment_id($transaction_id);
314 +
315 + if($waiting_requests){
316 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_by_payment_id($transaction_id);
317 + $this->validate_payment($order_id, false);
318 + };
319 + }
217 320 }
218 321
219 322 /**
220 323 * Get payment icons.
@@ -224,10 +327,10 @@
224 327 public function get_icon()
225 328 {
226 329
227 330 $src = ('it_IT' === get_locale())
228 - ? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_PostePay.svg'
229 - : PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_CB.svg';
331 + ? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_PostePay.svg'
332 + : PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_CB.svg';
230 333
231 334 $icons = apply_filters('payplug_payment_icons', [
232 335 'payplug' => sprintf('<img src="%s" alt="Visa & Mastercard" class="payplug-payment-icon" />', esc_url($src)),
233 336 ]);
@@ -265,12 +368,18 @@
265 368 * Register gateway settings.
266 369 */
267 370 public function init_form_fields()
268 371 {
269 - $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
270 - $min_oney_price = (isset($oney_range['min'])) ? $oney_range['min'] : 100;
271 - $max_oney_price = (isset($oney_range['max'])) ? $oney_range['max'] : 3000;
272 -
372 +
373 + $anchor = esc_html_x( __("More informations", 'payplug'), 'modal', 'payplug' );
374 + $domain = __( 'support.payplug.com/hc/fr/articles/4408142346002', 'payplug' );
375 + $link = sprintf( ' <a href="https://%s" target="_blank">%s</a>', $domain, $anchor );
376 +
377 +
378 + $anchor_bancontact = esc_html_x( __("payplug_bancontact_activation_request", 'payplug'), 'modal', 'payplug' );
379 + $domain_bancontact = __( 'payplug_bancontact_activation_url', 'payplug' );
380 + $bancontact_call_to_action = sprintf( ' <a id="bancontact_call_to_action" href="https://%s" target="_blank">%s</a>', $domain_bancontact, $anchor_bancontact );
381 +
273 382 $fields = [
274 383 'enabled' => [
275 384 'title' => __('Enable/Disable', 'payplug'),
276 385 'type' => 'checkbox',
@@ -280,18 +389,18 @@
280 389 ],
281 390 'title' => [
282 391 'title' => __('Title', 'payplug'),
283 392 'type' => 'text',
284 - 'description' => __('The payment solution title displayed during checkout.', 'payplug'),
393 + 'description' => __('The payment solution title displayed to your customers during checkout', 'payplug'),
285 394 'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
286 - 'desc_tip' => true,
395 + 'desc_tip' => false,
287 396 ],
288 397 'description' => [
289 398 'title' => __('Description', 'payplug'),
290 399 'type' => 'text',
291 - 'description' => __('The payment solution description displayed during checkout.', 'payplug'),
400 + 'description' => __('The payment solution description displayed to your customers during checkout', 'payplug'),
292 401 'default' => '',
293 - 'desc_tip' => true,
402 + 'desc_tip' => false,
294 403 ],
295 404 'title_connexion' => [
296 405 'title' => __('Connection', 'payplug'),
297 406 'type' => 'title',
@@ -336,15 +445,15 @@
336 445 ],
337 446 'payment_method' => [
338 447 'title' => __('Payment page', 'payplug'),
339 448 'type' => 'radio',
449 + 'options' => array(
450 + 'redirect' => __('Redirect', 'payplug'),
451 + 'embedded' => __('Integrated', 'payplug'),
452 + ),
340 453 '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'),
341 454 'default' => 'redirect',
342 - 'desc_tip' => true,
343 - 'options' => array(
344 - 'redirect' => __('Redirect', 'payplug'),
345 - 'embedded' => __('Integrated', 'payplug'),
346 - ),
455 + 'desc_tip' => false
347 456 ],
348 457 'debug' => [
349 458 'title' => __('Debug', 'payplug'),
350 459 'type' => 'checkbox',
@@ -350,12 +459,12 @@
350 459 'type' => 'checkbox',
351 460 'description' => __('Debug mode saves additional information on your server for each operation done via the PayPlug plugin (Developer setting).', 'payplug'),
352 461 'label' => __('Activate debug mode', 'payplug'),
353 462 'default' => 'yes',
354 - 'desc_tip' => true,
463 + 'desc_tip' => false
355 464 ],
356 465 'title_advanced_settings' => [
357 - 'title' => __('Advanced Settings', 'payplug'),
466 + 'title' => __('payplug_advanced_settings', 'payplug'),
358 467 'description' => __(
359 468 'Your current offer does not allow this option. Try it on TEST mode. More information <a href="https://www.payplug.com/pricing" target="_blank">here.</a>',
360 469 'payplug'
361 470 ),
@@ -366,34 +475,96 @@
366 475 'type' => 'checkbox',
367 476 'label' => __('Activate', 'payplug'),
368 477 'description' => __('Allow your customers to save their credit card information for later purchases.', 'payplug'),
369 478 'default' => 'no',
370 - 'desc_tip' => true
479 + 'desc_tip' => false
371 480 ],
372 - 'oney' => [
373 - 'title' => __('Split Oney Payment', 'payplug'),
374 - 'type' => 'checkbox',
375 - 'label' => __('Activate', 'payplug'),
376 - // TRAD
377 - 'description' => sprintf(__('Allow customers to spread out payments over 3 or 4 installments from %s€ to %s€.', 'payplug'), $min_oney_price, $max_oney_price),
378 - 'default' => 'no',
379 - 'desc_tip' => true
380 - ],
381 - 'oneycgv' => [
382 - 'type' => 'checkbox',
383 - 'label' => __(' I have integrated the Oney legal notices into the GCSs of my site', 'payplug'),
384 - // TRAD
385 - 'default' => 'no'
386 - ]
481 + 'bancontact' => [
482 + 'title' => __('payplug_bancontact_activate_title', 'payplug'),
483 + 'type' => 'checkbox',
484 + 'label' => __('Activate', 'payplug'),
485 + 'description' => '<p class="description" id="bancontact_test_mode_description"> '. __('payplug_bancontact_testmode_description', 'payplug') .' </p>' .
486 + '<p class="description" id="bancontact_live_mode_description_disabled"> '. __('payplug_bancontact_livemode_description_disabled', 'payplug') .' </p>' .
487 + $bancontact_call_to_action,
488 + 'default' => 'no',
489 + ],
490 + 'apple_pay' => [
491 + 'title' => __('payplug_apple_pay_activate_title', 'payplug'),
492 + 'type' => 'checkbox',
493 + 'label' => __('Activate', 'payplug'),
494 + 'description' => '<p class="description" id="apple_pay_test_mode_description"> '. __('payplug_apple_pay_testmode_description', 'payplug') .' </p>' .
495 + '<p class="description" id="apple_pay_live_mode_description"> '. __('payplug_apple_pay_livemode_description', 'payplug') .' </p>' ,
496 + 'default' => 'no',
497 + ],
498 + 'american_express' => [
499 + 'title' => __('payplug_amex_title', 'payplug'),
500 + 'type' => 'checkbox',
501 + 'label' => __('payplug_amex_activate', 'payplug'),
502 + 'description' => '<p class="description" id="amex_test_mode_description"> '. __('payplug_amex_testmode_description', 'payplug') .' </p>' .
503 + '<p class="description" id="amex_live_mode_description"> '. __('payplug_amex_livemode_description', 'payplug') .' </p>' ,
504 + 'default' => 'no',
505 + ],
506 + 'oney' => [
507 + 'title' => __('3x 4x Oney payments', 'payplug'),
508 + 'type' => 'checkbox',
509 + 'label' => __('Activate', 'payplug'),
510 + // TRAD
511 + '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,
512 + 'default' => 'no',
513 + 'desc_tip' => false
514 + ],
515 + 'oney_type' => [
516 + 'title' => '',
517 + 'type' => 'oney_type',
518 + 'options' => array(
519 + 'with_fees' => __('Oney with fees', 'payplug'),
520 + 'without_fees' => __('Oney without fees', 'payplug'),
521 + ),
522 + 'descriptions' => array(
523 + 'with_fees' => __('The fees are split between you and your customers', 'payplug'),
524 + 'without_fees' => __('You pay the fees', 'payplug'),
525 + ),
526 + 'description' => '',
527 + 'default' => 'with_fees',
528 + 'desc_tip' => false
529 + ],
530 + 'oney_thresholds' => [
531 + 'title' => '',
532 + 'type' => 'oney_thresholds',
533 + 'description' => sprintf(__('I would like to offer guaranteed payment in installments for amounts between %s€ and %s€.', 'payplug'),
534 + '<b class="min">' . $this->oney_thresholds_min . '</b>', '<b class="max">' . $this->oney_thresholds_max . '</b>'),
535 + 'desc_tip' => false
536 + ],
537 + 'oney_thresholds_min' => [
538 + 'title' => '',
539 + 'type' => 'hidden',
540 + 'label' => '',
541 + 'description' => '',
542 + 'default' => 'no',
543 + ],
544 + 'oney_thresholds_max' => [
545 + 'title' => '',
546 + 'type' => 'hidden',
547 + 'label' => '',
548 + 'description' => '',
549 + 'default' => 'no',
550 + ],
551 + 'oney_product_animation' => [
552 + 'title' => __('oney_installments_pop_up', 'payplug'),
553 + 'description' => __('display_the_oney_installments_pop_up_on_the_product_page', 'payplug'),
554 + 'label' => __('Activate', 'payplug'),
555 + 'default' => 'no',
556 + 'desc_tip' => false,
557 + 'type' => 'oney_product_animation'
558 + ],
387 559 ];
388 -
389 -
560 +
390 561 if ($this->user_logged_in()) {
391 562 if ($this->permissions->has_permissions(PayplugPermissions::SAVE_CARD)) {
392 563 unset($fields['title_advanced_settings']);
393 564 } else if ('live' === $this->get_current_mode()){
394 565 $fields['oneclick']['disabled'] = true;
395 - }
566 + }
396 567 }
397 568
398 569 /**
399 570 * Filter PayPlug gateway settings.
@@ -419,49 +590,101 @@
419 590 new PayplugIpnResponse($this);
420 591
421 592 }
422 593
423 - /**
424 - * Embedded payment form scripts.
425 - *
426 - * Register scripts and additionnal data needed for the
427 - * embedded payment form.
428 - */
429 - public function scripts()
430 - {
431 - if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) {
432 - return;
433 - }
594 + public function integrated_payments_scripts(){
434 595
435 - // If PayPlug is not enabled bail.
436 - if ('no' === $this->enabled) {
437 - return;
438 - }
596 + $translations = array(
597 + "cardholder" => __('payplug_integrated_payment_cardholder', 'payplug'),
598 + "your_card" => __('payplug_integrated_payment_your_card', 'payplug'),
599 + "card_number" => __('payplug_integrated_payment_card_number', 'payplug'),
600 + "expiration_date" => __('payplug_integrated_payment_expiration_date', 'payplug'),
601 + "cvv" => __('payplug_integrated_payment_cvv', 'payplug'),
602 + "one_click" => __('payplug_integrated_payment_oneClick', 'payplug'),
603 + 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
604 + 'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'),
605 + 'nonce' => wp_create_nonce('woocommerce-process_checkout'),
606 + 'mode' => PayplugWoocommerceHelper::check_mode(), // true for TEST, false for LIVE
607 + 'check_payment_url' => \WC_AJAX::get_endpoint('payplug_check_payment')
608 + );
439 609
440 - // If keys are not set bail.
441 - if (empty($this->get_api_key($this->mode))) {
442 - PayplugGateway::log('Keys are not set correctly.');
610 + //TODO:: if integrated payment is active please active form and comment the one above
611 + /**x
612 + * Integrated payments scripts
613 + */
614 + wp_enqueue_style('payplugIP', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-integrated-payments.css', [], PAYPLUG_GATEWAY_VERSION);
443 615
444 - return;
445 - }
616 + wp_register_script('payplug-domain', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-domain.js', [], 'v1.0');
617 + wp_enqueue_script('payplug-domain');
446 618
447 - // Register checkout styles.
448 - wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION);
449 - wp_enqueue_style('payplug-checkout');
619 + wp_register_script('payplug-integrated-payments-api', 'https://cdn.payplug.com/js/integrated-payment/v1@1/index.js', [], 'v1.1', true);
620 + wp_enqueue_script('payplug-integrated-payments-api');
450 621
451 - wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
452 - wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [
453 - 'jquery',
454 - 'payplug'
455 - ], PAYPLUG_GATEWAY_VERSION, true);
456 - wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
457 - 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
458 - 'nonce' => [
459 - 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
460 - ],
461 - 'is_embedded' => 'redirect' !== $this->payment_method
462 - ]);
463 - wp_enqueue_script('payplug-checkout');
622 + wp_register_script( 'jquery-bind-first', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/jquery.bind-first-0.2.3.min.js', array( 'jquery' ), '1.0.0', true );
623 + wp_enqueue_script('jquery-bind-first');
624 +
625 + wp_register_script('payplug-integrated-payments', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-integrated-payments.js', ['jquery', 'jquery-bind-first', 'payplug-integrated-payments-api'], 'v1.1', true);
626 + wp_enqueue_script('payplug-integrated-payments');
627 +
628 + wp_localize_script( 'payplug-integrated-payments', 'payplug_integrated_payment_params', $translations);
629 + }
630 +
631 + /**
632 + * Embedded payment form scripts.
633 + *
634 + * Register scripts and additionnal data needed for the
635 + * embedded payment form.
636 + */
637 + public function scripts()
638 + {
639 + if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) {
640 + return;
641 + }
642 +
643 + // If PayPlug is not enabled bail.
644 + if ('no' === $this->enabled) {
645 + return;
646 + }
647 +
648 + // If keys are not set bail.
649 + if (empty($this->get_api_key($this->mode))) {
650 + PayplugGateway::log('Keys are not set correctly.');
651 +
652 + return;
653 + }
654 +
655 + // Register checkout styles.
656 + wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION);
657 + wp_enqueue_style('payplug-checkout');
658 +
659 + if (
660 + ( $this->payment_method == "integrated" && !$this->is_checkout_block() ) ||
661 + ($this->payment_method == "integrated" && is_wc_endpoint_url('order-pay') )
662 + ) {
663 + $this->integrated_payments_scripts();
664 + }
665 +
666 + if (($this->payment_method == "popup" ) && ($this->id === "payplug") && !$this->is_checkout_block() ) {
667 +
668 + //load popup features
669 + //TODO:: if integrated payment is not active please active this and comment the one bellow
670 + wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
671 + wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [
672 + 'jquery',
673 + 'payplug'
674 + ], PAYPLUG_GATEWAY_VERSION, true);
675 + wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
676 + 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
677 + 'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'),
678 + 'nonce' => [
679 + 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
680 + ],
681 + 'is_embedded' => 'redirect' !== $this->payment_method
682 + ]);
683 +
684 + wp_enqueue_script('payplug-checkout');
685 + }
686 +
464 687 }
465 688
466 689 /**
467 690 * Filter saved tokens for the gateway.
@@ -511,9 +734,9 @@
511 734 unset($tokens[$k]);
512 735 continue;
513 736 }
514 737
515 - if ($current_year === (int) $token->get_expiry_year() && $current_month >= (int) $token->get_expiry_month()) {
738 + if ($current_year === (int) $token->get_expiry_year() && $current_month > (int) $token->get_expiry_month()) {
516 739 unset($tokens[$k]);
517 740 continue;
518 741 }
519 742 }
@@ -523,12 +746,17 @@
523 746
524 747 public function payment_fields()
525 748 {
526 749 $description = $this->get_description();
527 - if (!empty($description)) {
528 - echo wpautop(wptexturize($description));
529 - }
530 750
751 + if (!empty($description)) {
752 + echo wpautop(wptexturize($description));
753 + }
754 +
755 + if(($this->payment_method === 'integrated') && ($this->id == 'payplug')){
756 + echo IntegratedPayment::template_form($this->oneclick);
757 + }
758 +
531 759 if ($this->oneclick_available()) {
532 760 $this->tokenization_script();
533 761 $this->saved_payment_methods();
534 762 }
@@ -538,259 +766,30 @@
538 766 * Handle admin display.
539 767 */
540 768 public function admin_options()
541 769 {
542 - wp_enqueue_style(
543 - 'payplug-gateway-style',
544 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/app.css',
545 - [],
546 - PAYPLUG_GATEWAY_VERSION
547 - );
548 -
549 - wp_enqueue_script(
550 - 'payplug-gateway-admin',
551 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin.js',
552 - ['jquery-ui-dialog'],
553 - PAYPLUG_GATEWAY_VERSION
554 - );
770 + /************ VUE Code *************/
771 + wp_enqueue_script('chunk-vendors.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/chunk-vendors-1.7.2.js', [], PAYPLUG_GATEWAY_VERSION);
772 + wp_enqueue_script('app.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/app-1.7.2.js', [], PAYPLUG_GATEWAY_VERSION);
773 + wp_enqueue_style('app.css', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/css/app.css', [], PAYPLUG_GATEWAY_VERSION);
774 + wp_localize_script('app.js', 'payplug_admin_config',
775 + array(
776 + "img_path" => esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/'),
777 + 'ajax_url' => get_home_url()
778 + ));
555 779
556 - wp_localize_script('payplug-gateway-admin', 'payplug_admin_config', array(
557 - 'ajax_url' => admin_url('admin-ajax.php'),
558 - 'has_live_key' => (false === $this->has_api_key('live')) ? false : true,
559 - 'btn_ok' => _x('Ok', 'modal', 'payplug'),
560 - 'btn_label' => _x('Cancel', 'modal', 'payplug'),
561 - 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'),
562 - ));
780 + ?>
781 + <script>window.get_data_url = "<?php echo rest_url('payplug/data'); ?>"</script>
782 + <script>window.set_data_url = "<?php echo rest_url('payplug/save_data'); ?>"</script>
783 + <div id="payplug_admin"></div>
563 784
564 - wp_enqueue_script(
565 - 'payplug-gateway-admin-oney',
566 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-oney.js',
567 - ['jquery-ui-dialog'],
568 - PAYPLUG_GATEWAY_VERSION
569 - );
785 + <?php
570 786
571 - wp_localize_script('payplug-gateway-admin-oney', 'payplug_admin_config', array(
572 - 'ajax_url' => admin_url('admin-ajax.php'),
573 - 'btn_ok' => _x('Ok', 'modal', 'payplug'),
574 - ));
575 - if ($this->user_logged_in() && false === $this->has_api_key('live')) {
576 - add_action('admin_footer', function () {
577 - $email = $this->get_option('email');
578 -?>
579 - <div id="payplug-refresh-keys-modal" title="<?php echo esc_attr_x('Mode LIVE', 'modal', 'payplug'); ?>">
580 - <form id="payplug-refresh-keys-modal__form">
581 - <p id="dialog-msg"></p>
582 - <p><?php echo esc_html_x('Please enter your PayPlug account password', 'modal', 'payplug'); ?></p>
583 - <input type="password" name="password" required title="<?php echo esc_attr_x('Enter your PayPlug account password', 'modal', 'payplug'); ?>" />
584 - <input type="hidden" name="email" value="<?php echo esc_attr($email); ?>">
585 - <input type="hidden" name="action" value="<?php echo esc_attr(Ajax::REFRESH_KEY_ACTION); ?>">
586 - <?php wp_nonce_field(sprintf('%s_%s', $email, Ajax::REFRESH_KEY_ACTION)); ?>
587 - <input class="ui-dialog-sronly" type="submit" tabindex="-1">
588 - </form>
589 - </div>
590 - <?php
591 - });
592 - }
787 + /*********** End VUE Code ***********/
593 788
594 - $payplug_requirements = new PayplugGatewayRequirements($this); ?>
595 -
596 - <h2 class="title--logo"><?php esc_html($this->get_method_title()) ?></h2>
597 - <p><?php _e(sprintf('Version %s', PAYPLUG_GATEWAY_VERSION)); ?></p>
598 - <div class="payplug-requirements">
599 - <?php echo $payplug_requirements->curl_requirement(); ?>
600 - <?php echo $payplug_requirements->php_requirement(); ?>
601 - <?php echo $payplug_requirements->openssl_requirement(); ?>
602 - <?php echo $payplug_requirements->account_requirement(); ?>
603 - <?php echo $payplug_requirements->currency_requirement(); ?>
604 - <?php echo $payplug_requirements->oney_requirement(); ?>
605 - </div>
606 - <?php echo wp_kses_post(wpautop($this->get_method_description())); ?>
607 -
608 - <?php if ($this->user_logged_in()) : ?>
609 - <table class="form-table">
610 - <?php $this->generate_settings_html($this->get_form_fields()); ?>
611 - </table>
612 - <?php else :
613 - $GLOBALS['hide_save_button'] = true; ?>
614 - <h3 class="wc-settings-sub-title"><?php _e('Connection', 'payplug'); ?></h3>
615 - <table class="form-table">
616 - <tbody>
617 - <tr valign="top">
618 - <th scope="row" class="titledesc">
619 - <label for="payplug_email"><?php _e('Email', 'payplug'); ?></label>
620 - </th>
621 - <td class="forminp">
622 - <fieldset>
623 - <legend class="screen-reader-text"><span><?php _e('Email', 'payplug'); ?></span></legend>
624 - <input class="input-text regular-input" type="text" name="payplug_email" id="payplug_email" value="" placeholder="<?php _e('your@email.com', 'payplug'); ?>" />
625 - </fieldset>
626 - </td>
627 - </tr>
628 - <tr valign="top">
629 - <th scope="row" class="titledesc">
630 - <label for="payplug_password"><?php _e('Password', 'payplug'); ?></label>
631 - </th>
632 - <td class="forminp">
633 - <fieldset>
634 - <legend class="screen-reader-text"><span><?php _e('Password', 'payplug'); ?></span>
635 - </legend>
636 - <input class="input-text regular-input" type="password" name="payplug_password" id="payplug_password" value="" />
637 - </fieldset>
638 - </td>
639 - </tr>
640 - <tr valign="top">
641 - <td class="forminp">
642 - <input id="payplug-login" class="button" type="submit" value="<?php _e('Login', 'payplug'); ?>">
643 - <input type="hidden" name="save" value="login">
644 - <?php wp_nonce_field('payplug_user_login', '_loginaction'); ?>
645 - </td>
646 - </tr>
647 - </tbody>
648 - </table>
649 - <?php
650 - endif;
651 - ?>
652 - <div id="payplug-oney-modal" title="<?php echo esc_attr_x('Mode LIVE', 'modal', 'payplug'); ?>">
653 - <p>
654 - <?php echo esc_html_x('Attention, pour utiliser la méthode de paiement Oney en mode LIVE merci de nous contacter à', 'modal', 'payplug'); ?>
655 - <br/>
656 - <a href="mailto:support@payplug.com">support@payplug.com</a>
657 - </p>
658 - </div>
659 - <?php
660 789 }
661 790
662 791 /**
663 - * Process admin options.
664 - *
665 - * @return bool
666 - */
667 - public function process_admin_options()
668 - {
669 - $data = $this->get_post_data();
670 - $settings = get_option( 'woocommerce_payplug_settings', [] );
671 - $oneclick_fieldkey = $this->get_field_key('oneclick');
672 -
673 - // Handle logout process
674 - if (
675 - isset($data['submit_logout'])
676 - && false !== check_admin_referer('payplug_user_logout', '_logoutaction')
677 - ) {
678 -
679 - if ($this->permissions) {
680 - $this->permissions->clear_permissions();
681 - }
682 -
683 - $data = get_option($this->get_option_key());
684 - $data['payplug_test_key'] = '';
685 - $data['payplug_live_key'] = '';
686 - $data['payplug_merchant_id'] = '';
687 - $data['enabled'] = 'no';
688 - $data['mode'] = 'no';
689 - $data['oneclick'] = 'no';
690 - update_option(
691 - $this->get_option_key(),
692 - apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
693 - );
694 - if("payplug" === $this->id) {
695 - \WC_Admin_Settings::add_message(__('Successfully logged out.', 'payplug'));
696 - }
697 -
698 - return true;
699 - }
700 -
701 - // Handle login process
702 - if (
703 - isset($data['payplug_email'])
704 - && false !== check_admin_referer('payplug_user_login', '_loginaction')
705 - ) {
706 - $email = $data['payplug_email'];
707 - $password = wp_unslash($data['payplug_password']);
708 - $response = $this->retrieve_user_api_keys($email, $password);
709 - if (is_wp_error($response)) {
710 - \WC_Admin_Settings::add_error($response->get_error_message());
711 -
712 - return false;
713 - }
714 -
715 - // try to use the api keys to retrieve the merchant id
716 - $merchant_id = isset($response['test']) ? $this->retrieve_merchant_id($response['test']) : '';
717 -
718 - $this->init_form_fields();
719 - $fields = $this->get_form_fields();
720 - $data = [];
721 -
722 - // Load existing values if the user is re-login.
723 - foreach ($fields as $key => $field) {
724 - if (in_array($field['type'], ['title', 'login'])) {
725 - continue;
726 - }
727 -
728 - switch ($key) {
729 - case 'enabled':
730 - $val = 'yes';
731 - break;
732 - case 'mode':
733 - $val = 'no';
734 - break;
735 - case 'payplug_test_key':
736 - $val = esc_attr($response['test']);
737 - break;
738 - case 'payplug_live_key':
739 - $val = esc_attr($response['live']);
740 - break;
741 - case 'payplug_merchant_id':
742 - $val = esc_attr($merchant_id);
743 - break;
744 - case 'email':
745 - $val = esc_html($email);
746 - break;
747 - default:
748 - $val = $this->get_option($key);
749 - }
750 -
751 - $data[$key] = $val;
752 - }
753 -
754 - $this->set_post_data($data);
755 - update_option(
756 - $this->get_option_key(),
757 - apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
758 - );
759 - if("payplug" === $this->id) {
760 - \WC_Admin_Settings::add_message(__('Successfully logged in.', 'payplug'));
761 - }
762 -
763 - return true;
764 - }
765 -
766 - // Don't let user without live key leave TEST mode.
767 - $mode_fieldkey = $this->get_field_key('mode');
768 - $live_key_fieldkey = $this->get_field_key('payplug_live_key');
769 - if (isset($data[$mode_fieldkey]) && '1' === $data[$mode_fieldkey] && empty($data[$live_key_fieldkey])) {
770 - $data[$mode_fieldkey] = null;
771 - $this->set_post_data($data);
772 - \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'));
773 - }
774 -
775 - // Check user permissions before activating one-click feature.
776 - $oneclick_fieldkey = $this->get_field_key('oneclick');
777 - if (
778 - isset($data[$oneclick_fieldkey])
779 - && '1' === $data[$oneclick_fieldkey]
780 - && '1' === $data[$mode_fieldkey]
781 - && (!$this->user_logged_in()
782 - || false === $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD))
783 - ) {
784 - $data[$oneclick_fieldkey] = null;
785 - \WC_Admin_Settings::add_error(__('Only PREMIUM accounts can enable the One Click option in LIVE mode.', 'payplug'));
786 - }
787 -
788 - $this->data = $data;
789 - parent::process_admin_options();
790 - }
791 -
792 - /**
793 792 * Process payment.
794 793 *
795 794 * @param int $order_id
796 795 *
@@ -805,9 +804,10 @@
805 804 $order = wc_get_order($order_id);
806 805 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
807 806 $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total());
808 807 $amount = $this->validate_order_amount($amount);
809 - if (is_wp_error($amount)) {
808 +
809 + if (is_wp_error($amount)) {
810 810 PayplugGateway::log(sprintf('Invalid amount %s for the order.', $order->get_total()), 'error');
811 811 throw new \Exception($amount->get_error_message());
812 812 }
813 813
@@ -820,11 +820,87 @@
820 820
821 821 return $this->process_payment_with_token($order, $amount, $customer_id, $payment_token_id);
822 822 }
823 823
824 - return $this->process_standard_payment($order, $amount, $customer_id);
824 + return $this->process_standard_payment($order, $amount, $customer_id);
825 825 }
826 826
827 + /**
828 + * if payment was generated by an intend, we shouldn't generate another one and try to pay it, this would generate duplications
829 + * @param $order
830 + * @return array|null
831 + * @throws \Exception
832 + */
833 + private function process_standard_intent_payment($order){
834 +
835 + //no order-pay page, no ajax_on_order_review_page
836 + if ( !is_wc_endpoint_url('order-pay') &&
837 + $this->is_checkout_block() &&
838 + (
839 + ( $this->id === "payplug" && ($this->payment_method === 'integrated'|| $this->payment_method === 'popup') ) ||
840 + ( $this->id === "american_express" && $this->payment_method === 'popup')
841 + ) &&
842 + $_GET["wc-ajax"] !== "payplug_order_review_url"
843 + ) {
844 +
845 + $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
846 +
847 + try {
848 + $payment = $this->api->payment_retrieve($order->get_transaction_id());
849 + if (ob_get_length() > 0) {
850 + ob_clean();
851 + }
852 +
853 + // Save transaction id for the order
854 + PayplugWoocommerceHelper::is_pre_30()
855 + ? update_post_meta($order_id, '_transaction_id', $payment->id)
856 + : $order->set_transaction_id($payment->id);
857 +
858 + if($payment->is_paid){
859 + $finished_status = wc_get_is_paid_statuses();
860 + $order->set_status($finished_status[0]);
861 + }
862 +
863 + if (is_callable([$order, 'save'])) {
864 + $order->save();
865 + }
866 +
867 + /**
868 + * Fires once a payment has been created.
869 + *
870 + * @param int $order_id Order ID
871 + * @param PaymentResource $payment Payment resource
872 + */
873 + \do_action('payplug_gateway_payment_created', $order_id, $payment);
874 +
875 + $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
876 + PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
877 +
878 + PayplugGateway::log(sprintf('Payment intent created for order #%s', $order_id));
879 +
880 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
881 +
882 + wp_send_json_success(array(
883 + 'payment_id' => $payment->id,
884 + 'result' => 'success',
885 + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
886 + 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null
887 + ));
888 +
889 + return array("stt" => "OK");
890 +
891 + } catch (HttpException $e) {
892 + PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
893 + throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
894 + } catch (\Exception $e) {
895 + PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
896 + throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
897 + }
898 + }
899 +
900 + return null;
901 + }
902 +
827 903 /**
828 904 * @param \WC_Order $order
829 905 * @param int $amount
830 906 * @param int $customer_id
@@ -832,16 +908,27 @@
832 908 * @return array
833 909 * @throws \Exception
834 910 */
835 911 public function process_standard_payment($order, $amount, $customer_id)
836 - {
912 + {
837 913
838 - $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
914 + $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
839 915
840 - try {
916 + $intent = $this->process_standard_intent_payment($order);
917 + if( !empty($intent) ){
918 + return $intent;
919 + }
920 +
921 + try {
841 922 $address_data = PayplugAddressData::from_order($order);
842 923
843 - $payment_data = [
924 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
925 +
926 + if (!(substr( $return_url, 0, 4 ) === "http")) {
927 + $return_url = get_site_url().$return_url;
928 + }
929 +
930 + $payment_data = [
844 931 'amount' => $amount,
845 932 'currency' => get_woocommerce_currency(),
846 933 'allow_save_card' => $this->oneclick_available() && (int) $customer_id > 0,
847 934 'billing' => $address_data->get_billing(),
@@ -846,9 +933,9 @@
846 933 'allow_save_card' => $this->oneclick_available() && (int) $customer_id > 0,
847 934 'billing' => $address_data->get_billing(),
848 935 'shipping' => $address_data->get_shipping(),
849 936 'hosted_payment' => [
850 - 'return_url' => esc_url_raw($order->get_checkout_order_received_url()),
937 + 'return_url' => $return_url,
851 938 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
852 939 ],
853 940 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
854 941 'metadata' => [
@@ -857,8 +944,14 @@
857 944 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
858 945 ],
859 946 ];
860 947
948 + if($this->payment_method === 'integrated'){
949 + $payment_data['initiator'] = 'PAYER';
950 + $payment_data['integration'] = 'INTEGRATED_PAYMENT';
951 + unset($payment_data['hosted_payment']['cancel_url']);
952 + }
953 +
861 954 /**
862 955 * Filter the payment data before it's used
863 956 *
864 957 * @param array $payment_data
@@ -890,13 +983,19 @@
890 983 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
891 984
892 985 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
893 986
894 - return [
895 - 'result' => 'success',
896 - 'redirect' => $payment->hosted_payment->payment_url,
897 - 'cancel' => $payment->hosted_payment->cancel_url,
898 - ];
987 + if(ob_get_length() > 0){
988 + ob_clean();
989 + }
990 +
991 + return array(
992 + 'payment_id' => $payment->id,
993 + 'result' => 'success',
994 + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
995 + 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null
996 + );
997 +
899 998 } catch (HttpException $e) {
900 999 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
901 1000 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
902 1001 } catch (\Exception $e) {
@@ -926,8 +1025,14 @@
926 1025
927 1026 try {
928 1027 $address_data = PayplugAddressData::from_order($order);
929 1028
1029 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
1030 +
1031 + if (!(substr( $return_url, 0, 4 ) === "http")) {
1032 + $return_url = get_site_url().$return_url;
1033 + }
1034 +
930 1035 $payment_data = [
931 1036 'amount' => $amount,
932 1037 'currency' => get_woocommerce_currency(),
933 1038 'payment_method' => $payment_token->get_token(),
@@ -935,9 +1040,9 @@
935 1040 'billing' => $address_data->get_billing(),
936 1041 'shipping' => $address_data->get_shipping(),
937 1042 'initiator' => 'PAYER',
938 1043 'hosted_payment' => [
939 - 'return_url' => esc_url_raw($order->get_checkout_order_received_url()),
1044 + 'return_url' => $return_url,
940 1045 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
941 1046 ],
942 1047 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
943 1048 'metadata' => [
@@ -957,12 +1062,21 @@
957 1062 $this->response->process_payment($payment, true);
958 1063
959 1064 PayplugGateway::log(sprintf('Payment process complete for order #%s', $order_id));
960 1065
1066 + if(($payment->__get('is_paid'))){
1067 + $redirect = $order->get_checkout_order_received_url();
1068 + }else if(isset($payment->__get('hosted_payment')->payment_url)){
1069 + $redirect = $payment->__get('hosted_payment')->payment_url;
1070 + }else{
1071 + $redirect = $return_url;
1072 + }
1073 +
961 1074 return [
962 - 'result' => 'success',
1075 + 'payment_id' => $payment->id,
1076 + 'result' => 'success',
963 1077 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
964 - 'redirect' => ($payment->__get('is_paid')) ? $order->get_checkout_order_received_url() : $payment->__get('hosted_payment')->payment_url
1078 + 'redirect' => $redirect
965 1079 ];
966 1080 } catch (HttpException $e) {
967 1081 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
968 1082 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
@@ -984,8 +1098,13 @@
984 1098 public function process_refund($order_id, $amount = null, $reason = '')
985 1099 {
986 1100 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
987 1101
1102 + if( !$this->user_logged_in()){
1103 + PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
1104 + return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
1105 + }
1106 +
988 1107 $order = wc_get_order($order_id);
989 1108 if (!$order instanceof \WC_Order) {
990 1109 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
991 1110
@@ -1129,15 +1248,17 @@
1129 1248 }
1130 1249
1131 1250 try {
1132 1251 $response = Authentication::getKeysByLogin($email, $password);
1133 - if (empty($response) || !isset($response['httpResponse'])) {
1252 + if (empty($response) || !isset($response['httpResponse']) && "payplug" === $this->id) {
1134 1253 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1135 1254 }
1136 1255
1137 1256 return $response['httpResponse']['secret_keys'];
1138 1257 } catch (HttpException $e) {
1139 - return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1258 + if("payplug" === $this->id) {
1259 + return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1260 + }
1140 1261 }
1141 1262 }
1142 1263
1143 1264 /**
@@ -1152,9 +1273,10 @@
1152 1273 */
1153 1274 public function retrieve_merchant_id($key = null)
1154 1275 {
1155 1276 try {
1156 - $response = !is_null($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1277 + $response = !is_null($key) && !empty($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1278 + PayplugWoocommerceHelper::set_transient_data($response);
1157 1279 $merchant_id = isset($response['httpResponse']['id']) ? $response['httpResponse']['id'] : '';
1158 1280 } catch (ConfigurationException $e) {
1159 1281 PayplugGateway::log(sprintf('Missing API key for PayPlug client : %s', wc_print_r($e->getMessage(), true)), 'error');
1160 1282
@@ -1256,9 +1378,9 @@
1256 1378 <div class="radio--custom">
1257 1379 <input class="radio radio-no <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-no" value="0" <?php checked('0', $checked); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1258 1380 <label for="<?php echo esc_attr($field_key); ?>-no"><?php echo esc_html($data['no']); ?></label>
1259 1381 </div>
1260 - <?php echo $this->get_description_html($data); ?>
1382 + <div id="live-mode-test-p"><?php echo $this->get_description_html($data); ?></div>
1261 1383 </fieldset>
1262 1384 </td>
1263 1385 </tr>
1264 1386 <?php
@@ -1308,8 +1430,9 @@
1308 1430 <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1309 1431 <input class="radio <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" value="<?php echo esc_attr($option_key); ?>" <?php checked($option_key, $this->get_option($key)); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1310 1432 <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value); ?></label>
1311 1433 <?php endforeach; ?>
1434 + <?php echo $this->get_description_html($data); ?>
1312 1435 </fieldset>
1313 1436 </td>
1314 1437 </tr>
1315 1438 <?php
@@ -1350,8 +1473,165 @@
1350 1473
1351 1474 return ob_get_clean();
1352 1475 }
1353 1476
1477 +
1478 + /**
1479 + * Generate Oney popup option HTML.
1480 + *
1481 + * @param string $key
1482 + * @param array $data
1483 + *
1484 + * @return string
1485 + */
1486 + public function generate_oney_product_animation_html($key, $data)
1487 + {
1488 + $field_key = $this->get_field_key($key);
1489 +
1490 + $defaults = array(
1491 + 'title' => '',
1492 + 'disabled' => false,
1493 + 'class' => '',
1494 + 'css' => '',
1495 + 'placeholder' => '',
1496 + 'type' => 'checkbox',
1497 + 'desc_tip' => false,
1498 + 'description' => '',
1499 + 'custom_attributes' => [],
1500 + 'options' => [],
1501 + );
1502 +
1503 + $data = wp_parse_args($data, $defaults);
1504 +
1505 + ob_start();
1506 + ?>
1507 + <tr valign="top" id="oney_installments_pop_up">
1508 + <th scope="row" class="titledesc">
1509 + <label for="<?php echo esc_attr($field_key); ?>">
1510 + <?php echo wp_kses_post($data['title']); ?>
1511 + <?php echo $this->get_tooltip_html($data); ?>
1512 + </label>
1513 + </th>
1514 + <td class="forminp">
1515 + <fieldset>
1516 + <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span></legend>
1517 + <label for="woocommerce_payplug_oney_product_animation">
1518 + <input class="" type="checkbox" name="woocommerce_payplug_oney_product_animation" id="woocommerce_payplug_oney_product_animation" style="" <?php echo (($this->oney_product_animation == 'yes') ? "checked" : ''); ?>> <?php echo wp_kses_post($data['label']); ?></label><br>
1519 + <p class="description"> <?php echo wp_kses_post($data['description']); ?></p>
1520 + </fieldset>
1521 + </td>
1522 + </tr>
1523 + <?php
1524 +
1525 + return ob_get_clean();
1526 + }
1527 +
1528 + /**
1529 + * Generate Oney Type Input HTML.
1530 + *
1531 + * @param string $key
1532 + * @param array $data
1533 + *
1534 + * @return string
1535 + */
1536 + public function generate_oney_type_html($key, $data)
1537 + {
1538 + $field_key = $this->get_field_key($key);
1539 + $defaults = array(
1540 + 'title' => '',
1541 + 'disabled' => false,
1542 + 'class' => '',
1543 + 'css' => '',
1544 + 'placeholder' => '',
1545 + 'type' => 'text',
1546 + 'desc_tip' => false,
1547 + 'description' => '',
1548 + 'custom_attributes' => [],
1549 + 'options' => [],
1550 + );
1551 +
1552 + $data = wp_parse_args($data, $defaults);
1553 +
1554 + ob_start();
1555 + ?>
1556 + <tr valign="top" id="woocommerce_payplug_oney_type">
1557 + <th scope="row" class="titledesc" style="padding-top: 0px;">
1558 + <label for="<?php echo esc_attr($field_key); ?>">
1559 + <?php echo wp_kses_post($data['title']); ?>
1560 + <?php echo $this->get_tooltip_html($data); ?>
1561 + </label>
1562 + </th>
1563 + <td class="forminp" style="padding-top: 0px;">
1564 + <fieldset>
1565 + <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1566 + </legend>
1567 + <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1568 + <input class="radio <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" value="<?php echo esc_attr($option_key); ?>" <?php checked($option_key, $this->get_option($key)); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1569 + <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" style="margin-right: 20px !important;">
1570 + <span style="font-weight: 500;"><?php echo esc_html($option_value); ?></span>
1571 + <span style="color:#646970;"> : <?php echo $data['descriptions'][$option_key] ;?></span>
1572 + </label>
1573 + <?php endforeach; ?>
1574 + <?php echo $this->get_description_html($data); ?>
1575 + </fieldset>
1576 + </td>
1577 + </tr>
1578 + <?php
1579 +
1580 + return ob_get_clean();
1581 + }
1582 +
1583 + /**
1584 + * Generate Oney Thresholds Input HTML.
1585 + *
1586 + * @param string $key
1587 + * @param array $data
1588 + *
1589 + * @return string
1590 + */
1591 + public function generate_oney_thresholds_html($key, $data)
1592 + {
1593 + $field_key = $this->get_field_key($key);
1594 + $defaults = array(
1595 + 'title' => '',
1596 + 'disabled' => false,
1597 + 'class' => '',
1598 + 'css' => '',
1599 + 'placeholder' => '',
1600 + 'type' => 'text',
1601 + 'desc_tip' => false,
1602 + 'description' => '',
1603 + 'custom_attributes' => [],
1604 + 'options' => [],
1605 + );
1606 +
1607 + $data = wp_parse_args($data, $defaults);
1608 +
1609 + ob_start();
1610 + ?>
1611 + <tr valign="top" id="woocommerce_payplug_oney_thresholds">
1612 + <th scope="row" class="titledesc" style="padding-top: 0px;">
1613 + <label for="<?php echo esc_attr($field_key); ?>">
1614 + <?php echo wp_kses_post($data['title']); ?>
1615 + <?php echo $this->get_tooltip_html($data); ?>
1616 + </label>
1617 + </th>
1618 + <td class="forminp" style="padding-top: 0px;">
1619 + <fieldset>
1620 + <div id="oney_thresholds_description"><?php echo $this->get_description_html($data); ?></div>
1621 + <input type="number" id="payplug_oney_thresholds_min" min="<?php echo $this->min_oney_price;?>" max="<?php echo $this->max_oney_price;?>" class="payplug-admin-oney-threshold-input">
1622 + <b class="d-inline-block">€</b>
1623 + <div class="d-inline-block" id="slider-range"></div>
1624 + <input type="number" id="payplug_oney_thresholds_max" min="<?php echo $this->min_oney_price;?>" max="<?php echo $this->max_oney_price;?>" class="payplug-admin-oney-threshold-input">
1625 + <b class="d-inline-block">€</b>
1626 + </fieldset>
1627 + </td>
1628 + </tr>
1629 + <?php
1630 +
1631 + return ob_get_clean();
1632 + }
1633 +
1354 1634 /**
1355 1635 * Validate Radio Field.
1356 1636 *
1357 1637 * Make sure the data is escaped correctly, etc.
@@ -1380,8 +1660,55 @@
1380 1660 {
1381 1661 return ('1' === (string) $value) ? 'yes' : 'no';
1382 1662 }
1383 1663
1664 + /**
1665 + * Validate Yes/No Field.
1666 + *
1667 + * @param string $key
1668 + * @param string $value Posted Value
1669 + *
1670 + * @return string
1671 + */
1672 + public function validate_oney_product_animation_field($key, $value)
1673 + {
1674 + return ('on' === (string) $value) ? 'yes' : 'no';
1675 + }
1676 +
1677 + /**
1678 + * Validate Oney Type Field.
1679 + *
1680 + * Make sure the data is escaped correctly, etc.
1681 + *
1682 + * @param string $key
1683 + * @param string|null $value Posted Value
1684 + *
1685 + * @return string
1686 + */
1687 + public function validate_oney_type_field($key, $value)
1688 + {
1689 + $value = is_null($value) ? 'with_fees' : $value;
1690 +
1691 + return wc_clean(stripslashes($value));
1692 + }
1693 +
1694 + /**
1695 + * Validate Oney Thresholds Field.
1696 + *
1697 + * Make sure the data is escaped correctly, etc.
1698 + *
1699 + * @param string $key
1700 + * @param string|null $value Posted Value
1701 + *
1702 + * @return string
1703 + */
1704 + public function validate_oney_thresholds_field($key, $value)
1705 + {
1706 + $value = is_null($value) ? [100, 3000] : $value;
1707 +
1708 + return wc_clean($value);
1709 + }
1710 +
1384 1711 /**
1385 1712 * Get PayPlug gateway mode.
1386 1713 *
1387 1714 * @return string
@@ -1470,14 +1797,23 @@
1470 1797 * @return array
1471 1798 */
1472 1799 public function check_gateway($gateways)
1473 1800 {
1474 - if (isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1801 + if ( !empty( WC()->cart ) && isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1475 1802 $order_amount = $this->get_order_total();
1476 1803 if ($order_amount < self::MIN_AMOUNT || $order_amount > self::MAX_AMOUNT) {
1477 1804 unset($gateways[$this->id]);
1478 1805 }
1479 1806 }
1807 +
1808 + if($this->oney_type == 'with_fees'){
1809 + unset($gateways['oney_x3_without_fees']);
1810 + unset($gateways['oney_x4_without_fees']);
1811 + } else{
1812 + unset($gateways['oney_x3_with_fees']);
1813 + unset($gateways['oney_x4_with_fees']);
1814 + }
1815 +
1480 1816 return $gateways;
1481 1817 }
1482 1818
1483 1819 /**
@@ -1491,5 +1827,18 @@
1491 1827 {
1492 1828 $status = $order->get_status();
1493 1829 return $order && $this->supports('refunds') && $status !== "cancelled" && $status !== "failed";
1494 1830 }
1831 +
1832 + public function getPayplugMerchantCountry(){
1833 + return $this->payplug_merchant_country;
1834 + }
1835 +
1836 + public function setPayplugMerchantCountry($country){
1837 + $this->payplug_merchant_country = $country;
1838 + }
1839 +
1840 + function is_checkout_block() {
1841 + return WC_Blocks_Utils::has_block_in_page( wc_get_page_id('checkout'), 'woocommerce/checkout' );
1842 + }
1843 +
1495 1844 }