$merchantAccount,
"merchantDomainName" => $merchantDomainName,
"orderReference" => $orderReference,
"orderDate" => $orderDate,
"amount" => $amount,
"currency" => $currency,
"productName" => [$productName],
"productCount" => [$productCount],
"productPrice" => [$productPrice],
"clientFirstName" => explode(' ', $name)[0],
"clientLastName" => explode(' ', $name)[1] ?? '',
"clientEmail" => $email,
"clientPhone" => $phone,
"merchantSignature" => $merchantSignature
];
// Initialize cURL to send the request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.wayforpay.com/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
// Execute the request and capture the response
$response = curl_exec($ch);
curl_close($ch);
if ($response === false) {
die('Curl error: ' . curl_error($ch));
}
$responseData = json_decode($response, true);
// Check for errors in the response
if (isset($responseData['reason'])) {
die('Error: ' . $responseData['reason']);
}
// Redirect to the payment URL
if (isset($responseData['invoiceUrl'])) {
header('Location: ' . $responseData['invoiceUrl']);
exit();
} else {
die('Unable to generate invoice URL.');
}
?>