PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.15
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.15
6.2.15 6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 All 197 releases
fluentform / app / Modules / Payments / PaymentMethods / Stripe / API / ApiRequest.php

ApiRequest.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.15, at app/Modules/Payments/PaymentMethods/Stripe/API/ApiRequest.php

226 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API;
4
5 if (!defined('ABSPATH')) {
6 exit; // Exit if accessed directly.
7 }
8
9 use FluentForm\Framework\Helpers\ArrayHelper;
10
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 /**
16 * WC_Stripe_API class.
17 *
18 * Communicates with Stripe API.
19 */
20 class ApiRequest
21 {
22 /**
23 * Stripe API Endpoint
24 */
25 private static $ENDPOINT = 'https://api.stripe.com/v1/';
26 const STRIPE_API_VERSION = '2020-08-27';
27 /**
28 * Secret API Key.
29 * @var string
30 */
31 private static $secret_key = '';
32
33 /**
34 * Set secret API Key.
35 * @param string $secret_key
36 */
37 public static function set_secret_key($secret_key)
38 {
39 self::$secret_key = $secret_key;
40 }
41
42 /**
43 * Set secret API Key.
44 * @param string $secret_key
45 */
46 public static function set_end_point($endpont)
47 {
48 self::$ENDPOINT = $endpont;
49 }
50
51 /**
52 * Get secret key.
53 * @return string
54 */
55 public static function get_secret_key()
56 {
57 return self::$secret_key;
58 }
59
60 /**
61 * Generates the user agent we use to pass to API request so
62 * Stripe can identify our application.
63 *
64 * @since 4.0.0
65 * @version 4.0.0
66 */
67 public static function get_user_agent()
68 {
69 $app_info = array(
70 'name' => 'Fluent Forms',
71 'version' => FLUENTFORM_VERSION,
72 'url' => site_url(),
73 'partner_id' => 'pp_partner_FN62GfRLM2Kx5d'
74 );
75 return array(
76 'lang' => 'php',
77 'lang_version' => phpversion(),
78 'publisher' => 'wpmanageninja',
79 'uname' => function_exists('php_uname') ? php_uname() : PHP_OS,
80 'application' => $app_info,
81 );
82 }
83
84 /**
85 * Generates the headers to pass to API request.
86 *
87 * @since 4.0.0
88 * @version 4.0.0
89 */
90 public static function get_headers()
91 {
92 $user_agent = self::get_user_agent();
93 $app_info = $user_agent['application'];
94 $headers = [
95 'Authorization' => 'Basic ' . base64_encode(self::get_secret_key() . ':'),
96 'Stripe-Version' => self::STRIPE_API_VERSION,
97 'User-Agent' => $app_info['name'] . '/' . $app_info['version'] . ' (' . $app_info['url'] . ')',
98 'X-Stripe-Client-User-Agent' => json_encode($user_agent),
99 ];
100
101 $headers = apply_filters_deprecated(
102 'fluentform_stripe_request_headers',
103 [
104 $headers
105 ],
106 FLUENTFORM_FRAMEWORK_UPGRADE,
107 'fluentform/stripe_request_headers',
108 'Use fluentform/stripe_request_headers instead of fluentform_stripe_request_headers.'
109 );
110 return apply_filters('fluentform/stripe_request_headers', $headers);
111 }
112
113 /**
114 * Send the request to Stripe's API
115 *
116 * @param array $request
117 * @param string $api
118 * @param string $method
119 * @return object|\WP_Error
120 * @since 3.1.0
121 * @version 4.0.6
122 */
123 public static function request($request, $api = 'charges', $method = 'POST')
124 {
125 $headers = self::get_headers();
126 if ('POST' === $method && in_array($api, ['charges', 'payment_intents', 'checkout/sessions'], true)) {
127 $customer = !empty($request['customer']) ? $request['customer'] : '';
128 $source = !empty($request['source']) ? $request['source'] : $customer;
129 $transactionId = ArrayHelper::get($request, 'metadata.transaction_id');
130 if (!$transactionId) {
131 $transactionId = ArrayHelper::get($request, 'payment_intent_data.metadata.transaction_id');
132 }
133 $key = $transactionId
134 ? 'fluentform-' . $transactionId . '-' . $api
135 : ArrayHelper::get($request, 'metadata.fluentform_tid') . '-' . $source . '-' . $api;
136 $key = apply_filters_deprecated(
137 'fluentform_stripe_idempotency_key',
138 [
139 $key,
140 $request
141 ],
142 FLUENTFORM_FRAMEWORK_UPGRADE,
143 'fluentform/stripe_idempotency_key',
144 'Use fluentform/stripe_idempotency_key instead of fluentform_stripe_idempotency_key.'
145 );
146 $idempotency_key = apply_filters('fluentform/stripe_idempotency_key', $key, $request);
147 $headers['Idempotency-Key'] = $idempotency_key;
148 }
149
150 $request = apply_filters_deprecated(
151 'fluentform_stripe_request_body',
152 [
153 $request,
154 $api
155 ],
156 FLUENTFORM_FRAMEWORK_UPGRADE,
157 'fluentform/stripe_request_body',
158 'Use fluentform/stripe_request_body instead of fluentform_stripe_request_body.'
159 );
160
161 $requestArgs = [
162 'method' => $method,
163 'headers' => $headers,
164 'body' => apply_filters('fluentform/stripe_request_body', $request, $api),
165 'timeout' => 50,
166 ];
167 $response = wp_safe_remote_post(self::$ENDPOINT . $api, $requestArgs);
168
169 // Stripe replays the original result for this identical idempotent request.
170 if (
171 'POST' === $method &&
172 'payment_intents' === $api &&
173 (is_wp_error($response) || empty($response['body']))
174 ) {
175 $response = wp_safe_remote_post(self::$ENDPOINT . $api, $requestArgs);
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.', 'fluentform'));
180 }
181
182 $body = json_decode(wp_remote_retrieve_body($response));
183 // check if it's a stripe error or not
184 $responseCode = wp_remote_retrieve_response_code($response);
185
186 if($responseCode > 299) {
187 $code = __('general_error', 'fluentform');
188 if(!empty($body->error->code)) {
189 $code = $body->error->code;
190 }
191 $message = __('Stripe General Error', 'fluentform');
192 if(!empty($body->error->message)) {
193 $message = $body->error->message;
194 }
195
196 return new \WP_Error($code, $message, $body);
197 }
198
199 return $body;
200 }
201
202 /**
203 * Retrieve API endpoint.
204 *
205 * @param string $api
206 * @return mixed|\WP_Error|null
207 * @since 4.0.0
208 * @version 4.0.0
209 */
210 public static function retrieve($api)
211 {
212 $response = wp_safe_remote_get(
213 self::$ENDPOINT . $api,
214 array(
215 'method' => 'GET',
216 'headers' => self::get_headers(),
217 'timeout' => 70,
218 )
219 );
220 if (is_wp_error($response) || empty($response['body'])) {
221 return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluentform'));
222 }
223 return json_decode($response['body']);
224 }
225 }
226