<?php
include "merchant/config.php";
require_once "payment/order_logic.php";
// URL of the PHP page
error_reporting(0);

$token = '4f4f2d5860edb2ee76ba899d3b63bd02';
$orderid=rand(1000000000,9999999999);

// Data for the demo order. This used to be POSTed over cURL to this same
// site's own /api/create-order -- on the PHP built-in dev server (`php -S`,
// single-threaded, one request at a time) that self-call deadlocks: the
// request can't be answered until the server is free, but the server won't
// be free until this request finishes waiting for it. It would hang until
// PHP's max_execution_time killed it, taking the whole dev server down.
// Calling create_paytm_order() in-process (same fix already used by
// merchant/lib/pay.php) avoids the deadlock and the pointless network hop.
$data = array(
    'customer_mobile' => '9876543210',
    'user_token' => $token,
    'amount' => '1',
    'order_id' => $orderid,
    'redirect_url' => $site_url.'/success.php',
    'remark1' => 'aluu1',
    'remark2' => 'aluu2',
);

$jsonResponse = create_paytm_order($data);

// Check if a payment URL came back
if (!empty($jsonResponse['result']['payment_url'])) {

    // Redirect the user to the payment URL
    $paymentUrl = $jsonResponse['result']['payment_url'];
    header('Location: ' . $paymentUrl);
    exit;

} else {
    echo json_encode($jsonResponse);
}
?>
