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.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 / ApiRequest.php

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

181 lines 5.3 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 = Arr::get($request, 'metadata.fluentcart_tid') . '-' . $source . '-' . $api;
116 $idempotency_key = apply_filters('fluent_cart/stripe_idempotency_key', $idempotency_key, ['request' => $request]);
117 $headers['Idempotency-Key'] = $idempotency_key;
118 }
119
120 $response = wp_safe_remote_post(
121 self::$ENDPOINT . $api,
122 array(
123 'method' => $method,
124 'headers' => $headers,
125 'body' => (function() use ($request, $api) {
126 $body = $request;
127 return apply_filters('fluent_cart/stripe_request_body', $body, ['api' => $api]);
128 })(),
129 'timeout' => 50,
130 )
131 );
132
133 if (is_wp_error($response) || empty($response['body'])) {
134 return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluent-cart'));
135 }
136
137 $body = json_decode(wp_remote_retrieve_body($response));
138 // check if it's a stripe error or not
139 $responseCode = wp_remote_retrieve_response_code($response);
140
141 if ($responseCode > 299) {
142 $code = 'general_error';
143 if (!empty($body->error->code)) {
144 $code = $body->error->code;
145 }
146 $message = 'Stripe General Error';
147 if (!empty($body->error->message)) {
148 $message = $body->error->message;
149 }
150
151 return new \WP_Error($code, $message, $body);
152 }
153
154 return $body;
155 }
156
157 /**
158 * Retrieve API endpoint.
159 *
160 * @param string $api
161 * @return mixed|\WP_Error|null
162 * @since 4.0.0
163 * @version 4.0.0
164 */
165 public static function retrieve($api)
166 {
167 $response = wp_safe_remote_get(
168 self::$ENDPOINT . $api,
169 array(
170 'method' => 'GET',
171 'headers' => self::get_headers(),
172 'timeout' => 70,
173 )
174 );
175 if (is_wp_error($response) || empty($response['body'])) {
176 return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluent-cart'));
177 }
178 return json_decode($response['body']);
179 }
180 }
181