-
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 May 29, 2020
·
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');
// 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,
],
],
]);
Instead, with the new client/services infrastructure, you can do this:
<?php
// Initialize the API key
$stripe = new \Stripe\StripeClient('sk_test_xxxx');
// Create a coupon
$stripe->coupons->create([
'percent_off' => 25,
'duration' => 'repeating',
'duration_in_months' => 3,
]);
// Confirm a PaymentIntent
$stripe->paymentIntents->confirm(
'pi_1DeQ7b2eZvKYlo2C5FUypnEA',
['payment_method' => 'pm_card_visa']
);
// Create a Checkout Session
$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,
],
],
]);