PHP code example of xenon / laravel-paystation

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

    

xenon / laravel-paystation example snippets


use Xenon\LaravelPaystation\Facades\Paystation;

class CheckoutController
{
    public function pay(Order $order)
    {
        return Paystation::pay([
            'invoice_number' => $order->invoice_number,
            'payment_amount' => $order->total,
            'cust_name'      => $order->customer_name,
            'cust_phone'     => $order->customer_phone,
            'cust_email'     => $order->customer_email,
        ]);
    }
}

$session = Paystation::initiate($payload);

$order->update(['invoice_number' => $session->invoiceNumber]);

return response()->json(['checkout_url' => $session->paymentUrl]);

use Xenon\LaravelPaystation\Data\PaymentPayload;

$payload = PaymentPayload::make($fields)
    ->customerBearsCharge()   // pay_with_charge = 1; false => the merchant bears it
    ->withEmi()               // emi = 1
    ->set('checkout_items', $order->summary());

return Paystation::pay($payload);

use Xenon\LaravelPaystation\Exceptions\PaymentCreationException;

try {
    return Paystation::pay($payload);
} catch (PaymentCreationException $e) {
    if ($e->isDuplicateInvoice()) {
        return $this->resumeExistingPayment($order);   // look it up, do not re-create
    }

    throw $e;
}

// v1 -- by invoice number, the reference you always have
$result = Paystation::transactionStatus($order->invoice_number);

// v2 -- by the gateway's own transaction id, once a payment has been attempted
$result = Paystation::transactionStatusV2($trxId);

if (! $result->found()) {
    return back()->withErrors($result->describeStatusCode());
}

if ($result->isPending()) {
    return back()->with('status', 'Payment still in progress, check again shortly.');
}

if (! $result->isPaid()) {
    return back()->withErrors('Payment did not succeed: '.$result->transactionStatus());
}

$order->markPaid(
    transactionId: $result->transactionId(),
    method: $result->paymentMethod(),
    amount: $result->amount(),
);

use Xenon\LaravelPaystation\Ipn\Events\IpnReceived;

class FulfilOrder
{
    public function handle(IpnReceived $event): void
    {
        $ipn = $event->payload;

        $order = Order::where('invoice_number', $ipn->invoiceNumber())->first();

        if (! $order || ! $ipn->matchesAmount($order->total)) {
            return;
        }

        // idempotency is yours: a notification can arrive more than once
        if ($order->isPaid()) {
            return;
        }

        $order->markPaid($ipn->trxId(), $ipn->paymentMethod(), $ipn->orderDateTimeString());
    }
}

Paystation::environment('live')->pay($payload);

Paystation::withConfig([
    'merchant_id' => config('services.paystation.second_merchant'),
    'password'    => config('services.paystation.second_password'),
])->pay($payload);

Http::fake([
    '*/initiate-payment' => Http::response([
        'status_code' => '200',
        'status'      => 'success',
        'payment_url' => 'https://sandbox.paystation.com.bd/checkout/test',
    ]),
]);

$this->assertSame(
    'https://sandbox.paystation.com.bd/checkout/test',
    Paystation::pay($payload)->getTargetUrl()
);
bash
php artisan vendor:publish --tag=paystation-config