PHP code example of vega-pay / laravel

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

    

vega-pay / laravel example snippets


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use VegaPay\Laravel\Facades\VegaPay;

Route::post('/checkout', function (Request $request) {
    $payment = VegaPay::createPayment([
        'amount' => $request->integer('amount'),
        'currency' => 'XOF',
        'orderId' => $request->string('order_id')->toString(),
        'customerPhone' => $request->string('phone')->toString(),
        'successUrl' => route('payment.success'),
        'errorUrl' => route('payment.error'),
        'metadata' => [
            'source' => 'laravel',
        ],
    ], idempotencyKey: $request->string('order_id')->toString());

    return redirect()->away($payment['checkoutUrl']);
});

$payment = VegaPay::retrievePayment('pay_...');

if ($payment['status'] === 'succeeded') {
    // Livrer la commande.
}

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use VegaPay\Laravel\Facades\VegaPay;
use VegaPay\Laravel\Exceptions\VegaPaySignatureVerificationException;

Route::post('/webhooks/vega-pay', function (Request $request) {
    try {
        $event = VegaPay::constructWebhookEvent(
            $request->getContent(),
            $request->header('X-Pay-Signature', '')
        );
    } catch (VegaPaySignatureVerificationException) {
        abort(400);
    }

    if ($event['type'] === 'payment.succeeded') {
        $payment = $event['data']['payment'];
        // Marquer la commande comme payee.
    }

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

use VegaPay\Laravel\Exceptions\VegaPayException;

try {
    $payment = VegaPay::createPayment([
        'amount' => 10000,
        'currency' => 'XOF',
        'orderId' => 'CMD-10001',
    ], idempotencyKey: 'CMD-10001');
} catch (VegaPayException $exception) {
    report($exception);

    return back()->withErrors([
        'payment' => $exception->getMessage(),
    ]);
}
bash
php artisan vendor:publish --tag=vega-pay-config