Posted inphp

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 "
";
print_r($invoices);
echo "

";

Retrieve Create, Update, and Schedule Subscriptions

review their current subscriptions:

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

echo "
";
print_r($subscriptions);
echo "

";

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;
}

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype