-
Notifications
You must be signed in to change notification settings - Fork 850
Migration to StripeClient and services in 7.33.0
remi-stripe edited this page Aug 17, 2022
·
6 revisions
Starting with 7.33.0 of the library, we released a new StripeClient
class which lets you interact with the API without using static methods or having to retrieve a resource before deleting it for example.
In the past, your code would look like this
<?php
// Initialize the API key
\Stripe\Stripe::setApiKey('sk_test_xxx');
// Set a global API version
\Stripe\Stripe::setApiVersion('2022-08-01');
// Retrieve the customer and then delete it
$customer = \Stripe\Customer::retrieve('cus_xxx');
$customer->delete();
// Create a Coupon
$coupon = \Stripe\Coupon::create([
'percent_off' => 25,
'duration' => 'repeating',
'duration_in_months' => 3,
]);
// Retrieve and confirm a PaymentIntent
$paymentIntent = \Stripe\PaymentIntent::retrieve('pi_123');
$paymentIntent->confirm([
'payment_method' => 'pm_card_visa'
];
// Create a Checkout Session
$checkoutSession = \Stripe\Checkout\Session::create([
'success_url' => 'https://www.example.com/success',
'cancel_url' => 'https://www.example.com/failure',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_ABCDE',
'quantity' => 2,
],
],
]);
// Retrieve a Checkout Session and expand line_items
// Note the first parameter is now an array with id as a key to pass the resource id which was hard to figure out
$checkoutSession = \Stripe\Checkout\Session::retrieve([
'id' => 'cs_test_123',
'expand' => ['line_items'],
]);
Instead, with the new client/services infrastructure, you can do this:
<?php
// Initialize a client with just the API key
$stripe = new \Stripe\StripeClient('sk_test_xxxx');
// Alternatively initialize a client with the API key and API version
$stripe = new \Stripe\StripeClient([
'api_key' => 'sk_test_xxxx',
'stripe_version' => '2022-08-01',
]);
// Create a coupon
$coupon = $stripe->coupons->create([
'percent_off' => 25,
'duration' => 'repeating',
'duration_in_months' => 3,
]);
// Confirm a PaymentIntent
$paymentIntent = $stripe->paymentIntents->confirm(
'pi_1DeQ7b2eZvKYlo2C5FUypnEA',
['payment_method' => 'pm_card_visa']
);
// Create a Checkout Session
$checkoutSession = $stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_ABCDE',
'quantity' => 2,
],
],
]);
// Retrieve a Checkout Session and expand line_items
$checkoutSession = $stripe->checkout->sessions->retrieve(
'cs_test_123',
[
'expand' => ['line_items'],
]
);