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
payplug / src / Front / Oney / OneyDisplay.php

OneyDisplay.php in PayPlug for WooCommerce (Official) 3.1.0, at src/Front/Oney/OneyDisplay.php

133 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Front\Oney;
4
5 use Payplug\PayplugWoocommerce\Gateway\Oney\OneyAccountMapper;
6 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
7
8 class OneyDisplay
9 {
10 public function __construct()
11 {
12 // Registered on init rather than on wp_enqueue_scripts: on WooCommerce block-theme
13 // product templates, content hooks like woocommerce_before_add_to_cart_form can fire
14 // BEFORE wp_enqueue_scripts (block rendering doesn't follow the classic head-then-body
15 // template lifecycle). wp_localize_script() only needs the handle to be *registered*,
16 // not enqueued, so registering on init guarantees it's available by the time
17 // show_on_product()/show_on_cart() call wp_localize_script() on it, regardless of which
18 // hook happens to fire first on a given request.
19 //
20 // It must not happen any earlier: this class is built on plugins_loaded, and
21 // wp_register_script() before init trips WP's _doing_it_wrong() notice, which prints
22 // output before any header() call - breaking redirects and corrupting the JSON of AJAX
23 // endpoints such as WooCommerce's update_order_review.
24 if (did_action('init')) {
25 $this->register_assets();
26 } else {
27 add_action('init', [$this, 'register_assets']);
28 }
29
30 add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']);
31 add_action('woocommerce_cart_totals_after_order_total', [$this, 'show_on_cart']);
32
33 $options = PayplugWoocommerceHelper::get_payplug_options();
34 if (!empty($options['payment_methods']['configuration']['oney']['cta_product'])) {
35 add_action('woocommerce_before_add_to_cart_form', [$this, 'show_on_product']);
36 }
37 }
38
39 public function register_assets(): void
40 {
41 wp_register_script('payplug-oney-loader', PayplugWoocommerceHelper::get_oney_loader_url(), [], PAYPLUG_GATEWAY_VERSION, true);
42 wp_register_script('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-oney.js', [
43 'jquery',
44 'payplug-oney-loader',
45 ], PAYPLUG_GATEWAY_VERSION, true);
46 }
47
48 public function enqueue_assets(): void
49 {
50 wp_enqueue_style('payplug-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-oney.css', [], PAYPLUG_GATEWAY_VERSION);
51 wp_enqueue_script('payplug-oney');
52 }
53
54 public function show_on_product(): void
55 {
56 global $product;
57
58 if (!function_exists('is_product') || !is_product() || empty($product)) {
59 return;
60 }
61
62 if (in_array($product->get_type(), ['subscription', 'downloadable_subscription', 'virtual_subscription', 'variable-subscription'], true)) {
63 return;
64 }
65
66 $this->render_badge((float) $product->get_price(), 1);
67 }
68
69 public function show_on_cart(): void
70 {
71 if (!function_exists('is_cart') || !is_cart() || PayplugWoocommerceHelper::is_subscription()) {
72 return;
73 }
74
75 $total_products = 0;
76 foreach (WC()->cart->cart_contents as $item) {
77 $total_products += $item['quantity'];
78 }
79
80 $this->render_badge((float) WC()->cart->total, $total_products);
81 }
82
83 private function render_badge(float $price, int $total_products): void
84 {
85 if (!PayplugWoocommerceHelper::check_order_max_amount($price)) {
86 return;
87 }
88
89 $account = PayplugWoocommerceHelper::get_account_data_from_options();
90 $options = PayplugWoocommerceHelper::get_payplug_options();
91 $oney_settings = $options['payment_methods']['configuration']['oney'] ?? [];
92 $country = PayplugWoocommerceHelper::getISOCountryCode();
93
94 $mapper = OneyAccountMapper::map((array) $account, $oney_settings, $country);
95 if (!$mapper->is_enabled()) {
96 return;
97 }
98
99 $fee_mode = !empty($oney_settings['with_fees']) ? 'with_fees' : 'without_fees';
100 $amount_in_cents = (int) round($price * 100);
101 $eligible = $mapper->is_eligible($country, $amount_in_cents);
102
103 // Only country + amount-in-range decide eligibility (see is_eligible()), and country
104 // can't change client-side, so probing at the min bound tells us whether country alone
105 // already disqualifies this badge - if it does, JS must never re-enable it regardless of
106 // quantity; the min/max attributes are omitted so the client has nothing to recalculate.
107 $country_eligible = $mapper->is_eligible($country, $mapper->get_min_amount());
108
109 wp_localize_script('payplug-oney', 'payplug_oney_config', [
110 'merchant_guid' => $mapper->get_merchant_guid(),
111 'business_transaction_codes' => $mapper->business_transaction_codes($fee_mode),
112 'country' => $country,
113 'language' => strtoupper(substr(get_locale(), 0, 2)),
114 ]);
115
116 $logo_class = 'with_fees' === $fee_mode ? 'oney-3x4x' : 'oney-without-fees-3x4x';
117
118 $threshold_attrs = $country_eligible
119 ? sprintf(' data-min-oney="%s" data-max-oney="%s"', esc_attr($mapper->get_min_amount() / 100), esc_attr($mapper->get_max_amount() / 100))
120 : '';
121
122 printf(
123 '<div class="payplug-oney%1$s" data-price="%2$s" data-total-products="%3$d"%6$s>%4$s<div class="payplug-oney-popup"><div class="oney-img %5$s"></div><div id="oney-show-popup" class="bold oney-color">?</div></div></div>',
124 $eligible ? '' : ' disabled',
125 esc_attr($price),
126 (int) $total_products,
127 esc_html__('OR PAY IN', 'payplug'),
128 esc_attr($logo_class),
129 $threshold_attrs
130 );
131 }
132 }
133