1. Go to this page and download the library: Download sarfarazstark/laravel-payu 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/ */
sarfarazstark / laravel-payu example snippets
use App\Models\PayUTransaction;
use App\Models\PayURefund;
use App\Models\PayUWebhook;
// Using published models works exactly the same as package models
$transaction = PayUTransaction::where('txnid', 'TXN123')->first();
$refunds = PayURefund::successful()->get();
$webhooks = PayUWebhook::verified()->recent()->get();
// Validate UPI ID
$upiValidation = PayU::validateUpi(['vpa' => 'user@paytm']);
// Check netbanking status
$netbankingStatus = PayU::getNetbankingStatus();
// Get issuing bank status
$bankStatus = PayU::getIssuingBankStatus();
// Daily transaction summary
$dailyStats = PayUTransaction::selectRaw('
DATE(created_at) as date,
COUNT(*) as total_transactions,
SUM(CASE WHEN status = "success" THEN 1 ELSE 0 END) as successful,
SUM(CASE WHEN status = "success" THEN amount ELSE 0 END) as revenue,
AVG(CASE WHEN status = "success" THEN amount ELSE NULL END) as avg_amount
')
->groupBy('date')
->orderBy('date', 'desc')
->limit(30)
->get();
// Payment mode performance
$modePerformance = PayUTransaction::selectRaw('
payment_mode,
COUNT(*) as transactions,
SUM(amount) as total_amount,
AVG(amount) as avg_amount,
SUM(CASE WHEN status = "success" THEN 1 ELSE 0 END) / COUNT(*) * 100 as success_rate
')
->whereNotNull('payment_mode')
->groupBy('payment_mode')
->get();
// Failed transaction analysis
$failureReasons = PayUTransaction::failed()
->selectRaw('error, COUNT(*) as count')
->groupBy('error')
->orderBy('count', 'desc')
->get();
// Monthly refund trends
$refundTrends = PayURefund::selectRaw('
YEAR(created_at) as year,
MONTH(created_at) as month,
COUNT(*) as refund_requests,
SUM(CASE WHEN status = "success" THEN amount ELSE 0 END) as refunded_amount,
AVG(TIMESTAMPDIFF(HOUR, refund_requested_at, refund_processed_at)) as avg_processing_hours
')
->groupBy('year', 'month')
->orderBy('year', 'desc')
->orderBy('month', 'desc')
->get();
use SarfarazStark\LaravelPayU\Models\PayUTransaction;
use Tests\TestCase;
class PaymentTest extends TestCase
{
public function testTransactionCreation()
{
$transaction = PayUTransaction::create([
'txnid' => 'TEST123',
'amount' => 100.00,
'status' => PayUTransaction::STATUS_PENDING,
// ... other fields
]);
$this->assertDatabaseHas('payu_transactions', [
'txnid' => 'TEST123',
'status' => PayUTransaction::STATUS_PENDING
]);
}
}
// Always verify hash in success/failure callbacks
if (!PayU::verifyHash($request->all())) {
// Handle invalid hash - possible tampering
return response()->json(['error' => 'Invalid hash'], 400);
}
// Use different credentials for staging/production
// .env.staging
PAYU_ENV_PROD=false
PAYU_KEY=test_key
PAYU_SALT=test_salt
// .env.production
PAYU_ENV_PROD=true
PAYU_KEY=live_key
PAYU_SALT=live_salt
// Use fillable arrays to prevent mass assignment
// Models already
// Add database indexes for better query performance
// Indexes are already
// - Payment date range queries
// Use eager loading to prevent N+1 queries
$transactions = PayUTransaction::with(['refunds', 'webhooks'])
->successful()
->paginate(50);