PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Hooks / Handlers / GlobalPaymentHandler.php

GlobalPaymentHandler.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.23, at app/Hooks/Handlers/GlobalPaymentHandler.php

166 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Hooks\Handlers;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Modules\PaymentMethods\AirwallexGateway\Airwallex;
7 use FluentCart\App\Modules\PaymentMethods\Cod\Cod;
8 use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager;
9 use FluentCart\App\Modules\PaymentMethods\PayPalGateway\PayPal;
10 use FluentCart\App\Modules\PaymentMethods\SquareGateway\Square;
11 use FluentCart\App\Modules\PaymentMethods\StripeGateway\Stripe;
12 use FluentCart\App\Modules\PaymentMethods\StripeGateway\Connect\ConnectConfig;
13 use FluentCart\Framework\Support\Arr;
14 use FluentCart\Api\PaymentMethods;
15 use FluentCart\Framework\Support\Collection;
16
17 class GlobalPaymentHandler
18 {
19 public function register()
20 {
21 $this->init();
22 }
23
24 public function init()
25 {
26 add_action('init', function () {
27 $gateway = GatewayManager::getInstance();
28 $gateway->register('stripe', new Stripe());
29 $gateway->register('paypal', new PayPal());
30 $gateway->register('offline_payment', new Cod());
31 $gateway->register('square', new Square());
32 $gateway->register('airwallex', new Airwallex());
33
34 $this->verifyStripeConnect();
35
36 $this->appAuthenticator();
37 //This hook will allow others to register their payment method with ours
38 do_action('fluent_cart/register_payment_methods', [
39 'gatewayManager' => $gateway
40 ]);
41 });
42
43 add_action('fluent_cart_action_fct_payment_listener_ipn', function () {
44 $this->initIpnListener();
45 });
46 }
47
48 // IPN / Payment Webhook Listener
49 public function initIpnListener(): void
50 {
51 $paymentMethod = App::request()->getSafe('method', 'sanitize_text_field');
52 $gateway = GatewayManager::getInstance($paymentMethod);
53 if (is_object($gateway) && method_exists($gateway, 'handleIPN')) {
54 try {
55 $gateway->handleIPN();
56 } catch (\Throwable $e) {
57 fluent_cart_error_log('IPN Handler Error: ' . $paymentMethod,
58 $e->getMessage() . '. Debug Trace: ' . $e->getTraceAsString()
59 );
60 wp_send_json([
61 'message' => sprintf(
62 /* translators: %s is the payment method name */
63 __('IPN processing failed. - %s', 'fluent-cart'),
64 $paymentMethod
65 )
66 ], 500);
67 }
68 }
69 }
70
71 public function appAuthenticator()
72 {
73 $request = App::request()->all();
74 if (isset($request['fct_app_authenticator'])) {
75 $paymentMethod = sanitize_text_field($request['method']);
76
77 if (GatewayManager::has($paymentMethod)) {
78 $methodInstance = GatewayManager::getInstance($paymentMethod);
79 if (method_exists($methodInstance, 'appAuthenticator')) {
80 $methodInstance->appAuthenticator($request);
81 }
82 }
83 }
84 }
85
86 public function verifyStripeConnect()
87 {
88 $request = App::request()->all();
89 if (isset($request['vendor_source']) && $request['vendor_source'] == 'fluent_cart') {
90 if (isset($request['ff_stripe_connect']) && current_user_can('manage_options')) {
91 $data = Arr::only($request, ['ff_stripe_connect', 'mode', 'state', 'code']);
92 ConnectConfig::verifyAuthorizeSuccess($data);
93 }
94
95 wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/stripe'));
96 }
97 }
98
99 public function disconnect($method, $mode)
100 {
101 if (GatewayManager::has($method)) {
102 $methodInstance = GatewayManager::getInstance($method);
103 if (method_exists($methodInstance, 'getConnectInfo')) {
104 wp_send_json(
105 $methodInstance->disconnect($mode),
106 200
107 );
108 }
109 }
110 }
111
112 public function getSettings($method): array
113 {
114 if (GatewayManager::has($method)) {
115 $methodInstance = GatewayManager::getInstance($method);
116 $filtered = Collection::make($methodInstance->fields())->filter(function ($item) {
117 return Arr::get($item, 'visible', 'yes') === 'yes';
118 })->toArray();
119
120 $result = [
121 'fields' => $filtered,
122 'settings' => $methodInstance->settings->get()
123 ];
124
125 // Add addon metadata if it's an addon
126 $meta = $methodInstance->meta();
127
128 if (isset($result['settings']) && empty(Arr::get($result, 'settings.checkout_label')) && Arr::has($meta, 'title')) {
129 Arr::set($result, 'settings.checkout_label', $meta['title']);
130 }
131
132 if (Arr::get($meta, 'is_addon') === true) {
133 $result['addon_info'] = [
134 'is_addon' => true,
135 'addon_source' => $meta['addon_source'] ?? [],
136 ];
137 }
138
139 return $result;
140 } else {
141 throw new \Exception(esc_html__('No valid payment method found!', 'fluent-cart'));
142 }
143 }
144
145 /**
146 * @throws \Exception
147 */
148 public function getAll(): array
149 {
150 $gateways = (new PaymentMethods())->getAll();
151
152 // Sort by saved order if available
153 $savedOrder = get_option('fluent_cart_payment_methods_order', []);
154 if (!empty($savedOrder) && is_array($savedOrder)) {
155 $orderMap = array_flip($savedOrder);
156 usort($gateways, function($a, $b) use ($orderMap) {
157 $aOrder = $orderMap[$a['route']] ?? PHP_INT_MAX;
158 $bOrder = $orderMap[$b['route']] ?? PHP_INT_MAX;
159 return $aOrder <=> $bOrder;
160 });
161 }
162
163 return $gateways;
164 }
165 }
166