PHPackages                             jaap-tech/laravel-nepali-payment - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. jaap-tech/laravel-nepali-payment

ActiveLibrary[Payment Processing](/categories/payments)

jaap-tech/laravel-nepali-payment
================================

Laravel package for integrating Nepali payment gateways like eSewa, Khalti, and ConnectIps.

v1.0.0(2mo ago)239MITPHPPHP ^8.1

Since Feb 8Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/aryanmalla19/laravel-nepali-payment)[ Packagist](https://packagist.org/packages/jaap-tech/laravel-nepali-payment)[ RSS](/packages/jaap-tech-laravel-nepali-payment/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (11)Versions (3)Used By (0)

Laravel Nepali Payment Gateway
==============================

[](#laravel-nepali-payment-gateway)

A comprehensive Laravel package for integrating Nepali payment gateways (eSewa, Khalti, and ConnectIps) with optional database tracking and payment management.

Features
--------

[](#features)

✅ **Multiple Gateway Support**

- eSewa integration
- Khalti integration
- ConnectIps integration

✅ **Database Integration (Optional)**

- Track payment history
- Store gateway responses
- Polymorphic payment associations
- UUID support

✅ **Event System**

- Payment lifecycle events
- Custom event listeners

✅ **Query Scopes &amp; Helpers**

- Easy payment retrieval
- Status filtering
- Gateway filtering

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require jaap-tech/laravel-nepali-payment
```

The package will auto-register the service provider.

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --tag=nepali-payment-config
```

This creates `config/nepali-payment.php` with the following structure:

```
return [
    'esewa' => [
        'product_code' => env('ESEWA_PRODUCT_CODE'),
        'secret_key'   => env('ESEWA_SECRET_KEY'),
        'success_url'   => env('ESEWA_SUCCESS_URL'),
        'failure_url'   => env('ESEWA_FAILURE_URL'),
    ],
    'khalti' => [
        'secret_key' => env('KHALTI_SECRET_KEY'),
        'environment' => strtolower(env('KHALTI_ENVIRONMENT', 'test')),
        'success_url'   => env('KHALTI_SUCCESS_URL'),
        'website_url'   => env('KHALTI_WEBSITE_URL'),
    ],
    'connectips' => [
        'merchant_id' => env('CONNECTIPS_MERCHANT_ID'),
        'app_id' => env('CONNECTIPS_APP_ID'),
        'app_name' => env('CONNECTIPS_APP_NAME'),
        'private_key_path' => env('CONNECTIPS_PRIVATE_KEY_PATH'),
        'password' => env('CONNECTIPS_PASSWORD'),
        'environment' => strtolower(env('CONNECTIPS_ENVIRONMENT', 'test')),
    ],
    'database' => [
        'enabled' => env('NEPALI_PAYMENT_DATABASE_ENABLED', false),
    ],
];
```

### Environment Variables

[](#environment-variables)

Add these to your `.env` file:

**eSewa:**

```
ESEWA_PRODUCT_CODE=your_product_code
ESEWA_SECRET_KEY=your_secret_key
ESEWA_SUCCESS_URL=https://yourapp.com/payment/success # (optional)
ESEWA_FAILURE_URL=https://yourapp.com/payment/failure # (optional)

```

**Khalti:**

```
KHALTI_SECRET_KEY=your_secret_key
KHALTI_ENVIRONMENT=test  # or 'live'
KHALTI_SUCCESS_URL=https://yourapp.com/payment/success # (optional)
KHALTI_WEBSITE_URL=https://yourapp.com # (optional)

```

**ConnectIps:**

```
CONNECTIPS_MERCHANT_ID=your_merchant_id
CONNECTIPS_APP_ID=your_app_id
CONNECTIPS_APP_NAME=your_app_name
CONNECTIPS_PRIVATE_KEY_PATH=/path/to/private/key
CONNECTIPS_PASSWORD=your_password
CONNECTIPS_ENVIRONMENT=test  # or 'live'

```

**Database Integration:**

```
NEPALI_PAYMENT_DATABASE_ENABLED=true

```

Quick Start
-----------

[](#quick-start)

### Basic Payment Flow (Without Database)

[](#basic-payment-flow-without-database)

```
use NepaliPayment;

// Initiate eSewa payment
$response = NepaliPayment::esewa()->payment([
    'amount' => 1000,
    'transaction_uuid' => 'unique-id-123', # optional, can be generated automatically
    'success_url' => route('payment.success'), # optional, can be set in config
    'failure_url' => route('payment.failure'), # optional, can be set in config
]);

// Redirect to payment gateway
$response->redirect();
```

### With Database Integration

[](#with-database-integration)

First, enable the database integration:

```
# 1. Publish migrations
php artisan vendor:publish --tag=nepali-payment-migrations

# 2. Run migrations
php artisan migrate

# 3. Enable in .env
NEPALI_PAYMENT_DATABASE_ENABLED=true
```

Now you can track payments:

```
use NepaliPayment;

// Initiate payment with gateway
$response = NepaliPayment::esewa()->payment([
    'amount' => 1000,
    'transaction_uuid' => 'unique-id-123', # optional, can be generated automatically
    'success_url' => route('payment.success'), # optional, can be set in config
    'failure_url' => route('payment.failure'), # optional, can be set in config
]);

// Record verification
$verification = NepaliPayment::esewa()->verify([
    'total_amount' => 1000,
    'transaction_uuid' => 'same-unique-id-123',
]);
```

Database Models
---------------

[](#database-models)

### Payment Model

[](#payment-model)

The `Payment` model stores all payment records with full lifecycle tracking.

**Scopes:**

```
// Find payment by reference ID
PaymentTransaction::byReference('ref-123')->first();

// Filter by gateway
PaymentTransaction::byGateway('esewa')->get();

// Filter by status
PaymentTransaction::byStatus('completed')->get();

// Completed payments only
PaymentTransaction::completed()->get();

// Failed payments only
PaymentTransaction::failed()->get();

// Pending/processing payments
PaymentTransaction::pending()->get();

// Filter by payable type and ID
PaymentTransaction::forPayable('App\Models\User', $userId)->get();
```

**Methods:**

```
$payment = Payment::find($id);

// Check status
$payment->isCompleted();
$payment->isFailed();
$payment->isPending();

// Update status
$payment->markAsProcessing();
$payment->markAsCompleted();
$payment->markAsFailed('Reason for failure');
$payment->markAsCancelled();

// Relationships
$payment->payable;        // The associated model (User, Order, etc)
```

Helper Functions
----------------

[](#helper-functions)

Quick helpers for common operations:

```
// Check if database is enabled
nepali_payment_enabled();

// Find payments
nepali_payment_find('ref-123');                    // by reference

// Query payments
nepali_payment_get_by_status('completed')->paginate();
nepali_payment_get_by_gateway('khalti')->latest()->get();
```

Events
------

[](#events)

The package dispatches events at key lifecycle points:

```
use JaapTech\NepaliPayment\Events\{
    PaymentInitiatedEvent,
    PaymentProcessingEvent,
    PaymentCompletedEvent,
    PaymentFailedEvent,
};

// Listen to events in EventServiceProvider
protected $listen = [
    PaymentCompletedEvent::class => [
        \App\Listeners\SendPaymentConfirmation::class,
        \App\Listeners\UpdateUserBalance::class,
    ],
    PaymentFailedEvent::class => [
        \App\Listeners\NotifyPaymentFailure::class,
    ],
];

// Or listen inline
Event::listen(PaymentCompletedEvent::class, function ($event) {
    // $event->payment
});
```

Verify Configuration
--------------------

[](#verify-configuration)

Check if all gateway configurations are valid:

```
php artisan nepali-payment:check
```

Output example:

```
Checking Nepali Payment Gateway Configuration:
- esewa: ✅ configured
- khalti: ✅ configured
- connectips: ⚠️  missing keys: environment

```

Example: Complete Payment Flow
------------------------------

[](#example-complete-payment-flow)

```
// PaymentController.php
