Installation Add the package via Composer:
composer require ekipower/nganluong
Publish the configuration file:
php artisan vendor:publish --provider="Ekipower\Nganluong\NganluongServiceProvider"
Configuration
Locate the published config at config/nganluong.php and set your API credentials:
'api' => [
'key' => env('NGANLUONG_API_KEY'),
'secret' => env('NGANLUONG_API_SECRET'),
],
First Use Case: Payment Request
Use the Nganluong facade to create a payment request:
use Ekipower\Nganluong\Facades\Nganluong;
$payment = Nganluong::createPayment([
'amount' => 100000,
'description' => 'Order #12345',
'return_url' => route('payment.return'),
'cancel_url' => route('payment.cancel'),
]);
Redirect the user to $payment->payment_url.
Payment Processing
Nganluong::createPayment() to generate a payment link.Nganluong webhook callbacks (e.g., POST /payment/webhook).
public function handleWebhook(Request $request) {
$event = Nganluong::parseWebhook($request->getContent());
// Process event (e.g., payment.success, payment.failed)
}
$payment = Nganluong::getPayment($paymentId);
Subscription Management
$subscription = Nganluong::createSubscription([
'amount' => 50000,
'interval' => 'monthly',
'customer_id' => $customerId,
]);
Nganluong::cancelSubscription($subscriptionId);
Refunds
Nganluong::createRefund($paymentId, ['amount' => 20000]);
Cashier logic to sync with Nganluong subscriptions.auth or verified middleware.\Log::info('Nganluong Webhook', $event->toArray());
Webhook Verification
$isValid = Nganluong::verifyWebhook($request->getContent(), $request->header('X-Signature'));
Nganluong::parseWebhook() only if $isValid is true.API Rate Limits
Currency Handling
100000 for 100,000 VND).Idempotency
idempotency_key in API requests to avoid duplicate charges:
Nganluong::createPayment([...], ['idempotency_key' => Str::uuid()]);
Enable Debug Mode:
config(['nganluong.debug' => true]);
Logs API requests/responses to storage/logs/nganluong.log.
Test Mode:
Use the test environment to simulate payments without real transactions:
config(['nganluong.api.test_mode' => true]);
Custom Events
Bind to nganluong.* events in EventServiceProvider:
protected $listen = [
'nganluong.payment.succeeded' => [PaymentHandler::class, 'handleSuccess'],
];
Service Provider
Extend the NganluongServiceProvider to add custom logic:
public function register() {
$this->app->extend('nganluong', function ($nganluong) {
$nganluong->extend('custom_method', function ($params) {
// Custom logic
});
return $nganluong;
});
}
API Client
Override the default Guzzle client for custom headers/retries:
config(['nganluong.client' => function () {
return new \GuzzleHttp\Client([
'headers' => ['X-Custom-Header' => 'value'],
]);
}]);
How can I help you explore Laravel packages today?