PHP code example of richnessagency / rich-payments

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

    

richnessagency / rich-payments example snippets


return [
    'route_prefix' => 'payments',
    'admin_route_prefix' => 'admin/rich-payments',
    'public_payment_start_enabled' => true,

    'middleware' => [
        'checkout' => ['web'],
        'admin' => ['web', 'admin.placeholder', 'permission:payments.view'],
        'admin_manage' => ['permission:payments.manage'],
        'admin_transactions' => ['permission:payments.transactions'],
        'webhook' => ['api'],
    ],

    'default_currency' => 'EGP',
    'default_gateway' => 'paymob',
    'response_redirect_route' => null,
    'response_redirect_parameter' => 'order',
    'response_verified_reference_session_key' => null,
];

use App\Models\Order;
use Richness\RichPayments\Data\PaymentRequest;
use Richness\RichPayments\Models\PaymentGateway;
use Richness\RichPayments\RichPayments;

$gateway = PaymentGateway::query()
    ->where('code', 'paymob')
    ->where('active', true)
    ->firstOrFail();

$order = Order::query()->findOrFail($orderId);

$session = app(RichPayments::class)->start(
    gateway: $gateway,
    request: new PaymentRequest(
        amountMinor: 150000,
        currency: 'EGP',
        merchantReference: (string) $order->getKey(),
        methodCode: 'cards',
        items: [
            ['name' => 'Order #' . $order->getKey(), 'amount_minor' => 150000, 'quantity' => 1],
        ],
        customer: [
            'name' => $order->customer_name,
            'email' => $order->customer_email,
            'phone' => $order->customer_phone,
        ],
        metadata: [
            'order_id' => $order->getKey(),
        ],
        notificationUrl: route('rich-payments.webhook', ['gateway' => 'paymob']),
        redirectionUrl: route('rich-payments.response', ['gateway' => 'paymob']),
    ),
    payable: $order,
);

return redirect()->away($session->checkoutUrl);

'public_payment_start_enabled' => true,

'response_redirect_route' => 'orders.show',
'response_redirect_parameter' => 'order',
'response_verified_reference_session_key' => 'verified_payment_references',

route('orders.show', ['order' => $merchantReference])

use Richness\RichPayments\Contracts\ManagesTransactions;

$driver = app(\Richness\RichPayments\Gateways\GatewayManager::class)->driver($gateway);

if ($driver instanceof ManagesTransactions) {
    $result = $driver->refund($gateway, $transactionId, 50000);
}



declare(strict_types=1);

namespace App\Payments\Gateways\Stripe;

use Illuminate\Http\Request;
use Richness\RichPayments\Contracts\PaymentGatewayDriver;
use Richness\RichPayments\Contracts\SupportsConnectionTest;
use Richness\RichPayments\Data\ConnectionResult;
use Richness\RichPayments\Data\InquiryResult;
use Richness\RichPayments\Data\PaymentRequest;
use Richness\RichPayments\Data\PaymentSession;
use Richness\RichPayments\Data\WebhookResult;
use Richness\RichPayments\Enums\PaymentStatus;
use Richness\RichPayments\Models\PaymentGateway;

final class StripeGateway implements PaymentGatewayDriver, SupportsConnectionTest
{
    public function createSession(PaymentGateway $gateway, PaymentRequest $request): PaymentSession
    {
        // Read encrypted credentials, call the gateway API, and return checkout data.
        return new PaymentSession(
            gatewayCode: $gateway->code,
            status: PaymentStatus::Redirected->value,
            externalReference: 'gateway-session-id',
            checkoutUrl: 'https://checkout.example/session',
            payload: [],
        );
    }

    public function handleWebhook(PaymentGateway $gateway, Request $request): WebhookResult
    {
        // Verify signature, normalize status, and return a gateway-independent result.
        return new WebhookResult(
            verified: true,
            success: true,
            status: PaymentStatus::Paid->value,
            merchantReference: 'order-123',
            externalTransactionId: 'txn-123',
            paidAmountMinor: 150000,
            currency: 'EGP',
            payload: $request->all(),
        );
    }

    public function inquire(PaymentGateway $gateway, string $externalTransactionId): InquiryResult
    {
        // Return normalized inquiry status from the gateway.
    }

    public function checkoutUrl(PaymentGateway $gateway, string $clientSecret): string
    {
        return 'https://checkout.example/' . $clientSecret;
    }

    public function testConnection(PaymentGateway $gateway): ConnectionResult
    {
        // Call a lightweight authenticated gateway endpoint.
    }
}

'gateways' => [
    'paymob' => [
        'driver' => \Richness\RichPayments\Gateways\Paymob\PaymobGateway::class,
        'name' => 'Paymob',
    ],

    'stripe' => [
        'driver' => \App\Payments\Gateways\Stripe\StripeGateway::class,
        'name' => 'Stripe',
        'base_url' => env('STRIPE_BASE_URL', 'https://api.stripe.com'),
    ],
],
bash
php artisan migrate
bash
php artisan db:seed --class="Richness\\RichPayments\\Database\\Seeders\\RichPaymentsPaymobSeeder"
bash
php artisan vendor:publish --tag=rich-payments-config
php artisan vendor:publish --tag=rich-payments-views
php artisan vendor:publish --tag=rich-payments-migrations
text
/admin/rich-payments/gateways
bash
php artisan vendor:publish --tag=rich-payments-views
text
POST /payments/{gateway}/webhook