How to Integrate a Tron Payment API in PHP

PHP Tron API Integration

Tags & Keywords

Tron API PHP TRC-20 Payment Gateway PHP Crypto Integration Accept TRX on Website Smart CloudPay Developer Guide

The Tron blockchain has rapidly become the network of choice for merchant payments. Thanks to its incredibly fast block times and microscopic gas fees, tokens like TRX and USDT (TRC-20) are ideal for daily e-commerce transactions.

If you are building a custom web application in PHP and want to accept Tron payments, you need a robust API. Interacting directly with a Tron Full Node in PHP can be incredibly complex, requiring you to manage event listeners, private keys, and energy/bandwidth calculations manually.

The solution? Using the Smart CloudPay REST API. In this guide, we'll show you exactly how to generate a Tron payment invoice using pure PHP and cURL.

Why Use Smart CloudPay for Tron?

  • Non-Custodial: The API generates an invoice, but the customer sends the TRC-20 tokens directly to your personal wallet.
  • Real-time Webhooks: We monitor the Tron blockchain for you. The second a transaction hits the ledger, we send a POST request to your PHP server.
  • Zero Maintenance: You don't have to run a Tron node or worry about API rate limits from public explorers.

Prerequisites

  1. A PHP 7.4+ or PHP 8.x environment.
  2. The curl extension enabled in your php.ini.
  3. A Smart CloudPay Account. You will need your Merchant ID and API Key from the dashboard.

Step 1: Generating a Payment Invoice in PHP

To accept a payment, you first need to create an invoice. This tells Smart CloudPay how much to charge and which currency to expect. We will use PHP's built-in cURL functions to make a POST request to the API.

The PHP Code

<?php

// 1. Define your API credentials
$merchant_id = 'YOUR_MERCHANT_ID';
$api_key = 'YOUR_API_KEY';
$endpoint = 'https://api.smartcloudpay.com/v1/invoice/create';

// 2. Prepare the invoice payload
$payload = [
    'order_id' => 'ORD-' . time(), // Your internal system order ID
    'price_amount' => 50.00,       // Charge the customer $50.00
    'price_currency' => 'usd',     // Fiat currency to calculate exchange rate from
    'pay_currency' => 'trx',       // The crypto they will pay in (TRX or USDT-TRC20)
    'ipn_callback_url' => 'https://yourwebsite.com/webhook.php' // Where we ping you upon payment
];

// 3. Initialize cURL
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

// 4. Set the required headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'x-api-key: ' . $api_key,
    'merchant-id: ' . $merchant_id
]);

// 5. Execute the request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// 6. Handle the response
if ($http_code === 200) {
    $data = json_decode($response, true);
    // Redirect user to the generated payment page
    header("Location: " . $data['payment_url']);
    exit;
} else {
    // Handle API error
    echo "Failed to create invoice. Error: " . $response;
}
?>

In the script above, we define an order for $50.00 USD, but tell the API we want to be paid in TRX. The API handles the real-time exchange rate calculation and returns a payment_url. You simply redirect your user to this URL where they are presented with a QR code and a Tron wallet address to send the funds to.

Step 2: Handling the Webhook (IPN)

Once the customer sends the TRX and the Tron network confirms the transaction, Smart CloudPay will immediately send a POST request to the ipn_callback_url you provided. You need to write a simple script to listen for this.

<?php
// webhook.php

// 1. Get the raw POST data from Smart CloudPay
$payload = file_get_contents('php://input');
$data = json_decode($payload, true);

// 2. Verify the payment status
if (isset($data['status']) && $data['status'] === 'completed') {
    
    $order_id = $data['order_id'];
    $amount_paid = $data['actually_paid'];
    
    // 3. TODO: Update your database
    // "UPDATE orders SET status = 'paid' WHERE id = :order_id"
    
    // Log success
    error_log("Order $order_id was successfully paid via Tron!");
    
    // Respond with 200 OK so the API knows you received the webhook
    http_response_code(200);
    echo "OK";
} else {
    http_response_code(400);
    echo "Ignored";
}
?>

Start Building with Tron Today

Integrating Tron payments in PHP doesn't require complex node management or dealing with smart contract ABIs. By leveraging a dedicated API, you can start accepting TRX and USDT on the Tron network with less than 50 lines of code.

Get Your API Keys

Sign up for a free Smart CloudPay developer account and generate your keys instantly.

Start Developing