Knet package is provides an expressive, fluent interface to KNet's payment services.
asciisd/knet is a Laravel package for knet package is provides an expressive, fluent interface to knet's payment services..
It currently has 14 GitHub stars and 1.427 downloads on Packagist (latest version v7.2.0).
Install it with composer require asciisd/knet.
Discover more Laravel packages by asciisd
or browse all Laravel packages to compare alternatives.
Last updated
A robust Laravel package for integrating KNET payment gateway services in your applications. This package provides a clean and elegant way to handle payment processing, refunds, and transaction management with KNET.
You can install the package via composer:
composer require asciisd/knet
After installation, publish the configuration file:
php artisan knet:publish"
Configure your KNET credentials in your .env file:
KNET_TRANSPORT_ID=your_transport_id
KNET_TRANSPORT_PASSWORD=your_transport_password
KNET_RESOURCE_KEY=your_resource_key
# URLs Optional
KNET_RESPONSE_URL=/knet/response
KNET_REDIRECT_URL=/dashboard
KNET_DEBUG=false
use Asciisd\Knet\Services\KnetPaymentService;
class PaymentController extends Controller
{
public function createPayment(
Request $request,
KnetPaymentService $paymentService
) {
$transaction = $paymentService->createPayment(
user: $request->user(),
amount: 10.000,
options: [
'udf1' => 'custom_data_1',
'udf2' => 'custom_data_2',
]
);
return redirect($transaction->url);
}
}
public function handleResponse(
Request $request,
KnetPaymentService $paymentService
) {
$transaction = $paymentService->handlePaymentResponse($request->all());
if ($transaction->paid) {
return redirect()->route('payment.success');
}
return redirect()->route('payment.failed');
}
public function refund(
KnetTransaction $transaction,
KnetPaymentService $paymentService
) {
// Full refund
$result = $paymentService->refundPayment($transaction);
// Partial refund
$result = $paymentService->refundPayment($transaction, 5.000);
return $result;
}
public function checkStatus(
KnetTransaction $transaction,
KnetPaymentService $paymentService
) {
$updatedTransaction = $paymentService->inquireAndUpdateTransaction($transaction);
return $updatedTransaction;
}
The package dispatches several events that you can listen to:
KnetResponseReceived: Fired when a payment response is receivedKnetResponsehandled: Fired when a payment response is handleduse Asciisd\Knet\Events\KnetResponseReceived;
class PaymentReceivedListener
{
public function handle(KnetResponseReceived $event)
{
$transactionArray = $event->payload;
// Handle payload
}
}
The KnetTransaction model provides several helpful methods:
$transaction->rawAmount(); // Get the raw amount
$transaction->isPaid(); // Check if transaction is paid
$transaction->isRefunded(); // Check if transaction is refunded
$transaction->isRefundable(); // Check if transaction can be refunded
The package includes comprehensive error handling:
try {
$result = $paymentService->refundPayment($transaction);
} catch (\Exception $e) {
Log::error('Refund failed', [
'message' => $e->getMessage(),
'transaction_id' => $transaction->id
]);
}
The package includes migrations for the knet_transactions table with the following fields:
id: Primary keyuser_id: Foreign key to users tabletrackid: KNET tracking IDamt: Transaction amountpaymentid: KNET payment IDtranid: KNET transaction IDref: Reference numberresult: Transaction resultauth: Authorization codeavr: AVR valuepostdate: Posting datepaid: Payment statuserror_text: Error message if anyurl: Payment URLlivemode: Production/Test mode flagcomposer test
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.