PHPackages                             paybridge-np/sdk - 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. paybridge-np/sdk

ActiveLibrary[Payment Processing](/categories/payments)

paybridge-np/sdk
================

Official PHP SDK for PayBridgeNP — Nepal payment gateway aggregator

4.0.0(1mo ago)118MITPHPPHP &gt;=7.4

Since Apr 13Pushed 3w agoCompare

[ Source](https://github.com/paybridgenp/paybridgenp-php-sdk)[ Packagist](https://packagist.org/packages/paybridge-np/sdk)[ Docs](https://paybridgenp.com)[ RSS](/packages/paybridge-np-sdk/feed)WikiDiscussions main Synced 4w ago

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

PayBridgeNP — PHP SDK
=====================

[](#paybridgenp--php-sdk)

Official PHP SDK for [PayBridgeNP](https://paybridgenp.com) — accept eSewa, Khalti, and Fonepay through a single API.

**Requirements:** PHP 7.4+, `ext-curl`, `ext-json`

---

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

[](#installation)

```
composer require paybridge-np/sdk
```

---

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

[](#quick-start)

```
use PayBridgeNP\PayBridgeNP;

$pb = new PayBridgeNP(['api_key' => 'sk_test_...']);

$session = $pb->checkout->create([
    'amount'     => 5000,          // NPR 50.00 — always in paisa (NPR × 100)
    'return_url' => 'https://myshop.com/thank-you',
    'cancel_url' => 'https://myshop.com/cart',
    'metadata'   => ['order_id' => 'ORD-001'],
]);

header('Location: ' . $session['checkout_url']);
exit;
```

The customer lands on a hosted checkout page, picks their payment method (eSewa / Khalti), pays, and is redirected back to your `return_url` with:

```
https://myshop.com/thank-you?session_id=cs_xxx&status=success&payment_id=pay_xxx

```

---

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

[](#configuration)

```
$pb = new PayBridgeNP([
    'api_key'     => 'sk_live_...',              // required
    'base_url'    => 'https://api.paybridgenp.com', // optional, default shown
    'timeout'     => 30,                         // optional, seconds (default: 30)
    'max_retries' => 2,                          // optional, retries on 5xx (default: 2)
]);
```

Use `sk_test_` keys for sandbox mode — no real money moves. Switch to `sk_live_` for production.

---

Checkout
--------

[](#checkout)

### Create a session

[](#create-a-session)

```
$session = $pb->checkout->create([
    'amount'     => 10000,       // NPR 100.00 in paisa — required
    'return_url' => 'https://myshop.com/thank-you', // required
    'cancel_url' => 'https://myshop.com/cart',       // optional
    'provider'   => 'esewa',     // optional — omit to let the customer pick
    'currency'   => 'NPR',       // optional, default: NPR
    'metadata'   => [            // optional — any key/value pairs
        'order_id'       => 'ORD-001',
        'customer_email' => 'ram@example.com',
    ],
]);

// $session['id']           — cs_xxxxxxxxxxxxxxxx
// $session['checkout_url'] — redirect the customer here
// $session['expires_at']   — ISO 8601 timestamp (1 hour from creation)
```

If you pass `provider` upfront, the response also includes `payment_method` with the direct redirect URL or form fields — useful if you want to skip the hosted page entirely.

### Expire a session

[](#expire-a-session)

Use this when you mint a fresh session for a logical purchase that already had one outstanding (e.g. a customer requesting a new payment link), so the old URL stops being payable immediately rather than waiting for its 30-minute TTL. Idempotent on already-terminal sessions.

```
$pb->checkout->expire('cs_xxxxxxxxxxxxxxxx');
```

### Laravel example

[](#laravel-example)

```
// routes/web.php
Route::post('/checkout', function (Request $request) {
    $pb = new PayBridgeNP(['api_key' => config('services.paybridge.key')]);

    $session = $pb->checkout->create([
        'amount'     => $request->input('amount'),
        'return_url' => route('checkout.return'),
        'cancel_url' => route('checkout.cancel'),
        'metadata'   => ['order_id' => $request->input('order_id')],
    ]);

    return redirect($session['checkout_url']);
});

Route::get('/checkout/return', function (Request $request) {
    $status    = $request->query('status');      // "success" or "failed"
    $paymentId = $request->query('payment_id');

    if ($status === 'success') {
        // fulfill the order
    }

    return view('checkout.result', compact('status', 'paymentId'));
});
```

---

Payments
--------

[](#payments)

### List payments

[](#list-payments)

```
$result = $pb->payments->list(['limit' => 20, 'offset' => 0]);

foreach ($result['data'] as $payment) {
    echo $payment['id'];        // pay_xxxxxxxxxxxxxxxx
    echo $payment['amount'];    // paisa
    echo $payment['status'];    // success | failed | pending | ...
    echo $payment['provider'];  // esewa | khalti | fonepay
}

// Pagination
$total  = $result['meta']['total'];
$limit  = $result['meta']['limit'];
$offset = $result['meta']['offset'];
```

### Retrieve a payment

[](#retrieve-a-payment)

```
$payment = $pb->payments->get('pay_xxxxxxxxxxxxxxxx');

echo $payment['status'];       // success
echo $payment['provider_ref']; // provider's own transaction ID
echo $payment['metadata']['order_id']; // data you passed at checkout
```

---

Webhooks
--------

[](#webhooks)

Webhooks let PayBridgeNP notify your server when a payment is completed or fails. You register an endpoint URL, and we POST a signed JSON payload to it for every event.

### 1. Register an endpoint

[](#1-register-an-endpoint)

```
$endpoint = $pb->webhooks->create([
    'url'    => 'https://myshop.com/webhooks/paybridge',
    'events' => ['payment.succeeded', 'payment.failed'],
]);

// Save $endpoint['signing_secret'] somewhere safe (e.g. .env)
// It is shown ONCE and cannot be retrieved again.
echo $endpoint['signing_secret']; // whsec_...
```

### 2. Handle incoming webhooks

[](#2-handle-incoming-webhooks)

Always verify the signature before trusting the payload.

**Plain PHP:**

```
