PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
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 / Modules / PaymentMethods / StripeGateway / API / ApiRequest.php

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

184 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\Modules\PaymentMethods\StripeGateway\API;
4
5 use FluentCart\Framework\Support\Arr;
6
7 /**
8 * WC_Stripe_API class.
9 *
10 * Communicates with Stripe API.
11 */
12 class ApiRequest
13 {
14 /**
15 * Stripe API Endpoint
16 */
17 private static $ENDPOINT = 'https://api.stripe.com/v1/';
18 const STRIPE_API_VERSION = '2025-02-24.acacia';
19 /**
20 * Secret API Key.
21 * @var string
22 */
23 private static $secret_key = '';
24
25 /**
26 * Set secret API Key.
27 * @param string $secret_key
28 */
29 public static function set_secret_key($secret_key)
30 {
31 static::$secret_key = $secret_key;
32 }
33
34 /**
35 * Set secret API Key.
36 * @param string $secret_key
37 */
38 public static function set_end_point($endpont)
39 {
40 static::$ENDPOINT = $endpont;
41 }
42
43 /**
44 * Get secret key.
45 * @return string
46 */
47 public static function get_secret_key()
48 {
49 return static::$secret_key;
50 }
51
52 /**
53 * Generates the user agent we use to pass to API request so
54 * Stripe can identify our application.
55 *
56 * @since 4.0.0
57 * @version 4.0.0
58 */
59 public static function get_user_agent()
60 {
61 $app_info = array(
62 'name' => 'FluentCart',
63 'version' => FLUENTCART_VERSION,
64 'url' => site_url(),
65 'partner_id' => ''
66 // 'partner_id' => 'pp_partner_FN62GfRLM2Kx5d'
67 );
68 return array(
69 'lang' => 'php',
70 'lang_version' => phpversion(),
71 'publisher' => 'fluentcart',
72 'uname' => function_exists('php_uname') ? php_uname() : PHP_OS,
73 'application' => $app_info,
74 );
75 }
76
77 /**
78 * Generates the headers to pass to API request.
79 *
80 * @since 4.0.0
81 * @version 4.0.0
82 */
83 public static function get_headers()
84 {
85 $user_agent = self::get_user_agent();
86 $app_info = $user_agent['application'];
87 return apply_filters(
88 'fluent_cart_stripe_request_headers',
89 array(
90 'Authorization' => 'Basic ' . base64_encode(self::get_secret_key() . ':'),
91 'Stripe-Version' => self::STRIPE_API_VERSION,
92 'User-Agent' => $app_info['name'] . '/' . $app_info['version'] . ' (' . $app_info['url'] . ')',
93 'X-Stripe-Client-User-Agent' => json_encode($user_agent),
94 ),
95 []
96 );
97 }
98
99 /**
100 * Send the request to Stripe's API
101 *
102 * @param array $request
103 * @param string $api
104 * @param string $method
105 * @return object|\WP_Error
106 * @since 3.1.0
107 * @version 4.0.6
108 */
109 public static function request($request, $api = 'charges', $method = 'POST')
110 {
111 $headers = self::get_headers();
112 if ('charges' === $api && 'POST' === $method) {
113 $customer = !empty($request['customer']) ? $request['customer'] : '';
114 $source = !empty($request['source']) ? $request['source'] : $customer;
115 $idempotency_key = apply_filters_deprecated('fluent_cart_stripe_idempotency_key', [
116 Arr::get($request, 'metadata.fluentcart_tid') . '-' . $source . '-' . $api,
117 ['request' => $request]
118 ], '1.3.16', 'fluent_cart/stripe_idempotency_key', 'Use fluent_cart/stripe_idempotency_key instead of fluent_cart_stripe_idempotency_key.');
119 $idempotency_key = apply_filters('fluent_cart/stripe_idempotency_key', $idempotency_key, ['request' => $request]);
120 $headers['Idempotency-Key'] = $idempotency_key;
121 }
122
123 $response = wp_safe_remote_post(
124 self::$ENDPOINT . $api,
125 array(
126 'method' => $method,
127 'headers' => $headers,
128 'body' => (function() use ($request, $api) {
129 $body = apply_filters_deprecated('fluent_cart_stripe_request_body', [$request, ['api' => $api]], '1.3.16', 'fluent_cart/stripe_request_body', 'Use fluent_cart/stripe_request_body instead of fluent_cart_stripe_request_body.');
130 return apply_filters('fluent_cart/stripe_request_body', $body, ['api' => $api]);
131 })(),
132 'timeout' => 50,
133 )
134 );
135
136 if (is_wp_error($response) || empty($response['body'])) {
137 return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluent-cart'));
138 }
139
140 $body = json_decode(wp_remote_retrieve_body($response));
141 // check if it's a stripe error or not
142 $responseCode = wp_remote_retrieve_response_code($response);
143
144 if ($responseCode > 299) {
145 $code = 'general_error';
146 if (!empty($body->error->code)) {
147 $code = $body->error->code;
148 }
149 $message = 'Stripe General Error';
150 if (!empty($body->error->message)) {
151 $message = $body->error->message;
152 }
153
154 return new \WP_Error($code, $message, $body);
155 }
156
157 return $body;
158 }
159
160 /**
161 * Retrieve API endpoint.
162 *
163 * @param string $api
164 * @return mixed|\WP_Error|null
165 * @since 4.0.0
166 * @version 4.0.0
167 */
168 public static function retrieve($api)
169 {
170 $response = wp_safe_remote_get(
171 self::$ENDPOINT . $api,
172 array(
173 'method' => 'GET',
174 'headers' => self::get_headers(),
175 'timeout' => 70,
176 )
177 );
178 if (is_wp_error($response) || empty($response['body'])) {
179 return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluent-cart'));
180 }
181 return json_decode($response['body']);
182 }
183 }
184