PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
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.6.5, at app/Hooks/Handlers/GlobalPaymentHandler.php

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