PHP code example of kodemeric / payment-gateways

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

    

kodemeric / payment-gateways example snippets


return [
    'default_gateway' => 'paystack',
    
    'gateways' => [
        'paystack' => [
            'test_mode' => env('PAYSTACK_TEST_MODE', true),
            'public_key' => env('PAYSTACK_PUBLIC_KEY', ''),
            'secret_key' => env('PAYSTACK_SECRET_KEY', ''),
        ],
        // ... other gateways
    ],
];

use SoftPayment\PaymentGateways\Facades\PaymentGateway;

// Using default gateway
$gateway = PaymentGateway::default();

// Or specify a gateway
$gateway = PaymentGateway::gateway('paystack');

use SoftPayment\PaymentGateways\Facades\PaymentGateway;
use SoftPayment\PaymentGateways\Dtos\InitiateTransactionRequest;

// Create request using DTO
$request = InitiateTransactionRequest::fromArray([
    'amount' => 1000, // Amount in base currency
    'email' => '[email protected]',
    'reference' => 'unique_ref_123', // Optional, auto-generated if not provided
    'callback_url' => 'https://yoursite.com/callback', // Optional
    'user_id' => auth()->id(), // Optional, track which user initiated the transaction
    'metadata' => ['order_id' => 123], // Optional
]);

// Or create directly
$request = new InitiateTransactionRequest(
    amount: 1000,
    email: '[email protected]',
    reference: 'unique_ref_123',
    callbackUrl: 'https://yoursite.com/callback',
    userId: auth()->id(), // Track the authenticated user
    metadata: ['order_id' => 123]
);

$result = PaymentGateway::gateway('paystack')->initiateTransaction($request);

if ($result->success) {
    return redirect($result->paymentUrl);
}

use SoftPayment\PaymentGateways\Dtos\VerifyTransactionResponse;

$result = PaymentGateway::gateway('paystack')->verifyTransaction('unique_ref_123');

if ($result->success && $result->status === 'success') {
    // Transaction successful
    $amount = $result->amount;
    $currency = $result->currency;
}

use SoftPayment\PaymentGateways\PaymentGatewayManager;
use SoftPayment\PaymentGateways\Dtos\InitiateTransactionRequest;

class PaymentController extends Controller
{
    public function __construct(
        protected PaymentGatewayManager $paymentManager
    ) {}
    
    public function pay(Request $request)
    {
        $gateway = $this->paymentManager->gateway('paystack');
        
        $transactionRequest = InitiateTransactionRequest::fromArray([
            'amount' => $request->amount,
            'email' => $request->email,
            'user_id' => auth()->id(), // Track the authenticated user
            'metadata' => ['order_id' => $request->order_id],
        ]);
        
        $result = $gateway->initiateTransaction($transactionRequest);
        
        if ($result->success) {
            return redirect($result->paymentUrl);
        }
    }
}

use SoftPayment\PaymentGateways\Models\Transaction;

$transaction = Transaction::where('reference', 'ref_123')->first();
$logs = $transaction->logs; // Get related transaction logs

// Get user's transactions
$userTransactions = Transaction::where('user_id', auth()->id())->get();

// Get the user who made the transaction
$user = $transaction->user;

use SoftPayment\PaymentGateways\Models\TransactionLog;

$logs = TransactionLog::where('gateway', 'paystack')->get();

use SoftPayment\PaymentGateways\Dtos\InitiateTransactionRequest;

// Using fromArray (recommended)
$request = InitiateTransactionRequest::fromArray([
    'amount' => 5000.00,
    'email' => '[email protected]',
    'currency' => 'NGN',
    'customer_name' => 'John Doe',
    'description' => 'Payment for Order #12345',
    'reference' => 'ORDER_12345',
    'callback_url' => route('payment.callback'),
    'user_id' => auth()->id(),
    'metadata' => [
        'order_id' => 12345,
        'product_id' => 789,
    ],
]);

// Using constructor
$request = new InitiateTransactionRequest(
    amount: 5000.00,
    email: '[email protected]',
    currency: 'NGN',
    customerName: 'John Doe',
    description: 'Payment for Order #12345',
    reference: 'ORDER_12345',
    callbackUrl: route('payment.callback'),
    userId: auth()->id(),
    metadata: ['order_id' => 12345]
);

$response = $gateway->initiateTransaction($request);

if ($response->success) {
    // Redirect customer to payment page
    return redirect($response->paymentUrl);
    
    // Or get transaction details
    $reference = $response->reference; // 'ref_abc123'
    $accessCode = $response->accessCode; // 'ACC_xyz789' (if applicable)
}

if (!$response->success) {
    $errorMessage = $response->message; // 'Failed to initialize transaction'
    $gatewayError = $response->gatewayResponse; // Raw gateway error details
}

use SoftPayment\PaymentGateways\Dtos\VerifyTransactionResponse;

$response = $gateway->verifyTransaction('ref_abc123');

if ($response->success) {
    if ($response->status === 'success') {
        // Transaction was successful
        $amount = $response->amount; // 5000.00
        $currency = $response->currency; // 'NGN'
        
        // Process successful payment
        // ...
    } elseif ($response->status === 'pending') {
        // Payment is still pending
    } else {
        // Payment failed
    }
} else {
    // Verification failed (network error, invalid reference, etc.)
    $errorMessage = $response->message;
}

use SoftPayment\PaymentGateways\Dtos\WebhookPayload;

// In your webhook handler
public function handleWebhook(Request $request)
{
    // WebhookPayload automatically parses different gateway formats
    $payload = WebhookPayload::fromArray($request->all());
    
    // Get the transaction reference (works across all gateways)
    $reference = $payload->getReference();
    
    // Get event type
    $event = $payload->event ?? $payload->eventType;
    
    // Get full payload data
    $data = $payload->data;
}

use SoftPayment\PaymentGateways\Dtos\WebhookPayload;
use SoftPayment\PaymentGateways\Dtos\WebhookResponse;

// In WebhookController (handled automatically by the package)
$payload = WebhookPayload::fromArray($request->all());
$response = $gateway->handleWebhook($payload);

// Response is automatically returned as JSON
// Success: 200 status with response data
// Failure: 400 status with error message
bash
php artisan payment-gateways:install
bash
php artisan payment-gateways:publish
bash
php artisan payment-gateways:migrate
bash
php artisan migrate
bash
php artisan payment-gateways:install
bash
php artisan payment-gateways:publish
bash
php artisan payment-gateways:publish --force
bash
php artisan payment-gateways:migrate
bash
php artisan payment-gateways:migrate --force