Jump to content

API remotely create user and order


lcbo

Recommended Posts

Hello

Would you please help me with this ?

I need an api external request to WHMCS and the licensing add-on to create a client and an order in the same time.

 

Also if the clients exists the api call should create only the correlative order.

 

I managed only to create the order for an existing client.

I followed the api documentation but I was unable to call the api for a client creation.

 

A difficulty exists: the order creation api is based on the client ID but if I create in the same time the client and order I have no idea how to get the client ID in order to use it for the order creation.

 

Thank you for any clue or code snippet.

Link to comment
Share on other sites

I have setup a remote payment system that once the payment is done, it calls a local file on my wHMCS install that will do the following:

 

1. check if the user exists

2. if the user isnt registered it creates an account and add an order

3. if the client exists it add the order to his account only. In this case the script will skip the account creation. Here it is my issue. Have no idea how to write the logic in order to skip the user creation if the account already exists.

 

I am testing this snippet found on a forum. It works but as I said, I have no idea how to skip the user creation if the user already exists.

 

Thank you for any help on this.

 

 

Here it is:

 

 

<?php

$url = "http://domain.com/customers/includes/api.php"; # URL to WHMCS API file goes here
$apiusername = "myusername"; # Admin username goes here
$apipassword = "mypass"; # Admin password goes here


if ($_POST["action"]=="submit") {

# Get Values
$domain = trim(htmlentities($_POST["domain"]));
$firstname = trim(htmlentities($_POST["firstname"]));
$lastname = trim(htmlentities($_POST["lastname"]));
$companyname = trim(htmlentities($_POST["companyname"]));
$email = trim(htmlentities($_POST["email"]));
$address1 = trim(htmlentities($_POST["address1"]));
$address2 = trim(htmlentities($_POST["address2"]));
$city = trim(htmlentities($_POST["city"]));
$state = trim(htmlentities($_POST["state"]));
$postcode = trim(htmlentities($_POST["postcode"]));
$country = trim(htmlentities($_POST["country"]));
$phonenumber = trim(htmlentities($_POST["phonenumber"]));
$password = trim(htmlentities($_POST["password"]));
$password2 = trim(htmlentities($_POST["password2"]));
$customfield1 = trim(htmlentities($_POST["customfield1"]));
$securityqans = trim(htmlentities($_POST["securityqans"]));
$tosagreement = $_POST["accepttos"];

# Error Checking
if ($domain == "") { 
 $errors[] = "You did not enter a domain"; 
}  
if (ereg('[^a-z0-9.-]', $domain)) {
$errors[] = "The domain you entered is not valid.";
}
if (!$firstname) {
$errors[] = "You did not enter your first name.";
}
if (!$lastname) {
$errors[] = "You did not enter your last name.";
}
if (!$email) {
$errors[] = "You did not enter your email address";
} elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) {
$errors[] = "The email address you entered is invalid.";
}
if (!$address1) {
$errors[] = "You did not enter the first line of your address.";
}
if (!$city) {
$errors[] = "You did not enter your city.";
}
if (!$state) {
$errors[] = "You did not enter your state.";
}
if (!$postcode) {
$errors[] = "You did not enter your postcode.";
}
if (!$country) {
$errors[] = "You did not enter your country.";
}
if (!$phonenumber) {
$errors[] = "You did not enter your phone number.";
}
if (!$password) {
$errors[] = "You must enter a password";
} elseif ($password!=$password2) {
$errors[] = "The password you entered did not match.";
}
if (!$customfield1) {
$errors[] = "You must choose a forum username.";
}
if (!$securityqans) {
$errors[] = "You must provide an answer to your secret question.";
}
if (!$tosagreement) {
$errors[] = "You must agree to the terms of service.";
}

