PHP code example of squareboat / razorpay-cashier
1. Go to this page and download the library: Download squareboat/razorpay-cashier library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
squareboat / razorpay-cashier example snippets
return [
'key' => env('RAZORPAY_KEY'),
'secret' => env('RAZORPAY_SECRET'),
'currency' => 'INR',
];
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Squareboat\RazorpayCashier\Traits\Billable;
class User extends Authenticatable
{
use Billable;
}
$user = auth()->user();
$payment = $user->charge(10000); // Amount in paise (100 INR = 10000 paise)
$user = auth()->user();
$subscription = $user->newSubscription('default', 'plan_id_from_razorpay')
->create($paymentMethodId);
Route::get('/test-payment', function () {
$razorpayApi = new \Razorpay\Api\Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
$plan = $razorpayApi->plan->fetch('plan_QEv23lVNekinmU');
return view('payment', [
'plan_amount' => $plan->item->amount, // Access amount from item
'charge_amount' => 10000,
]);
});
Route::post('/charge', function (Request $request) {
$user = auth()->user();
$paymentMethodId = $request->input('payment_method_id');
if (!$paymentMethodId) {
return response()->json(['error' => 'Payment method ID missing'], 400);
}
$razorpay = new \Squareboat\RazorpayCashier\RazorpayCashier();
$payment = $razorpay->capturePayment($paymentMethodId, 10000); // 100 INR
return response()->json(['message' => 'Payment successful', 'payment' => $payment]);
});
Route::post('/subscribe', function (Request $request) {
$user = auth()->user();
$paymentMethodId = $request->input('payment_method_id');
if (!$paymentMethodId) {
return response()->json(['error' => 'Payment method ID missing'], 400);
}
// Replace 'plan_xxxxxxxxxx' with a real Razorpay plan ID from your dashboard
$subscription = $user->newSubscription('default', 'plan_QEv23lVNekinmU')
->trialDays(7) // 7-day trial
->create($paymentMethodId);
return response()->json(['message' => 'Subscription created with trial', 'subscription' => $subscription]);
});
use App\Models\User;
$user = User::firstOrCreate([
'email' => '[email protected] ',
'name' => 'Test User',
'password' => bcrypt('password'),
]);
$subscription = $user->newSubscription('default', 'plan_xxxxxxxxxx')
->trialDays(7) // Set a 7-day trial
->create($paymentMethodId);
return response()->json(['message' => 'Subscription created with trial', 'subscription' => $subscription]);
$subscription = $user->subscriptions()->first();
if ($subscription->hasTrialEnded()) {
$subscription->endTrial();
logger('Trial has ended for subscription: ' . $subscription->id);
}
Route::get('/test-trial-status/{subscriptionId}', function ($subscriptionId) {
$razorpay = new \Squareboat\RazorpayCashier\RazorpayCashier();
$subscription = $razorpay->syncTrialStatus($subscriptionId);
return response()->json(['message' => 'Trial status synced', 'subscription' => $subscription]);
});
$user->pauseSubscription('sub_QGXpowM0Ewrq0');
$user->resumeSubscription('sub_QGXpowM0Ewrq0');
$user->cancelSubscription('sub_QGXpowM0Ewrq0', 7); // 7-day grace period
$user->swapPlan('sub_QGXpowM0Ewrq0', 'plan_yyyyyyyyyy');
$user->createRazorpayInvoice('sub_QGXpowM0Ewrq0', null, 'First invoice', [
['name' => 'Subscription Fee', 'amount' => 1000, 'quantity' => 1]
]);
$user->getRazorpayInvoice('inv_1abcde');
$user->updateRazorpayInvoiceStatus('inv_1abcde', 'issue');
bash
php artisan vendor:publish --tag=razorpay-config
bash
php artisan migrate
bash
php artisan migrate
bash
php artisan migrate