private const API_URL = 'https://accept.paymob.com/api/'; private $api_key; private $hmac_secret; private $integration_id; private $iframe_id; public function __construct() { // Load Paymob settings from the database $this->api_key = osc_get_preference('paymob_api_key', 'payment'); $this->hmac_secret = osc_get_preference('paymob_hmac_secret', 'payment'); $this->integration_id = osc_get_preference('paymob_integration_id', 'payment'); $this->iframe_id = osc_get_preference('paymob_iframe_id', 'payment'); } /** * Step 1: Authentication Request */ private function getAuthToken() { $response = $this->sendRequest('auth/tokens', 'POST', ['api_key' => $this->api_key]); return $response->token ?? null; } /** * Step 2: Order Registration */ private function registerOrder($authToken, $orderId, $amount, $currency, $items, $billingData) { $data = [ 'auth_token' => $authToken, 'delivery_needed' => 'false', 'merchant_order_id' => $orderId, 'amount_cents' => $amount * 100, // Amount in cents 'currency' => $currency, 'items' => $items, 'billing_data' => $billingData ]; $response = $this->sendRequest('ecommerce/orders', 'POST', $data); return $response->id ?? null; } /** * Step 3: Get Payment Key */ private function getPaymentKey($authToken, $orderId, $amount, $currency, $billingData) { $data = [ 'auth_token' => $authToken, 'amount_cents' => $amount * 100, 'expiration' => 3600, 'order_id' => $orderId, 'billing_data' => $billingData, 'currency' => $currency, 'integration_id' => $this->integration_id ]; $response = $this->sendRequest('acceptance/payment_keys', 'POST', $data); return $response->token ?? null; } /** * Main method to initiate a payment */ public function createPayment($orderId, $amount, $currency, $items, $customer) { $authToken = $this->getAuthToken(); if (!$authToken) { // Handle error: Could not authenticate return null; } $billingData = [ "apartment" => "NA", "email" => $customer['email'], "floor" => "NA", "first_name" => $customer['first_name'], "street" => "NA", "building" => "NA", "phone_number" => $customer['phone'], "shipping_method" => "NA", "postal_code" => "NA", "city" => "NA", "country" => "NA", "last_name" => $customer['last_name'], "state" => "NA" ]; $paymobOrderId = $this->registerOrder($authToken, $orderId, $amount, $currency, $items, $billingData); if (!$paymobOrderId) { // Handle error: Could not register order return null; } $paymentKey = $this->getPaymentKey($authToken, $paymobOrderId, $amount, $currency, $billingData); if (!$paymentKey) { // Handle error: Could not get payment key return null; } return "https://accept.paymob.com/api/acceptance/iframes/{$this->iframe_id}?payment_token={$paymentKey}"; } /** * Verify HMAC for callback/webhook */ public function verifyHmac($data) { $hmac = $data['hmac']; unset($data['hmac']); // Paymob concatenates these specific keys in alphabetical order $concatenated = ''; $keys = [ 'amount_cents', 'created_at', 'currency', 'error_occured', 'has_parent_transaction', 'id', 'integration_id', 'is_3d_secure', 'is_auth', 'is_capture', 'is_refunded', 'is_standalone_payment', 'is_voided', 'order', 'owner', 'pending', 'source_data_pan', 'source_data_sub_type', 'source_data_type', 'success' ]; // Some keys might not be present in all callbacks $requestKeys = array_keys($data); $sortedKeys = array_intersect($keys, $requestKeys); sort($sortedKeys); foreach ($sortedKeys as $key) { // Convert boolean to string 'true' or 'false' if (is_bool($data[$key])) { $concatenated .= $data[$key] ? 'true' : 'false'; } else { $concatenated .= $data[$key]; } } $calculatedHmac = hash_hmac('sha512', $concatenated, $this->hmac_secret); return hash_equals($calculatedHmac, $hmac); } /** * Helper to send cURL requests */ private function sendRequest($endpoint, $method, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, self::API_URL . $endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $headers = array(); $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { // Handle curl error curl_close($ch); return null; } curl_close($ch); return json_decode($result); } } Search and explore all listings - Souqfriday
Are you a seller and do not have an account? Create an account
Non-logged user
Hello
You have an account? Sign in