File: /var/www/html/SendToKipu/index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$validApiKey = "kPpUNPQgaJngyUNr7iRIXbcalSfm6PQAw0fNi";
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
if (!isset($headers['api-key']) || $headers['api-key'] !== $validApiKey) {
http_response_code(401);
echo json_encode(["status" => "error", "message" => "Unauthorized: Invalid or missing API key"]);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(["status" => "error", "message" => "Method Not Allowed. Use POST."]);
exit;
}
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? strtolower($_SERVER["CONTENT_TYPE"]) : "";
$input = file_get_contents("php://input");
$data = (strpos($contentType, "application/json") !== false)
? (json_decode($input, true) ?? [])
: $_POST;
// ✅ Required field validation
$requiredFields = ["client_first_name", "client_last_name"];
$missing = [];
foreach ($requiredFields as $field) {
if (empty($data[$field])) {
$missing[] = $field;
}
}
if (!empty($missing)) {
http_response_code(400);
echo json_encode([
"status" => "error",
"message" => "Missing required fields",
"missing" => $missing
]);
exit;
}
// ✅ Build request payload
$requestData = [
"document" => [
"recipient_id" => "JyrdYNdKv5nLgJPoACwY6GHbyyxZ25YT6r-ndIY-IjM",
"sending_app_name" => "Bridge Connector",
"data" => [
"phone" => $data['phone'] ?? '',
"client_first_name" => $data['client_first_name'],
"client_last_name" => $data['client_last_name'],
"preferred_name" => $data['preferred_name'] ?? '',
"client_dob" => $data['client_dob'] ?? '',
"email" => $data['email'] ?? '',
"street1" => $data['street1'] ?? '',
"street2" => $data['street2'] ?? '',
"city" => $data['city'] ?? '',
"zip_code" => $data['zip_code'] ?? '',
"state" => $data['state'] ?? '',
"prim_subscriber_first_name" => $data['prim_subscriber_first_name'] ?? '',
"prim_subscriber_last_name" => $data['prim_subscriber_last_name'] ?? '',
"prim_insurance_company" => $data['prim_insurance_company'] ?? '',
"prim_sub_relationship_to_client"=> $data['prim_sub_relationship_to_client'] ?? '',
"primary_member_id" => $data['primary_member_id'] ?? '',
"prim_subscriber_dob" => $data['prim_subscriber_dob'] ?? '',
"prim_insurance_group_number" => $data['prim_insurance_group_number'] ?? '',
"prim_insurance_phone" => $data['prim_insurance_phone'] ?? '',
"prim_insurance_plan_type" => $data['prim_insurance_plan_type'] ?? '',
"plan_type" => $data['plan_type'] ?? '',
"external_mapping_id" => $data['external_mapping_id'] ?? '',
"external_app_name" => $data['external_app_name'] ?? '',
"emergency_contact_name" => $data['emergency_contact_name'] ?? '',
"emergency_contact_note" => $data['emergency_contact_note'] ?? '',
"emergency_contact_number" => $data['emergency_contact_number'] ?? ''
]
]
];
// ✅ Prepare JSON body
$finalBody = json_encode($requestData, JSON_UNESCAPED_SLASHES);
// ✅ Kipu API details
$BASE_URL = 'https://kipuapi.kipuworks.com';
$ACCESS_ID = '4bKm8x-ACsrhS7dydgz1kPcPKFu_EJ_yznJF7sdB2nM';
$SECRET_KEY = 'hupTtcrI1EyRjQh95Ve+8XB9OqkuwbfAjbDfvwsdy0YcHEz8yLvZXx9by36OFgibggSftlBr+cpfHpEgTrLyNw==';
$request_uri = '/api/patients';
// ✅ Signature generation
$content_type = 'application/json';
$content_md5 = base64_encode(md5($finalBody, true));
$currDate = gmdate('D, d M Y H:i:s T');
$canonical_string = $content_type . ',' . $content_md5 . ',' . $request_uri . ',' . $currDate;
$encoded_signature = base64_encode(hash_hmac('sha1', $canonical_string, $SECRET_KEY, true));
$authorization = 'APIAuth ' . $ACCESS_ID . ':' . $encoded_signature;
// ✅ Headers
$headers = [
'Accept: application/vnd.kipusystems+json; version=1',
'Authorization: ' . $authorization,
'Content-Type: ' . $content_type,
'Content-MD5: ' . $content_md5,
'Date: ' . $currDate
];
// ✅ Send to Kipu
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $BASE_URL . $request_uri);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $finalBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$server_output = curl_exec($ch);
if ($server_output === false) {
http_response_code(500);
echo json_encode(["status" => "error", "message" => curl_error($ch)]);
exit;
}
curl_close($ch);
// ✅ Response from Kipu
$server_output_arr = json_decode($server_output, true);
echo json_encode([
"success" => true,
"response-code" => 200,
"message" => "Data sent to Kipu successfully",
"data_received" => $requestData,
"kipu_response" => $server_output_arr
], JSON_PRETTY_PRINT);