if (!$errors) {

# Submit Order

$postfields = array();
$postfields["username"] = $apiusername;
$postfields["password"] = md5($apipassword);
$postfields["action"] = "addclient";
$postfields["firstname"] = $firstname;
$postfields["lastname"] = $lastname;
$postfields["companyname"] = $companyname;
$postfields["email"] = $email;
$postfields["address1"] = $address1;
$postfields["address2"] = $address2;
$postfields["city"] = $city;
$postfields["state"] = $state;
$postfields["postcode"] = $postcode;
$postfields["country"] = $country;
$postfields["phonenumber"] = '0000000000000000';
$postfields["password2"] = $password;
$postfields["securityqid"] = $securityqid;
$postfields["securityqans"] = $securityqans;
$postfields["customfield[1]"] = $customfield1;

$query_string = "";
foreach ($postfields AS $k=>$v) {
$query_string .= "$k=".urlencode($v)."&";
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
if (curl_error($ch)) die("CURL Error: ".curl_error($ch));
curl_close($ch);

$data = explode(";",$data);
foreach ($data AS $temp) {
$temp = explode("=",$temp);
$results[$temp[0]] = $temp[1];
}

if ($results["result"]=="success") {
$clientid = $results["clientid"];
} else {
echo 'llop';
die("An error occured1. Please contact support. ({$results['message']})");
}

$postfields = array();
$postfields["username"] = $apiusername;
$postfields["password"] = md5($apipassword);
$postfields["action"] = "addorder";
$postfields["clientid"] = $clientid;
$postfields["pid"] = '57';
$postfields["domain"] = $domain;
$postfields["domaintype"] = $domaintype;
$postfields["billingcycle"] = $billingcycle;
$postfields["paymentmethod"] = 'paypal';

$query_string = "";
foreach ($postfields AS $k=>$v) {
$query_string .= "$k=".urlencode($v)."&";
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
if (curl_error($ch)) die("CURL Error: ".curl_error($ch));
curl_close($ch);

$data = explode(";",$data);
foreach ($data AS $temp) {
$temp = explode("=",$temp);
$results[$temp[0]] = $temp[1];
}

if ($results["result"]=="success") {
$invoiceid = $results["invoiceid"];
header("Location: ".$whmcsurl."dologin.php?username=$email&password=$password&goto=viewinvoice&id=$invoiceid");
exit;
} else {
die("An error occured2. Please contact support. ({$results['message']})");
}

}

}

?><html>
<head>
<title>Order Form</title>
</head>
<body>

<h1>Order Form</h1>

<?php
if ($errors) {
echo "<p>The following errors occured.</p><ul>";
foreach ($errors AS $error) {
echo "<li>$error</li>";
}
echo "</ul>";
}
?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
<input type="hidden" name="action" value="submit" />
<table>
<tr>
<td>First Name:</td><td><input type="text" name="firstname" size="30" value="<?php echo $firstname ?>" /></td>
<td>Last Name:</td><td><input type="text" name="lastname" size="30" value="<?php echo $lastname ?>" /></td>
</tr>
<tr>
<td>Address 1:</td><td><input type="text" name="address1" value="<?php echo $address1 ?>" size="30" /></td>
<td>Address 2:</td><td><input type="text" name="address2" value="<?php echo $address2 ?>" size="30" /></td>
</tr>
<tr>
<td>Company Name:</td><td><input type="text" name="companyname" value="<?php echo $companyname ?>" size="30" /></td>
<td>E-mail Address:</td><td><input type="text" name="email" value="<?php echo $email ?>" size="30" /></td>
</tr>
<tr>
<td>City:</td><td><input type="text" name="city" size="30" value="<?php echo $city ?>" /></td>
<td>State:</td><td><input type="text" name="state" size="30" value="<?php echo $state ?>" /></td>
</tr>
<tr>
<td>Zip Code:</td><td><input type="text" name="postcode" size="30" value="<?php echo $postcode ?>" /></td>
<td>Country:</td><td><select name="country"><option value="AU">Australia</option><option value="CA">Canada</option><option value="IE">Ireland</option><option value="GB">United Kingdom</option><option value="US" selected="selected">United States</option></select></td>
</tr>
<tr>
<td>Phone Number:</td><td><input type="text" name="phonenumber" size="30" value="<?php echo $phonenumber ?>" /></td>
<td>(Used by our automated anti-fraud system.)</td><td></td>
</tr>
<tr>
<td>Domain Name:</td><td><input type="text" name="domain" size="20" value="<?php echo $domain ?>" />.CXR.CC</td>
<td>(For example.cxr.cc type: <i>example</i>)</td><td></td>
</tr>
<tr>
<td>Forum Username:</td><td><input type="text" size="30" name="customfield1" value=""></td>
<td>(Automatically Generated)</td><td></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" name="password" size="30" /></td>
<td>Confirm Password</td><td><input type="password" name="password2" size="30" /></td>
</tr>
<tr>
<td>Security Question:</td><td><select name="securityqid">
<option value=2>Mother's maiden name?</option>
<option value=3>Name of first pet?</option>
<option value=4>Color of first car?</option>
<option value=5>Favorite color?</option>
</select></td>
<td>Security Answer:</td><td><input type="password" name="securityqans" size="30"></td>
</tr>
<tr>
<td colspan="4">
<input type="hidden" name="domaintype" value="selsubdomain" />
<input type="hidden" name="paymentmethod" value="paypal" />
<input type="hidden" name="pid" value="16" />
<input type="hidden" name="billingcycle" value="free" />
<input type="checkbox" name="accepttos" id="accepttos" />I have read and agree to the <a href="http://www.jweb2.com/tos.php" target="_blank">Terms of Service</a><br />
<strong>Please have your phone available because we use an automated telephone system to verify high-risk orders.</strong>
<p align="center"><input type="submit" value="Submit Order" /></p>
</td>
</tr>
</table>
</form>
</body>
</html>

Edited by lcbo
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