⚙️ Request Handler
🔄 Payment Request Processing
How Request Handler Works
The payment data posted via the checkout form is handled by HesabeRequestHandler. The posted data is first validated and encrypted using the encryption keys and IV (Initialization Vector) from your Hesabe Merchant dashboard Profile page.
The following PHP Laravel code snippets demonstrate how the payment request process can be implemented.
📋 Step-by-Step Implementation
1️⃣ Request Validation
First, validate the incoming payment request data to ensure all required fields are present and properly formatted.
$validator = $this->validate($request, [
'merchantCode' => 'required',
'amount' => 'required|numeric|between:0.200,100000|regex:/^\d+(\.\d{1,3})?$/',
'paymentType' => 'required|in:0,1,2',
'responseUrl' => 'required|url',
'failureUrl' => 'required|url',
'version' => 'required'
]);
2️⃣ Configuration Setup
Load the encryption keys, access code, and API URLs from your application configuration.
$ivKey = HSB_IV_KEY;
$encryptionKey = HSB_ENCRYPTION_KEY;
$accessCode = HSB_ACCESS_CODE;
$checkoutApiUrl = HSB_CHECKOUT_API_URL;
$paymentUrl = HSB_PAYMENT_URL;
3️⃣ JSON Encoding
After validation, convert the request data to JSON format containing all the request keys and values.
$requestDataJson = json_encode($request->input());
4️⃣ Data Encryption
Encrypt the JSON data using AES algorithm with the HesabeCrypt library for secure transmission.
$encryptedData = HesabeCrypt::encrypt($requestDataJson, $encryptionKey, $ivKey);
5️⃣ API Request to Checkout Endpoint
Send the encrypted data to Hesabe's checkout API endpoint with proper headers including the access code.
$baseUrl = "https://sandbox.hesabe.com";
$checkoutApiUrl = $baseUrl . "/checkout";
$checkoutRequestData = new Request(['data' => $encryptedData]);
$checkoutRequest = Request::create($checkoutApiUrl, 'POST', $checkoutRequestData->all());
$checkoutRequest->headers->set('accessCode', $accessCode);
$checkoutRequest->headers->set('Accept', 'application/json'); // Only for JSON response
6️⃣ Response Handling
Process the API response and extract the content for further processing.
$checkoutResponse = Route::dispatch($checkoutRequest);
$checkoutResponseContent = $checkoutResponse->content();
7️⃣ Response Decryption
Decrypt the response using the same encryption keys and parse the JSON data.
$decryptedResponse = HesabeCrypto::decrypt($checkoutResponseContent, $encryptionKey, $ivKey);
$responseDataJson = json_decode($decryptedResponse);
8️⃣ Payment Redirection
Extract the payment token from the response and redirect to Hesabe's payment URL.
// $paymentUrl = {{baseUrl}}/payment
$responseToken = $responseDataJson->response->data;
return Redirect::to($paymentUrl . '?data=' . $responseToken);