PHP code example of basgate / laravel-payment-sdk

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

    

basgate / laravel-payment-sdk example snippets


use Bas\LaravelPayment\Facades\BasPaymentFacade as BasPayment;

// Initiate a payment transaction
$orderId = 'ORDER_' . time();
$amount = 100.00;
$currency = 'YER';

$payment = BasPayment::initiateTransaction($orderId, $amount, $currency);

if ($payment['status'] === 1 && $payment['code'] === '1111') {
    $trxToken = $payment['body']['trxToken'];
    // Store trxToken for later reference
}

use Bas\LaravelPayment\Facades\BasPaymentFacade as BasPayment;

$orderId = 'ORDER_123';
$status = BasPayment::checkTransactionStatus($orderId);

if ($status['body']['transaction_status'] === 'SUCCESS') {
    // Payment successful - update order status
} elseif ($status['body']['transaction_status'] === 'FAILED') {
    // Payment failed - handle accordingly
}

use Bas\LaravelPayment\Facades\BasPaymentFacade as BasPayment;

$trxToken = 'your_transaction_token';
$reason = 'Customer requested refund';

$refund = BasPayment::refund($trxToken, $reason);

if ($refund['status'] === 1) {
    // Refund processed successfully
}

use Bas\LaravelPayment\Facades\BasPaymentFacade as BasPayment;

$orderId = 'ORDER_123';
$message = 'Your payment has been received successfully';


[
    'status' => 1,  // 1 = success, 0 = failure
    'code' => '1111',  // Response code
    'head' => [
        'responseCode' => 'string',
        'responseMessage' => 'string',
        'signature' => 'string',  // For secured endpoints
    ],
    'body' => [
        // Response data
    ]
]



namespace App\Http\Controllers;

use Bas\LaravelPayment\Facades\BasPaymentFacade as BasPayment;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    public function initiatePayment(Request $request)
    {
        try {
            $orderId = 'ORDER_' . time();
            $amount = $request->input('amount');
            
            // Initiate transaction
            $payment = BasPayment::initiateTransaction($orderId, $amount, 'YER');
            
            if ($payment['status'] === 1 && $payment['code'] === '1111') {
                // Store payment details in database
                // Redirect customer to payment page or return trxToken
                
                return response()->json([
                    'success' => true,
                    'orderId' => $orderId,
                    'trxToken' => $payment['body']['trxToken']
                ]);
            }
            
            return response()->json([
                'success' => false,
                'message' => 'Payment initiation failed'
            ], 400);
            
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }
    
    public function checkStatus($orderId)
    {
        try {
            $status = BasPayment::checkTransactionStatus($orderId);
            
            if ($status['body']['transaction_status'] === 'SUCCESS') {
                // Update order as paid in database
                // Send confirmation to customer via your preferred method
            }
            
            return response()->json($status);
            
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }
}