PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.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) 2.18.0, at src/Gateway/Blocks/PayplugGenericBlock.php

119 lines 4.7 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()
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 return $features;
89 }
90
91 /**
92 * Returns an associative array of data to be exposed for the payment method's client side.
93 */
94 public function get_payment_method_data()
95 {
96 $account = PayplugWoocommerceHelper::generic_get_account_data_from_options($this->name);
97 $this->allowed_country_codes = !empty($account['payment_methods'][$this->name]['allowed_countries']) ? $account['payment_methods'][$this->name]['allowed_countries'] : null;
98
99 // Only show saved cards when order amount is between min and max allowed values
100 $cart_total = WC()->cart ? WC()->cart->get_total('') : 0;
101 $cart_total_cents = (int) PayplugWoocommerceHelper::get_payplug_amount($cart_total);
102 $min_amount = PayplugWoocommerceHelper::get_minimum_amount();
103 $max_amount = PayplugWoocommerceHelper::get_maximum_amount();
104 $configuration = $this->get_service('configuration');
105 $save_card = $configuration->get_option('payment_methods.configuration.payplug.save_card')
106 && $cart_total_cents >= $min_amount
107 && $cart_total_cents <= $max_amount;
108
109 return [
110 'enabled' => $this->is_active(),
111 'name' => $this->gateway->id,
112 'title' => $this->gateway->title,
113 'description' => $this->gateway->description,
114 'allowed_country_codes' => $this->allowed_country_codes,
115 'save_card' => $save_card,
116 ];
117 }
118 }
119