PHP code example of gokulsingh / laravel-payhub

1. Go to this page and download the library: Download gokulsingh/laravel-payhub 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/ */

    

gokulsingh / laravel-payhub example snippets


use Gokulsingh\LaravelPayhub\Facades\Payment;

$order = Payment::createOrder([
    'amount'   => 500,             // ₹500 (Razorpay will send 50000 paise to API)
    'currency' => 'INR',
    'metadata' => ['receipt' => 'ORD-1001', 'user_id' => auth()->id()],
]);

$orderCF = Payment::gateway('cashfree')->createOrder([
             'order_id' => uniqid('ord_'), //else remove automatic generate
            'amount' => 1500,
            'currency' => 'INR',
            'customer_id' => "3297842",
            'email' => '[email protected]',
            'phone' => '9999999999',
            'metadata' => [
                'return_url' => 'https://mysite.domain/return_url', // https url else remove 
                'notify_url' => 'https://mysite.domain/notify_url', // https url else remove 
                'payment_methods' =>  "cc", "dc", "ccc", "ppc","nb","upi","paypal","app","paylater","cardlessemi","dcemi","ccemi", //check for all available options in cashfree documentation 
                "banktransfer"
            ],
            'order_tags' => [
                'note1' => 'note1',
                'note2' => 'note2',
            ]
        ]);

// Razorpay verification by payment id from JS
$payment = Payment::gateway('razorpay')->charge([
    'payment_id' => 'pay_XXXXXXXX',
]);

// Cashfree check status by order id
$payment = Payment::gateway('cashfree')->charge([
    'order_id' => 'cf_order_XXXX',
]);

$refund = Payment::gateway('razorpay')->refund('pay_XXXXXXXX', ['amount' => 200]);
$refund = Payment::gateway('cashfree')->refund('cf_order_XXXX', ['amount' => 500, 'note' => 'Partial refund']);

// Controller
public function createRazorpayOrder()
{
    $order = Payment::gateway('razorpay')->createOrder([
        'amount'   => 500,
        'currency' => 'INR',
        'metadata' => ['receipt' => 'rzp_order_101'],
    ]);
    return response()->json($order);
}

public function createCashfreeOrder()
{
    $order = Payment::gateway('cashfree')->createOrder([
            'order_id' => uniqid('ord_'), //else remove automatic generate
            'amount' => 1500,
            'currency' => 'INR',
            'customer_id' => "3297842",
            'email' => '[email protected]',
            'phone' => '9999999999',
            'metadata' => [
                'return_url' => 'https://mysite.domain/return_url', // https url else remove 
                'notify_url' => 'https://mysite.domain/notify_url', // https url else remove 
                'payment_methods' =>  "cc", "dc", "ccc", "ppc","nb","upi","paypal","app","paylater","cardlessemi","dcemi","ccemi", //check for all available options in cashfree documentation 
                "banktransfer"
            ],
            'order_tags' => [
                'note1' => 'note1',
                'note2' => 'note2',
            ]
        ]);
    return response()->json($order);
}

return redirect($order['data']['custom']['payment_link'] ?? $order['data']['raw']['payment_link']);

// routes/web.php
use App\Http\Controllers\PaymentController;
Route::post('/payment/success', [PaymentController::class, 'success']);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Gokulsingh\LaravelPayhub\Facades\Payment;

class PaymentController extends Controller
{
    public function success(Request $request)
    {
        $gateway = $request->input('gateway');

        if ($gateway === 'razorpay') {
            $result = Payment::gateway('razorpay')->charge([
                'payment_id' => $request->input('payment_id'),
            ]);
        } elseif ($gateway === 'cashfree') {
            $result = Payment::gateway('cashfree')->charge([
                'order_id' => $request->input('order_id'),
            ]);
        } else {
            return response()->json(['message' => 'Unsupported gateway'], 400);
        }

        if ($result['success']) {
            // Payment verified — mark order as paid in your DB
            return response()->json(['message' => 'Payment successful', 'data' => $result]);
        }

        return response()->json(['message' => 'Payment verification failed', 'data' => $result], 400);
    }
}

// In routes/web.php (or anywhere routes are loaded)
Route::paymentWebhooks('payment/webhook'); // by default POST /payment/webhook/{gateway}

// app/Providers/EventServiceProvider.php
protected $listen = [
    \Gokulsingh\LaravelPayhub\Events\PaymentSucceeded::class => [
        \App\Listeners\HandlePaymentSucceeded::class,
    ],
];

$verified = Payment::gateway('razorpay')->verifyWebhook([
    'payload' => file_get_contents('php://input'),
    'headers' => request()->headers->all(),
]);

$order = Payment::gateway('cashfree')->createOrder([
    'amount' => 1500,
    'currency' => 'INR',
    'metadata' => [
        'user_id' => auth()->id(),
        'cart_id' => 999,
        'custom_flag' => 'gift',
    ],
]);

// config/payment.php
'logging' => [
  'enabled' => false,
],
bash
php artisan vendor:publish --provider="Gokulsingh\LaravelPayhub\PaymentServiceProvider" --tag=config
php artisan vendor:publish --provider="Gokulsingh\LaravelPayhub\PaymentServiceProvider" --tag=migrations
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Gokulsingh\LaravelPayhub\PaymentServiceProvider" --tag=migrations
php artisan migrate