PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.1
1.7.1 1.7.0 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 All 51 releases
fluent-cart / app / Hooks / Handlers / GlobalPaymentHandler.php

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

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