susheelbhai / larapay
Multiple payment gateway for Laravel Application
Requires
- cashfree/cashfree-pg: 4.*
- razorpay/razorpay: 2.*
- stripe/stripe-php: ^10.16
README
Installation
Laravel
Require this package in your composer.json and update composer. This will download the package and install Larapay package.
composer require susheelbhai/larapay
Configuration
Vendor Publish
Publish all the required files using the following command
php artisan vendor:publish --tag="larapay"
Add credientials in .env file
php artisan larapay:initial_settings
Migrate database
Migrate databse tables and seed with the following commands
php artisan migrate
Complete step in single action
composer require susheelbhai/larapay
php artisan vendor:publish --tag="larapay"
php artisan larapay:initial_settings
php artisan migrate
Uses
Add a form inside your view file
<form action="{{ route('pay') }}" method="post">
@csrf
<input name="action_url" type="hidden" value="{{ route('dashboard') }}" />
<input name="redirect_url" type="hidden" value="" />
<input name="gateway" type="hidden" value="{{ config('payment.gateway_id') }}" />
<input name="amount" type="hidden" value="{{ 100*1.18 }}"/>
<input name="gst_percentage" type="hidden" value="{{ $gst_percentage ?? 18}}"/>
<input name="button" type="submit" title="Pay Now" />
</form>
.env variables
ENABLE_PAYMENT_RESPONSE -> 0 or 1 0 it will wait for webhook to respond 1 it will directly respond if callback responce received
PAYMENT_TEMPLATE -> blade or react bassed on the starter kit you are using
Setting up webhook
Razorpay
Rooturl/webhook/2
Phonepe V1
webhook url will be sent with payment request autometically, no action needed.
Phonepe V2
Rooturl/webhook/6
Cashfree
Rooturl/webhook/7
PayU
Rooturl/webhook/8
Allow callback url work without csrf token
Add the callback_url in validateCsrfTokens except so that it can work even if this page is redirected by payment gateway
bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
//------
$middleware->validateCsrfTokens(except: [
'callback_url',
'payout/webhook/*',
]);
//------
})
Refund Payment
Add the callback_url in validateCsrfTokens except so that it can work even if this page is redirected by payment gateway
use Susheelbhai\Larapay\Http\Controllers\PaymentController;
$Larapay = new PaymentController();
$response = $Larapay->refundPayment($payment_id, $amount, $speed);
Refend data is stored in $response and you can use it according to your need.
| variable | value | description |
|---|---|---|
| $payment_id | int | this is the primary key of payment table |
| $amount | full or custom amount |
'full' for full refund and the custom value for partial refund |
| $speed | normal or instant |
'normal' for standard refund and 'instant' for instant refund |
Payout (Money Out)
The payout feature allows you to send money TO beneficiaries (users, dealers, partners) via Razorpay X, Cashfree Payouts, or PayU Payouts.
Payout .env Variables
# Default payout gateway
PAYOUT_GATEWAY=razorpay
# Razorpay X Payouts
RAZORPAY_PAYOUT_KEY_ID=
RAZORPAY_PAYOUT_KEY_SECRET=
RAZORPAY_PAYOUT_ACCOUNT_NUMBER=
RAZORPAY_PAYOUT_WEBHOOK_SECRET=
# Cashfree Payouts
CASHFREE_PAYOUT_CLIENT_ID=
CASHFREE_PAYOUT_CLIENT_SECRET=
CASHFREE_PAYOUT_WEBHOOK_SECRET=
# PayU Payouts
PAYU_PAYOUT_MERCHANT_ID=
PAYU_PAYOUT_MERCHANT_SALT=
PAYU_PAYOUT_CLIENT_ID=
PAYU_PAYOUT_CLIENT_SECRET=
PAYU_PAYOUT_WEBHOOK_SECRET=
Gateway IDs (for payout)
| Gateway | ID |
|---|---|
| Razorpay | 1 |
| Cashfree | 2 |
| PayU | 3 |
Create a Beneficiary
Pre-register a beneficiary for repeated payouts:
use Susheelbhai\Larapay\Http\Controllers\PayoutController; $payout = new PayoutController(); // Bank account beneficiary $result = $payout->createBeneficiary([ 'name' => 'John Doe', 'email' => 'john@example.com', 'phone' => '9876543210', 'account_type' => 'bank_account', 'account_number' => '1234567890123', 'ifsc' => 'HDFC0001234', 'user_id' => $user->id, // optional 'user_type' => get_class($user), // optional ], $gateway_id); // VPA beneficiary $result = $payout->createBeneficiary([ 'name' => 'Jane Doe', 'email' => 'jane@example.com', 'phone' => '9876543211', 'account_type' => 'vpa', 'vpa' => 'jane@upi', ], $gateway_id);
Initiate a Payout
Option A: Using a stored beneficiary
$result = $payout->createPayout([ 'beneficiary_id' => $beneficiary_id, 'amount' => 1500.00, 'currency' => 'INR', 'mode' => 'IMPS', // IMPS, NEFT, UPI, RTGS 'purpose' => 'cashback', ], $gateway_id);
Option B: Inline (raw account details — beneficiary created automatically)
$result = $payout->createPayout([ 'name' => 'John Doe', 'email' => 'john@example.com', 'phone' => '9876543210', 'account_type' => 'bank_account', 'account_number' => '1234567890123', 'ifsc' => 'HDFC0001234', 'amount' => 1500.00, 'currency' => 'INR', 'mode' => 'IMPS', 'purpose' => 'commission', ], $gateway_id);
Fetch Payout Status (Manual Sync)
$result = $payout->fetchPayoutStatus($payout_record_id);
Payout Webhook Setup
| Gateway | Webhook URL |
|---|---|
| Razorpay | Rooturl/payout/webhook/1 |
| Cashfree | Rooturl/payout/webhook/2 |
| PayU | Rooturl/payout/webhook/3 |
Add the payout webhook to CSRF exceptions in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { $middleware->validateCsrfTokens(except: [ 'callback_url', 'payout/webhook/*', ]); })
Payout Hook Methods
After publishing, override these methods in app/Http/Controllers/LarapayPayoutController.php to add your business logic:
| Method | When it's called |
|---|---|
prePayoutMethod($data, $gateway) |
Before sending payout to gateway |
postPayoutMethod($data, $gatewayPayoutId, $gateway) |
After gateway confirms payout creation |
payoutSuccessful($payoutRecord) |
When payout status becomes "processed" |
payoutFailed($payoutRecord, $reason) |
When payout status becomes "failed" |
payoutReversed($payoutRecord, $reason) |
When payout status becomes "reversed" |
Payout Statuses
| Status | Meaning |
|---|---|
pending |
Payout created locally, not yet sent to gateway |
processing |
Sent to gateway, awaiting confirmation |
processed |
Money transferred successfully |
failed |
Transfer failed (see failure_reason) |
reversed |
Transfer was reversed by bank (see reversal_reason) |
License
This Multi Auth Package is developed by susheelbhai for personal use software licensed under the MIT license