PHP WebShell

Текущая директория: /var/www/bitcardoApp/models/fiat

Просмотр файла: create_paystack_fiat.php

<?php
session_start();
require_once "../../config/db_config.php";

// Get user_id from session
$user_id = $_SESSION['user_id'] ?? null;
if (!$user_id) exit("User not logged in.");

// Get user info from DB
$stmt = $conn->prepare("SELECT first_name, last_name, email, customer_code FROM users WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
if (!$user) exit("User not found.");

$first_name = $user['first_name'];
$last_name  = $user['last_name'];
$email      = $user['email'];
$customer_code = $user['customer_code'] ?? null;

// 1. Create Paystack customer if needed
if (!$customer_code) {
    $customer_data = [
        "first_name" => $first_name,
        "last_name"  => $last_name,
        "email"      => $email,
    ];
    $ch = curl_init("https://api.paystack.co/customer");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customer_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $paystackSecret",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $result = json_decode($response, true);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 200 && $result['status'] && isset($result['data']['customer_code'])) {
        $customer_code = $result['data']['customer_code'];
        $stmt = $conn->prepare("UPDATE users SET customer_code = ? WHERE user_id = ?");
        $stmt->bind_param("si", $customer_code, $user_id);
        $stmt->execute();
    } else {
        $msg = $result['message'] ?? "Could not create Paystack customer.";
        exit("Failed to create Paystack customer. ($msg)");
    }
}

// 2. Create virtual account for this customer_code
$data = [
    "customer" => $customer_code,
    // "preferred_bank" => "", // Leave empty for Paystack Titan or for default
];
$ch = curl_init("https://api.paystack.co/dedicated_account");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $paystackSecret",
    "Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$result = json_decode($response, true);

if ($http_code == 200 && $result['status'] && isset($result['data']['account_number'])) {
    // Save virtual account details in user_wallets if not already present
    $account_number = $result['data']['account_number'];
    $bank_name      = $result['data']['bank']['name'] ?? 'Paystack';
    $account_name   = $result['data']['account_name'] ?? ($first_name . " " . $last_name);
    $wallet_address = $account_number;
    $label          = "Naira Wallet";
    $coin           = "NGN";
    $icon           = "ngn.png";
    $balance        = 0.00;
    $type           = "fiat";
    $wallet_status  = 'active';
    $cwallet_id     = null;
    $wallet_qr      = null;

    // Check for existing wallet for this user & account_number
    $stmt = $conn->prepare("SELECT wallet_id FROM user_wallets WHERE user_id = ? AND coin = ? AND wallet_add = ?");
    $stmt->bind_param("iss", $user_id, $coin, $wallet_address);
    $stmt->execute();
    $existing = $stmt->get_result()->fetch_assoc();

    if (!$existing) {
        $stmt = $conn->prepare(
            "INSERT INTO user_wallets 
            (cwallet_id, user_id, wallet_add, wallet_qr, coin, icon, balance, type, label, wallet_status, bank_name, updated_at, created_at)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())"
        );
        $stmt->bind_param(
            "iissssdssss",
            $cwallet_id, $user_id, $wallet_address, $wallet_qr, $coin, $icon, $balance, $type, $label, $wallet_status, $bank_name
        );
        $stmt->execute();
        $wallet_id = $conn->insert_id;
    }

    // Redirect to success page
    header("Location: ../../user/fiat/fiat_successful.php?wallet_id=" . $wallet_id);
    exit;
} else {
    $msg = $result['message'] ?? 'Failed to create virtual account.';
    exit("Failed to create virtual account. ($msg)");
}
?>

Выполнить команду


Для локальной разработки. Не используйте в интернете!