<?php
/**
 * EXAMPLE (on the merchant's website): the "Add money to wallet" button posts here.
 */
session_start();
require __DIR__ . '/../php/MiplPay.php';

$pg = new MiplPay('mipl_live_xxxxxxxx', 'sk_xxxxxxxxxxxxxxxx', 'https://pay.moldable.in');

$userId = $_SESSION['user_id'] ?? 1;        // your logged-in user
$amount = (float)($_POST['amount'] ?? 100);
$orderId = 'WALLET' . $userId . 'T' . time(); // unique ID for every order

// 1) Create a pending entry in your DB (wallet_topups table)
// INSERT INTO wallet_topups (order_id, user_id, amount, status) VALUES (?, ?, ?, 'pending')

try {
    // 2) Create the MIPL Payment Gateway order
    $order = $pg->createOrder([
        'amount' => $amount,
        'order_id' => $orderId,
        'customer_name' => 'Rahul Kumar',
        'customer_mobile' => '9876543210',
        'note' => 'Wallet top-up',
        'redirect_url' => 'https://yoursite.com/payment-return.php',
        'webhook_url' => 'https://yoursite.com/mipl-webhook.php',
    ]);
    // 3) Send the customer to the payment page
    header('Location: ' . $order['payment_url']);
    exit;
} catch (Throwable $e) {
    echo 'Could not start payment: ' . htmlspecialchars($e->getMessage());
}
