Edit - mpxpays.php
Back
Editing:
/order/mpxpays.php
Save Code
<?php // New Gateway File // 1. Add strict CORS headers header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST, OPTIONS"); header("Access-Control-Allow-Headers: Content-Type"); header("Content-Type: application/json"); // Handle Preflight OPTIONS request if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); } // 3. Require config require_once '../lib/config.php'; // Gateway Credentials (Replace with actual values or fetch from DB/Config) $merchant_id = '953042'; $api_key = '5ec35bc12a05d494634bca822055fbf7453db6e879f68e344e2be603abfa209c'; $gateway_url = 'https://api.mpxpayss.com/api/payIn'; // 2. Read JSON input $input = json_decode(file_get_contents('php://input'), true); $order_id = $input['order_id'] ?? null; $amount = $input['amount'] ?? null; if (!$order_id || !$amount) { http_response_code(400); echo json_encode(["error" => "order_id and amount are required"]); exit(); } // Format amount to exactly 2 decimal places as per docs $formatted_amount = number_format((float)$amount, 2, '.', ''); // 5. Send the callback_url $callback_url = rtrim(DOMAIN, '/') . '/webhook/mpxpays.php'; // 4. Apply Signature Logic: MD5(api_key + amount + callback_url + merchant_id + merchant_order_no) $signString = $api_key . $formatted_amount . $callback_url . $merchant_id . $order_id; $signature = md5($signString); // Prepare Gateway Payload $payload = [ "merchant_id" => $merchant_id, "api_key" => $api_key, "amount" => (float)$formatted_amount, "merchant_order_no" => $order_id, "callback_url" => $callback_url, "currency" => "INR", "signature" => $signature ]; // 7. Logging - Log Request log_api_data("MPXPays Order Request Payload", $payload, "mpxpays_order"); // 6. Use cURL to POST the data to the Gateway $ch = curl_init($gateway_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_error = curl_error($ch); curl_close($ch); // 7. Logging errors & success if ($curl_error) { log_api_data("MPXPays cURL Error", ["error" => $curl_error], "mpxpays_error"); http_response_code(500); echo json_encode(["error" => "Internal server error during gateway connection"]); exit(); } if ($http_code !== 200) { log_api_data("MPXPays API Down/Error ($http_code)", $response, "mpxpays_error"); http_response_code(502); echo json_encode(["error" => "Payment Gateway is currently unavailable"]); exit(); } $responseData = json_decode($response, true); if (isset($responseData['status']) && $responseData['status'] == 1 && isset($responseData['url'])) { // Gateway Success log_api_data("MPXPays Order Success", $responseData, "mpxpays_order"); // 8. Return JSON response for frontend echo json_encode(["payment_url" => $responseData['url']]); } else { // Gateway Rejection log_api_data("MPXPays Order Rejected", $responseData, "mpxpays_error"); http_response_code(400); echo json_encode(["error" => $responseData['message'] ?? "Failed to create order"]); }