PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.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 All 102 releases
← All changes | src/Gateway/PayplugGateway.php +657 -1188 1.10.0 → 3.1.0 View file →
@@ -9,12 +9,12 @@
9 9
10 10 use Payplug\Authentication;
11 11 use Payplug\Exception\ConfigurationException;
12 12 use Payplug\Exception\HttpException;
13 -use Payplug\Exception\ForbiddenException;
14 13 use Payplug\Payplug;
15 -use Payplug\PayplugWoocommerce\Admin\Ajax;
14 +use Payplug\PayplugWoocommerce\Helper\Lock;
16 15 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
16 +use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
17 17 use Payplug\Resource\Payment as PaymentResource;
18 18 use Payplug\Resource\Refund as RefundResource;
19 19 use WC_Payment_Gateway_CC;
20 20 use WC_Payment_Tokens;
@@ -20,15 +20,47 @@
20 20 use WC_Payment_Tokens;
21 21
22 22 /**
23 23 * PayPlug WooCommerce Gateway.
24 - *
25 - * @package Payplug\PayplugWoocommerce\Gateway
26 24 */
27 25 class PayplugGateway extends WC_Payment_Gateway_CC
28 26 {
27 + use ServiceGetter;
29 28
29 + public const OPTION_NAME = 'payplug_config';
30 +
30 31 /**
32 + * @var string
33 + */
34 + public $mode;
35 + /**
36 + * @var bool
37 + */
38 + public $debug;
39 + /**
40 + * @var string
41 + */
42 + public $email;
43 + /**
44 + * @var string
45 + */
46 + public $embedded_mode;
47 + /**
48 + * @var bool
49 + */
50 + public $save_card;
51 +
52 + /**
53 + * @var string
54 + */
55 + public $oney_type;
56 +
57 + /**
58 + * @var string
59 + */
60 + public $oney_product_animation;
61 +
62 + /**
31 63 * @var PayplugGatewayRequirements
32 64 */
33 65 private $requirements;
34 66
@@ -44,9 +76,9 @@
44 76
45 77 /**
46 78 * @var PayplugApi
47 79 */
48 - public $api;
80 + public $payplug_api;
49 81
50 82 /**
51 83 * @var \WC_Logger
52 84 */
@@ -59,32 +91,36 @@
59 91
60 92 /**
61 93 * @var float
62 94 */
63 - const MIN_AMOUNT = 0.99;
95 + public const MIN_AMOUNT = 0.99;
64 96
65 - /**
66 - * @var float
67 - */
68 - const MAX_AMOUNT = 20000;
97 + /**
98 + * @var float
99 + */
100 + public const MAX_AMOUNT = 20000;
69 101
70 - /**
71 - * @var string
72 - */
73 - private $payplug_merchant_country = 'FR';
102 + /**
103 + * @var string
104 + */
105 + private $payplug_merchant_country = 'FR';
74 106
75 - protected $oney_response;
76 - protected $min_oney_price, $oney_thresholds_min;
77 - protected $max_oney_price, $oney_thresholds_max;
107 + protected $oney_response;
108 + public $min_oney_price;
109 + public $oney_thresholds_min;
110 + public $max_oney_price;
111 + public $oney_thresholds_max;
78 112
113 + public const ENABLE_ON_TEST_MODE = true;
114 +
79 115 /**
80 116 * Logging method.
81 117 *
82 118 * @param string $message Log message.
83 119 * @param string $level Optional. Default 'info'.
84 - * emergency|alert|critical|error|warning|notice|info|debug
120 + * emergency|alert|critical|error|warning|notice|info|debug
85 121 */
86 - public static function log($message, $level = 'info')
122 + public static function log($message, $level = 'info'): void
87 123 {
88 124 if (!self::$log_enabled) {
89 125 return;
90 126 }
@@ -94,9 +130,9 @@
94 130 }
95 131
96 132 PayplugWoocommerceHelper::is_pre_30()
97 133 ? self::$log->add('payplug_gateway', $message)
98 - : self::$log->log($level, $message, array('source' => 'payplug_gateway'));
134 + : self::$log->log($level, $message, ['source' => 'payplug_gateway']);
99 135 }
100 136
101 137 /**
102 138 * Construct method
@@ -104,67 +140,70 @@
104 140 * @return void
105 141 */
106 142 public function __construct()
107 143 {
108 - $this->id = 'payplug';
109 - $this->icon = '';
110 - $this->has_fields = false;
111 - $this->method_title = _x('PayPlug', 'Gateway method title', 'payplug');
112 - $this->method_description = __('Enable PayPlug for your customers.', 'payplug');
113 - $this->supports = array(
144 + //required plugin id
145 + $this->id = 'payplug';
146 + $this->supports = [
114 147 'products',
115 148 'refunds',
116 149 'tokenization',
117 - );
118 - $this->new_method_label = __('Pay with another credit card', 'payplug');
150 + ];
119 151
152 + $payplug_gateways = ['payplug', 'american_express', 'apple_pay', 'bancontact', 'oney_x3_with_fees', 'oney_x3_without_fees', 'oney_x4_with_fees', 'oney_x4_without_fees', 'satispay', 'ideal', 'mybank', 'wero', 'bizum', 'scalapay'];
153 +
154 + //save buttom admin
155 + if ((!empty($_GET['section'])) && (in_array($_GET['section'], $payplug_gateways))) {
156 + $GLOBALS['hide_save_button'] = true;
157 + }
158 +
120 159 $this->init_settings();
121 160 $this->requirements = new PayplugGatewayRequirements($this);
122 161 if ($this->user_logged_in()) {
123 162 $this->init_payplug();
124 - }else{
125 - delete_option('woocommerce_payplug_settings');
126 - set_transient( PayplugWoocommerceHelper::get_transient_key(get_option('woocommerce_payplug_settings', [])), null );
127 - }
163 + }
128 164
129 - $this->title = $this->get_option('title');
130 - $this->description = $this->get_option('description');
131 - $this->mode = 'yes' === $this->get_option('mode', 'no') ? 'live' : 'test';
132 - $this->debug = 'yes' === $this->get_option('debug', 'no');
133 - $this->email = $this->get_option('email');
134 - $this->payment_method = $this->get_option('payment_method');
135 - $this->oneclick = 'yes' === $this->get_option('oneclick', 'no');
136 - $this->oney_type = $this->get_option('oney_type', 'with_fees');
137 - $oney_range = PayplugWoocommerceHelper::get_min_max_oney();
138 - $this->min_oney_price = (isset($oney_range['min'])) ? intval($oney_range['min']) : 100;
139 - $this->max_oney_price = (isset($oney_range['max'])) ? intval($oney_range['max']) : 3000;
140 - $this->oney_thresholds_min = $this->get_option('oney_thresholds_min', $this->min_oney_price );
141 - $this->oney_thresholds_max = $this->get_option('oney_thresholds_max', $this->max_oney_price );
165 + $this->mode = (bool) $this->get_configuration()->get_option('mode') ? 'live' : 'test';
166 + $this->debug = (bool) $this->get_configuration()->get_option('debug');
167 + $this->email = (string) $this->get_configuration()->get_option('email');
168 +
169 + //admin form
142 170 $this->init_form_fields();
143 - $this->payplug_merchant_country = PayplugWoocommerceHelper::get_payplug_merchant_country();
144 - $this->oney_product_animation = $this->get_option('oney_product_animation');
145 171
146 172 add_filter('woocommerce_get_customer_payment_tokens', [$this, 'filter_tokens'], 10, 3);
147 173
148 174 self::$log_enabled = $this->debug;
149 175
150 - // Ensure the description is not empty to correctly display users's save cards
151 - if (empty($this->description) && 0 !== count($this->get_tokens())) {
152 - $this->description = ' ';
176 + add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
177 + add_action('woocommerce_thankyou', [$this, 'validate_payment']);
178 + add_action('the_post', [$this, 'validate_payment']);
179 + add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
180 + }
181 +
182 + /**
183 + * @param $option
184 + * @param $value
185 + *
186 + * @return bool|void
187 + */
188 + public function update_option($option, $value = '')
189 + {
190 + if ($this->needs_setup()) {
191 + wp_send_json_error('needs_setup');
192 + wp_die();
153 193 }
154 194
155 - if ('test' === $this->mode) {
156 - $this->description .= " \n";
195 + parent::update_option($option, $value);
196 + }
157 197
158 - $this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug');
159 - $this->description = trim($this->description);
160 - }
161 -
162 - add_filter('woocommerce_get_order_item_totals', [$this, 'customize_gateway_title'], 10, 2);
163 - add_action('wp_enqueue_scripts', [$this, 'scripts']);
164 - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
165 - add_action('the_post', [$this, 'validate_payment']);
166 - add_action('woocommerce_available_payment_gateways', [$this, 'check_gateway']);
198 + /**
199 + * this payment gateway cannot be updated on the wooco payment settings
200 + *
201 + * @return bool
202 + */
203 + public function needs_setup()
204 + {
205 + return true;
167 206 }
168 207
169 208 /**
170 209 * Customize gateway title in emails.
@@ -177,15 +216,14 @@
177 216 * @author Clément Boirie
178 217 */
179 218 public function customize_gateway_title($total_rows, $order)
180 219 {
220 + $get_payment_method = $this->id;
221 + if (method_exists($order, 'get_payment_method')) {
222 + $get_payment_method = $order->get_payment_method();
223 + }
181 224
182 - $get_payment_method = $this->id;
183 - if( method_exists($order, "get_payment_method") ) {
184 - $get_payment_method = $order->get_payment_method();
185 - }
186 -
187 - $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $get_payment_method;
225 + $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $get_payment_method;
188 226 if (
189 227 $this->id !== $payment_method
190 228 || !isset($total_rows['payment_method'])
191 229 ) {
@@ -201,16 +239,39 @@
201 239 * Validate order payment when the user is redirected to the success confirmation page.
202 240 *
203 241 * @throws \WC_Data_Exception
204 242 */
205 - public function validate_payment()
243 + public function validate_payment($id = null, $save_request = true, $ipn = false): void
206 244 {
207 - if (!is_wc_endpoint_url('order-received') || empty($_GET['key'])) {
208 - return;
245 + global $wp;
246 +
247 + if (!$ipn) {
248 + if (!is_wc_endpoint_url('order-received') || (empty($_GET['key']) && empty($id))) {
249 + return;
250 + }
209 251 }
210 252
211 - $order_id = wc_get_order_id_by_order_key(wc_clean($_GET['key']));
253 + if (!empty($_GET['order-received'])) {
254 + $order_id = (int) ($_GET['order-received']);
255 + } elseif (!empty($id) && !is_object($id)) {
256 + $order_id = (int) $id;
257 + }
258 +
259 + if (empty($order_id) && (isset($wp->query_vars['order-received']))) {
260 + $order_id = apply_filters(
261 + 'woocommerce_thankyou_order_id',
262 + absint($wp->query_vars['order-received'])
263 + );
264 + }
265 +
212 266 if (empty($order_id)) {
267 + if (empty($_GET['key']) && empty($id) && !is_object($id)) {
268 + return;
269 + }
270 + $order_id = wc_get_order_id_by_order_key(wc_clean((!empty($_GET['key']) ? $_GET['key'] : (int) $id)));
271 + }
272 +
273 + if (empty($order_id)) {
213 274 return;
214 275 }
215 276
216 277 $order = wc_get_order($order_id);
@@ -218,65 +279,49 @@
218 279 return;
219 280 }
220 281
221 282 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
222 - 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'])) {
283 + 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', 'mybank', 'ideal', 'wero', 'bizum', 'scalapay'])) {
223 284 return;
224 285 }
225 286
287 + if ($payment_method === $this->id) {
288 + $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
289 + if (empty($transaction_id)) {
290 + self::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
226 291
227 - $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
228 - if (empty($transaction_id)) {
229 - PayplugGateway::log(sprintf('Order #%s : Missing transaction id.', $order_id), 'error');
292 + return;
293 + }
230 294
231 - return;
232 - }
295 + $lock_id = Lock::handle_insert($save_request, $transaction_id);
296 + if (!$lock_id) {
297 + return;
298 + }
233 299
234 - try {
235 - $payment = $this->api->payment_retrieve($transaction_id);
236 - } catch (\Exception $e) {
237 - PayplugGateway::log(
238 - sprintf(
239 - 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
240 - $order_id,
241 - $e->getMessage()
242 - )
243 - );
300 + try {
301 + $payment = $this->payplug_api->payment_retrieve($transaction_id);
302 + } catch (\Exception $e) {
303 + self::log(
304 + sprintf(
305 + 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
306 + $order_id,
307 + $e->getMessage()
308 + )
309 + );
244 310
245 - return;
246 - }
311 + return;
312 + }
247 313
248 - //FIXME:: this is being runned 1 time for each gateway,
249 - // this comparisson is only needed to only run the process_method one time
250 - if($payment_method != $this->id){
251 - return;
252 - }
314 + $this->response->process_payment($payment);
253 315
254 - $this->response->process_payment($payment);
255 - }
316 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock($lock_id);
317 + $waiting_requests = \Payplug\PayplugWoocommerce\Model\Lock::get_lock_by_payment_id($transaction_id);
256 318
257 - /**
258 - * Get payment icons.
259 - *
260 - * @return string
261 - */
262 - public function get_icon()
263 - {
264 -
265 - $src = ('it_IT' === get_locale())
266 - ? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_PostePay.svg'
267 - : PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/logos_scheme_CB.svg';
268 -
269 - $icons = apply_filters('payplug_payment_icons', [
270 - 'payplug' => sprintf('<img src="%s" alt="Visa & Mastercard" class="payplug-payment-icon" />', esc_url($src)),
271 - ]);
272 -
273 - $icons_str = '';
274 - foreach ($icons as $icon) {
275 - $icons_str .= $icon;
319 + if ($waiting_requests) {
320 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_by_payment_id($transaction_id);
321 + $this->validate_payment($order_id, false);
322 + }
276 323 }
277 -
278 - return $icons_str;
279 324 }
280 325
281 326 /**
282 327 * Check if this gateway is enabled
@@ -282,10 +327,18 @@
282 327 * Check if this gateway is enabled
283 328 */
284 329 public function is_available()
285 330 {
286 - if ('yes' === $this->enabled) {
287 - return $this->requirements->satisfy_requirements() && !empty($this->get_api_key($this->get_current_mode()));
331 + if ('yes' == $this->enabled) {
332 + $available = $this->requirements->satisfy_requirements() && !empty($this->get_api_key($this->get_current_mode()));
333 +
334 + // $this->enabled only reflects the country/context check as it was at gateway construction time,
335 + // so re-run it here to catch context that only becomes known later in the request (e.g. order-pay).
336 + if ($available && method_exists($this, 'checkGateway')) {
337 + $available = $this->checkGateway();
338 + }
339 +
340 + return $available;
288 341 }
289 342
290 343 return parent::is_available();
291 344 }
@@ -292,212 +345,200 @@
292 345
293 346 /**
294 347 * Load gateway settings.
295 348 */
296 - public function init_settings()
349 + public function init_settings(): void
297 350 {
298 351 parent::init_settings();
299 - $this->enabled = !empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no';
352 + $enabled = !empty($this->settings['enabled']) && (bool) $this->settings['enabled'];
353 + $this->enabled = $enabled ? 'yes' : 'no';
300 354 }
301 355
302 356 /**
303 357 * Register gateway settings.
304 358 */
305 - public function init_form_fields()
359 + public function init_form_fields(): void
306 360 {
307 - $anchor = esc_html_x( __("More informations", 'payplug'), 'modal', 'payplug' );
308 - $domain = __( 'support.payplug.com/hc/fr/articles/4408142346002', 'payplug' );
309 - $link = sprintf( ' <a href="https://%s" target="_blank">%s</a>', $domain, $anchor );
361 + $anchor = esc_html_x(__('More informations', 'payplug'), 'modal', 'payplug');
362 + $domain = __('support.payplug.com/hc/fr/articles/4408142346002', 'payplug');
363 + $link = sprintf(' <a href="https://%s" target="_blank">%s</a>', $domain, $anchor);
310 364
365 + $anchor_bancontact = esc_html_x(__('payplug_bancontact_activation_request', 'payplug'), 'modal', 'payplug');
366 + $domain_bancontact = __('payplug_bancontact_activation_url', 'payplug');
367 + $bancontact_call_to_action = sprintf(' <a id="bancontact_call_to_action" href="https://%s" target="_blank">%s</a>', $domain_bancontact, $anchor_bancontact);
311 368
312 - $anchor_bancontact = esc_html_x( __("payplug_bancontact_activation_request", 'payplug'), 'modal', 'payplug' );
313 - $domain_bancontact = __( 'payplug_bancontact_activation_url', 'payplug' );
314 - $bancontact_call_to_action = sprintf( ' <a id="bancontact_call_to_action" href="https://%s" target="_blank">%s</a>', $domain_bancontact, $anchor_bancontact );
315 -
369 + $oney_cfg = $this->get_configuration()->get_option('payment_methods.configuration.oney');
370 + if (!empty($oney_cfg)) {
371 + $oney_amount = json_decode($oney_cfg['custom_amounts'], true);
372 + $oney_amount['min'] = (float) $oney_amount['min'] / 100;
373 + $oney_amount['max'] = (float) $oney_amount['max'] / 100;
374 + } else {
375 + $oney_amount = [
376 + 'min' => 100,
377 + 'max' => 3000,
378 + ];
379 + }
316 380 $fields = [
317 - 'enabled' => [
318 - 'title' => __('Enable/Disable', 'payplug'),
319 - 'type' => 'checkbox',
320 - 'label' => __('Enable PayPlug', 'payplug'),
381 + 'enabled' => [
382 + 'title' => __('Enable/Disable', 'payplug'),
383 + 'type' => 'checkbox',
384 + 'label' => __('Enable PayPlug', 'payplug'),
321 385 'description' => __('Only Euro payments can be processed with PayPlug.', 'payplug'),
322 - 'default' => 'no',
386 + 'default' => false,
323 387 ],
324 - 'title' => [
325 - 'title' => __('Title', 'payplug'),
326 - 'type' => 'text',
388 + 'title' => [
389 + 'title' => __('Title', 'payplug'),
390 + 'type' => 'text',
327 391 'description' => __('The payment solution title displayed to your customers during checkout', 'payplug'),
328 - 'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
329 - 'desc_tip' => false,
392 + 'default' => _x('Credit card checkout', 'Default gateway title', 'payplug'),
393 + 'desc_tip' => false,
330 394 ],
331 - 'description' => [
332 - 'title' => __('Description', 'payplug'),
333 - 'type' => 'text',
395 + 'description' => [
396 + 'title' => __('Description', 'payplug'),
397 + 'type' => 'text',
334 398 'description' => __('The payment solution description displayed to your customers during checkout', 'payplug'),
335 - 'default' => '',
336 - 'desc_tip' => false,
399 + 'default' => '',
400 + 'desc_tip' => false,
337 401 ],
338 - 'title_connexion' => [
402 + 'title_connexion' => [
339 403 'title' => __('Connection', 'payplug'),
340 - 'type' => 'title',
404 + 'type' => 'title',
341 405 ],
342 - 'email' => [
343 - 'type' => 'hidden',
406 + 'email' => [
407 + 'type' => 'hidden',
344 408 'default' => '',
345 409 ],
346 - 'login' => [
347 - 'type' => 'login',
410 + 'login' => [
411 + 'type' => 'login',
348 412 'default' => '',
349 413 ],
350 - 'payplug_test_key' => [
351 - 'type' => 'hidden',
352 - 'default' => '',
353 - ],
354 - 'payplug_live_key' => [
355 - 'type' => 'hidden',
356 - 'default' => '',
357 - ],
358 - 'payplug_merchant_id' => [
359 - 'type' => 'hidden',
360 - 'default' => '',
361 - ],
362 - 'title_testmode' => [
414 + 'title_testmode' => [
363 415 'title' => __('Mode', 'payplug'),
364 - 'type' => 'title',
416 + 'type' => 'title',
365 417 ],
366 - 'mode' => [
367 - 'title' => '',
368 - 'label' => '',
369 - 'type' => 'yes_no',
370 - 'yes' => 'Live',
371 - 'no' => 'Test',
418 + 'mode' => [
419 + 'title' => '',
420 + 'label' => '',
421 + 'type' => 'yes_no',
422 + 'yes' => 'Live',
423 + 'no' => 'Test',
372 424 'description' => __('In TEST mode, all payments will be simulations and will not generate real transactions.', 'payplug'),
373 - 'default' => 'no',
374 - 'hide_label' => true,
425 + 'default' => false,
426 + 'hide_label' => true,
375 427 ],
376 - 'title_settings' => [
428 + 'title_settings' => [
377 429 'title' => __('Settings', 'payplug'),
378 - 'type' => 'title',
430 + 'type' => 'title',
379 431 ],
380 - 'payment_method' => [
381 - 'title' => __('Payment page', 'payplug'),
382 - 'type' => 'radio',
383 - 'options' => array(
384 - 'redirect' => __('Redirect', 'payplug'),
385 - 'embedded' => __('Integrated', 'payplug'),
386 - ),
432 + 'payment_method' => [
433 + 'title' => __('Payment page', 'payplug'),
434 + 'type' => 'radio',
435 + 'options' => [
436 + 'redirect' => __('Redirect', 'payplug'),
437 + 'embedded' => __('Integrated', 'payplug'),
438 + ],
387 439 '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'),
388 - 'default' => 'redirect',
389 - 'desc_tip' => false
440 + 'default' => 'redirect',
441 + 'desc_tip' => false,
390 442 ],
391 - 'debug' => [
392 - 'title' => __('Debug', 'payplug'),
393 - 'type' => 'checkbox',
443 + 'debug' => [
444 + 'title' => __('Debug', 'payplug'),
445 + 'type' => 'checkbox',
394 446 'description' => __('Debug mode saves additional information on your server for each operation done via the PayPlug plugin (Developer setting).', 'payplug'),
395 - 'label' => __('Activate debug mode', 'payplug'),
396 - 'default' => 'yes',
397 - 'desc_tip' => false
447 + 'label' => __('Activate debug mode', 'payplug'),
448 + 'default' => true,
449 + 'desc_tip' => false,
398 450 ],
399 451 'title_advanced_settings' => [
400 - 'title' => __('Advanced Settings', 'payplug'),
452 + 'title' => __('payplug_advanced_settings', 'payplug'),
401 453 'description' => __(
402 454 '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>',
403 455 'payplug'
404 456 ),
405 - 'type' => 'title',
457 + 'type' => 'title',
406 458 ],
407 - 'oneclick' => [
408 - 'title' => __('One Click Payment', 'payplug'),
409 - 'type' => 'checkbox',
410 - 'label' => __('Activate', 'payplug'),
459 + 'save_card' => [
460 + 'title' => __('One Click Payment', 'payplug'),
461 + 'type' => 'checkbox',
462 + 'label' => __('Activate', 'payplug'),
411 463 'description' => __('Allow your customers to save their credit card information for later purchases.', 'payplug'),
412 - 'default' => 'no',
413 - 'desc_tip' => false
464 + 'default' => false,
465 + 'desc_tip' => false,
414 466 ],
415 - 'bancontact' => [
416 - 'title' => __('payplug_bancontact_activate_title', 'payplug'),
417 - 'type' => 'checkbox',
418 - 'label' => __('Activate', 'payplug'),
419 - 'description' => '<p class="description" id="bancontact_test_mode_description"> '. __('payplug_bancontact_testmode_description', 'payplug') .' </p>' .
420 - '<p class="description" id="bancontact_live_mode_description_disabled"> '. __('payplug_bancontact_livemode_description_disabled', 'payplug') .' </p>' .
421 - $bancontact_call_to_action,
422 - 'default' => 'no',
423 - ],
424 - 'apple_pay' => [
425 - 'title' => __('payplug_apple_pay_activate_title', 'payplug'),
426 - 'type' => 'checkbox',
427 - 'label' => __('Activate', 'payplug'),
428 - 'description' => '<p class="description" id="apple_pay_test_mode_description"> '. __('payplug_apple_pay_testmode_description', 'payplug') .' </p>' .
429 - '<p class="description" id="apple_pay_live_mode_description"> '. __('payplug_apple_pay_livemode_description', 'payplug') .' </p>' ,
430 - 'default' => 'no',
431 - ],
432 - 'american_express' => [
433 - 'title' => __('payplug_amex_title', 'payplug'),
434 - 'type' => 'checkbox',
435 - 'label' => __('payplug_amex_activate', 'payplug'),
436 - 'description' => '<p class="description" id="amex_test_mode_description"> '. __('payplug_amex_testmode_description', 'payplug') .' </p>' .
437 - '<p class="description" id="amex_live_mode_description"> '. __('payplug_amex_livemode_description', 'payplug') .' </p>' ,
438 - 'default' => 'no',
439 - ],
440 - 'oney' => [
441 - 'title' => __('3x 4x Oney payments', 'payplug'),
442 - 'type' => 'checkbox',
443 - 'label' => __('Activate', 'payplug'),
444 - // TRAD
445 - '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,
446 - 'default' => 'no',
447 - 'desc_tip' => false
448 - ],
449 - 'oney_type' => [
450 - 'title' => '',
451 - 'type' => 'oney_type',
452 - 'options' => array(
453 - 'with_fees' => __('Oney with fees', 'payplug'),
454 - 'without_fees' => __('Oney without fees', 'payplug'),
455 - ),
456 - 'descriptions' => array(
457 - 'with_fees' => __('The fees are split between you and your customers', 'payplug'),
458 - 'without_fees' => __('You pay the fees', 'payplug'),
459 - ),
460 - 'description' => '',
461 - 'default' => 'with_fees',
462 - 'desc_tip' => false
463 - ],
464 - 'oney_thresholds' => [
465 - 'title' => '',
466 - 'type' => 'oney_thresholds',
467 - 'description' => sprintf(__('I would like to offer guaranteed payment in installments for amounts between %s€ and %s€.', 'payplug'),
468 - '<b class="min">' . $this->oney_thresholds_min . '</b>', '<b class="max">' . $this->oney_thresholds_max . '</b>'),
469 - 'desc_tip' => false
470 - ],
471 - 'oney_thresholds_min' => [
472 - 'title' => '',
473 - 'type' => 'hidden',
474 - 'label' => '',
475 - 'description' => '',
476 - 'default' => 'no',
477 - ],
478 - 'oney_thresholds_max' => [
479 - 'title' => '',
480 - 'type' => 'hidden',
481 - 'label' => '',
482 - 'description' => '',
483 - 'default' => 'no',
484 - ],
485 - 'oney_product_animation' => [
486 - 'title' => __('oney_installments_pop_up', 'payplug'),
487 - 'description' => __('display_the_oney_installments_pop_up_on_the_product_page', 'payplug'),
488 - 'label' => __('Activate', 'payplug'),
489 - 'default' => 'no',
490 - 'desc_tip' => false,
491 - 'type' => 'oney_product_animation'
492 - ],
467 + 'bancontact' => [
468 + 'title' => __('payplug_bancontact_activate_title', 'payplug'),
469 + 'type' => 'checkbox',
470 + 'label' => __('Activate', 'payplug'),
471 + 'description' => '<p class="description" id="bancontact_test_mode_description"> ' . __('payplug_bancontact_testmode_description', 'payplug') . ' </p>' .
472 + '<p class="description" id="bancontact_live_mode_description_disabled"> ' . __('payplug_bancontact_livemode_description_disabled', 'payplug') . ' </p>' .
473 + $bancontact_call_to_action,
474 + 'default' => false,
475 + ],
476 + 'apple_pay' => [
477 + 'title' => __('payplug_apple_pay_activate_title', 'payplug'),
478 + 'type' => 'checkbox',
479 + 'label' => __('Activate', 'payplug'),
480 + 'description' => '<p class="description" id="apple_pay_test_mode_description"> ' . __('payplug_apple_pay_testmode_description', 'payplug') . ' </p>' .
481 + '<p class="description" id="apple_pay_live_mode_description"> ' . __('payplug_apple_pay_livemode_description', 'payplug') . ' </p>',
482 + 'default' => false,
483 + ],
484 + 'american_express' => [
485 + 'title' => __('payplug_amex_title', 'payplug'),
486 + 'type' => 'checkbox',
487 + 'label' => __('payplug_amex_activate', 'payplug'),
488 + 'description' => '<p class="description" id="amex_test_mode_description"> ' . __('payplug_amex_testmode_description', 'payplug') . ' </p>' .
489 + '<p class="description" id="amex_live_mode_description"> ' . __('payplug_amex_livemode_description', 'payplug') . ' </p>',
490 + 'default' => false,
491 + ],
492 + 'oney' => [
493 + 'title' => __('3x 4x Oney payments', 'payplug'),
494 + 'type' => 'checkbox',
495 + 'label' => __('Activate', 'payplug'),
496 + // TRAD
497 + '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,
498 + 'default' => false,
499 + 'desc_tip' => false,
500 + ],
501 + 'oney_type' => [
502 + 'title' => '',
503 + 'type' => 'oney_type',
504 + 'options' => [
505 + 'with_fees' => __('Oney with fees', 'payplug'),
506 + 'without_fees' => __('Oney without fees', 'payplug'),
507 + ],
508 + 'descriptions' => [
509 + 'with_fees' => __('The fees are split between you and your customers', 'payplug'),
510 + 'without_fees' => __('You pay the fees', 'payplug'),
511 + ],
512 + 'description' => '',
513 + 'default' => 'with_fees',
514 + 'desc_tip' => false,
515 + ],
516 + 'oney_thresholds' => [
517 + 'title' => '',
518 + 'type' => 'oney_thresholds',
519 + 'description' => sprintf(
520 + __('I would like to offer guaranteed payment in installments for amounts between %s€ and %s€.', 'payplug'),
521 + '<b class="min">' . $oney_amount['min'] . '</b>',
522 + '<b class="max">' . $oney_amount['max'] . '</b>'
523 + ),
524 + 'desc_tip' => false,
525 + ],
526 + 'oney_product_animation' => [
527 + 'title' => __('oney_installments_pop_up', 'payplug'),
528 + 'description' => __('display_the_oney_installments_pop_up_on_the_product_page', 'payplug'),
529 + 'label' => __('Activate', 'payplug'),
530 + 'default' => false,
531 + 'desc_tip' => false,
532 + 'type' => 'oney_product_animation',
533 + ],
493 534 ];
494 535
495 536 if ($this->user_logged_in()) {
496 537 if ($this->permissions->has_permissions(PayplugPermissions::SAVE_CARD)) {
497 538 unset($fields['title_advanced_settings']);
498 - } else if ('live' === $this->get_current_mode()){
499 - $fields['oneclick']['disabled'] = true;
539 + } elseif ('live' === $this->get_current_mode()) {
540 + $fields['save_card']['disabled'] = true;
500 541 }
501 542 }
502 543
503 544 /**
@@ -504,9 +545,9 @@
504 545 * Filter PayPlug gateway settings.
505 546 *
506 547 * @param array $fields
507 548 */
508 - $fields = apply_filters('payplug_gateway_settings', $fields);
549 + $fields = apply_filters('payplug_gateway_settings', $fields);
509 550 $this->form_fields = $fields;
510 551 }
511 552
512 553 /**
@@ -511,65 +552,23 @@
511 552
512 553 /**
513 554 * Set global configuration for PayPlug instance.
514 555 */
515 - public function init_payplug()
556 + public function init_payplug(): void
516 557 {
517 - $this->api = new PayplugApi($this);
518 - $this->api->init();
558 + $this->payplug_api = new PayplugApi($this);
559 + $this->payplug_api->init();
519 560
520 - $this->permissions = new PayplugPermissions($this);
521 - $this->response = new PayplugResponse($this);
561 + // init() just resolved this for the same mode - reuse it instead of asking
562 + // Service\Api::get_bearer_token() to do so again right after.
563 + $this->permissions = new PayplugPermissions($this, $this->payplug_api->get_current_bearer_token());
564 + $this->response = new PayplugResponse($this);
522 565
523 566 // Register IPN handler
524 567 new PayplugIpnResponse($this);
525 -
526 568 }
527 569
528 570 /**
529 - * Embedded payment form scripts.
530 - *
531 - * Register scripts and additionnal data needed for the
532 - * embedded payment form.
533 - */
534 - public function scripts()
535 - {
536 - if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) {
537 - return;
538 - }
539 -
540 - // If PayPlug is not enabled bail.
541 - if ('no' === $this->enabled) {
542 - return;
543 - }
544 -
545 - // If keys are not set bail.
546 - if (empty($this->get_api_key($this->mode))) {
547 - PayplugGateway::log('Keys are not set correctly.');
548 -
549 - return;
550 - }
551 -
552 - // Register checkout styles.
553 - wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION);
554 - wp_enqueue_style('payplug-checkout');
555 -
556 - wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
557 - wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', [
558 - 'jquery',
559 - 'payplug'
560 - ], PAYPLUG_GATEWAY_VERSION, true);
561 - wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
562 - 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
563 - 'nonce' => [
564 - 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
565 - ],
566 - 'is_embedded' => 'redirect' !== $this->payment_method
567 - ]);
568 - wp_enqueue_script('payplug-checkout');
569 - }
570 -
571 - /**
572 571 * Filter saved tokens for the gateway.
573 572 *
574 573 * A token will be removed if :
575 574 * - it doesn't match the current merchant logged in,
@@ -583,16 +582,27 @@
583 582 * @return array
584 583 */
585 584 public function filter_tokens($tokens, $user_id, $gateway_id)
586 585 {
586 + if (!is_user_logged_in() || !class_exists('WC_Payment_Gateway_CC')) {
587 + return $tokens;
588 + }
587 589
588 - if (!is_user_logged_in() || !class_exists('WC_Payment_Gateway_CC')) {
590 + $saved_card = $this->get_configuration()->get_option('payment_methods.configuration.payplug.save_card')
591 + && is_user_logged_in();
592 +
593 + if (!$saved_card) {
594 + foreach ($tokens as $token_id => $token) {
595 + if ('payplug' == (string) $token->get_gateway_id()) {
596 + unset($tokens[$token_id]);
597 + }
598 + }
599 +
589 600 return $tokens;
590 601 }
591 602
592 603 /* @var \WC_Payment_Token_CC $token */
593 604 foreach ($tokens as $k => $token) {
594 -
595 605 if ($this->id !== $token->get_gateway_id()) {
596 606 continue;
597 607 }
598 608
@@ -603,9 +613,9 @@
603 613 continue;
604 614 }
605 615
606 616 // check if token is available for the current gateway mode
607 - if ($this->mode !== $token->get_meta('mode', true)) {
617 + if ($this->mode != $token->get_meta('mode', true)) {
608 618 unset($tokens[$k]);
609 619 continue;
610 620 }
611 621
@@ -610,9 +620,9 @@
610 620 }
611 621
612 622 // check if token is not expired
613 623 $current_month = \absint(date('n'));
614 - $current_year = \absint(date('Y'));
624 + $current_year = \absint(date('Y'));
615 625 if ($current_year > (int) $token->get_expiry_year()) {
616 626 unset($tokens[$k]);
617 627 continue;
618 628 }
@@ -625,16 +635,20 @@
625 635
626 636 return $tokens;
627 637 }
628 638
629 - public function payment_fields()
639 + /**
640 + * extra payment fields
641 + */
642 + public function payment_fields(): void
630 643 {
631 644 $description = $this->get_description();
645 +
632 646 if (!empty($description)) {
633 647 echo wpautop(wptexturize($description));
634 648 }
635 649
636 - if ($this->oneclick_available()) {
650 + if ($this->save_card_available()) {
637 651 $this->tokenization_script();
638 652 $this->saved_payment_methods();
639 653 }
640 654 }
@@ -641,356 +655,185 @@
641 655
642 656 /**
643 657 * Handle admin display.
644 658 */
645 - public function admin_options()
659 + public function admin_options(): void
646 660 {
647 - wp_enqueue_style(
648 - 'payplug-gateway-style',
649 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/app.css',
650 - [],
651 - PAYPLUG_GATEWAY_VERSION
652 - );
661 + /************ VUE Code *************/
662 + wp_enqueue_script('chunk-vendors.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/chunk-vendors-' . PAYPLUG_GATEWAY_VERSION . '.js', [], PAYPLUG_GATEWAY_VERSION);
663 + wp_enqueue_script('app.js', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/js/app-' . PAYPLUG_GATEWAY_VERSION . '.js', [], PAYPLUG_GATEWAY_VERSION);
664 + wp_enqueue_style('app.css', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/css/app-' . PAYPLUG_GATEWAY_VERSION . '.css', [], PAYPLUG_GATEWAY_VERSION);
665 + wp_localize_script(
666 + 'app.js',
667 + 'payplug_admin_config',
668 + [
669 + 'img_path' => esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/dist/'),
670 + 'ajax_url' => get_home_url(),
671 + 'rest_url' => get_home_url() . '/?rest_route=/payplug_api/',
672 + ]
673 + ); ?>
674 + <script>window.get_data_url = "<?php echo rest_url('payplug/data'); ?>"</script>
675 + <script>window.set_data_url = "<?php echo rest_url('payplug/save_data'); ?>"</script>
676 + <div id="payplug_admin"></div>
653 677
654 - wp_enqueue_script(
655 - 'payplug-gateway-admin',
656 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin.js',
657 - ['jquery-ui-dialog'],
658 - PAYPLUG_GATEWAY_VERSION
659 - );
678 + <?php
660 679
661 - wp_enqueue_script(
662 - 'payplug-gateway-admin-bancontact',
663 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-bancontact.js',
664 - ['jquery-ui-dialog'],
665 - PAYPLUG_GATEWAY_VERSION
666 - );
680 + /*********** End VUE Code ***********/
681 + }
667 682
668 - wp_enqueue_script(
669 - 'payplug-gateway-admin-applepay',
670 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-applepay.js',
671 - ['jquery-ui-dialog'],
672 - PAYPLUG_GATEWAY_VERSION
673 - );
683 + /**
684 + * Process payment.
685 + *
686 + * @param int $order_id
687 + *
688 + * @throws \Exception
689 + *
690 + * @return array
691 + */
692 + public function process_payment($order_id)
693 + {
694 + self::log(sprintf('Processing payment for order #%s', $order_id));
674 695
675 - wp_enqueue_script(
676 - 'payplug-gateway-admin-amex',
677 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-amex.js',
678 - [],
679 - PAYPLUG_GATEWAY_VERSION
680 - );
696 + $order = wc_get_order($order_id);
697 + if (!$order instanceof \WC_Order) {
698 + self::log(sprintf('Order #%s not found.', $order_id), 'error');
699 + throw new \Exception(__('Order not found.', 'payplug'));
700 + }
701 + $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
702 + $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total());
703 + $amount = $this->validate_order_amount($amount);
681 704
682 - wp_localize_script('payplug-gateway-admin-bancontact', 'payplug_admin_config', array(
683 - 'ajax_url' => admin_url('admin-ajax.php'),
684 - 'has_live_key' => (false === $this->has_api_key('live')) ? false : true,
685 - 'btn_ok' => _x('Ok', 'modal', 'payplug'),
686 - 'btn_label' => _x('Cancel', 'modal', 'payplug'),
687 - 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'),
688 - ));
705 + if (is_wp_error($amount)) {
706 + self::log(sprintf('Invalid amount %s for the order.', $order->get_total()), 'error');
707 + throw new \Exception($amount->get_error_message());
708 + }
689 709
690 - wp_localize_script('payplug-gateway-admin', 'payplug_admin_config', array(
691 - 'ajax_url' => admin_url('admin-ajax.php'),
692 - 'has_live_key' => (false === $this->has_api_key('live')) ? false : true,
693 - 'btn_ok' => _x('Ok', 'modal', 'payplug'),
694 - 'btn_label' => _x('Cancel', 'modal', 'payplug'),
695 - 'general_error' => _x('Something went wrong. Please refresh the page and retry.', 'modal', 'payplug'),
696 - ));
710 + $payment_token_id = (isset($_POST['wc-' . $this->id . '-payment-token']) && 'new' !== $_POST['wc-' . $this->id . '-payment-token'])
711 + ? wc_clean($_POST['wc-' . $this->id . '-payment-token'])
712 + : false;
697 713
698 - wp_enqueue_script(
699 - 'payplug-gateway-admin-oney',
700 - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-admin-oney.js',
701 - ['jquery-ui-dialog'],
702 - PAYPLUG_GATEWAY_VERSION
703 - );
714 + if ($payment_token_id && (int) $customer_id > 0) {
715 + self::log(sprintf('Payment token found.', $amount));
704 716
705 - wp_localize_script('payplug-gateway-admin-oney', 'payplug_admin_config', array(
706 - 'ajax_url' => admin_url('admin-ajax.php'),
707 - 'btn_ok' => _x('Ok', 'modal', 'payplug'),
708 - 'has_live_key' => (false === $this->has_api_key('live')) ? false : true,
709 - 'min_oney_price' => $this->min_oney_price,
710 - 'max_oney_price' => $this->max_oney_price,
711 - 'oney_thresholds_min' =>$this->oney_thresholds_min,
712 - 'oney_thresholds_max' => $this->oney_thresholds_max,
713 - ));
714 -
715 - if ($this->user_logged_in() && false === $this->has_api_key('live')) {
716 - add_action('admin_footer', function () {
717 - $email = $this->get_option('email');
718 -?>
719 - <div id="payplug-refresh-keys-modal" title="<?php echo esc_attr_x('Mode LIVE', 'modal', 'payplug'); ?>">
720 - <form id="payplug-refresh-keys-modal__form">
721 - <p id="dialog-msg"></p>
722 - <p><?php echo esc_html_x('Please enter your PayPlug account password', 'modal', 'payplug'); ?></p>
723 - <input type="password" name="password" required title="<?php echo esc_attr_x('Enter your PayPlug account password', 'modal', 'payplug'); ?>" />
724 - <input type="hidden" name="email" value="<?php echo esc_attr($email); ?>">
725 - <input type="hidden" name="action" value="<?php echo esc_attr(Ajax::REFRESH_KEY_ACTION); ?>">
726 - <?php wp_nonce_field(sprintf('%s_%s', $email, Ajax::REFRESH_KEY_ACTION)); ?>
727 - <input class="ui-dialog-sronly" type="submit" tabindex="-1">
728 - </form>
729 - </div>
730 - <?php
731 - });
717 + return $this->process_payment_with_token($order, $amount, $customer_id, $payment_token_id);
732 718 }
733 719
734 - $payplug_requirements = new PayplugGatewayRequirements($this); ?>
735 -
736 - <h2 class="title--logo"><?php esc_html($this->get_method_title()) ?></h2>
737 - <p><?php _e(sprintf('Version %s', PAYPLUG_GATEWAY_VERSION)); ?></p>
738 - <div class="payplug-requirements">
739 - <?php echo $payplug_requirements->curl_requirement(); ?>
740 - <?php echo $payplug_requirements->php_requirement(); ?>
741 - <?php echo $payplug_requirements->openssl_requirement(); ?>
742 - <?php echo $payplug_requirements->account_requirement(); ?>
743 - <?php echo $payplug_requirements->currency_requirement(); ?>
744 - <?php echo $payplug_requirements->oney_requirement(); ?>
745 - </div>
746 - <?php echo wp_kses_post(wpautop($this->get_method_description())); ?>
747 -
748 - <?php if ($this->user_logged_in()) : ?>
749 - <table class="form-table">
750 - <?php $this->generate_settings_html($this->get_form_fields()); ?>
751 - </table>
752 - <?php else :
753 - $GLOBALS['hide_save_button'] = true; ?>
754 - <h3 class="wc-settings-sub-title"><?php _e('Connection', 'payplug'); ?></h3>
755 - <table class="form-table">
756 - <tbody>
757 - <tr valign="top">
758 - <th scope="row" class="titledesc">
759 - <label for="payplug_email"><?php _e('Email', 'payplug'); ?></label>
760 - </th>
761 - <td class="forminp">
762 - <fieldset>
763 - <legend class="screen-reader-text"><span><?php _e('Email', 'payplug'); ?></span></legend>
764 - <input class="input-text regular-input" type="text" name="payplug_email" id="payplug_email" value="" placeholder="<?php _e('[email protected]', 'payplug'); ?>" />
765 - </fieldset>
766 - </td>
767 - </tr>
768 - <tr valign="top">
769 - <th scope="row" class="titledesc">
770 - <label for="payplug_password"><?php _e('Password', 'payplug'); ?></label>
771 - </th>
772 - <td class="forminp">
773 - <fieldset>
774 - <legend class="screen-reader-text"><span><?php _e('Password', 'payplug'); ?></span>
775 - </legend>
776 - <input class="input-text regular-input" type="password" name="payplug_password" id="payplug_password" value="" />
777 - </fieldset>
778 - </td>
779 - </tr>
780 - <tr valign="top">
781 - <td class="forminp">
782 - <input id="payplug-login" class="button" type="submit" value="<?php _e('Login', 'payplug'); ?>">
783 - <input type="hidden" name="save" value="login">
784 - <?php wp_nonce_field('payplug_user_login', '_loginaction'); ?>
785 - </td>
786 - </tr>
787 - </tbody>
788 - </table>
789 - <?php
790 - endif;
791 - ?>
792 - <div id="payplug-oney-modal" title="<?php echo esc_attr_x('Mode LIVE', 'modal', 'payplug'); ?>">
793 - <p>
794 - <?php echo esc_html_x('Attention, pour utiliser la méthode de paiement Oney en mode LIVE merci de nous contacter à', 'modal', 'payplug'); ?>
795 - <br/>
796 - <a href="mailto:[email protected]">[email protected]</a>
797 - </p>
798 - </div>
799 - <?php
720 + return $this->process_standard_payment($order, $amount, $customer_id);
800 721 }
801 722
802 723 /**
803 - * Process admin options.
724 + * Whether the current request is repaying an existing order (order-pay page, or one of
725 + * the order-pay AJAX flows). The order_key/order_pay_key posted by those AJAX flows must
726 + * match the order's own key: their mere presence isn't proof of anything, since any
727 + * request can set them.
804 728 *
729 + * @param \WC_Order|null $order
730 + *
805 731 * @return bool
806 732 */
807 - public function process_admin_options()
733 + protected function is_order_pay_request($order): bool
808 734 {
809 - $data = $this->get_post_data();
810 - $oneclick_fieldkey = $this->get_field_key('oneclick');
811 -
812 - // Handle logout process
813 - if (
814 - isset($data['submit_logout'])
815 - && false !== check_admin_referer('payplug_user_logout', '_logoutaction')
816 - ) {
817 -
818 - if ($this->permissions) {
819 - $this->permissions->clear_permissions();
820 - }
821 -
822 - $data = get_option($this->get_option_key());
823 - $data['payplug_test_key'] = '';
824 - $data['payplug_live_key'] = '';
825 - $data['payplug_merchant_id'] = '';
826 - $data['enabled'] = 'no';
827 - $data['mode'] = 'no';
828 - $data['oneclick'] = 'no';
829 - update_option(
830 - $this->get_option_key(),
831 - apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
832 - );
833 - if("payplug" === $this->id) {
834 - \WC_Admin_Settings::add_message(__('Successfully logged out.', 'payplug'));
835 - }
836 -
735 + if (is_wc_endpoint_url('order-pay')) {
837 736 return true;
838 737 }
839 738
840 - // Handle login process
841 - if (
842 - isset($data['payplug_email'])
843 - && false !== check_admin_referer('payplug_user_login', '_loginaction')
844 - ) {
845 - $email = $data['payplug_email'];
846 - $password = wp_unslash($data['payplug_password']);
847 - $response = $this->retrieve_user_api_keys($email, $password);
848 - if (is_wp_error($response)) {
849 - \WC_Admin_Settings::add_error($response->get_error_message());
850 -
851 - return false;
852 - }
853 -
854 - // try to use the api keys to retrieve the merchant id
855 - $merchant_id = isset($response['test']) ? $this->retrieve_merchant_id($response['test']) : '';
856 -
857 - $this->init_form_fields();
858 - $fields = $this->get_form_fields();
859 - $data = [];
860 -
861 - // Load existing values if the user is re-login.
862 - foreach ($fields as $key => $field) {
863 - if (in_array($field['type'], ['title', 'login'])) {
864 - continue;
865 - }
866 -
867 - switch ($key) {
868 - case 'enabled':
869 - $val = 'yes';
870 - break;
871 - case 'mode':
872 - $val = 'no';
873 - break;
874 - case 'payplug_test_key':
875 - $val = !empty($response['test']) ? esc_attr($response['test']) : null;
876 - break;
877 - case 'payplug_live_key':
878 - $val = !empty($response['live']) ? esc_attr($response['live']) : null;
879 - break;
880 - case 'payplug_merchant_id':
881 - $val = esc_attr($merchant_id);
882 - break;
883 - case 'email':
884 - $val = esc_html($email);
885 - break;
886 - default:
887 - $val = $this->get_option($key);
888 - }
889 -
890 - $data[$key] = $val;
891 - }
892 -
893 - $this->set_post_data($data);
894 - update_option(
895 - $this->get_option_key(),
896 - apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->id, $data)
897 - );
898 - if("payplug" === $this->id) {
899 - \WC_Admin_Settings::add_message(__('Successfully logged in.', 'payplug'));
900 - }
901 -
902 - return true;
739 + $posted_key = $_POST['order_pay_key'] ?? $_POST['order_key'] ?? '';
740 + if (empty($posted_key)) {
741 + return false;
903 742 }
904 743
905 - // Don't let user without live key leave TEST mode.
906 - $mode_fieldkey = $this->get_field_key('mode');
907 - $live_key_fieldkey = $this->get_field_key('payplug_live_key');
908 - if (isset($data[$mode_fieldkey]) && '1' === $data[$mode_fieldkey] && empty($data[$live_key_fieldkey])) {
909 - $data[$mode_fieldkey] = null;
910 - $this->set_post_data($data);
911 - \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'));
912 - }
913 -
914 - // Check user permissions before activating one-click feature.
915 - $oneclick_fieldkey = $this->get_field_key('oneclick');
916 - if (
917 - isset($data[$oneclick_fieldkey])
918 - && '1' === $data[$oneclick_fieldkey]
919 - && '1' === $data[$mode_fieldkey]
920 - && (!$this->user_logged_in()
921 - || false === $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD))
922 - ) {
923 - $data[$oneclick_fieldkey] = null;
924 - \WC_Admin_Settings::add_error(__('Only PREMIUM accounts can enable the One Click option in LIVE mode.', 'payplug'));
925 - }
926 -
927 - // Force getAccount to set transient data on live mode
928 - if (
929 - $mode_fieldkey === "woocommerce_payplug_mode" &&
930 - "1" === $data[$mode_fieldkey] &&
931 - !empty($data[$live_key_fieldkey])
932 - ) {
933 - try{
934 - $response = Authentication::getAccount(new Payplug($data[$live_key_fieldkey]));
935 - } catch (ForbiddenException $e){
936 - PayplugGateway::log('Error while saving account : ' . $e->getMessage(), 'error');
937 - \WC_Admin_Settings::add_error($e->getMessage());
938 - return false;
939 - }
940 - PayplugWoocommerceHelper::set_transient_data($response, [
941 - 'mode' => 'yes'
942 - ]);
943 - }
944 -
945 - // Validate Oney thresholds
946 - if($data['woocommerce_payplug_oney_thresholds_min'] < $this->min_oney_price || $data['woocommerce_payplug_oney_thresholds_max'] > $this->max_oney_price){
947 - \WC_Admin_Settings::add_error(sprintf(__('The amount must be between %s€ and %s€.', 'payplug'), $this->min_oney_price, $this->max_oney_price));
948 - return false;
949 - }
950 - if($data['woocommerce_payplug_oney_thresholds_min'] > $data['woocommerce_payplug_oney_thresholds_max']){
951 - \WC_Admin_Settings::add_error(sprintf(__('Please note that the minimum amount entered is greater than the maximum amount entered.', 'payplug'), $this->min_oney_price, $this->max_oney_price));
952 - return false;
953 - }
954 -
955 - $this->data = $data;
956 - parent::process_admin_options();
744 + return $order instanceof \WC_Order && hash_equals($order->get_order_key(), wc_clean(wp_unslash($posted_key)));
957 745 }
958 746
959 747 /**
960 - * Process payment.
748 + * if payment was generated by an intend, we shouldn't generate another one and try to pay it, this would generate duplications
961 749 *
962 - * @param int $order_id
750 + * @param $order
963 751 *
964 - * @return array
965 752 * @throws \Exception
753 + *
754 + * @return array|null
966 755 */
967 - public function process_payment($order_id)
756 + private function process_standard_intent_payment($order)
968 757 {
758 + // This can run from AJAX endpoints whose own request URL never carries the order-pay
759 + // query var, so is_wc_endpoint_url() alone can't detect that context here: fall back
760 + // to the order_key/order_pay_key sent by the order-pay AJAX flows.
761 + $is_order_pay = $this->is_order_pay_request($order);
969 762
970 - PayplugGateway::log(sprintf('Processing payment for order #%s', $order_id));
763 + //no order-pay page, no ajax_on_order_review_page
764 + if (!$is_order_pay &&
765 + PayplugWoocommerceHelper::is_checkout_block() &&
766 + (
767 + ('payplug' == $this->id && in_array($this->embedded_mode, ['integrated', 'popup'])) ||
768 + ('american_express' == $this->id && 'popup' == $this->embedded_mode)
769 + ) &&
770 + ($_GET['wc-ajax'] ?? '') !== 'payplug_order_review_url' &&
771 + !empty($order->get_transaction_id())
772 + ) {
773 + $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
971 774
972 - $order = wc_get_order($order_id);
973 - $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
974 - $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($order->get_total());
975 - $amount = $this->validate_order_amount($amount);
775 + try {
776 + $payment = $this->payplug_api->payment_retrieve($order->get_transaction_id());
777 + if (ob_get_length() > 0) {
778 + ob_clean();
779 + }
976 780
977 - if (is_wp_error($amount)) {
978 - PayplugGateway::log(sprintf('Invalid amount %s for the order.', $order->get_total()), 'error');
979 - throw new \Exception($amount->get_error_message());
980 - }
781 + // Save transaction id for the order
782 + PayplugWoocommerceHelper::is_pre_30()
783 + ? update_post_meta($order_id, '_transaction_id', $payment->id)
784 + : $order->set_transaction_id($payment->id);
981 785
982 - $payment_token_id = (isset($_POST['wc-' . $this->id . '-payment-token']) && 'new' !== $_POST['wc-' . $this->id . '-payment-token'])
983 - ? wc_clean($_POST['wc-' . $this->id . '-payment-token'])
984 - : false;
786 + if ($payment->is_paid) {
787 + $finished_status = wc_get_is_paid_statuses();
788 + $order->set_status($finished_status[0]);
789 + }
985 790
986 - if ($payment_token_id && $this->oneclick_available() && (int) $customer_id > 0) {
987 - PayplugGateway::log(sprintf('Payment token found.', $amount));
791 + if (is_callable([$order, 'save'])) {
792 + $order->save();
793 + }
988 794
989 - return $this->process_payment_with_token($order, $amount, $customer_id, $payment_token_id);
795 + /**
796 + * Fires once a payment has been created.
797 + *
798 + * @param int $order_id Order ID
799 + * @param PaymentResource $payment Payment resource
800 + */
801 + \do_action('payplug_gateway_payment_created', $order_id, $payment);
802 +
803 + $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
804 + PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
805 +
806 + self::log(sprintf('Payment intent created for order #%s', $order_id));
807 +
808 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
809 +
810 + $result = [
811 + 'payment_id' => $payment->id,
812 + 'result' => 'success',
813 + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
814 + 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null,
815 + ];
816 +
817 + // wp_send_json_success() calls die(), which is only safe for the classic
818 + // wc-ajax request this was written for: the Store API checkout flow (used by
819 + // the checkout block) calls process_payment() through the REST framework,
820 + // and killing the process mid-request there produces a broken response.
821 + if (wp_doing_ajax()) {
822 + wp_send_json_success($result);
823 + }
824 +
825 + return $result;
826 + } catch (HttpException $e) {
827 + self::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
828 + throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
829 + } catch (\Exception $e) {
830 + self::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
831 + throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
832 + }
990 833 }
991 834
992 - return $this->process_standard_payment($order, $amount, $customer_id);
835 + return null;
993 836 }
994 837
995 838 /**
996 839 * @param \WC_Order $order
@@ -996,43 +839,70 @@
996 839 * @param \WC_Order $order
997 840 * @param int $amount
998 841 * @param int $customer_id
999 842 *
843 + * @throws \Exception
844 + *
1000 845 * @return array
1001 - * @throws \Exception
1002 846 */
1003 847 public function process_standard_payment($order, $amount, $customer_id)
1004 848 {
849 + $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
1005 850
1006 - $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
851 + $intent = $this->process_standard_intent_payment($order);
852 + if (!empty($intent)) {
853 + return $intent;
854 + }
1007 855
1008 856 try {
1009 857 $address_data = PayplugAddressData::from_order($order);
1010 858
1011 - $return_url = esc_url_raw($order->get_checkout_order_received_url());
859 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
1012 860
1013 - if (!(substr( $return_url, 0, 4 ) === "http")) {
1014 - $return_url = get_site_url().$return_url;
1015 - }
861 + if (!(substr($return_url, 0, 4) === 'http')) {
862 + $return_url = get_site_url() . $return_url;
863 + }
1016 864
1017 865 $payment_data = [
1018 - 'amount' => $amount,
1019 - 'currency' => get_woocommerce_currency(),
1020 - 'allow_save_card' => $this->oneclick_available() && (int) $customer_id > 0,
1021 - 'billing' => $address_data->get_billing(),
1022 - 'shipping' => $address_data->get_shipping(),
1023 - 'hosted_payment' => [
866 + 'amount' => $amount,
867 + 'currency' => get_woocommerce_currency(),
868 + 'allow_save_card' => $this->save_card_available() && (int) $customer_id > 0,
869 + 'billing' => $address_data->get_billing(),
870 + 'shipping' => $address_data->get_shipping(),
871 + 'hosted_payment' => [
1024 872 'return_url' => $return_url,
1025 873 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
1026 874 ],
1027 875 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
1028 - 'metadata' => [
1029 - 'order_id' => $order_id,
876 + 'metadata' => [
877 + 'order_id' => $order_id,
1030 878 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
1031 - 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
879 + 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
1032 880 ],
1033 881 ];
1034 882
883 + if (PayplugWoocommerceHelper::is_checkout_block() && is_checkout()) {
884 + $payment_data['metadata']['woocommerce_block'] = 'CHECKOUT';
885 + } elseif (PayplugWoocommerceHelper::is_cart_block() && is_cart()) {
886 + $payment_data['metadata']['woocommerce_block'] = 'CART';
887 + }
888 +
889 + //IP request required variables
890 + if ('integrated' == $this->embedded_mode) {
891 + $payment_data['initiator'] = 'PAYER';
892 + $payment_data['integration'] = 'INTEGRATED_PAYMENT';
893 + unset($payment_data['hosted_payment']['cancel_url']);
894 + }
895 +
896 + //for subscriptions the card needs to be saved
897 + $is_subscription = PayplugWoocommerceHelper::is_subscription();
898 + if (!empty($is_subscription) && $is_subscription === true) {
899 + $payment_data['allow_save_card'] = false;
900 + $payment_data['save_card'] = true;
901 + $payment_data['force_3ds'] = true;
902 + $payment_data['metadata']['subscription'] = 'subscription';
903 + }
904 +
1035 905 /**
1036 906 * Filter the payment data before it's used
1037 907 *
1038 908 * @param array $payment_data
@@ -1040,9 +910,9 @@
1040 910 * @param array $customer_details
1041 911 * @param PayplugAddressData $address_data
1042 912 */
1043 913 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
1044 - $payment = $this->api->payment_create($payment_data);
914 + $payment = $this->payplug_api->payment_create($payment_data);
1045 915
1046 916 // Save transaction id for the order
1047 917 PayplugWoocommerceHelper::is_pre_30()
1048 918 ? update_post_meta($order_id, '_transaction_id', $payment->id)
@@ -1047,8 +917,11 @@
1047 917 PayplugWoocommerceHelper::is_pre_30()
1048 918 ? update_post_meta($order_id, '_transaction_id', $payment->id)
1049 919 : $order->set_transaction_id($payment->id);
1050 920
921 + $order->set_payment_method($this->id);
922 + $order->set_payment_method_title($this->method_title);
923 +
1051 924 if (is_callable([$order, 'save'])) {
1052 925 $order->save();
1053 926 }
1054 927
@@ -1062,20 +935,25 @@
1062 935
1063 936 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
1064 937 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
1065 938
1066 - PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
939 + self::log(sprintf('Payment creation complete for order #%s', $order_id));
1067 940
941 + if (ob_get_length() > 0) {
942 + ob_clean();
943 + }
944 +
1068 945 return [
1069 - 'result' => 'success',
1070 - 'redirect' => $payment->hosted_payment->payment_url,
1071 - 'cancel' => $payment->hosted_payment->cancel_url,
946 + 'payment_id' => $payment->id,
947 + 'result' => 'success',
948 + 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
949 + 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null,
1072 950 ];
1073 951 } catch (HttpException $e) {
1074 - PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
952 + self::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1075 953 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
1076 954 } catch (\Exception $e) {
1077 - PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
955 + self::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
1078 956 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
1079 957 }
1080 958 }
1081 959
@@ -1084,18 +962,18 @@
1084 962 * @param int $amount
1085 963 * @param int $customer_id
1086 964 * @param string $token_id
1087 965 *
966 + * @throws \Exception
967 + *
1088 968 * @return array
1089 - * @throws \Exception
1090 969 */
1091 970 public function process_payment_with_token($order, $amount, $customer_id, $token_id)
1092 971 {
1093 -
1094 - $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
972 + $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
1095 973 $payment_token = WC_Payment_Tokens::get($token_id);
1096 974 if (!$payment_token || (int) $customer_id !== (int) $payment_token->get_user_id()) {
1097 - PayplugGateway::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error');
975 + self::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error');
1098 976 throw new \Exception(__('Invalid payment method.', 'payplug'));
1099 977 }
1100 978
1101 979 try {
@@ -1100,55 +978,79 @@
1100 978
1101 979 try {
1102 980 $address_data = PayplugAddressData::from_order($order);
1103 981
1104 - $return_url = esc_url_raw($order->get_checkout_order_received_url());
982 + $return_url = esc_url_raw($order->get_checkout_order_received_url());
1105 983
1106 - if (!(substr( $return_url, 0, 4 ) === "http")) {
1107 - $return_url = get_site_url().$return_url;
1108 - }
984 + if (!(substr($return_url, 0, 4) === 'http')) {
985 + $return_url = get_site_url() . $return_url;
986 + }
1109 987
1110 988 $payment_data = [
1111 - 'amount' => $amount,
1112 - 'currency' => get_woocommerce_currency(),
1113 - 'payment_method' => $payment_token->get_token(),
1114 - 'allow_save_card' => false,
1115 - 'billing' => $address_data->get_billing(),
1116 - 'shipping' => $address_data->get_shipping(),
1117 - 'initiator' => 'PAYER',
1118 - 'hosted_payment' => [
989 + 'amount' => $amount,
990 + 'currency' => get_woocommerce_currency(),
991 + 'payment_method' => $payment_token->get_token(),
992 + 'allow_save_card' => false,
993 + 'billing' => $address_data->get_billing(),
994 + 'shipping' => $address_data->get_shipping(),
995 + 'initiator' => 'PAYER',
996 + 'hosted_payment' => [
1119 997 'return_url' => $return_url,
1120 998 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
1121 999 ],
1122 1000 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
1123 - 'metadata' => [
1124 - 'order_id' => $order_id,
1001 + 'metadata' => [
1002 + 'order_id' => $order_id,
1125 1003 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
1126 - 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
1004 + 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
1005 + 'woocommerce_block' => \WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout'),
1127 1006 ],
1128 1007 ];
1129 1008
1009 + $is_subscription = PayplugWoocommerceHelper::is_subscription();
1010 + if (!empty($is_subscription) && $is_subscription === true) {
1011 + $payment_data['metadata']['subscription'] = 'subscription';
1012 + }
1013 +
1130 1014 /** This filter is documented in src/Gateway/PayplugGateway */
1131 1015 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
1132 - $payment = $this->api->payment_create($payment_data);
1016 + $payment = $this->payplug_api->payment_create($payment_data);
1133 1017
1018 + // Save transaction id for the order
1019 + PayplugWoocommerceHelper::is_pre_30()
1020 + ? update_post_meta($order_id, '_transaction_id', $payment->id)
1021 + : $order->set_transaction_id($payment->id);
1022 +
1023 + if (is_callable([$order, 'save'])) {
1024 + $order->save();
1025 + }
1026 +
1134 1027 /** This action is documented in src/Gateway/PayplugGateway */
1135 1028 \do_action('payplug_gateway_payment_created', $order_id, $payment);
1136 1029
1030 + $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
1031 + PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
1032 +
1137 1033 $this->response->process_payment($payment, true);
1034 + if (($payment->__get('is_paid'))) {
1035 + $redirect = $order->get_checkout_order_received_url();
1036 + } elseif (isset($payment->__get('hosted_payment')->payment_url)) {
1037 + $redirect = $payment->__get('hosted_payment')->payment_url;
1038 + } else {
1039 + $redirect = $return_url;
1040 + }
1138 1041
1139 - PayplugGateway::log(sprintf('Payment process complete for order #%s', $order_id));
1140 -
1141 1042 return [
1142 - 'result' => 'success',
1143 - 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
1144 - 'redirect' => ($payment->__get('is_paid')) ? $order->get_checkout_order_received_url() : $payment->__get('hosted_payment')->payment_url
1043 + 'payment_id' => $payment->id,
1044 + 'result' => 'success',
1045 + 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
1046 + 'redirect' => $redirect,
1145 1047 ];
1146 1048 } catch (HttpException $e) {
1147 - PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1049 + self::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1148 1050 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
1149 1051 } catch (\Exception $e) {
1150 - PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
1052 + self::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
1151 1053 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
1152 1054 }
1153 1055 }
1154 1056
@@ -1162,24 +1064,25 @@
1162 1064 * @return bool|\WP_Error
1163 1065 */
1164 1066 public function process_refund($order_id, $amount = null, $reason = '')
1165 1067 {
1166 - PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
1068 + self::log(sprintf('Processing refund for order #%s', $order_id));
1167 1069
1168 - if( !$this->user_logged_in()){
1169 - PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
1170 - return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
1171 - }
1070 + if (!$this->user_logged_in()) {
1071 + self::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
1172 1072
1073 + return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
1074 + }
1075 +
1173 1076 $order = wc_get_order($order_id);
1174 1077 if (!$order instanceof \WC_Order) {
1175 - PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
1078 + self::log(sprintf('The order #%s does not exist.', $order_id), 'error');
1176 1079
1177 1080 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
1178 1081 }
1179 1082
1180 - if ($order->get_status() === "cancelled") {
1181 - PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
1083 + if ($order->get_status() === 'cancelled') {
1084 + self::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
1182 1085
1183 1086 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
1184 1087 }
1185 1088
@@ -1184,9 +1087,9 @@
1184 1087 }
1185 1088
1186 1089 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
1187 1090 if (empty($transaction_id)) {
1188 - PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
1091 + self::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
1189 1092
1190 1093 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
1191 1094 }
1192 1095
@@ -1193,12 +1096,12 @@
1193 1096 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
1194 1097
1195 1098 $data = [
1196 1099 'metadata' => [
1197 - 'order_id' => $order_id,
1100 + 'order_id' => $order_id,
1198 1101 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
1199 1102 'refund_from' => 'woocommerce',
1200 - ]
1103 + ],
1201 1104 ];
1202 1105
1203 1106 if (!is_null($amount)) {
1204 1107 $data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount);
@@ -1217,9 +1120,9 @@
1217 1120 */
1218 1121 $data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id);
1219 1122
1220 1123 try {
1221 - $refund = $this->api->refund_create($transaction_id, $data);
1124 + $refund = $this->payplug_api->refund_create($transaction_id, $data);
1222 1125
1223 1126 /**
1224 1127 * Fires once a refund has been created.
1225 1128 *
@@ -1243,23 +1146,23 @@
1243 1146 }
1244 1147 $order->add_order_note($note);
1245 1148
1246 1149 try {
1247 - $payment = $this->api->payment_retrieve($transaction_id);
1150 + $payment = $this->payplug_api->payment_retrieve($transaction_id);
1248 1151 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
1249 1152 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
1250 1153 } catch (\Exception $e) {
1251 1154 }
1252 1155
1253 - PayplugGateway::log('Refund process complete for the order.');
1156 + self::log('Refund process complete for the order.');
1254 1157
1255 1158 return true;
1256 1159 } catch (HttpException $e) {
1257 - PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1160 + self::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
1258 1161
1259 1162 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1260 1163 } catch (\Exception $e) {
1261 - PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
1164 + self::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
1262 1165
1263 1166 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
1264 1167 }
1265 1168 }
@@ -1314,15 +1217,15 @@
1314 1217 }
1315 1218
1316 1219 try {
1317 1220 $response = Authentication::getKeysByLogin($email, $password);
1318 - if (empty($response) || !isset($response['httpResponse']) && "payplug" === $this->id) {
1221 + if (empty($response) || !isset($response['httpResponse']) && 'payplug' === $this->id) {
1319 1222 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1320 1223 }
1321 1224
1322 1225 return $response['httpResponse']['secret_keys'];
1323 1226 } catch (HttpException $e) {
1324 - if("payplug" === $this->id) {
1227 + if ('payplug' === $this->id) {
1325 1228 return new \WP_Error('invalid_credentials', __('Invalid credentials.', 'payplug'));
1326 1229 }
1327 1230 }
1328 1231 }
@@ -1338,24 +1241,20 @@
1338 1241 * @return string
1339 1242 */
1340 1243 public function retrieve_merchant_id($key = null)
1341 1244 {
1245 + $merchant_id = '';
1342 1246 try {
1343 - $response = !is_null($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1247 + $response = !is_null($key) && !empty($key) ? Authentication::getAccount(new Payplug($key)) : Authentication::getAccount();
1344 1248 PayplugWoocommerceHelper::set_transient_data($response);
1345 1249 $merchant_id = isset($response['httpResponse']['id']) ? $response['httpResponse']['id'] : '';
1346 1250 } catch (ConfigurationException $e) {
1347 - PayplugGateway::log(sprintf('Missing API key for PayPlug client : %s', wc_print_r($e->getMessage(), true)), 'error');
1348 -
1349 - $merchant_id = '';
1251 + self::log(sprintf('Missing API key for PayPlug client : %s', wc_print_r($e->getMessage(), true)), 'error');
1350 1252 } catch (HttpException $e) {
1351 - PayplugGateway::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error');
1352 -
1353 - $merchant_id = '';
1253 + self::log(sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getErrorObject(), true)), 'error');
1254 + PayplugWoocommerceHelper::exception_handler_400_logout($e->getCode(), '', sprintf('Account request error from PayPlug API : %s', wc_print_r($e->getMessage(), true)));
1354 1255 } catch (\Exception $e) {
1355 - PayplugGateway::log(sprintf('Account request error : %s', wc_clean($e->getMessage())), 'error');
1356 -
1357 - $merchant_id = '';
1256 + self::log(sprintf('Account request error : %s', wc_clean($e->getMessage())), 'error');
1358 1257 }
1359 1258
1360 1259 return $merchant_id;
1361 1260 }
@@ -1360,422 +1259,8 @@
1360 1259 return $merchant_id;
1361 1260 }
1362 1261
1363 1262 /**
1364 - * Generate Hidden HTML.
1365 - *
1366 - * @param string $key
1367 - * @param array $data
1368 - *
1369 - * @return string
1370 - */
1371 - public function generate_hidden_html($key, $data)
1372 - {
1373 - $field_key = $this->get_field_key($key);
1374 - $defaults = array(
1375 - 'title' => '',
1376 - 'disabled' => false,
1377 - 'class' => '',
1378 - 'css' => '',
1379 - 'placeholder' => '',
1380 - 'type' => 'text',
1381 - 'desc_tip' => false,
1382 - 'description' => '',
1383 - 'custom_attributes' => array(),
1384 - );
1385 -
1386 - $data = wp_parse_args($data, $defaults);
1387 -
1388 - ob_start();
1389 - ?>
1390 - <input type="<?php echo esc_attr($data['type']); ?>" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>" value="<?php echo esc_attr($this->get_option($key)); ?>" />
1391 - <?php
1392 -
1393 - return ob_get_clean();
1394 - }
1395 -
1396 - /**
1397 - * Generate Yes/No Input HTML.
1398 - *
1399 - * @param string $key
1400 - * @param array $data
1401 - *
1402 - * @return string
1403 - */
1404 - public function generate_yes_no_html($key, $data)
1405 - {
1406 - $field_key = $this->get_field_key($key);
1407 - $defaults = array(
1408 - 'title' => '',
1409 - 'no' => 'No',
1410 - 'yes' => 'Yes',
1411 - 'disabled' => false,
1412 - 'class' => '',
1413 - 'css' => '',
1414 - 'placeholder' => '',
1415 - 'type' => 'text',
1416 - 'desc_tip' => false,
1417 - 'description' => '',
1418 - 'custom_attributes' => [],
1419 - 'hide_label' => false,
1420 - );
1421 -
1422 - $data = wp_parse_args($data, $defaults);
1423 - $checked = 'yes' === $this->get_option($key) ? '1' : '0';
1424 -
1425 - ob_start();
1426 - ?>
1427 - <tr valign="top">
1428 - <?php if (!$data['hide_label']) : ?>
1429 - <th scope="row" class="titledesc">
1430 - <label for="<?php echo esc_attr($field_key); ?>">
1431 - <?php echo wp_kses_post($data['title']); ?>
1432 - <?php echo $this->get_tooltip_html($data); ?>
1433 - </label>
1434 - </th>
1435 - <?php endif; ?>
1436 - <td class="forminp">
1437 - <fieldset>
1438 - <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1439 - </legend>
1440 - <div class="radio--custom">
1441 - <input class="radio radio-yes <?php echo esc_attr($data['class']); ?>" type="radio" name="<?php echo esc_attr($field_key); ?>" id="<?php echo esc_attr($field_key); ?>-yes" value="1" <?php checked('1', $checked); ?> <?php disabled($data['disabled'], true); ?> <?php echo $this->get_custom_attribute_html($data); ?>>
1442 - <label for="<?php echo esc_attr($field_key); ?>-yes"><?php echo esc_html($data['yes']); ?></label>
1443 - </div>
1444 - <div class="radio--custom">
1445 - <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); ?>>
1446 - <label for="<?php echo esc_attr($field_key); ?>-no"><?php echo esc_html($data['no']); ?></label>
1447 - </div>
1448 - <div id="live-mode-test-p"><?php echo $this->get_description_html($data); ?></div>
1449 - </fieldset>
1450 - </td>
1451 - </tr>
1452 - <?php
1453 -
1454 - return ob_get_clean();
1455 - }
1456 -
1457 - /**
1458 - * Generate Radio Input HTML.
1459 - *
1460 - * @param string $key
1461 - * @param array $data
1462 - *
1463 - * @return string
1464 - */
1465 - public function generate_radio_html($key, $data)
1466 - {
1467 - $field_key = $this->get_field_key($key);
1468 - $defaults = array(
1469 - 'title' => '',
1470 - 'disabled' => false,
1471 - 'class' => '',
1472 - 'css' => '',
1473 - 'placeholder' => '',
1474 - 'type' => 'text',
1475 - 'desc_tip' => false,
1476 - 'description' => '',
1477 - 'custom_attributes' => [],
1478 - 'options' => [],
1479 - );
1480 -
1481 - $data = wp_parse_args($data, $defaults);
1482 -
1483 - ob_start();
1484 - ?>
1485 - <tr valign="top">
1486 - <th scope="row" class="titledesc">
1487 - <label for="<?php echo esc_attr($field_key); ?>">
1488 - <?php echo wp_kses_post($data['title']); ?>
1489 - <?php echo $this->get_tooltip_html($data); ?>
1490 - </label>
1491 - </th>
1492 - <td class="forminp">
1493 - <fieldset>
1494 - <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1495 - </legend>
1496 - <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1497 - <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); ?>>
1498 - <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value); ?></label>
1499 - <?php endforeach; ?>
1500 - <?php echo $this->get_description_html($data); ?>
1501 - </fieldset>
1502 - </td>
1503 - </tr>
1504 - <?php
1505 -
1506 - return ob_get_clean();
1507 - }
1508 -
1509 - /**
1510 - * Generate Login HTML.
1511 - *
1512 - * @param string $key
1513 - * @param array $data
1514 - *
1515 - * @return string
1516 - */
1517 - public function generate_login_html($key, $data)
1518 - {
1519 - $field_key = $this->get_field_key($key);
1520 - $defaults = [];
1521 -
1522 - $data = wp_parse_args($data, $defaults);
1523 -
1524 - ob_start();
1525 - ?>
1526 - <tr valign="top">
1527 - <td class="forminp">
1528 - <p><?php echo $this->get_option('email'); ?></p>
1529 - <p>
1530 - <input id="payplug-logout" type="submit" name="submit_logout" value="<?php _e('Logout', 'payplug'); ?>">
1531 - <input type="hidden" name="save" value="logout">
1532 - <?php wp_nonce_field('payplug_user_logout', '_logoutaction'); ?>
1533 - |
1534 - <a href="https://portal.payplug.com" target="_blank"><?php _e('Go to your PayPlug Portal', 'payplug'); ?></a>
1535 - </p>
1536 - </td>
1537 - </tr>
1538 -<?php
1539 -
1540 - return ob_get_clean();
1541 - }
1542 -
1543 -
1544 - /**
1545 - * Generate Oney popup option HTML.
1546 - *
1547 - * @param string $key
1548 - * @param array $data
1549 - *
1550 - * @return string
1551 - */
1552 - public function generate_oney_product_animation_html($key, $data)
1553 - {
1554 - $field_key = $this->get_field_key($key);
1555 -
1556 - $defaults = array(
1557 - 'title' => '',
1558 - 'disabled' => false,
1559 - 'class' => '',
1560 - 'css' => '',
1561 - 'placeholder' => '',
1562 - 'type' => 'checkbox',
1563 - 'desc_tip' => false,
1564 - 'description' => '',
1565 - 'custom_attributes' => [],
1566 - 'options' => [],
1567 - );
1568 -
1569 - $data = wp_parse_args($data, $defaults);
1570 -
1571 - ob_start();
1572 - ?>
1573 - <tr valign="top" id="oney_installments_pop_up">
1574 - <th scope="row" class="titledesc">
1575 - <label for="<?php echo esc_attr($field_key); ?>">
1576 - <?php echo wp_kses_post($data['title']); ?>
1577 - <?php echo $this->get_tooltip_html($data); ?>
1578 - </label>
1579 - </th>
1580 - <td class="forminp">
1581 - <fieldset>
1582 - <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span></legend>
1583 - <label for="woocommerce_payplug_oney_product_animation">
1584 - <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>
1585 - <p class="description"> <?php echo wp_kses_post($data['description']); ?></p>
1586 - </fieldset>
1587 - </td>
1588 - </tr>
1589 - <?php
1590 -
1591 - return ob_get_clean();
1592 - }
1593 -
1594 - /**
1595 - * Generate Oney Type Input HTML.
1596 - *
1597 - * @param string $key
1598 - * @param array $data
1599 - *
1600 - * @return string
1601 - */
1602 - public function generate_oney_type_html($key, $data)
1603 - {
1604 - $field_key = $this->get_field_key($key);
1605 - $defaults = array(
1606 - 'title' => '',
1607 - 'disabled' => false,
1608 - 'class' => '',
1609 - 'css' => '',
1610 - 'placeholder' => '',
1611 - 'type' => 'text',
1612 - 'desc_tip' => false,
1613 - 'description' => '',
1614 - 'custom_attributes' => [],
1615 - 'options' => [],
1616 - );
1617 -
1618 - $data = wp_parse_args($data, $defaults);
1619 -
1620 - ob_start();
1621 - ?>
1622 - <tr valign="top" id="woocommerce_payplug_oney_type">
1623 - <th scope="row" class="titledesc" style="padding-top: 0px;">
1624 - <label for="<?php echo esc_attr($field_key); ?>">
1625 - <?php echo wp_kses_post($data['title']); ?>
1626 - <?php echo $this->get_tooltip_html($data); ?>
1627 - </label>
1628 - </th>
1629 - <td class="forminp" style="padding-top: 0px;">
1630 - <fieldset>
1631 - <legend class="screen-reader-text"><span><?php echo wp_kses_post($data['title']); ?></span>
1632 - </legend>
1633 - <?php foreach ($data['options'] as $option_key => $option_value) : ?>
1634 - <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); ?>>
1635 - <label for="<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($option_key); ?>" style="margin-right: 20px !important;">
1636 - <span style="font-weight: 500;"><?php echo esc_html($option_value); ?></span>
1637 - <span style="color:#646970;"> : <?php echo $data['descriptions'][$option_key] ;?></span>
1638 - </label>
1639 - <?php endforeach; ?>
1640 - <?php echo $this->get_description_html($data); ?>
1641 - </fieldset>
1642 - </td>
1643 - </tr>
1644 - <?php
1645 -
1646 - return ob_get_clean();
1647 - }
1648 -
1649 - /**
1650 - * Generate Oney Thresholds Input HTML.
1651 - *
1652 - * @param string $key
1653 - * @param array $data
1654 - *
1655 - * @return string
1656 - */
1657 - public function generate_oney_thresholds_html($key, $data)
1658 - {
1659 - $field_key = $this->get_field_key($key);
1660 - $defaults = array(
1661 - 'title' => '',
1662 - 'disabled' => false,
1663 - 'class' => '',
1664 - 'css' => '',
1665 - 'placeholder' => '',
1666 - 'type' => 'text',
1667 - 'desc_tip' => false,
1668 - 'description' => '',
1669 - 'custom_attributes' => [],
1670 - 'options' => [],
1671 - );
1672 -
1673 - $data = wp_parse_args($data, $defaults);
1674 -
1675 - ob_start();
1676 - ?>
1677 - <tr valign="top" id="woocommerce_payplug_oney_thresholds">
1678 - <th scope="row" class="titledesc" style="padding-top: 0px;">
1679 - <label for="<?php echo esc_attr($field_key); ?>">
1680 - <?php echo wp_kses_post($data['title']); ?>
1681 - <?php echo $this->get_tooltip_html($data); ?>
1682 - </label>
1683 - </th>
1684 - <td class="forminp" style="padding-top: 0px;">
1685 - <fieldset>
1686 - <div id="oney_thresholds_description"><?php echo $this->get_description_html($data); ?></div>
1687 - <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">
1688 - <b class="d-inline-block">€</b>
1689 - <div class="d-inline-block" id="slider-range"></div>
1690 - <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">
1691 - <b class="d-inline-block">€</b>
1692 - </fieldset>
1693 - </td>
1694 - </tr>
1695 - <?php
1696 -
1697 - return ob_get_clean();
1698 - }
1699 -
1700 - /**
1701 - * Validate Radio Field.
1702 - *
1703 - * Make sure the data is escaped correctly, etc.
1704 - *
1705 - * @param string $key
1706 - * @param string|null $value Posted Value
1707 - *
1708 - * @return string
1709 - */
1710 - public function validate_radio_field($key, $value)
1711 - {
1712 - $value = is_null($value) ? '' : $value;
1713 -
1714 - return wc_clean(stripslashes($value));
1715 - }
1716 -
1717 - /**
1718 - * Validate Yes/No Field.
1719 - *
1720 - * @param string $key
1721 - * @param string $value Posted Value
1722 - *
1723 - * @return string
1724 - */
1725 - public function validate_yes_no_field($key, $value)
1726 - {
1727 - return ('1' === (string) $value) ? 'yes' : 'no';
1728 - }
1729 -
1730 - /**
1731 - * Validate Yes/No Field.
1732 - *
1733 - * @param string $key
1734 - * @param string $value Posted Value
1735 - *
1736 - * @return string
1737 - */
1738 - public function validate_oney_product_animation_field($key, $value)
1739 - {
1740 - return ('on' === (string) $value) ? 'yes' : 'no';
1741 - }
1742 -
1743 - /**
1744 - * Validate Oney Type Field.
1745 - *
1746 - * Make sure the data is escaped correctly, etc.
1747 - *
1748 - * @param string $key
1749 - * @param string|null $value Posted Value
1750 - *
1751 - * @return string
1752 - */
1753 - public function validate_oney_type_field($key, $value)
1754 - {
1755 - $value = is_null($value) ? 'with_fees' : $value;
1756 -
1757 - return wc_clean(stripslashes($value));
1758 - }
1759 -
1760 - /**
1761 - * Validate Oney Thresholds Field.
1762 - *
1763 - * Make sure the data is escaped correctly, etc.
1764 - *
1765 - * @param string $key
1766 - * @param string|null $value Posted Value
1767 - *
1768 - * @return string
1769 - */
1770 - public function validate_oney_thresholds_field($key, $value)
1771 - {
1772 - $value = is_null($value) ? [100, 3000] : $value;
1773 -
1774 - return wc_clean($value);
1775 - }
1776 -
1777 - /**
1778 1263 * Get PayPlug gateway mode.
1779 1264 *
1780 1265 * @return string
1781 1266 */
@@ -1780,9 +1265,9 @@
1780 1265 * @return string
1781 1266 */
1782 1267 public function get_current_mode()
1783 1268 {
1784 - return ('yes' === $this->get_option('mode')) ? 'live' : 'test';
1269 + return $this->get_configuration()->get_option('mode');
1785 1270 }
1786 1271
1787 1272 /**
1788 1273 * Get user API key.
@@ -1792,40 +1277,12 @@
1792 1277 * @return string
1793 1278 */
1794 1279 public function get_api_key($mode = 'test')
1795 1280 {
1796 -
1797 - switch ($mode) {
1798 - case 'test':
1799 - $key = $this->get_option('payplug_test_key');
1800 - break;
1801 - case 'live':
1802 - $key = $this->get_option('payplug_live_key');
1803 - break;
1804 - default:
1805 - $key = '';
1806 - break;
1807 - }
1808 -
1809 - return $key;
1281 + return $this->get_api()->get_bearer_token($mode);
1810 1282 }
1811 1283
1812 1284 /**
1813 - * Check if an api key exist for a mode.
1814 - *
1815 - * @param string $mode
1816 - *
1817 - * @return bool
1818 - */
1819 - public function has_api_key($mode = 'test')
1820 - {
1821 - $key = $this->get_api_key($mode);
1822 - $key = trim($key);
1823 -
1824 - return !empty($key);
1825 - }
1826 -
1827 - /**
1828 1285 * Get current merchant id.
1829 1286 *
1830 1287 * @return string
1831 1288 */
@@ -1830,9 +1287,9 @@
1830 1287 * @return string
1831 1288 */
1832 1289 public function get_merchant_id()
1833 1290 {
1834 - return $this->get_option('payplug_merchant_id', '');
1291 + return $this->get_configuration()->get_option('company_id');
1835 1292 }
1836 1293
1837 1294 /**
1838 1295 * Check if user is logged in and we have an API key for TEST mode.
@@ -1840,9 +1297,21 @@
1840 1297 * @return bool
1841 1298 */
1842 1299 public function user_logged_in()
1843 1300 {
1844 - return !empty($this->get_option('payplug_test_key'));
1301 + $options = $this->get_configuration()->get_options();
1302 + if (empty($options) || !isset($options['api_key']) || !isset($options['jwt'])) {
1303 + return false;
1304 + }
1305 +
1306 + $jwt = json_decode($options['jwt'], true);
1307 + if (!empty($jwt) && isset($jwt['test']) && isset($jwt['test']['access_token'])) {
1308 + return true;
1309 + }
1310 +
1311 + $api_key = json_decode($options['api_key'], true);
1312 +
1313 + return !empty($api_key) && isset($api_key['test']) && !empty($api_key['test']);
1845 1314 }
1846 1315
1847 1316 /**
1848 1317 * Check if oneclick payment is activated and merchant can use it.
@@ -1848,12 +1317,12 @@
1848 1317 * Check if oneclick payment is activated and merchant can use it.
1849 1318 *
1850 1319 * @return bool
1851 1320 */
1852 - public function oneclick_available()
1321 + public function save_card_available()
1853 1322 {
1854 - return $this->user_logged_in()
1855 - && $this->oneclick
1323 + return 'payplug' == $this->id && $this->user_logged_in()
1324 + && $this->save_card
1856 1325 && $this->permissions->has_permissions(PayplugPermissions::SAVE_CARD);
1857 1326 }
1858 1327
1859 1328 /**
@@ -1859,25 +1328,31 @@
1859 1328 /**
1860 1329 * Check if the gatteway is allowed for the order amount
1861 1330 *
1862 1331 * @param array
1332 + *
1863 1333 * @return array
1864 1334 */
1865 1335 public function check_gateway($gateways)
1866 1336 {
1867 - if ( !empty( WC()->cart ) && isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1337 + if (!empty(WC()->cart) && isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) {
1868 1338 $order_amount = $this->get_order_total();
1869 - if ($order_amount < self::MIN_AMOUNT || $order_amount > self::MAX_AMOUNT) {
1870 - unset($gateways[$this->id]);
1339 + foreach ($gateways[$this->id]->settings['payment_methods']['permissions'] as $key => &$permission) {
1340 + $method_amounts = json_decode($permission['amounts'], true);
1341 + if ($order_amount < $method_amounts['min']['EUR'] / 100 || $order_amount > $method_amounts['max']['EUR'] / 100) {
1342 + unset($gateways[$key]);
1343 + }
1871 1344 }
1872 1345 }
1873 - if($this->oney_type == 'with_fees'){
1874 - unset($gateways['oney_x3_without_fees']);
1875 - unset($gateways['oney_x4_without_fees']);
1876 - } else{
1877 - unset($gateways['oney_x3_with_fees']);
1878 - unset($gateways['oney_x4_with_fees']);
1879 - }
1346 +
1347 + if ((bool) $this->get_configuration()->get_option('payment_methods.configuration.oney.with_fees')) {
1348 + unset($gateways['oney_x3_without_fees']);
1349 + unset($gateways['oney_x4_without_fees']);
1350 + } else {
1351 + unset($gateways['oney_x3_with_fees']);
1352 + unset($gateways['oney_x4_with_fees']);
1353 + }
1354 +
1880 1355 return $gateways;
1881 1356 }
1882 1357
1883 1358 /**
@@ -1883,21 +1358,15 @@
1883 1358 /**
1884 1359 * Can the order be refunded via this gateway?
1885 1360 *
1886 1361 *
1887 - * @param WC_Order $order Order object.
1362 + * @param WC_Order $order Order object.
1363 + *
1888 1364 * @return bool If false, the automatic refund button is hidden in the UI.
1889 1365 */
1890 1366 public function can_refund_order($order)
1891 1367 {
1892 1368 $status = $order->get_status();
1893 - return $order && $this->supports('refunds') && $status !== "cancelled" && $status !== "failed";
1369 +
1370 + return $order && $this->supports('refunds') && $status !== 'cancelled' && $status !== 'failed';
1894 1371 }
1895 -
1896 - public function getPayplugMerchantCountry(){
1897 - return $this->payplug_merchant_country;
1898 - }
1899 -
1900 - public function setPayplugMerchantCountry($country){
1901 - $this->payplug_merchant_country = $country;
1902 - }
1903 1372 }