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/ */
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()]);
});
}