PHP code example of usdpay / laravel

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

    

usdpay / laravel example snippets


use Usdpay\Laravel\Facades\Usdpay;

$invoice = Usdpay::createInvoice(
    [
        'amount' => '49.00',
        'currency' => 'USD',
        'orderId' => 'ORDER-1042',
        'expiresInMinutes' => 30,
        'returnUrl' => route('orders.show', 1042),
    ],
    idempotencyKey: 'ORDER-1042-create',
);

return redirect()->away($invoice->checkoutUrl);

use Usdpay\Laravel\UsdpayManager;

final class CheckoutController
{
    public function __construct(private UsdpayManager $usdpay) {}

    public function __invoke(Order $order)
    {
        $invoice = $this->usdpay->createInvoice([
            'amount' => $order->total_decimal,
            'currency' => $order->currency,
            'orderId' => (string) $order->id,
        ], 'order-'.$order->id.'-create');

        return redirect()->away($invoice->checkoutUrl);
    }
}

$invoice = Usdpay::getInvoice('inv_7Fq2xK9');

if ($invoice->status === 'paid') {
    // Read-only status check. Fulfilment should still be idempotent.
}

use Usdpay\Laravel\Events\InvoicePaid;

final class ActivateOrder
{
    public function handle(InvoicePaid $event): void
    {
        $invoice = $event->invoice;
        $idempotencyKey = $event->idempotencyKey;
        $deliveryId = $event->deliveryId;

        // Your application's idempotent business logic belongs here.
    }
}

use Illuminate\Support\Facades\DB;
use Usdpay\Laravel\Events\InvoicePaid;

public function handle(InvoicePaid $event): void
{
    DB::transaction(function () use ($event): void {
        $processed = ProcessedWebhook::query()->firstOrCreate([
            'key' => $event->idempotencyKey,
        ]);

        if (! $processed->wasRecentlyCreated) {
            return;
        }

        $order = Order::query()
            ->where('external_id', $event->invoice->orderId)
            ->lockForUpdate()
            ->firstOrFail();

        if ($order->paid_at !== null) {
            return;
        }

        $order->update(['paid_at' => now()]);
    });
}

use Illuminate\Support\Facades\Log;
use Usdpay\Laravel\Exceptions\UsdpayApiException;

try {
    $invoice = Usdpay::createInvoice($payload, 'ORDER-1042-create');
} catch (UsdpayApiException $exception) {
    Log::warning('USDPAY request failed', [
        'status' => $exception->statusCode,
        'code' => $exception->errorCode,
        'request_id' => $exception->requestId,
        'retry_after' => $exception->retryAfter,
    ]);
}
text
POST /usdpay/webhook
bash
php artisan vendor:publish --tag=usdpay-config