PHP code example of qbe-digital / payments

1. Go to this page and download the library: Download qbe-digital/payments 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/ */

    

qbe-digital / payments example snippets


use QBE\Payments\Facades\Payments;
use QBE\Payments\Support\CheckoutSession;
use QBE\Payments\Support\CustomerInfo;

$session = new CheckoutSession(
    amount: 500.00,
    currency: 'SAR',
    referenceId: 'order-123',
    description: 'Premium Package',
    successUrl: route('payment.success'),
    failureUrl: route('payment.failure'),
    customer: CustomerInfo::make('Ahmed', '[email protected]', '+966500000000'),
);

// Use default driver
$result = Payments::createCheckout($session);

// Or specify a driver
$result = Payments::driver('tabby')->createCheckout($session);

if ($result->

$result = Payments::verify($transactionId);

if ($result->isSuccessful()) {
    // Payment confirmed
}

$refund = Payments::refund($transactionId, 100.00, 'Customer requested');

// Pass the original transaction currency for multi-currency correctness:
$refund = Payments::refund($transactionId, 100.00, 'Customer requested', 'AED');

if ($refund->isSuccessful()) {
    // Refund processed
}

use QBE\Payments\Traits\Billable;
use QBE\Payments\Support\CustomerInfo;

class Order extends Model
{
    use Billable;

    public function getPaymentAmount(): float
    {
        return $this->total;
    }

    public function getPaymentReference(): string
    {
        return "order-{$this->id}";
    }

    public function getPaymentCustomer(): ?CustomerInfo
    {
        return CustomerInfo::make(
            $this->customer_name,
            $this->customer_email,
            $this->customer_phone,
        );
    }
}

// Then simply:
$result = $order->pay();            // Default driver
$result = $order->pay('tamara');     // Specific driver
$order->verifyPayment($transactionId);
$order->refundPayment($transactionId, 50.00);

use QBE\Payments\Events\PaymentSucceeded;
use QBE\Payments\Events\PaymentFailed;
use QBE\Payments\Events\WebhookReceived;
use QBE\Payments\Events\RefundProcessed;

protected $listen = [
    PaymentSucceeded::class => [
        UpdateOrderStatus::class,
        SendPaymentConfirmation::class,
    ],
    PaymentFailed::class => [
        HandleFailedPayment::class,
    ],
    WebhookReceived::class => [
        LogWebhook::class,
    ],
];

use QBE\Payments\Models\PaymentTransaction;

$transactions = $order->payments;            // if you add the morph relation
$paid = PaymentTransaction::paid()->get();   // built-in scopes: paid(), pending()

'transactions' => [
    'enabled' => env('PAYMENTS_RECORD_TRANSACTIONS', true),
    'model'   => \App\Models\PaymentTransaction::class,
],

use QBE\Payments\Contracts\SupportsInstallmentsInterface;

$driver = Payments::driver('tabby');

if ($driver instanceof SupportsInstallmentsInterface) {
    $plans = $driver->getInstallmentPlans(1000.00);
    // Returns available installment plans
}

use QBE\Payments\Enums\PaymentFeature;

if (Payments::driver('tamara')->supports(PaymentFeature::PARTIAL_REFUNDS->value)) {
    // Tamara supports partial refunds
}

use QBE\Payments\Drivers\AbstractDriver;

class StripeDriver extends AbstractDriver
{
    public function driverName(): string { return 'stripe'; }
    protected function baseUrl(): string { return 'https://api.stripe.com/v1/'; }
    protected function defaultHeaders(): array { /* ... */ }
    protected function supportedFeatures(): array { /* ... */ }
    public function createCheckout(CheckoutSession $session): PaymentResult { /* ... */ }
    public function verify(string $transactionId): PaymentResult { /* ... */ }
    public function refund(string $transactionId, float $amount, string $reason = ''): RefundResult { /* ... */ }
}

use QBE\Payments\PaymentManager;

app(PaymentManager::class)->extend('stripe', function () {
    return new StripeDriver(config('payments.drivers.stripe'));
});
bash
php artisan vendor:publish --tag=payments-config
bash
php artisan vendor:publish --tag=payments-migrations
php artisan migrate

POST /api/payments/webhook/tabby
POST /api/payments/webhook/tamara