PHP library for payments processing through the API of KNET, Kuwait's leading payment gateway.
Starting a payment in PAY.php ..
- $k = new KNET($c);
- $k->init();
- $p = $k->create(array("AMOUNT" => "10.000"));
- $k->start();
$k = new KNET($c);
$k->init();
$p = $k->create(array("AMOUNT" => "10.000"));
// assign $p["TRACKING_ID"] to a local record
$k->start();
Payee is sent to gateway. The following must be waiting in LISTEN_URI.php ..
- $k = new KNET($c);
- $k->init();
- $p = $k->listen();
-
- $k->respond();
$k = new KNET($c);
$k->init();
$p = $k->listen();
/*
update the local record payment status using
$p["PAYMENT_STATUS"] and other variables in $p
by identifying this record using $p["TRACKING_ID"].
*/
$k->respond();
Payee is sent to COMPLETE_URI.php and TRACKING_ID is bypassed. You're all set!
What are you getting?
Security – Even if some people could reach your LISTEN_URI, they won't be able to access listen() with their POST arguments.
The SDK has dedicated KNET's "trackid" and "udf5" properties to be private within the SDK; used as security tokens, along with KNET's tokenization requirement.
A successful call to LISTEN_URI must have "trackid", "udf5" and a secret key that match the ones generated while calling create();
However, neither is exposable beyond the scope of the SDK's public properties.
- array(
- "STATUS" => "success",
- "TRACKING_ID" => "[TRACKING_ID]",
- "PAYMENT_STATUS" => "failed",
- "DESCRIPTION" => "Unauthorized access or the gateway had technical issues processing this payment."
- )
// Returns of listen() with invalid tokens
array(
"STATUS" => "success",
"TRACKING_ID" => "[TRACKING_ID]",
"PAYMENT_STATUS" => "failed",
"DESCRIPTION" => "Unauthorized access or the gateway had technical issues processing this payment."
)
Testing environment – You'll have a functional demo, which works even if you do not have a
resource.cgn and or
keystore.bin file.
Once you configure DEBUG to TRUE while defining the class, the SDK will use a virtual gateway within this server. This helps you visualize how your p
arameters are passed from and back to you through a gateway. However, when you set DEBUG to FALSE, you still can test directly with KNET using their testing terminal.
Get it now ..
— As low as KWD 19.950 or USD 65.83
— Checkout with Paypal, and get a copy via Paypal associated e-mail as soon as you checkout.
Checkout with Paypal.
Go to your KNET control panel. Find the
Terminal Transportal ID and write it down. Then, within the control panel,
download both your terminal's
resource.cgn and
keystore.bin files.
Email both files to
pgsupport@knet.com.kw; asking them to extract both
Transportal Password and
Terminal Resource Key.
- require("path/to/KNET.php");
- $c = array(
- "TRANSPORTAL_ID" => "000000",
- "TRANSPORTAL_PASSWORD" => "000000",
- "TERMINAL_RESOURCE_KEY" => "000000",
- "TERMINAL_URI" => "https://kpay.com.kw",
- "CURRENCY" => "414",
- "DECIMALS" => "3",
- "LISTEN_URI" => "https://LISTEN_URI.php",
- "COMPLETE_URI" => "https://COMPLETE_URI.php",
- "ACTION" => "1",
- "DEBUG" => FALSE
- );
require("path/to/KNET.php");
$c = array(
"TRANSPORTAL_ID" => "000000",
"TRANSPORTAL_PASSWORD" => "000000",
"TERMINAL_RESOURCE_KEY" => "000000",
"TERMINAL_URI" => "https://kpay.com.kw",
"CURRENCY" => "414",
"DECIMALS" => "3",
"LISTEN_URI" => "https://LISTEN_URI.php",
"COMPLETE_URI" => "https://COMPLETE_URI.php",
"ACTION" => "1",
"DEBUG" => FALSE
);
Starting a payment in PAY.php ..
- include("CONFIG.php");
- $k = new KNET($c);
- $k->init();
- if($k->valid()) {
- $p = $k->create(array("AMOUNT" => "10.000"));
- if($p["STATUS"] == "success") {
-
- if(!$k->start()) {
- echo "Payment URI is invalid.";
- }
- } else {
- echo $p["DESCRIPTION"];
- }
- } else {
- echo "Invalid configurations.";
- }
include("CONFIG.php");
$k = new KNET($c);
$k->init();
if($k->valid()) {
$p = $k->create(array("AMOUNT" => "10.000"));
if($p["STATUS"] == "success") {
// assign $p["TRACKING_ID"] to a local record
if(!$k->start()) {
echo "Payment URI is invalid.";
}
} else {
echo $p["DESCRIPTION"];
}
} else {
echo "Invalid configurations.";
}
Payee is sent to gateway. The following must be waiting in LISTEN_URI.php ..
- include("CONFIG.php");
- $k = new KNET($c);
- $k->init();
- if($k->valid()) {
- $p = $k->listen();
- if($p["STATUS"] == "success") {
- if($p["PAYMENT_STATUS"] == "successful") {
-
- } else {
-
- }
-
-
- $k->respond();
- } else {
- echo $p["DESCRIPTION"];
- }
- } else {
- echo "Invalid configurations.";
- }
include("CONFIG.php");
$k = new KNET($c);
$k->init();
if($k->valid()) {
$p = $k->listen();
if($p["STATUS"] == "success") {
if($p["PAYMENT_STATUS"] == "successful") {
// Customer has paid
} else {
// Customer has not paid
}
/*
update the local record payment status using
$p["PAYMENT_STATUS"] and other variables in $p
by identifying this record using $p["TRACKING_ID"].
*/
$k->respond();
} else {
echo $p["DESCRIPTION"];
}
} else {
echo "Invalid configurations.";
}
Payee is sent to COMPLETE_URI.php and TRACKING_ID is bypassed.
Live Demonstration
A big thank you goes to
KuwaitNET for providing us with their testing-environment resource.cgn and keystore.bin files.
This is a test, and your card won't be charged by KNET.
Test Debit Card Information
— Bank: KNET Test Card [KNET1]
— Card: 888888 0000000001
— Card: 888888 0000000002
— Expiry Date: 09/21
— PIN: Any 4 digits
— For "successful" payments use the expiry date given above. Otherwise gives "failed" payments.
—
Test A
Amount: KWD 12.000
Interface Language: English
User Defined Variable 1: I_am_UDV_1
START TEST
—
Test B
Amount: KWD 17.450
Interface Language: Arabic
User Defined Variable 2: I_am_UDV_2
START TEST
Live Demonstration
Provided TRACKING_ID is invalid.
Want to test again?
Start another test!
This SDK is designated for KNET's new terminal API, as of 2019, which tokenizes back-and-forth requests. If you are still using KNET's old terminal, then this
SDK is not suitable for you.
Go to your KNET control panel. Find the
Terminal Transportal ID and write it down. Then, within the control panel,
download both your terminal's
resource.cgn and
keystore.bin files.
Email both files to
pgsupport@knet.com.kw; asking them to extract both
Transportal Password and
Terminal Resource Key.
Include the SDK main class to your code ..
- require("path/to/KNET.php");
require("path/to/KNET.php");
Define the class ..
- $c = array(
-
-
- "TRANSPORTAL_ID" => "000000",
-
-
- "TRANSPORTAL_PASSWORD" => "000000",
-
-
- "TERMINAL_RESOURCE_KEY" => "000000",
-
-
- "TERMINAL_URI" => "https://kpay.com.kw",
-
-
- "CURRENCY" => "414",
-
-
- "DECIMALS" => "3",
-
-
- "LISTEN_URI" => "https://LISTEN_URI.php",
-
-
- "COMPLETE_URI" => "https://COMPLETE_URI.php",
-
-
- "ACTION" => "1",
-
-
- "DEBUG" => FALSE
- );
-
- $k = new KNET($c);
$c = array(
// KNET's terminal transportal ID.
"TRANSPORTAL_ID" => "000000",
// KNET's terminal transportal password.
"TRANSPORTAL_PASSWORD" => "000000",
// KNET's terminal resource key.
"TERMINAL_RESOURCE_KEY" => "000000",
// KNET's terminal URI.
"TERMINAL_URI" => "https://kpay.com.kw",
// The currency of the transaction.
"CURRENCY" => "414", // means KWD
// The currency unit decimals, "3" for KWD.
"DECIMALS" => "3",
/*
This is where you must use listen() followed
by validation followed by respond(). This
must be accessible over HTTPS with a valid
SSL certificate.
*/
"LISTEN_URI" => "https://LISTEN_URI.php",
/*
This is the final destination where the payee
will be redirected to. This should display the
status of the payment based on the TRACKING_ID
given in GET. HTTPS is not mandatory for this
file.
*/
"COMPLETE_URI" => "https://COMPLETE_URI.php",
// This must always be "1"
"ACTION" => "1",
/*
Either TRUE or FALSE. If set to TRUE then
you will be using a virtual gateway to test
your code. Set this to false if you prefer
testing with KNET directly.
*/
"DEBUG" => FALSE
);
$k = new KNET($c);
Initialize the class ..
$k->init();
/*
[PARAMETERS]:
VOID
[RETURNS]:
- On success:
array(
"STATUS" => "success",
"DESCRIPTION" => "DESCRIPTIVE_MESSAGE"
)
- On error:
array(
"STATUS" => "error",
"DESCRIPTION" => "DESCRIPTIVE_MESSAGE"
)
*/
For a faster setup check, use ..
- if($k->valid()) {
- echo "setup is valid.";
- } else {
- echo "setup is invalid.";
- }
if($k->valid()) {
echo "setup is valid.";
} else {
echo "setup is invalid.";
}
Create a payment ..
- $p = array(
-
-
- "AMOUNT" => "10.000",
-
-
- "LANGUAGE" => "ENG",
-
-
-
- "USER_DEFINED_VAR_1" => "",
-
-
-
- "USER_DEFINED_VAR_2" => "",
-
-
-
- "USER_DEFINED_VAR_3" => "",
-
-
-
- "USER_DEFINED_VAR_4" => ""
-
- );
-
- $k->create($p);
-
$p = array(
// the amount to be charged
"AMOUNT" => "10.000",
/*
Language code of the interface you
will be redirecting the payee to.
*/
"LANGUAGE" => "ENG",
/*
Any defined variable that will be
retrieved after the gateway contact
you in LISTEN_URI.
*/
"USER_DEFINED_VAR_1" => "",
/*
Any defined variable that will be
retrieved after the gateway contact
you in LISTEN_URI.
*/
"USER_DEFINED_VAR_2" => "",
/*
Any defined variable that will be
retrieved after the gateway contact
you in LISTEN_URI.
*/
"USER_DEFINED_VAR_3" => "",
/*
Any defined variable that will be
retrieved after the gateway contact
you in LISTEN_URI.
*/
"USER_DEFINED_VAR_4" => ""
);
$k->create($p);
/*
[RETURNS]:
- On success:
array(
"STATUS" => "success",
"TRACKING_ID" => "UNIQUE_TRACKING_ID",
"PAYMENT_URI" => "GATEWAY_PAYMENT_URI"
)
- On error:
array(
"STATUS" => "error",
"DESCRIPTION" => "ERROR_DESCRIPTION"
)
*/
Assign TRACKING_ID to a record, then start the payment where the payee will be redirected to gateway ..
$k->start();
/*
[RETURNS]:
- On success:
bool(true)
- On error:
bool(false)
*/
Gateway will respond through LISTEN_URI, which must have ..
$k->listen();
/*
[RETURNS]:
- On success:
array(
"STATUS" => "success",
"PAYMENT_STATUS" => "either 'successful' or 'failed'",
"DESCRIPTION" => "Descriptive status",
"AMOUNT" => "The payment amount",
"PAYMENT_ID" => "The payment identification number",
"TRACKING_ID" => "The payment identification number",
"TRANSACTION_ID" => "Gateway transaction identification number",
"REFERENCE_ID" => "Gateway reference identification number",
"AUTHORIZATION_ID" => "Gateway authorization identification number",
"USER_DEFINED_VAR_1" => "Variable passed to create()",
"USER_DEFINED_VAR_2" => "Variable passed to create()",
"USER_DEFINED_VAR_3" => "Variable passed to create()",
"USER_DEFINED_VAR_4" => "Variable passed to create()",
"PAYMENT_TIME" => "Payment date"
)
- On error:
array(
"STATUS" => "error",
"DESCRIPTION" => "ERROR_DESCRIPTION"
)
*/
Please note that PAYMENT_STATUS is what defines the status of payment, where STATUS in the other hand tells whether the call has followed the protocol correctly.
Validate the received data, use TRACKING_ID to identify the purchase in your database/file records. Then, respond to the gateway ..
$k->respond();
/*
[RETURNS]:
- On success:
REDIRECT TO "[COMPLETE_URI]?TRACKING_ID=[TRACKING_ID]"
- On error:
REDIRECT TO "[COMPLETE_URI]?TRACKING_ID=[TRACKING_ID]"
*/
The gateway will redirect the payee to COMPLETE_URI bypassing the TRACKING_ID. You're all set!