Stripe, PHP API, Subscriptions Example – Quick Start Guide

Stripe Subscriptions PHP

Stripe PHP Subscriptions get list of products and for each product the price tiers to show the customer, Stripe Track and Manage Your Paid Subscriptions, Retrieve Create, Update, and Schedule Subscriptions, Subscription update , Subscription Update using subscription_id Create Payment Method and then a SetupIntent Integrate Recurring Payment using Stripe Checkout with Billing.

Stripe php get subscription, Stripe php get subscription, Update subscription Stripe php, PHP Stripe View and download invoices

How to Stripe Track and Manage Your Paid Subscriptions

view all invoices paid relating to a subscription.

$subscription_id = 'sub_1Jrr0CEHaKKg8tM88kviX3jR';
$invoices = \Stripe\Invoice::all(['subscription'=> $subscription_id])->data;

echo "<pre>";
print_r($invoices);
echo "</pre>";

Retrieve Create, Update, and Schedule Subscriptions

review their current subscriptions:

$stripe_customer_id = "cus_JDrjxieMrqzd7K";
$subscriptions = \Stripe\Subscription::all([
    'customer'=> $stripe_customer_id
]);

echo "<pre>";
print_r($subscriptions);
echo "</pre>";

Subscription update

\Stripe\Subscription::update($subscription_id,
      [
       'items' => [
        [   'id' => $subscription_item_id,
            'price' => $data->price_id
        ]
      ]
]);

Subscription Update using subscription_id

\Stripe\Subscription::update($subscription_id,
      ["cancel_at_period_end" => true, 
      "default_payment_method" => ""
]);

Subscription create

$stripe_sub = \Stripe\Subscription::create([
       'customer' => $stripe_customer_id,
       'default_payment_method' => $pmt_method->id,
       'items' => [
            ['price' => $price_id]
            ]
]);
$subscription_id = $stripe_sub->id;
$subscription_item_id = $stripe_sub->items->data[0]->id;

Create PaymentMethod

Create Payment Method and then a SetupIntent

$pmt_method = \Stripe\PaymentMethod::create([
     'type'=>'card',
     'card'=> [
         'number' => '4242424242424242',
         'exp_month' => 2,
         'exp_year'=> 2022,
         'cvc' => '343',
         ]
]);

$intent = \Stripe\SetupIntent::create([
      'payment_method_types'=>['card'],
      'payment_method' => $pmt_method->id,
      'customer' => $stripe_customer_id,
      'description' => 'Great simple Uniq Cust Name',
      'confirm' => true,
      'usage' => "off_session"
]);

Create a customer account

$customer = \Stripe\Customer::create(([
        'email' => '[email protected]',
        'description' => 'Great simple Uniq Cust Name',
        'name' => 'Pakainfo V1'
]));
$stripe_customer_id = $customer->id;

Stripe Get All Product details

Get list of products and prices:

$products = \Stripe\Product::all([]);

foreach($products as $product){
  $product->prices = \Stripe\Price::all(['product'=>$product->id])->data;
}

Read Also:  Laravel 7 Form Validation rules Tutorial Example

Stripe PHP Create customer and add subscription

$stripe->subscriptions->create([
  'customer' => 'cus_9565',
  'items' => [
    ['price' => 'plan_475'],
  ],
]);

$subscription = $stripe->subscriptions->create([
  'customer' => 'cus_8956',
  'items' => [[
    'price_data' => [
      'unit_amount' => 45852,
      'currency' => 'usd',
      'product' => 'prod_8956',
      'recurring' => [
        'interval' => 'month',
      ],
    ],
  ]],
]);

Here is the updated code

$stripe->subscriptions->create([
    'customer' => $cid,
    'items' => [['price' => 'price_0IR0OGH7HxDXZRHq3sIg9biB'],],
]);

I hope you get an idea about Stripe, PHP, Subscriptions.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.