Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-stripe/payments/Api.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace Ollyo\PaymentHub\Payments\Stripe;
4 +
5 + use Stripe\Stripe;
6 + use Stripe\StripeClient;
7 + use Ollyo\PaymentHub\Core\Support\Uri;
8 + use Ollyo\PaymentHub\Contracts\Config\RepositoryContract;
9 +
10 + final class Api {
11 +
12 + /**
13 + * The Stripe Api Client Instance
14 + *
15 + * @var StripeClient
16 + * @since 1.0.4
17 + */
18 + protected static $client;
19 +
20 + /**
21 + * Class constructor.
22 + *
23 + * Initializes the Stripe client using the provided secret key.
24 + *
25 + * @param string $secret_key Stripe API secret key.
26 + *
27 + * @since 1.0.4
28 + */
29 + public function __construct( string $secret_key ) {
30 +
31 + self::$client = new StripeClient( $secret_key );
32 + }
33 +
34 + /**
35 + * Creates a Stripe coupon.
36 + *
37 + * @since 1.0.4
38 + *
39 + * @param int $amount Amount to discount (in minor units, e.g. cents).
40 + * @param string $currency Currency code (e.g. 'usd').
41 + *
42 + * @return \Stripe\Coupon
43 + * @throws \Throwable If the coupon creation fails.
44 + */
45 + public static function createCoupon( $amount, $currency ) {
46 + try {
47 + return self::$client->coupons->create(
48 + array(
49 + 'amount_off' => $amount,
50 + 'duration' => 'once',
51 + 'currency' => strtolower( $currency ),
52 + )
53 + );
54 + } catch ( \Throwable $error ) {
55 + throw $error;
56 + }
57 + }
58 +
59 + /**
60 + * Creates a Stripe Checkout session.
61 + *
62 + * @param array $data Checkout session data.
63 + *
64 + * @since 1.0.4
65 + *
66 + * @return \Stripe\Checkout\Session
67 + * @throws \Throwable If the session creation fails.
68 + */
69 + public static function createSession( $data ) {
70 + try {
71 + return self::$client->checkout->sessions->create( $data );
72 +
73 + } catch ( \Throwable $error ) {
74 + throw $error;
75 + }
76 + }
77 +
78 + /**
79 + * Retrieves all configured Stripe webhook endpoints.
80 + *
81 + * @since 1.0.4
82 + *
83 + * @return \Stripe\Collection List of webhook endpoints.
84 + * @throws \Throwable If retrieval fails.
85 + */
86 + public static function getAllWebhookEndPoints() {
87 + try {
88 + return self::$client->webhookEndpoints->all();
89 +
90 + } catch ( \Throwable $error ) {
91 + throw $error;
92 + }
93 + }
94 +
95 + /**
96 + * Create the webhook for the webhook_url endpoint.
97 + *
98 + * @return object
99 + * @throws \Throwable
100 + */
101 + public static function createNewWebhook( RepositoryContract $config ): object {
102 +
103 + $webhookUrl = Uri::getInstance( $config->get( 'webhook_url' ) );
104 + $webhookUrl->setVar( 'payment_method', $config->get( 'name' ) );
105 +
106 + try {
107 + $newWebhook = self::$client->webhookEndpoints->create(
108 + array(
109 + 'url' => $webhookUrl->__toString(),
110 + 'enabled_events' => array(
111 + 'payment_intent.payment_failed',
112 + 'payment_intent.canceled',
113 + 'charge.refund.updated',
114 + 'charge.refunded',
115 + 'charge.updated',
116 + ),
117 + )
118 + );
119 +
120 + return (object) array(
121 + 'webhook_id' => $newWebhook->id,
122 + 'webhook_url' => $newWebhook->url,
123 + 'webhook_secret' => $newWebhook->secret,
124 + );
125 + } catch ( \Throwable $error ) {
126 + throw $error;
127 + }
128 + }
129 +
130 + /**
131 + * Deletes a Stripe coupon.
132 + *
133 + * @since 1.0.4
134 + *
135 + * @param string $couponId The ID of the coupon to delete.
136 + *
137 + * @return \Stripe\Coupon
138 + * @throws \Throwable If the deletion fails.
139 + */
140 + public static function deleteCoupon( $couponId ) {
141 + try {
142 + return self::$client->coupons->delete( $couponId );
143 + } catch ( \Throwable $error ) {
144 + throw $error;
145 + }
146 + }
147 +
148 + /**
149 + * Creates a Stripe PaymentIntent.
150 + *
151 + * @param array $data PaymentIntent creation parameters.
152 + *
153 + * @return \Stripe\PaymentIntent
154 + * @throws \Throwable If creation fails.
155 + */
156 + public static function createPaymentIntent( $data ) {
157 + try {
158 + return self::$client->paymentIntents->create( $data );
159 + } catch ( \Throwable $error ) {
160 + throw $error;
161 + }
162 + }
163 +
164 + /**
165 + * Creates a refund for a Stripe payment.
166 + *
167 + * @param array $data Refund parameters.
168 + *
169 + * @return \Stripe\Refund
170 + * @throws \Throwable If the refund fails.
171 + */
172 + public static function createRefund( $data ) {
173 + try {
174 + return self::$client->refunds->create( $data );
175 + } catch ( \Throwable $error ) {
176 + throw $error;
177 + }
178 + }
179 +
180 + /**
181 + * Retrieves all tax registration records from Stripe.
182 + *
183 + * @return \Stripe\Collection
184 + * @throws \Throwable If retrieval fails.
185 + */
186 + public static function getAllTaxRegistrationList() {
187 + try {
188 + return self::$client->tax->registrations->all();
189 + } catch ( \Throwable $th ) {
190 + throw $th;
191 + }
192 + }
193 +
194 +
195 + /**
196 + * Retrieves Stripe Checkout sessions linked to a specific PaymentIntent.
197 + *
198 + * @param string $paymentIntentID The ID of the PaymentIntent.
199 + *
200 + * @return \Stripe\Collection
201 + * @throws \Throwable If session retrieval fails.
202 + */
203 + public static function getCheckoutSession( $paymentIntentID ) {
204 +
205 + try {
206 + return self::$client->checkout->sessions->all( array( 'payment_intent' => $paymentIntentID ) );
207 + } catch ( \Throwable $th ) {
208 + throw $th;
209 + }
210 + }
211 +
212 +
213 + /**
214 + * Retrieves a Stripe Payment Intent by its ID.
215 + *
216 + * @param string $paymentIntent The ID of the Payment Intent to retrieve.
217 + *
218 + * @return \Stripe\PaymentIntent The retrieved Payment Intent object.
219 + * @throws \Throwable If retrieval fails.
220 + */
221 + public static function getPaymentIntent( $paymentIntent ) {
222 + try {
223 + return self::$client->paymentIntents->retrieve( $paymentIntent );
224 + } catch ( \Throwable $th ) {
225 + throw $th;
226 + }
227 + }
228 +
229 + /**
230 + * Retrieves the details of a balance transaction from Stripe.
231 + *
232 + * @param string $balance_transaction The ID of the balance transaction.
233 + *
234 + * @return \Stripe\BalanceTransaction
235 + * @throws \Throwable If retrieval fails.
236 + */
237 + public static function getBalanceTransaction( $balance_transaction ) {
238 + try {
239 + return self::$client->balanceTransactions->retrieve( $balance_transaction );
240 + } catch ( \Throwable $th ) {
241 + throw $th;
242 + }
243 + }
244 +
245 + /**
246 + * Retrieves the details of a charge from Stripe.
247 + *
248 + * @param string $charge The ID of the charge to retrieve.
249 + *
250 + * @return \Stripe\Charge
251 + * @throws \Throwable If retrieval fails.
252 + */
253 + public static function getCharge( $charge ) {
254 + try {
255 + return self::$client->charges->retrieve( $charge );
256 + } catch ( \Throwable $th ) {
257 + throw $th;
258 + }
259 + }
260 +
261 +
262 + /**
263 + * Retrieves the default settlement currency for the connected Stripe account.
264 + *
265 + * @return string|null The default currency code or null if not found.
266 + * @throws \Throwable If retrieval fails.
267 + */
268 + public static function getDefaultCurrency() {
269 +
270 + try {
271 +
272 + return self::$client->accounts->retrieve()->default_currency ?? null;
273 +
274 + } catch ( \Throwable $th ) {
275 + throw $th;
276 + }
277 + }
278 +
279 + /**
280 + * Creates an FX quote for converting a currency into the default currency via Stripe.
281 + *
282 + * @since 1.0.4
283 + *
284 + * @param string $defaultCurrency The destination currency.
285 + * @param string $currency The source currency.
286 + *
287 + * @return mixed The raw Stripe response containing FX quote details.
288 + * @throws \Throwable If quote creation fails.
289 + */
290 + public static function createFXQuote( $defaultCurrency, $currency ) {
291 + try {
292 +
293 + return self::$client->rawRequest(
294 + 'post',
295 + '/v1/fx_quotes',
296 + array(
297 + 'from_currencies' => array( $currency ),
298 + 'lock_duration' => 'none',
299 + 'to_currency' => $defaultCurrency,
300 + ),
301 + array(
302 + 'headers' => array(
303 + 'Stripe-Version' => '2023-10-16; fx_quote_preview=v1',
304 + ),
305 + )
306 + );
307 +
308 + } catch ( \Throwable $th ) {
309 + throw $th;
310 + }
311 + }
312 + }
313 +