PHP code example of amzad / apple-pay-knet
1. Go to this page and download the library: Download amzad/apple-pay-knet 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/ */
amzad / apple-pay-knet example snippets
'providers' => [
// ...
Amzad\ApplePayKnet\ApplePayKnetServiceProvider::class,
],
'aliases' => [
// ...
'ApplePayKnet' => Amzad\ApplePayKnet\Facades\ApplePayKnet::class,
],
// config/apple-pay-knet.php
return [
// Apple Pay display name shown on the payment sheet
'display_name' => env('APPLE_PAY_DISPLAY_NAME', ''),
// Absolute path to the merchant identity certificate (.pem file)
'certificate_path' => env('APPLE_PAY_CERTIFICATE_PATH', ''),
// Absolute path to the certificate private key (.pem file)
'certificate_key_path' => env('APPLE_PAY_CERTIFICATE_KEY_PATH', ''),
// Password used when encrypting the private key (leave empty if none)
'certificate_key_password' => env('APPLE_PAY_CERTIFICATE_KEY_PASSWORD', ''),
// Apple Pay merchant validation URL (do not change unless Apple updates it)
'validation_url' => env('APPLE_PAY_VALIDATION_URL', 'https://apple-pay-gateway-cert.apple.com/paymentservices/startSession'),
'initiative' => 'web',
'knet' => [
// KNET payment endpoint (use sandbox during development)
'endpoint' => env('KNET_ENDPOINT', 'https://www.kpaytest.com.kw/kpg/tranPipe.htm?param=tranInit&'),
'id' => env('KNET_ID', ''),
'password' => env('KNET_PASSWORD', ''),
// URL where KNET will POST the successful payment response
'response_url' => env('KNET_RESPONSE_URL', ''),
// URL where KNET will redirect on payment error
'error_url' => env('KNET_ERROR_URL', ''),
],
// URL prefix for all package routes
'route_prefix' => env('APPLE_PAY_ROUTE_PREFIX', 'apple-pay'),
// Middleware applied to all package routes
'route_middleware' => ['web'],
// Log every charge attempt to the apple_pay_transactions table
'log_transactions' => env('APPLE_PAY_LOG_TRANSACTIONS', true),
];
// routes/web.php
Route::post('/payment/callback', [PaymentController::class, 'callback'])->name('payment.callback');
Route::get('/payment/error', [PaymentController::class, 'error'])->name('payment.error');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
public function callback(Request $request)
{
// KNET posts these fields:
$result = $request->input('result'); // "CAPTURED" on success
$trackId = $request->input('trackid'); // Your reference/order ID
$tranId = $request->input('tranid'); // KNET transaction ID
$authCode = $request->input('auth'); // Authorization code
$paymentId = $request->input('paymentid'); // KNET payment ID
$amount = $request->input('amt'); // Amount charged
if ($result === 'CAPTURED') {
// Mark your order as paid
$order = Order::where('id', $trackId)->firstOrFail();
$order->update([
'status' => 'paid',
'knet_tran_id' => $tranId,
'knet_auth_code' => $authCode,
]);
return redirect()->route('orders.success', $order);
}
return redirect()->route('payment.error')->with('error', 'Payment was not captured.');
}
public function error(Request $request)
{
return view('payment.error');
}
}
use Amzad\ApplePayKnet\Facades\ApplePayKnet;
use Amzad\ApplePayKnet\Exceptions\KnetException;
use Amzad\ApplePayKnet\Exceptions\ApplePayException;
// $applePayment is the full event.payment object from the JS onpaymentauthorized event
try {
$response = ApplePayKnet::charge(
amount: '5.250', // KWD amount as string
reference: 'ORD-001', // Your unique order ID (becomes KNET trackid)
applePayment: $applePayment
);
// $response is the parsed KNET response array
if (($response['result'] ?? '') === 'CAPTURED') {
// Payment successful
}
} catch (KnetException $e) {
// KNET authorization failed
$code = $e->getResponseCode();
$message = $e->getMessage();
} catch (ApplePayException $e) {
// Apple Pay merchant validation failed
$message = $e->getMessage();
}
ApplePayKnet::charge(string $amount, string $reference, array $applePayment): array
// config/apple-pay-knet.php
'route_middleware' => ['web', 'auth'],
use Amzad\ApplePayKnet\Models\Transaction;
// All successful transactions
$successfulPayments = Transaction::successful()->get();
// All failed transactions
$failedPayments = Transaction::failed()->get();
// Transactions for a specific order
$orderTransactions = Transaction::where('order_id', 'ORD-001')->get();
// Amount formatted as KWD float
$transaction = Transaction::find(1);
$kwd = $transaction->amount_in_kwd; // e.g. 5.25
use Amzad\ApplePayKnet\Exceptions\ApplePayException;
try {
// ...
} catch (ApplePayException $e) {
// Reasons // - Certificate/key mismatch
// - Apple server returned non-200 response
logger()->error('Apple Pay error: ' . $e->getMessage());
}
use Amzad\ApplePayKnet\Exceptions\KnetException;
try {
// ...
} catch (KnetException $e) {
$responseCode = $e->getResponseCode(); // e.g. "NOT_CAPTURED"
$message = $e->getMessage();
logger()->error('KNET error: ' . $message, ['code' => $responseCode]);
}
bash
php artisan vendor:publish --tag=apple-pay-knet-config
bash
php artisan vendor:publish --tag=apple-pay-knet-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --tag=apple-pay-knet-assets
bash
php artisan vendor:publish --tag=apple-pay-knet-views
bash
php artisan vendor:publish --tag=apple-pay-knet-assets
dotenv
KNET_ENDPOINT=https://www.kpaytest.com.kw/kpg/tranPipe.htm?param=tranInit&
dotenv
KNET_ENDPOINT=https://www.kpay.com.kw/kpg/tranPipe.htm?param=tranInit&