PHP code example of coinpay / laravel-integration

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

    

coinpay / laravel-integration example snippets


use Coinpay\Finance\Services\CoinPay\CoinPayPaymentRequest;
use Coinpay\Finance\Facades\CoinPay;
use Coinpay\Finance\Exceptions\CoinPayException;

$paymentRequest = new CoinPayPaymentRequest(
    amount: 100000,                     // Amount in smallest currency unit
    clientRefId: 'INV12345',            // Unique reference ID
    payerIdentity: '[email protected]',  // Payer identity (email/phone)
    name: 'John Doe',                   // Payer name
    description: 'Invoice Payment',     // Payment description
    nationalCode: '1234567890'          // National code of payer
);

try {
    $response = CoinPay::createPayment($paymentRequest);

    $url = $response->url;
    // Optionally store $response->transactionId in your database
} catch (CoinPayException $e) {
    // $e->getMessage() / $e->getCode() as usual, plus extra context:
    $e->getData();            // raw context array (e.g. ['message' => ..., 'code' => ...])
    $e->getHttpStatusCode();  // HTTP status returned by the CoinPay API, if known
}

use Coinpay\Finance\Facades\CoinPay;

$status = CoinPay::checkStatus($transactionId);

$status->status;          // e.g. 'completed', 'failed', 'pending'
$status->amount;
$status->reason;          // nullable
$status->transactionHash; // nullable
$status->network;         // nullable

namespace App\Services;

use App\Models\Payment;
use Coinpay\Finance\Services\CoinPay\Webhook\WebhookServiceInterface;

class MyCustomWebhookService implements WebhookServiceInterface
{
    public function handleWebhook(array $payload): array
    {
        // $payload contains: status, reason, transaction_id, amount, transaction_hash
        Payment::updateOrCreate(
            ['transaction_id' => $payload['transaction_id']],
            [
                'amount' => $payload['amount'],
                'status' => $payload['status'],
                'transaction_hash' => $payload['transaction_hash'],
            ]
        );

        return [
            'is_success' => true,
            'message' => 'Stored successfully',
        ];
    }
}

// config/coinpay.php
'webhook_service' => \App\Services\MyCustomWebhookService::class,
bash
php artisan vendor:publish --tag=coinpay-config