PHP code example of paymentsnp / paymentsnp-php

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

    

paymentsnp / paymentsnp-php example snippets




$paymentsnp = new \Paymentsnp\PaymentsnpClient('np_test_...');

$session = $paymentsnp->checkout->sessions->create([
    'order_id' => 'ORD-1001',
    'amount_minor' => \Paymentsnp\Money::toPaisa('1,250.00'), // 125000
    'currency' => 'NPR',
    'customer' => ['email' => '[email protected]', 'name' => 'Sita Sharma'],
    'success_url' => 'https://shop.example.com/orders/ORD-1001',
    'cancel_url' => 'https://shop.example.com/cart',
], ['idempotency_key' => 'checkout-ORD-1001']);

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

new PaymentsnpClient('np_live_...', [
    'base_url' => 'https://api.paymentnp.com/v1', // default
    'timeout' => 30,                                // seconds
    'max_retries' => 2,
    'http_client' => $myClient,                     // implements Paymentsnp\HttpClient
]);
// or new PaymentsnpClient(['api_key' => 'np_live_...', ...])

try {
    $paymentsnp->payments->retrieve($id);
} catch (\Paymentsnp\InvalidRequestError $e) {
    error_log($e->getErrorCode() . ' ' . $e->getRequestId());
}

$event = \Paymentsnp\Webhook::constructEvent(
    file_get_contents('php://input'),            // raw body
    $_SERVER['HTTP_PAYMENTNP_SIGNATURE'] ?? '',
    getenv('PAYMENTSNP_WEBHOOK_SECRET'),
    300                                           // tolerance in seconds (default)
);

\Paymentsnp\Money::toPaisa('1,234.50'); // 123450 (string math; floats are rejected)
\Paymentsnp\Money::formatNpr(123450);   // "NPR 1,234.50"
sh
php tests/run.php
# against PHP 7.4 and 8.3:
docker run --rm -v "$PWD":/app -w /app php:7.4-cli php tests/run.php
docker run --rm -v "$PWD":/app -w /app php:8.3-cli php tests/run.php