PluginProbe
PayPlug for WooCommerce (Official) / 3.0.0
PayPlug for WooCommerce (Official) v3.0.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / Blocks / PayplugGenericBlock.php

PayplugGenericBlock.php in PayPlug for WooCommerce (Official) 3.0.0, at src/Gateway/Blocks/PayplugGenericBlock.php

124 lines 5.1 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\Gateway\Blocks;
4
5 use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
6 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
7 use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
8
9 class PayplugGenericBlock extends AbstractPaymentMethodType
10 {
11 use ServiceGetter;
12
13 protected $gateway;
14
15 protected $allowed_country_codes;
16
17 public function initialize(): void
18 {
19 if (class_exists('WC_Blocks_Utils')) {
20 if (\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout') ||
21 \WC_Blocks_Utils::has_block_in_page(wc_get_page_id('cart'), 'woocommerce/cart')
22 ) {
23 $gateways = WC()->payment_gateways->payment_gateways();
24 $this->gateway = $gateways[$this->name];
25 }
26 }
27 }
28
29 public function is_active()
30 {
31 if (class_exists('WC_Blocks_Utils')) {
32 if (\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout')) {
33 if (empty($this->gateway)) {
34 return false;
35 }
36
37 $active = false;
38 if (method_exists($this->gateway, 'is_available')) {
39 $active = $this->gateway->is_available();
40 }
41
42 if (method_exists($this->gateway, 'checkGateway')) {
43 $active = $this->gateway->checkGateway() && $active;
44 }
45
46 return $active && $this->gateway->check_gateway(WC()->payment_gateways->payment_gateways());
47 }
48 }
49 }
50
51 /**
52 * Returns an array of scripts/handles to be registered for this payment method.
53 *
54 * @return array
55 */
56 public function get_payment_method_script_handles()
57 {
58 $script_path = '/assets/js/blocks/wc-payplug-' . $this->get_name() . '-blocks.js';
59 $script_asset_path = PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/blocks/frontend/wc-payplug-' . $this->get_name() . '-blocks.asset.php';
60 $script_asset = file_exists($script_asset_path) ? require($script_asset_path) : ['dependencies' => [], 'version' => '1.0.0'];
61 $script_url = PAYPLUG_GATEWAY_PLUGIN_URL . $script_path;
62
63 wp_register_script('wc-payplug-' . $this->get_name() . '-blocks', $script_url, $script_asset['dependencies'], $script_asset['version'], true);
64
65 return ['wc-payplug-' . $this->get_name() . '-blocks'];
66 }
67
68 /**
69 * Returns an array of supported features.
70 *
71 * @return string[]
72 */
73 public function get_supported_features()
74 {
75 $features = $this->gateway->supports;
76
77 // Only include tokenization in supported features when order amount is between min and max allowed values
78 $cart_total = WC()->cart ? WC()->cart->get_total('') : 0;
79 $cart_total_cents = (int) PayplugWoocommerceHelper::get_payplug_amount($cart_total);
80 $min_amount = PayplugWoocommerceHelper::get_minimum_amount();
81 $max_amount = PayplugWoocommerceHelper::get_maximum_amount();
82
83 if (!($cart_total_cents >= $min_amount && $cart_total_cents <= $max_amount)) {
84 // Remove tokenization from supported features if outside allowed amount range
85 $features = array_diff($features, ['tokenization']);
86 }
87
88 // array_diff() preserves original keys, leaving gaps; a PHP array with non-sequential
89 // keys is JSON-encoded as an object, but WC Blocks' registerPaymentMethod() requires
90 // supports.features to be a plain array (or undefined), or it throws and the whole
91 // checkout payment-methods list fails to render.
92 return array_values($features);
93 }
94
95 /**
96 * Returns an associative array of data to be exposed for the payment method's client side.
97 */
98 public function get_payment_method_data()
99 {
100 $account = PayplugWoocommerceHelper::generic_get_account_data_from_options($this->name);
101 $this->allowed_country_codes = !empty($account['payment_methods'][$this->name]['allowed_countries']) ? $account['payment_methods'][$this->name]['allowed_countries'] : null;
102
103 // Only show saved cards when order amount is between min and max allowed values
104 $cart_total = WC()->cart ? WC()->cart->get_total('') : 0;
105 $cart_total_cents = (int) PayplugWoocommerceHelper::get_payplug_amount($cart_total);
106 $min_amount = PayplugWoocommerceHelper::get_minimum_amount();
107 $max_amount = PayplugWoocommerceHelper::get_maximum_amount();
108 $configuration = $this->get_service('configuration');
109 $save_card = $configuration->get_option('payment_methods.configuration.payplug.save_card')
110 && $cart_total_cents >= $min_amount
111 && $cart_total_cents <= $max_amount;
112
113 return [
114 'enabled' => $this->is_active(),
115 'name' => $this->gateway->id,
116 'title' => $this->gateway->title,
117 'description' => $this->gateway->description,
118 'allowed_country_codes' => $this->allowed_country_codes,
119 'save_card' => $save_card,
120 'wp_nonce' => wp_create_nonce('woocommerce-process_checkout'),
121 ];
122 }
123 }
124