What is Laravel Cashier ?
Laravel Cashier provides an communicative and fluent interface for Stripe. It handles almost all the code we need for subscription, coupons, cancellation grace periods, and even generate invoice PDFs.
What is Stripe ?
Stripe is a payment-processing service that provides easy-to-use APIs and powers e-commerce for businesses. Stripe is also providing very easy way to secure credit-card transactions while providing your users the great in-page checkout experience. Not only that, but Stripe has built-in support for subscriptions and auto renewal billing.
What is missing with Laravel Cashier ?
Laravel Cashier provides too many things related to cashier but sometimes it’s not enough, There are few points that can not be done by cashier,
-
Plans
-
Coupons
-
Custom account
-
Auth/Capture/Transfer/Refund/Transfer Reversals of Charges
-
Payout
For this above topics we need to use “cartalyst/stripe-laravel” package.
Getting started with configuration
1. Installation
Run this command to install package
$ composer require cartalyst/stripe-laravel
2. Integration
Set the Service Provider and Facade alias
After installing the package, add the following lines to your Laravel config file which is located at config/app.php.
In the $providers array add the following service provider for this package.
Cartalyst\Stripe\Laravel\StripeServiceProvider::class,
In the $aliases array add the following facade for this package.
‘Stripe’ => Cartalyst\Stripe\Laravel\Facades\Stripe::class,
3. Setup API Key
Now we need to setup the Stripe API key, to do this open or create the config/services.php file, and add or update the ‘stripe’ array:
‘stripe’ => [
‘secret’ => ‘your-stripe-key-here’,
],
Usage
Once setup finish it is ready for usage,
-
Products
-
Create Product
-
-
Plans
-
Create Plan
-
Delete Plan
-
-
Customer
-
Create Customer
-
Retrieve Customer
-
Delete Customer
-
-
Connect
-
Create Custom Account
-
Update Custom Account
-
Upload Identity Documents
-
Attaching Identity document
-
-
Transfer Amount To Connect Account
-
Transfer
-
Transfer Reversals
-
Payout
-
-
Charges
-
Auth Charges
-
Capture Auth charges
-
Refund Charges
-
Products and Plans
→ Plans are at the heart of subscriptions, establishing the billing cycle, currency, and base cost.
→ Every plan is attached to particular product, which represents the application or service offered to customers.
→ Products can have multiple plans, reflecting variations in price and duration—such as monthly and annual pricing at different rates.
1. Products
⇒ There are two different types of products: goods and services.
⇒ Goods are intended for use with the Orders API and mostly for one time payment while services are for subscriptions.
=>Create Product
\Stripe\Stripe::setApiKey("STRIPE_SECRET"); $product = \Stripe\Product::create([ “name” => ‘Product Name Here’, //the name, meant to be display to the customer. “type” => ‘service’, “metadata” => [] ]);
2. Plans
- Create Plan
Plans have the following parameters :-
Product: It’s the ID of an existing product to associate with the plan or the properties to define a new product.
Amount: It’s what the customer is charged per subscription per interval.
Interval: Which is billing period for the plan, which can range from a single day to a year. The interval options are like day, week, month, and year.
Id: A unique identifier. This is auto-generated by Stripe. You can also override this value, but plan ID must be unique across all the plans in your Stripe account.
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $plan = \Stripe\Plan::create([ ‘currency’ => ‘usd’, ‘interval’ => ‘month’, ‘product’ => ‘prod_********’, ‘nickname’ => ‘Pro Plan’, ‘amount’ => 1000, ]);
- Delete Plan
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $plan = \Stripe\Plan::retrieve(plan_id); $plan->delete();
3. Customers
→ Customers allow you to perform recurring charges, and to track multiple charges, that are associated with the same customer.
→ These examples for create, retrieve and delete customer.
1. Create Customer
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $customer = \Stripe\Customer::create(array( “description” => ”Customer for [email protected]”, //It is displayed alongside the customer in the dashboard. “source” => “tok_cardtoken”, //Bank or Card token “email” => “[email protected]”, “metadata” =>[], “coupon” => “25_5OFF”, //The code of the coupon to apply to this subscription. ));
2. Retrieve Customer
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $customer = \Stripe\Customer::retrieve(“customer_id”);
3. Delete Customer
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $customer = \Stripe\Customer::retrieve(“customer_id”); $customer->delete();
4. Connect
→ Stripe Connect is a full-stack resolution for using Stripe’s capabilities on behalf of others.
→ Connect provides a robust API and different tools you need to make charges, as well as onboard, verify, and pay sellers, contractors, service providers: whomever your business’s users are.
→ You can connect your platform to existing Stripe accounts or produce new ones.
→ With Connect, users of your platform will have one of 3 types of Stripe accounts:
Standard: A Standard Stripe account is a conventional Stripe account controlled directly by the account holder (i.e., your platform’s user).
Express: An Express Stripe account provides the platform with the ability to specify the flow of funds and the responsibility for handling disputes and refunds, similar to a Custom account. But with Express accounts, Stripe handles the onboarding and identity verification process.
Custom: A Custom Stripe account is nearly utterly completely invisible to the account holder. You—the platform—are to blame for all interactions along with your user, including collecting any data Stripe needs.
1. Create an Account
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $account = \Stripe\Account::create([ “type” => “custom”, “country” => “US”, “email” => “[email protected]“ ]);
2. Update Account Details
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $account = \Stripe\Account::retrieve(account_Id); $account->external_account = ‘source_token’; $account->legal_entity->first_name = first_name; $account->legal_entity->last_name = last_name; $account->save();
3. Upload Identity Document
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); \Stripe\FileUpload::create([ “purpose” => “identity_document”, “file” => fopen($file, ‘r’) ]);
4. Attaching Identity Document
\Stripe\Stripe::setApiKey(“STRIPE_SECRET”); $account = \Stripe\Account::retrieve(“account_id”); $account->legal_entity->verification->document = uploaded_document_id; $account->save();
5. Transfer To Connect Account
→ To transfer funds from your Stripe account to a connected account
→ Stripe Connect platforms can reverse transfers made to a connected account, either entirely or partially, and can also specify whether to refund any related application fees. Transfer reversals add to the platform’s balance and subtract from the destination account’s balance.
→ Manually Payout Transferred Amount to Connected Account
1. Create Transfer
$transfer = \Stripe\Transfer::create(array( “amount” => 500, // 500 = 5$ “currency” => “USD” //3-letter ISO code for currency, “destination” => “account_id” ));
2. Transfer Reversals
$transfer = \Stripe\Transfer::retrieve(“transfer_id”); $transferReversal =$transfer->reversals->create([ “amount” => (500) ]);
3. Payout
$payout = \Stripe\Payout::create([ “amount” => 500, “currency” => “usd”, ], [“stripe_account” => “account_id”]);
6. Charges
→ The charge issues an authorization (or pre-authorization), and will need to be captured later.
→ Uncaptured charges expire in seven days.
→ Capture the payment of an existing, uncaptured, charge.
→ This is the second half of the two-step payment flow, where first you created a charge with the capture option set to false.
→ Funds will be refunded to the credit or debit card that was originally charged.
→ You can optionally refund only part of a charge. You can do so multiple times, until the entire charge has been refunded.
1. Auth Charges
$authCharges = \Stripe\Charge::create([ “amount” => 500, “currency” => ”USD”, “description” => ”Payment for purchase XYZ product”, “capture” => false, // false is for authorise charge “customer” => “customer_id” ]);
2. Capture Auth Charges
$captureAuthCharges = \Stripe\Charge::retrieve(“charge_id”)->capture();
3. Refund Charges
$refundCharges = \Stripe\Refund::create([ “charge” => “charge_id” ]);
So we tried to cover almost all the required things for Stripe in Laravel Cashier. We have covered missing items from Laravel Cashier. We hope you find this blog helpful and if you have any questions or feedback feel free to add comment below this content.