PHP code example of tomise / laravel-barion

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

    

tomise / laravel-barion example snippets


use Tomise\Barion\DataTransferObjects\PaymentTransactionDto;
use Tomise\Barion\DataTransferObjects\TransactionItemDto;
use Tomise\Barion\Support\BarionGateway;

$payment = BarionGateway::createPaymentGateway()->startPaymentManual();

$payment->getPaymentData()
    ->setPaymentRequestId('ORDER-1001')      // your unique id (a UUID is generated when missing)
    ->setOrderNumber('ORDER-1001')
    ->setPayerHint('[email protected]')
    ->setPayerPhoneNumber('+36 30 123 4567') // sent as 36301234567
    ->addTransaction(
        (new PaymentTransactionDto)
            ->setPostTransactionId('ORDER-1001')
            ->setPayee(config('barion-gateway.payee'))
            ->setTotal(16500)
            ->setItems([
                (new TransactionItemDto)
                    ->setName('Sports massage')
                    ->setDescription('60 minutes')
                    ->setQuantity(1)
                    ->setUnit('pcs')
                    ->setUnitPrice(16500),   // ItemTotal defaults to quantity × unit price
            ])
    );

$response = $payment->sendSinglePayment();

// Store these with the order: the callback and the refund need them.
$response->getPaymentId();
$response->getTransactionId();

return redirect($response->getGatewayUrl());

class Order extends Model
{
    public $barion_casts = [
        'total' => 'total_price',            //  'email',
        'order_number' => 'reference',
        'phone_number' => 'phone',
        // 'card_holder_name_hint' => 'name',
        // 'payee' => 'payee_email',         // defaults to BARION_PAYEE
    ];
}

class OrderItem extends Model
{
    public $barion_casts = [
        'name' => 'name',
        'description' => 'description',
        'quantity' => 'quantity',
        'unit' => 'unit',
        'unit_price' => 'unit_price',
        'item_total' => 'item_total',
    ];
}

$payment = BarionGateway::createPaymentGateway()->startPayment($order, $order->items);
// Extra data before sending, e.g. $payment->getPaymentData()->setBillingAddress($address);
$response = $payment->sendSinglePayment();

$state = BarionGateway::createPaymentGateway()->startPaymentManual()->sendPaymentState($request->input('paymentId'));

$state->isSucceeded();              // Status === Succeeded
$state->status;                     // BarionStatus enum
$state->paymentRequestId;           // your id
$state->transaction('ORDER-1001');  // the customer's transaction of a POSTransactionId (no fees, no reversed ones)
$state->completedAt;                // DateTimeImmutable

use Tomise\Barion\DataTransferObjects\TransactionToRefundDto;

$response = BarionGateway::createPaymentGateway()->startPaymentManual()->sendRefund(
    $paymentId,
    [new TransactionToRefundDto($transactionId, 'ORDER-1001', 5000, 'Partial refund')],
    idempotencyKey: 'refund-ORDER-1001-1', // a retried request with the same key is refunded only once (sent as a GUID)
);

$response->refundedTransactions; // Collection of RefundedTransactionDto

use Tomise\Barion\DataTransferObjects\TransactionToFinishDto;
use Tomise\Barion\Enums\PaymentType;

$payment = BarionGateway::createPaymentGateway()->startPaymentManual();
$payment->getPaymentData()->setPaymentType(PaymentType::Reservation)->setReservationPeriod('3.00:00:00') /* ... */;
$response = $payment->sendSinglePayment();

// later
BarionGateway::createPaymentGateway()->startPaymentManual()
    ->sendFinishReservation($paymentId, [new TransactionToFinishDto($transactionId, 12000)]);

$payment->getPaymentData()->setPaymentType(PaymentType::DelayedCapture) /* ... */;

// charge (at most the authorized amount)
$client->sendCapture($paymentId, [new TransactionToFinishDto($transactionId, 16500, 'Treatment done')]);

// or release without charging
$client->sendCancelAuthorization($paymentId);

use Tomise\Barion\Enums\RecurrenceType;

// 1. Registration, by the customer (here without charging anything)
$payment->getPaymentData()
    ->setInitiateRecurrence(true)
    ->setRecurrenceId('guest-42-'.Str::uuid())  // unique per registration; store it
    ->setRecurrenceType(RecurrenceType::MerchantInitiatedPayment);

// 2. In the callback: store the TraceId (and the card for display)
$state = $client->sendPaymentState($paymentId);
$state->traceId;                       // essful, Failed, NotFound, ThreeDSAuthenticationRequired (TraceId missing)

$client = BarionGateway::createPaymentGateway()->startPaymentManual()->setPaymentId($paymentId);
$client->sendCancelAuthorization();

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tomise\Barion\Adapters\BarionAdapter;

$mock = new MockHandler([new Response(200, [], json_encode(['PaymentId' => 'test-payment', 'Status' => 'Prepared', 'Transactions' => []]))]);
$this->app->instance(BarionAdapter::class, new BarionAdapter(new Client(['handler' => HandlerStack::create($mock), 'http_errors' => false])));
bash
php artisan vendor:publish --provider="Tomise\Barion\Providers\BarionServiceProvider" --tag="config"