Effortlessly Set Up Products and Prices: A Laravel PHP Guide with Stripe API

 How to create products and prices over Stripe using api's in Laravel(PHP)


Effortlessly-Set-Up-Products-and-Prices-A-Laravel-PHP-Guide-with-Stripe-API

Stripe is a most common and trusted payment gateway used by lots of businesses and companies and it provides lots of functionalities to make payment more smoother, faster and secure in no time. Stripe provides every service to fulfill your requirement for implementing payment gateway to your business.

We have already discussed about why to use stripe , how to create a customer , how to save a card and how to create a token and continuing this series , in this article we will see how to create products and prices over stripe using stripe api in Laravel.


Products are nothing but the goods or services you are providing to the consumer. It might be any kind of goods like a pen, mobile, charger etc. and services like Netflix, Amazon Prime and hotstar etc. for which you need pay for the subscription in monthly or annual basis.


Prices are nothing but the amount, the consumer have to pay for buying your product.

In this article,  we will make an example of subscription based product and prices which will be on recurring basis. Lets assume we need to make a product named " premium_test " and we will make two prices, one is for monthly basis which cost Rs.100 and another one is yearly basis which costs Rs.1200.

paymentController.php :-

Here is the code example of how you can create products and prices over Stripe.


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class paymentController extends Controller
{
    public function createProduct(Request $request){
        try{
            // connect with stripe account.
            $stripe = new \Stripe\StripeClient(
                Config('constants.stripe_keys.secret_key')
              );

            // Create a product over stripe
            $product_detail = $stripe->products->create([
                'name' => $request->product_name,
            ]);

            // create prices for the product
            $product_id = $product_detail->id;
            $monthly_price = $stripe->prices->create([
                'unit_amount' => $request->monthly_price*100,
                'currency' => 'inr',
                'recurring' => ['interval' => 'month'], // it defines the recurring interval
                'product' => $product_id,
            ]);

            $yearly_price = $stripe->prices->create([
                'unit_amount' => $request->yearly_price*100,
                'currency' => 'inr',
                'recurring' => ['interval' => 'year'],
                'product' => $product_id,
            ]);
            return [$product_detail,$monthly_price,$yearly_price];
        }catch(\Exception $e){
            return false;
        }
    }
}


This is an basic example, but there more parameters you can pass inside stripe API, you can refer this by clicking here.


Output :-

[
    {
        "id": "prod_N003ZMKvxUySMg",
        "object": "product",
        "active": true,
        "attributes": [],
        "created": 1671282236,
        "default_price": null,
        "description": null,
        "images": [],
        "livemode": false,
        "metadata": [],
        "name": "premium_test",
        "package_dimensions": null,
        "shippable": null,
        "statement_descriptor": null,
        "tax_code": null,
        "type": "service",
        "unit_label": null,
        "updated": 1671282236,
        "url": null
    },
    {
        "id": "price_1MG03USJakTn1zSVhdnfenVQ",
        "object": "price",
        "active": true,
        "billing_scheme": "per_unit",
        "created": 1671282236,
        "currency": "inr",
        "custom_unit_amount": null,
        "livemode": false,
        "lookup_key": null,
        "metadata": [],
        "nickname": null,
        "product": "prod_N003ZMKvxUySMg",
        "recurring": {
            "aggregate_usage": null,
            "interval": "month",
            "interval_count": 1,
            "trial_period_days": null,
            "usage_type": "licensed"
        },
        "tax_behavior": "unspecified",
        "tiers_mode": null,
        "transform_quantity": null,
        "type": "recurring",
        "unit_amount": 10000,
        "unit_amount_decimal": "10000"
    },
    {
        "id": "price_1MG03VSJakTn1zSVfj1SaWJb",
        "object": "price",
        "active": true,
        "billing_scheme": "per_unit",
        "created": 1671282237,
        "currency": "inr",
        "custom_unit_amount": null,
        "livemode": false,
        "lookup_key": null,
        "metadata": [],
        "nickname": null,
        "product": "prod_N003ZMKvxUySMg",
        "recurring": {
            "aggregate_usage": null,
            "interval": "year",
            "interval_count": 1,
            "trial_period_days": null,
            "usage_type": "licensed"
        },
        "tax_behavior": "unspecified",
        "tiers_mode": null,
        "transform_quantity": null,
        "type": "recurring",
        "unit_amount": 120000,
        "unit_amount_decimal": "120000"
    }
]


Previous Post Next Post

Contact Form