| 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 ('charges' === $api && 'POST' === $method) { |
| 127 |
$customer = !empty($request['customer']) ? $request['customer'] : ''; |
| 128 |
$source = !empty($request['source']) ? $request['source'] : $customer; |
| 129 |
$key = ArrayHelper::get($request, 'metadata.fluentform_tid') . '-' . $source . '-' . $api; |
| 130 |
$key = apply_filters_deprecated( |
| 131 |
'fluentform_stripe_idempotency_key', |
| 132 |
[ |
| 133 |
$key, |
| 134 |
$request |
| 135 |
], |
| 136 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 137 |
'fluentform/stripe_idempotency_key', |
| 138 |
'Use fluentform/stripe_idempotency_key instead of fluentform_stripe_idempotency_key.' |
| 139 |
); |
| 140 |
$idempotency_key = apply_filters('fluentform/stripe_idempotency_key', $key, $request); |
| 141 |
$headers['Idempotency-Key'] = $idempotency_key; |
| 142 |
} |
| 143 |
|
| 144 |
$request = apply_filters_deprecated( |
| 145 |
'fluentform_stripe_request_body', |
| 146 |
[ |
| 147 |
$request, |
| 148 |
$api |
| 149 |
], |
| 150 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 151 |
'fluentform/stripe_request_body', |
| 152 |
'Use fluentform/stripe_request_body instead of fluentform_stripe_request_body.' |
| 153 |
); |
| 154 |
|
| 155 |
$response = wp_safe_remote_post( |
| 156 |
self::$ENDPOINT . $api, |
| 157 |
array( |
| 158 |
'method' => $method, |
| 159 |
'headers' => $headers, |
| 160 |
'body' => apply_filters('fluentform/stripe_request_body', $request, $api), |
| 161 |
'timeout' => 50, |
| 162 |
) |
| 163 |
); |
| 164 |
if (is_wp_error($response) || empty($response['body'])) { |
| 165 |
return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluentform')); |
| 166 |
} |
| 167 |
|
| 168 |
$body = json_decode(wp_remote_retrieve_body($response)); |
| 169 |
// check if it's a stripe error or not |
| 170 |
$responseCode = wp_remote_retrieve_response_code($response); |
| 171 |
|
| 172 |
if($responseCode > 299) { |
| 173 |
$code = __('general_error', 'fluentform'); |
| 174 |
if(!empty($body->error->code)) { |
| 175 |
$code = $body->error->code; |
| 176 |
} |
| 177 |
$message = __('Stripe General Error', 'fluentform'); |
| 178 |
if(!empty($body->error->message)) { |
| 179 |
$message = $body->error->message; |
| 180 |
} |
| 181 |
|
| 182 |
return new \WP_Error($code, $message, $body); |
| 183 |
} |
| 184 |
|
| 185 |
return $body; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Retrieve API endpoint. |
| 190 |
* |
| 191 |
* @param string $api |
| 192 |
* @return mixed|\WP_Error|null |
| 193 |
* @since 4.0.0 |
| 194 |
* @version 4.0.0 |
| 195 |
*/ |
| 196 |
public static function retrieve($api) |
| 197 |
{ |
| 198 |
$response = wp_safe_remote_get( |
| 199 |
self::$ENDPOINT . $api, |
| 200 |
array( |
| 201 |
'method' => 'GET', |
| 202 |
'headers' => self::get_headers(), |
| 203 |
'timeout' => 70, |
| 204 |
) |
| 205 |
); |
| 206 |
if (is_wp_error($response) || empty($response['body'])) { |
| 207 |
return new \WP_Error('stripe_error', __('There was a problem connecting to the Stripe API endpoint.', 'fluentform')); |
| 208 |
} |
| 209 |
return json_decode($response['body']); |
| 210 |
} |
| 211 |
} |
| 212 |
|