| 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', $idempotencyKey = null) |
| 31 |
{ |
| 32 |
$apiKey = (new StripeSettingsBase())->getApiKey($mode); |
| 33 |
return $this->remoteRequest($path, $data, $apiKey, 'POST', $idempotencyKey); |
| 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, $idempotencyKey = null) |
| 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 |
// Stripe dedupes any POST carrying the same Idempotency-Key (valid 24h), |
| 53 |
// so a duplicate/retried create request returns the original object instead |
| 54 |
// of charging the customer or creating a second subscription again. |
| 55 |
if ($idempotencyKey && $method === 'POST') { |
| 56 |
$sessionHeaders['Idempotency-Key'] = $idempotencyKey; |
| 57 |
} |
| 58 |
|
| 59 |
$url = $this->apiUrl . $path; |
| 60 |
|
| 61 |
if ($method === 'GET' && is_array($data) && !empty($data)) { |
| 62 |
$url .= '?' . http_build_query($data); |
| 63 |
$requestData = array( |
| 64 |
'headers' => $sessionHeaders, |
| 65 |
'method' => $method, |
| 66 |
); |
| 67 |
} else { |
| 68 |
$requestData = array( |
| 69 |
'headers' => $sessionHeaders, |
| 70 |
'body' => is_array($data) ? http_build_query($data) : $data, |
| 71 |
'method' => $method, |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$sessionResponse = wp_remote_request($url, $requestData); |
| 76 |
|
| 77 |
if (is_wp_error($sessionResponse)) { |
| 78 |
return $sessionResponse; |
| 79 |
} |
| 80 |
|
| 81 |
$sessionResponseData = wp_remote_retrieve_body($sessionResponse); |
| 82 |
$responseBodyArray = json_decode($sessionResponseData, true); |
| 83 |
|
| 84 |
$statusCode = wp_remote_retrieve_response_code($sessionResponse); |
| 85 |
|
| 86 |
if ($statusCode >= 300) { |
| 87 |
|
| 88 |
$message = Arr::get($responseBodyArray, 'detail'); |
| 89 |
if (!$message) { |
| 90 |
$message = Arr::get($responseBodyArray, 'error.message'); |
| 91 |
} |
| 92 |
if (!$message) { |
| 93 |
$message = __('Unknown Stripe API request error', 'fluent-cart'); |
| 94 |
} |
| 95 |
|
| 96 |
return new \WP_Error('api_error', $message, $responseBodyArray); |
| 97 |
} |
| 98 |
|
| 99 |
return $responseBodyArray; |
| 100 |
} |
| 101 |
|
| 102 |
public function verifyIPN() |
| 103 |
{ |
| 104 |
$rawPayload = @file_get_contents('php://input'); |
| 105 |
|
| 106 |
$data = $rawPayload ? json_decode($rawPayload) : null; |
| 107 |
|
| 108 |
if (empty($data) && !empty($_POST)) { |
| 109 |
$postArray = stripslashes_deep($_POST); |
| 110 |
$data = json_decode(wp_json_encode($postArray)); |
| 111 |
} |
| 112 |
|
| 113 |
if (!$data || empty($data->id)) { |
| 114 |
return new \WP_Error('invalid_data', __('Invalid or empty payload received from Stripe', 'fluent-cart')); |
| 115 |
} |
| 116 |
|
| 117 |
return $data; |
| 118 |
} |
| 119 |
|
| 120 |
public function getEvent($eventId) |
| 121 |
{ |
| 122 |
$api = $this->getApi(); |
| 123 |
return $api::request([], 'events/' . $eventId, 'GET'); |
| 124 |
} |
| 125 |
|
| 126 |
public function getApi() |
| 127 |
{ |
| 128 |
$api = new ApiRequest(); |
| 129 |
$api::set_secret_key((new StripeSettingsBase())->getApiKey()); |
| 130 |
return $api; |
| 131 |
} |
| 132 |
|
| 133 |
public function getApiKey($mode = 'current') |
| 134 |
{ |
| 135 |
return (new StripeSettingsBase())->getApiKey($mode); |
| 136 |
} |
| 137 |
|
| 138 |
public function addWebhookEndpoint() |
| 139 |
{ |
| 140 |
// get all the webhook endpoints first |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
public function getWebhookEndpoints() |
| 145 |
{ |
| 146 |
return $this->getStripeObject('webhooks'); |
| 147 |
} |
| 148 |
|
| 149 |
public function getActivatedPaymentMethodsConfigs($mode = 'live') |
| 150 |
{ |
| 151 |
$apiKey = (new StripeSettingsBase())->getApiKey($mode); |
| 152 |
|
| 153 |
if (!$apiKey) { |
| 154 |
return []; |
| 155 |
} |
| 156 |
|
| 157 |
$clientId = 'ca_TDs9okG0Jy8gY5GWbwmsDWHmIpOlyIoc'; |
| 158 |
if ($mode == 'test') { |
| 159 |
$clientId = 'ca_TDs9NGCHtEcklwK4EFKHe72TAxC2kQap'; |
| 160 |
} |
| 161 |
|
| 162 |
$stripeGateways = (new \FluentCart\App\Modules\PaymentMethods\StripeGateway\API\API)->makeRequest('payment_method_configurations', [ |
| 163 |
'application' => $clientId, |
| 164 |
], $apiKey); |
| 165 |
|
| 166 |
if (is_wp_error($stripeGateways) || empty($stripeGateways['data'])) { |
| 167 |
return []; |
| 168 |
} |
| 169 |
|
| 170 |
$gateway = Arr::first($stripeGateways['data']); |
| 171 |
|
| 172 |
$stripeGateways = [ |
| 173 |
'acss_debit' => 'ACSS Debit', |
| 174 |
'affirm' => 'Affirm', |
| 175 |
'afterpay_clearpay' => 'Afterpay / Clearpay', |
| 176 |
'alipay' => 'Alipay', |
| 177 |
'amazon_pay' => 'Amazon Pay', |
| 178 |
'apple_pay' => 'Apple Pay', |
| 179 |
'bacs_debit' => 'BACS Debit', |
| 180 |
'bancontact' => 'Bancontact', |
| 181 |
'blik' => 'BLIK', |
| 182 |
'card' => 'Credit/Debit Card', |
| 183 |
'cartes_bancaires' => 'Cartes Bancaires', |
| 184 |
'cashapp' => 'Cash App', |
| 185 |
'crypto' => 'Cryptocurrency', |
| 186 |
'eps' => 'EPS', |
| 187 |
'giropay' => 'Giropay', |
| 188 |
'google_pay' => 'Google Pay', |
| 189 |
'ideal' => 'iDEAL', |
| 190 |
'kakao_pay' => 'Kakao Pay', |
| 191 |
'klarna' => 'Klarna', |
| 192 |
'kr_card' => 'Korea Card', |
| 193 |
'link' => 'Link', |
| 194 |
'mb_way' => 'MB Way', |
| 195 |
'multibanco' => 'Multibanco', |
| 196 |
'naver_pay' => 'Naver Pay', |
| 197 |
'p24' => 'Przelewy24', |
| 198 |
'payco' => 'Payco', |
| 199 |
'pix' => 'Pix', |
| 200 |
'samsung_pay' => 'Samsung Pay', |
| 201 |
'sepa_debit' => 'SEPA Debit', |
| 202 |
'sofort' => 'Sofort', |
| 203 |
'us_bank_account' => 'US Bank Account', |
| 204 |
'wechat_pay' => 'WeChat Pay', |
| 205 |
'zip' => 'Zip' |
| 206 |
]; |
| 207 |
|
| 208 |
$allMethods = Arr::only($gateway, array_keys($stripeGateways)); |
| 209 |
|
| 210 |
$activatedMethods = array_filter($allMethods, function ($method) { |
| 211 |
return !!Arr::get($method, 'available'); |
| 212 |
}); |
| 213 |
|
| 214 |
$stripeGateways = Arr::only($stripeGateways, array_keys($activatedMethods)); |
| 215 |
$settings = (new StripeSettingsBase())->settings; |
| 216 |
$accountId = Arr::get($settings, $mode . '_account_id'); |
| 217 |
|
| 218 |
$liveMode = !!Arr::get($gateway, 'livemode'); |
| 219 |
|
| 220 |
return [ |
| 221 |
'id' => Arr::get($gateway, 'id'), |
| 222 |
'activated_methods' => $stripeGateways, |
| 223 |
'configure_url' => 'https://dashboard.stripe.com/' . $accountId . (!$liveMode ? '/test/' : '/') . 'settings/payment_methods/' . Arr::get($gateway, 'id'), |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|