PHP code example of shikhar / laravel-payments

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

    

shikhar / laravel-payments example snippets


return [

  'default' => env('PAYMENT_DEFAULT', 'khalti'),

  'gateways' => [

    'khalti' => [
      'public_key' => env('KHALTI_PUBLIC_KEY', ''),
      'secret_key' => env('KHALTI_SECRET_KEY', ''),
    ],

    'esewa' => [
      'form_url' => 'https://rc-epay.esewa.com.np/api/epay/main/v2/form',
      'merchant_id' => env('ESEWA_MERCHANT_ID', 'EPAYTEST'),
      'secret_key' => env('ESEWA_SECRET_KEY', '8gBm/:&EnhH.1/q'), 
    ],

  ],

];



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Shikhar\Payments\PaymentManager;

class PaymentController extends Controller
{

    public $payment;

    public function __construct(PaymentManager $payment)
    {
        $this->payment = $payment;
    }

    public function pay(Request $request)
    {

        $payload = [
            'tax_amount' => $request->input('tax_amount'),
            'transaction_uuid' => $request->input('transaction_uuid', uniqid()),
            'product_code' => $request->input('product_code'),
            'customer_name' => $request->input('customer_name', null),
            'customer_email' => $request->input('customer_email', null),
            'customer_phone' => $request->input('customer_phone', null),
            'success_url' => $request->input('success_url'),
            'failure_url' => $request->input('failure_url'),
        ];

        // Charge via selected gateway
        $response = $this->payment
            ->via($request->payment_gateway)
            ->charge($request->amount, $payload);

        // Return JSON or redirect as needed
        return view('shikhar-payments::esewa', compact('response'));
    }
}

$this->payment->via('esewa')->charge(100, $payload);

$this->payment->via('khalti')->charge(100, $payload);
bash
php artisan vendor:publish --tag=payments-config

config/payments.php
html
<form id="esewaForm" method="POST" action="{{ $response['action_url'] }}" style="display:none;">
    @foreach ($response['fields'] as $key => $value)
        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
    @endforeach
</form>

<button onclick="document.getElementById('esewaForm').submit();">
    Pay with Esewa
</button>

src/
  PaymentManager.php
  Providers/
      PaymentServiceProvider.php
config/
  payments.php