PHP code example of bd-payments / laravel-payment-gateway
1. Go to this page and download the library: Download bd-payments/laravel-payment-gateway 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/ */
bd-payments / laravel-payment-gateway example snippets
use BDPayments\LaravelPaymentGateway\Facades\PaymentGateway;
// Initialize a payment with security features
$response = PaymentGateway::initializePayment('nagad', [
'order_id' => 'ORDER123',
'amount' => 100.50,
'currency' => 'BDT',
'callback_url' => 'https://yourdomain.com/callback',
'payment_hash' => PaymentGateway::generatePaymentHash([
'order_id' => 'ORDER123',
'amount' => 100.50,
'currency' => 'BDT',
]),
]);
if ($response->success) {
// Redirect to payment gateway
return redirect($response->redirectUrl);
} else {
// Handle error
return back()->with('error', $response->message);
}
use BDPayments\LaravelPaymentGateway\Services\PaymentGatewayService;
class PaymentController extends Controller
{
public function __construct(
private readonly PaymentGatewayService $paymentService
) {}
public function processPayment(Request $request)
{
$response = $this->paymentService->initializePayment('nagad', [
'order_id' => $request->order_id,
'amount' => $request->amount,
'currency' => 'BDT',
]);
return response()->json($response->toArray());
}
}
use BDPayments\LaravelPaymentGateway\Models\Payment;
// Create a payment record
$payment = Payment::create([
'user_id' => auth()->id(),
'order_id' => 'ORDER123',
'gateway' => 'nagad',
'amount' => 100.50,
'currency' => 'BDT',
'status' => 'pending',
]);
// Check payment status
if ($payment->isPending()) {
// Payment is still pending
}
// Mark as completed
$payment->markAsCompleted($gatewayResponse);
// Check if refundable
if ($payment->canBeRefunded()) {
// Process refund
}
use BDPayments\LaravelPaymentGateway\Services\AIAgentService;
$aiAgent = app(AIAgentService::class);
// Analyze payment patterns and detect anomalies
$analysis = $aiAgent->analyzePaymentPatterns($payment);
// Generate intelligent notifications
$aiAgent->generateNotifications($payment, $analysis);
// Provide customer support
$support = $aiAgent->provideCustomerSupport(
'I need help with my payment',
['payment_id' => $payment->id]
);
// Auto-resolve common problems
$resolved = $aiAgent->autoResolveProblems();
// Generate payment insights
$insights = $aiAgent->generateInsights();
// Suggest optimal gateway
$optimalGateway = $aiAgent->suggestOptimalGateway($paymentData);
// Predict payment failure
$prediction = $aiAgent->predictPaymentFailure($payment);
// Get refund recommendations
$refundStrategy = $aiAgent->recommendRefundStrategy($payment);
use BDPayments\LaravelPaymentGateway\Models\PaymentProblem;
// Get payment problems
$problems = PaymentProblem::open()->critical()->get();
// Assign problem to admin
$problem->assignTo($adminId);
// Resolve problem
$problem->markAsResolved($adminId, 'Issue resolved by contacting gateway support');
// Add comments to problems
$problem->comments()->create([
'user_id' => auth()->id(),
'comment' => 'Working on this issue',
'is_internal' => true,
]);
use BDPayments\LaravelPaymentGateway\Models\Payment;
// Create payment
$payment = Payment::create($data);
// Status checks
$payment->isPending();
$payment->isCompleted();
$payment->isFailed();
$payment->isRefunded();
$payment->isExpired();
// Status updates
$payment->markAsCompleted($gatewayResponse);
$payment->markAsFailed($gatewayResponse);
$payment->markAsCancelled();
// Refund operations
$payment->canBeRefunded();
$payment->getTotalRefundedAmount();
$payment->getRemainingRefundableAmount();
$payment->updateRefundedAmount();
// Payment form
GET /payment/form
// Initialize payment
POST /payment/initialize
// Gateway-specific operations
POST /payment/{gateway}/verify
POST /payment/{gateway}/refund
GET /payment/{gateway}/status
GET /payment/{gateway}/callback
POST /payment/{gateway}/webhook
// Result pages
GET /payment/success
GET /payment/failed
// API routes (with api middleware)
POST /api/payment/initialize
POST /api/payment/{gateway}/verify
POST /api/payment/{gateway}/refund
GET /api/payment/{gateway}/status
POST /api/payment/{gateway}/webhook
use BDPayments\LaravelPaymentGateway\Services\PaymentLogger;
$logger = app(PaymentLogger::class);
// Get all logs
$logs = $logger->getLogs();
// Get logs by gateway
$nagadLogs = $logger->getLogsByGateway('nagad');
// Get logs by payment ID
$paymentLogs = $logger->getLogsByPaymentId('PAYMENT123');
// Export logs
$logger->exportToFile('payment_logs.json');