PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 trunk All 48 releases
fluent-cart / app / Modules / PaymentMethods / StripeGateway / API / API.php

API.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Modules/PaymentMethods/StripeGateway/API/API.php

221 lines 7.1 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\Modules\PaymentMethods\StripeGateway\API;
4
5 use FluentCart\App\Modules\PaymentMethods\StripeGateway\StripeSettingsBase;
6 use FluentCart\Framework\Support\Arr;
7
8 class API
9 {
10 private $createSessionUrl;
11
12 private $apiUrl = 'https://api.stripe.com/v1/';
13
14 public function makeRequest($path, $data = [], $apiKey = null, $method = 'GET')
15 {
16 if (!$apiKey) {
17 $apiKey = $this->getApiKey();
18 }
19
20 return $this->remoteRequest($path, $data, $apiKey, $method);
21 }
22
23 public function getStripeObject($path, $data = [], $mode = 'current')
24 {
25 $apiKey = (new StripeSettingsBase())->getApiKey($mode);
26
27 return $this->remoteRequest($path, $data, $apiKey, 'GET');
28 }
29
30 public function createStripeObject($path, $data = [], $mode = 'current')
31 {
32 $apiKey = (new StripeSettingsBase())->getApiKey($mode);
33 return $this->remoteRequest($path, $data, $apiKey, 'POST');
34 }
35
36 public function deleteStripeObject($path, $data = [], $mode = 'current')
37 {
38 $apiKey = (new StripeSettingsBase())->getApiKey($mode);
39 return $this->remoteRequest($path, $data, $apiKey, 'DELETE');
40 }
41
42 public function remoteRequest($path, $data, $apiKey, $method)
43 {
44 $stripeApiKey = $apiKey;
45 $apiVersion = '2025-02-24.acacia';
46 $sessionHeaders = array(
47 'Authorization' => 'Bearer ' . $stripeApiKey,
48 'Content-Type' => 'application/x-www-form-urlencoded',
49 'Stripe-Version' => $apiVersion
50 );
51
52 $url = $this->apiUrl . $path;
53
54 if ($method === 'GET' && is_array($data) && !empty($data)) {
55 $url .= '?' . http_build_query($data);
56 $requestData = array(
57 'headers' => $sessionHeaders,
58 'method' => $method,
59 );
60 } else {
61 $requestData = array(
62 'headers' => $sessionHeaders,
63 'body' => is_array($data) ? http_build_query($data) : $data,
64 'method' => $method,
65 );
66 }
67
68 $sessionResponse = wp_remote_request($url, $requestData);
69
70 if (is_wp_error($sessionResponse)) {
71 return $sessionResponse;
72 }
73
74 $sessionResponseData = wp_remote_retrieve_body($sessionResponse);
75 $responseBodyArray = json_decode($sessionResponseData, true);
76
77 $statusCode = wp_remote_retrieve_response_code($sessionResponse);
78
79 if ($statusCode >= 300) {
80
81 $message = Arr::get($responseBodyArray, 'detail');
82 if (!$message) {
83 $message = Arr::get($responseBodyArray, 'error.message');
84 }
85 if (!$message) {
86 $message = __('Unknown Stripe API request error', 'fluent-cart');
87 }
88
89 return new \WP_Error('api_error', $message, $responseBodyArray);
90 }
91
92 return $responseBodyArray;
93 }
94
95 public function verifyIPN()
96 {
97 $rawPayload = @file_get_contents('php://input');
98
99 $data = $rawPayload ? json_decode($rawPayload) : null;
100
101 if (empty($data) && !empty($_POST)) {
102 $postArray = stripslashes_deep($_POST);
103 $data = json_decode(wp_json_encode($postArray));
104 }
105
106 if (!$data || empty($data->id)) {
107 return new \WP_Error('invalid_data', __('Invalid or empty payload received from Stripe', 'fluent-cart'));
108 }
109
110 return $data;
111 }
112
113 public function getEvent($eventId)
114 {
115 $api = $this->getApi();
116 return $api::request([], 'events/' . $eventId, 'GET');
117 }
118
119 public function getApi()
120 {
121 $api = new ApiRequest();
122 $api::set_secret_key((new StripeSettingsBase())->getApiKey());
123 return $api;
124 }
125
126 public function getApiKey($mode = 'current')
127 {
128 return (new StripeSettingsBase())->getApiKey($mode);
129 }
130
131 public function addWebhookEndpoint()
132 {
133 // get all the webhook endpoints first
134
135 }
136
137 public function getWebhookEndpoints()
138 {
139 return $this->getStripeObject('webhooks');
140 }
141
142 public function getActivatedPaymentMethodsConfigs($mode = 'live')
143 {
144 $apiKey = (new StripeSettingsBase())->getApiKey($mode);
145
146 if (!$apiKey) {
147 return [];
148 }
149
150 $clientId = 'ca_TDs9okG0Jy8gY5GWbwmsDWHmIpOlyIoc';
151 if ($mode == 'test') {
152 $clientId = 'ca_TDs9NGCHtEcklwK4EFKHe72TAxC2kQap';
153 }
154
155 $stripeGateways = (new \FluentCart\App\Modules\PaymentMethods\StripeGateway\API\API)->makeRequest('payment_method_configurations', [
156 'application' => $clientId,
157 ], $apiKey);
158
159 if (is_wp_error($stripeGateways) || empty($stripeGateways['data'])) {
160 return [];
161 }
162
163 $gateway = Arr::first($stripeGateways['data']);
164
165 $stripeGateways = [
166 'acss_debit' => 'ACSS Debit',
167 'affirm' => 'Affirm',
168 'afterpay_clearpay' => 'Afterpay / Clearpay',
169 'alipay' => 'Alipay',
170 'amazon_pay' => 'Amazon Pay',
171 'apple_pay' => 'Apple Pay',
172 'bacs_debit' => 'BACS Debit',
173 'bancontact' => 'Bancontact',
174 'blik' => 'BLIK',
175 'card' => 'Credit/Debit Card',
176 'cartes_bancaires' => 'Cartes Bancaires',
177 'cashapp' => 'Cash App',
178 'crypto' => 'Cryptocurrency',
179 'eps' => 'EPS',
180 'giropay' => 'Giropay',
181 'google_pay' => 'Google Pay',
182 'ideal' => 'iDEAL',
183 'kakao_pay' => 'Kakao Pay',
184 'klarna' => 'Klarna',
185 'kr_card' => 'Korea Card',
186 'link' => 'Link',
187 'mb_way' => 'MB Way',
188 'multibanco' => 'Multibanco',
189 'naver_pay' => 'Naver Pay',
190 'p24' => 'Przelewy24',
191 'payco' => 'Payco',
192 'pix' => 'Pix',
193 'samsung_pay' => 'Samsung Pay',
194 'sepa_debit' => 'SEPA Debit',
195 'sofort' => 'Sofort',
196 'us_bank_account' => 'US Bank Account',
197 'wechat_pay' => 'WeChat Pay',
198 'zip' => 'Zip'
199 ];
200
201 $allMethods = Arr::only($gateway, array_keys($stripeGateways));
202
203 $activatedMethods = array_filter($allMethods, function ($method) {
204 return !!Arr::get($method, 'available');
205 });
206
207 $stripeGateways = Arr::only($stripeGateways, array_keys($activatedMethods));
208 $settings = (new StripeSettingsBase())->settings;
209 $accountId = Arr::get($settings, $mode . '_account_id');
210
211 $liveMode = !!Arr::get($gateway, 'livemode');
212
213 return [
214 'id' => Arr::get($gateway, 'id'),
215 'activated_methods' => $stripeGateways,
216 'configure_url' => 'https://dashboard.stripe.com/' . $accountId . (!$liveMode ? '/test/' : '/') . 'settings/payment_methods/' . Arr::get($gateway, 'id'),
217 ];
218 }
219
220 }
221