PluginProbe
HyperPay Payments / 5.3.0
HyperPay Payments v5.3.0
6.6.0 trunk 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.1.0 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.5 4.0.0 4.0.2 All 34 releases
hyperpay-gateways / src / App / Hyperpay_Blocks_Support.php

Hyperpay_Blocks_Support.php in HyperPay Payments 5.3.0, at src/App/Hyperpay_Blocks_Support.php

156 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hyperpay\Gateways\App;
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
7 use Hyperpay\Gateways\Main;
8 use WC_Order;
9
10 /**
11 * Dummy Payments Blocks integration
12 *
13 * @since 1.0.3
14 */
15 class Hyperpay_Blocks_Support extends AbstractPaymentMethodType
16 {
17 /**
18 * Payment method name/id/slug.
19 *
20 * @var string
21 */
22
23 public $gateways, $id = "hyperpay_blocks";
24
25 protected $name = 'hyperpay_blocks';
26
27
28
29
30 /**
31 * Initializes the payment method type.
32 */
33 public function initialize()
34 {
35 $this->gateways = $this->get_gateways();
36
37 foreach ($this->gateways as $gateway) {
38 $gateway->initialize();
39 }
40
41 add_action('wc_ajax_hyperpay_can_make_payment', [$this, 'canMakePayment']);
42 }
43
44
45 public function get_gateways()
46 {
47 $gateways = WC()->payment_gateways->payment_gateways();
48 $enabled_gateways = [];
49
50 foreach ($gateways as $gateway) {
51 if (str_starts_with($gateway->id, "hyperpay") && $gateway->enabled == "yes") {
52 $enabled_gateways[$gateway->id] = $gateway;
53 }
54 }
55
56 return $enabled_gateways;
57 }
58
59 public function canMakePayment()
60 {
61
62 if (!isset($_GET["payment_method"]) || !\array_key_exists($_GET["payment_method"], $this->gateways)) {
63 return wp_send_json(["canMakePayment" => false]);
64 }
65
66 $payment_method = sanitize_text_field($_GET["payment_method"]);
67 return $this->gateways[$payment_method]->canMakePayment();
68 }
69 /**
70 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
71 *
72 * @return boolean
73 */
74 public function is_active()
75 {
76 return true;
77 }
78
79
80 public function process_payment($order_id)
81 {
82
83 $order = new WC_Order($order_id);
84
85
86 return [
87 'result' => 'success',
88 'redirect' => $order->get_checkout_payment_url(true)
89 ];
90 }
91
92 /**
93 * Returns an array of scripts/handles to be registered for this payment method.
94 *
95 * @return array
96 */
97 public function get_payment_method_script_handles()
98 {
99 $script_path = '/src/assets/js/blocks/blocks.js';
100 $script_url = HYPERPAY_PLUGIN_DIR . $script_path;
101 $script_asset_path = HYPERPAY_PLUGIN_DIR . '/src/assets/js/blocks/blocks.asset.php';
102
103 $script_asset = file_exists($script_asset_path)
104 ? require($script_asset_path)
105 : array(
106 'dependencies' => array(),
107 'version' => '2.1.2'
108 );
109
110 wp_register_script(
111 $this->id,
112 $script_url,
113 $script_asset['dependencies'],
114 $script_asset['version'],
115 true
116 );
117
118
119 return [$this->id];
120 }
121
122
123 /**
124 * Returns an array of key=>value pairs of data made available to the payment methods script.
125 *
126 * @return array
127 */
128 public function get_payment_method_data()
129 {
130 $data = [];
131
132 foreach ($this->gateways as $gateway) {
133 $gateWayData = [
134 'name' => $gateway->id,
135 'title' => $gateway->title,
136 'supports' => array_filter($gateway->supports, [$gateway, 'supports']),
137 'icon' => $gateway->iconSrc(),
138 'description' => $gateway->description,
139 'gatewayMerchantId' => $gateway->entityId,
140 'currencyCode' => $gateway->currency,
141 'brands' => implode(' ', $gateway->brands),
142 'nonce' => $gateway->NONCE,
143 'isDynamicCheck' => $gateway->isDynamicCheck,
144 'isExpress' => $gateway->isExpress,
145 'action_button' => $gateway->action_button,
146 'extraScriptData' => $gateway->extraScriptData(),
147 'site_url' => site_url(),
148 ];
149
150 $key = $gateway->isExpress ? "express" : "blocks";
151 $data[$key][] = $gateWayData;
152 }
153 return $data;
154 }
155 }
156