Jump to content

Unable to fetch data (Client Email, ID, Phone Number, Address etc.)


sysconfig

Recommended Posts

Hi,

i was trying to create a payment gateway that i will host on another website, there i created a php file but that php file was unable to fetch data from my whmcs.
Here is the related codes:
 

$whmcs_url = 'https://my.domain.com/includes/api.php';
$whmcs_username = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$whmcs_password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Function to call WHMCS API
function whmcs_api($action, $data) {
    $postFields = array();
    $postFields['username'] = $GLOBALS['whmcs_username'];
    $postFields['password'] = md5($GLOBALS['whmcs_password']);
    $postFields['action'] = $action;
    $postFields['responsetype'] = 'json';
    $postFields += $data;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $GLOBALS['whmcs_url']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

$clientEmail = $customerData['email']; // Replace with the customer's email
$clients = whmcs_api('GetClients', array('search' => $clientEmail));

if (isset($clients['numreturned']) && $clients['numreturned'] > 0) {
    $customerId = $clients['clients']['client'][0]['id'];
    $customerData = $clients['clients']['client'][0];
} else {
    echo "Customer not found in WHMCS.";
    exit;
}

$invoices = whmcs_api('GetInvoices', array('userid' => $customerId, 'status' => 'Unpaid'));

if (isset($invoices['numreturned']) && $invoices['numreturned'] > 0) {
    $invoiceId = $invoices['invoices']['invoice'][0]['id'];
    $invoiceAmount = $invoices['invoices']['invoice'][0]['total'];
    echo "Redirecting...<br>";
} else {
    echo "No unpaid invoice found for the customer.";
    exit;
}

$store_id = 'XXXXXXXXX';
$store_passwd = 'XXXXXXXXXXX';
$submit_url = 'https://example.com/gwprocess/v4/api.php';
$easy_url = 'https://example.com/embed.min.js';
$success_url = 'https://example.com/success.php';
$fail_url = 'https://example.com/fail.php';
$cancel_url = 'https://example.com/cancel.php';
$ipn_url = 'https://example.com/ipn.php';

// Prepare data for payment request
$post_data = array();
$post_data['store_id'] = $store_id;
$post_data['store_passwd'] = $store_passwd;
$post_data['total_amount'] = $invoiceAmount;
$post_data['currency'] = 'USD'; // Set your desired currency
$post_data['tran_id'] = $invoiceId;
$post_data['success_url'] = $success_url;
$post_data['fail_url'] = $fail_url;
$post_data['cancel_url'] = $cancel_url;
$post_data['ipn_url'] = $ipn_url;
$post_data['cus_name'] = $customerData['firstname'] . ' ' . $customerData['lastname'];
$post_data['cus_email'] = $customerData['email'];
$post_data['cus_phone'] = $customerData['phonenumber'];
$post_data['cus_add1'] = $customerData['address1'];
$post_data['cus_city'] = $customerData['city'];
$post_data['cus_state'] = $customerData['state'];
$post_data['cus_postcode'] = $customerData['postcode'];
$post_data['cus_country'] = $customerData['country'];
$post_data['value_a'] = 'Payment for Invoice ' . $invoiceId;
$post_data['value_b'] = 'returnurl'; // Replace with your desired return URL
$post_data['shipping_method'] = 'NO';
$post_data['num_of_item'] = '1';
$post_data['product_name'] = 'Invoice ' . $invoiceId;
$post_data['product_profile'] = 'general';
$post_data['product_category'] = 'Domain-Hosting';

if (isset($customerData['firstname']) && isset($customerData['lastname'])) {
    $post_data['cus_name'] = $customerData['firstname'] . ' ' . $customerData['lastname'];
}
if (isset($customerData['email'])) {
    $post_data['cus_email'] = $customerData['email'];
}
if (isset($customerData['phonenumber'])) {
    $post_data['cus_phone'] = $customerData['phonenumber'];
}
if (isset($customerData['address1'])) {
    $post_data['cus_add1'] = $customerData['address1'];
}
if (isset($customerData['city'])) {
    $post_data['cus_city'] = $customerData['city'];
}
if (isset($customerData['state'])) {
    $post_data['cus_state'] = $customerData['state'];
}
if (isset($customerData['postcode'])) {
    $post_data['cus_postcode'] = $customerData['postcode'];
}
if (isset($customerData['country'])) {
    $post_data['cus_country'] = $customerData['country'];
}

$hidden_fields = '';
foreach ($post_data as $key => $value) {
    $hidden_fields .= '<input type="hidden" name="' . $key . '" value="' . htmlentities($value) . '">';
}

 

Getting errors:
 

Notice: Undefined variable: customerData in /public_html/pay/pay.php on line 47
\
Notice: Undefined index: phonenumber in /public_html/pay/pay.php on line 93
Notice: Undefined index: address] in /public_htm/pay/pay.php on line 94
Notice: Undefined index: city in /public_html/pay/pay.php on line 95

Notice: Undefined index: state in /public_html/pay/pay.php on line 96

Notice: Undefined index: postcode in /public_html/pay/pay.php on line 97

Notice: Undefined index: country in /public_html/pay/pay.php on line 98

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated