PHP code example of devneok / neokpay-php

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

    

devneok / neokpay-php example snippets


use Neok\Pay\Client;
use Neok\Pay\ClientConfig;

$factory = new \GuzzleHttp\Psr7\HttpFactory();
$client = new Client(
    new ClientConfig(apiKey: (string) getenv('NEOKPAY_API_KEY')),
    new \GuzzleHttp\Client(['timeout' => 15, 'connect_timeout' => 5]),
    $factory,
    $factory,
);

use Neok\Pay\DTO\CreateCheckoutRequest;

$checkout = $client->checkouts()->create(
    new CreateCheckoutRequest(
        reference: 'ORDER-1001', amount: '25.00', currency: 'USD',
        details: 'Example order', webhookUrl: 'https://merchant.test/neokpay/webhook',
        successUrl: 'https://merchant.test/payment/success',
        cancelUrl: 'https://merchant.test/payment/cancel',
        customerName: 'Ada Lovelace', customerEmail: '[email protected]',
    ),
    idempotencyKey: 'ORDER-1001',
);

header('Location: '.$checkout->checkoutUrl);

$payment = $client->payments()->retrieve($checkout->payment->id);
if ($payment->isPaid()) {
    // Fulfil only after server-confirmed paid state or a verified webhook.
}

use Neok\Pay\Webhooks\WebhookVerifier;

$event = (new WebhookVerifier())->verify(
    rawBody: file_get_contents('php://input'),
    signatureHeader: $_SERVER['HTTP_X_NEOKPAY_SIGNATURE'] ?? '',
    secret: (string) getenv('NEOKPAY_WEBHOOK_SECRET'),
);

try {
    $payment = $client->payments()->retrieve($storedPaymentId);
} catch (\Neok\Pay\Exceptions\AuthenticationException $e) {
    // Check server-issued credentials privately; never print/log them.
} catch (\Neok\Pay\Exceptions\RateLimitException $e) {
    // Back off before retrying.
} catch (\Neok\Pay\Exceptions\TransportException $e) {
    // Retry safely; keep the same idempotency key for checkout retries.
}
bash
composer