PHP code example of gonon / laravel-tripay

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

    

gonon / laravel-tripay example snippets


use Gonon\Tripay\Laravel\Facades\Tripay;
use Gonon\Tripay\DTO\CreateTransactionRequest;

// Get all available payment channels
$channels = Tripay::paymentChannels()->list();

foreach ($channels as $channel) {
    echo $channel->code; // e.g., 'BRIVA', 'OVO'
    echo $channel->name;
    echo $channel->isActive ? 'Active' : 'Inactive';
}

$payload = new CreateTransactionRequest(
    method: 'BRIVA',
    merchantRef: 'INV-12345',
    amount: 150000,
    customerName: 'John Doe',
    customerEmail: '[email protected]',
    customerPhone: '08123456789',
    orderItems: [
        [
            'sku'      => 'PROD-01',
            'name'     => 'Premium Package',
            'price'    => 150000,
            'quantity' => 1,
        ]
    ],
    returnUrl: route('payment.return')
);

try {
    $transaction = Tripay::transactions()->create($payload);
    
    // Redirect customer to the Tripay checkout page
    return redirect($transaction->checkoutUrl);
    
} catch (\Gonon\Tripay\Exceptions\TransactionException $e) {
    return back()->withError($e->getMessage());
}

$transaction = Tripay::transactions()->detail('INV-12345');

echo $transaction->status; // 'PAID', 'UNPAID', 'EXPIRED', 'FAILED'
echo $transaction->amount;
echo $transaction->checkoutUrl;

use Gonon\Tripay\DTO\FeeCalculationRequest;

$fees = Tripay::calculator()->calculate(
    new FeeCalculationRequest(amount: 150000, code: 'BRIVA')
);

echo $fees->totalFee;
echo $fees->feeMerchant;
echo $fees->feeCustomer;

use Illuminate\Http\Request;

public function handleWebhook(Request $request)
{
    $signature = $request->header('X-Callback-Signature');
    $payload = $request->getContent();

    // The webhook handler automatically uses your configured private key
    $isValid = Tripay::webhook()->verifySignature($signature, $payload);

    if (! $isValid) {
        abort(403, 'Invalid signature');
    }

    $event = json_decode($payload);

    if ($event->status === 'PAID') {
        // Update order status in your database
    }

    return response()->json(['success' => true]);
}

use Gonon\Tripay\Client\TripayClient;

class CheckoutController
{
    public function __construct(
        private readonly TripayClient $tripay
    ) {}

    public function process()
    {
        $channels = $this->tripay->paymentChannels()->all();
    }
}

$tripay = app(\Gonon\Tripay\Client\TripayClient::class);
// or via the bound alias
$tripay = app('tripay');
bash
php artisan vendor:publish --tag="tripay-config"
bash
php artisan vendor:publish --provider="Gonon\Tripay\Laravel\TripayServiceProvider"