iPay API Integration - PHP
To integrate iPay's payment gateway into your PHP-based forex CRM platform, follow these steps to enable on-ramp cryptocurrency deposits.
1. Build the Payment URL
Construct a payment URL with required parameters and redirect the user to initiate the payment process.
<?php
// Build iPay payment URL in PHP
$apiKey = 'YOUR_API_KEY';
$depositId = '12345'; // From CRM
$amount = 150; // Client-entered amount
$onRampProvider = 'provider1';
$defaultFiatCurrency = 'USD';
$baseUrl = 'https://us-central1-nfgdatabasedemo.cloudfunctions.net/app/api/invoice_external?';
$params = http_build_query([
'apiKey' => $apiKey,
'customId' => $depositId,
'onRampProvider' => $onRampProvider,
'defaultFiatAmount' => $amount,
'defaultFiatCurrency' => $defaultFiatCurrency
]);
$paymentUrl = $baseUrl . $params;
// Redirect the user to iPay payment page
header("Location: $paymentUrl");
exit();
After the customer completes the payment, they are sent to the redirect URL you registered with iPay. Do not credit the deposit on that page load — credit it only when the server-to-server callback below arrives.
2. Handle the Callback
Set up a callback handler to process the JSON payload sent by iPay when the deposit completes. Callbacks are sent for completed payments only, so there is no status field to check.
<?php
// iPay callback handler (e.g., callback.php)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$secretToken = 'YOUR_SECRET_TOKEN'; // Optional secret token from iPay
// Verify the authorization header (if a secret token is configured)
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
if ($secretToken !== '' && (($headers['authorization'] ?? '') !== $secretToken)) {
http_response_code(401);
exit('Invalid token');
}
$payload = json_decode(file_get_contents('php://input'), true);
if (!is_array($payload) || !isset($payload['customerID'], $payload['transferedAmount'])) {
http_response_code(400);
exit('Bad payload');
}
// Note the field spellings: transferedAmount (single r), transactionFeePrecent
$depositId = (string) $payload['customerID']; // your customId, returned as a string
$amountReceived = (float) $payload['transferedAmount']; // net USDT delivered to your wallet
$txId = $payload['blockchainTxId'] ?? null;
// TODO: Update deposit record $depositId in your CRM/database with $amountReceived.
// Make this idempotent: ignore the callback if $depositId (or $txId) was already credited.
// Send HTTP 200 OK to iPay
http_response_code(200);
exit('OK');
}
Explanation
- Building the URL: The
http_build_queryfunction safely encodes parameters likeapiKey,customId, anddefaultFiatAmount. The resulting URL redirects the user to iPay’s payment page. - Handling the Callback: The handler verifies the optional
authorizationheader, reads the JSON body, and credits the deposit identified bycustomerIDwithtransferedAmount. Respond with HTTP 200 to acknowledge receipt. - Integration Notes: Ensure your CRM can handle HTTP redirects and POST requests. Test the callback endpoint to confirm it receives iPay’s JSON payload correctly — see Testing Your Payment Integration.