Sunday, May 26, 2019

WHMCS sell in multiple currencies for the same client


# Hi there,
# So recently I ran into this weird scenario where I had to sell Product A in USD and Product B in EUR for same client on my website xsl.tel. (
in current WHMCS currency system the client can choose 1 currency at registration and have to create new account using new Email to use another currency)
)
# Unfortunately WHMCS don't have such feature due to its core design (database structure) and I couldn't find an addon to help with this scenario.
# I had to use my brain to figure out something so I came up with this solution .
# Basically we will use our licensed WHMCS installation to work with multiple databases based on client cookie we set through a small php script and then redirect coming client to requested WHMCS page.

# Pros
# * -> With this solution you can share your current clients with sub installations through DB Views (or you can your clients spreaded over multi databases)  .
# * -> You can run unlimited numbers of WHMCS licenses on same installation (testing, developing, etc...) .
# * -> You can have multi-currency / multi-language WHMCS installations .
# * -> You can run cron for every new system you create.

# Cons
# * -> Reply-to-answer ticket feature via pipe is lost because you can't pass parameter to pipe.php file. (however it is possible through pop3 import feature).
# * -> Paypal IPN won't work on sub installations (it send IPN to main installation only).
# * -> It may not work with a few modules .I didn't test everything.
# * -> you can login to one system per browser for both admin/client .
# * -> Enabling WHMCS auto data retention removed my clients data when I did the optional step of merging both system clients data .


# Let's get to work
# You will need to create new database with our main whmcs db in this tutorial its called eudb_name
# Below steps assumes that you already have installed WHMCS and don't have any data on it .
# Edit your configuration.php as follows (don't forget to change database name, database usernames, db user passwords) .
//======= BEGIN of configuration.php content
<?php
$license = 'YOURLICENSE';
$db_host = 'localhost';
switch($_COOKIE['clientbase']) {
    case "us":
        $db_username = 'usdb_usr';
        $db_password = 'usdb_password';
        $db_name = 'usdb_name';
        $cc_encryption_hash = 'some_random_chars_here';
        $mysql_charset = 'utf8';
        $attachments_dir = "/path/to/us/attachments";
        $downloads_dir = "/path/to/us/downloads/";
        $templates_compiledir = "/path/to/us/templates_c/";
        break;
    case "eu":
        $db_username = 'eudb_usr';
        $db_password = 'eudb_password';
        $db_name = 'eudb_name';
        $cc_encryption_hash = 'some_random_chars_here';
        $mysql_charset = 'utf8';
        $attachments_dir = "/path/to/eu/attachments";
        $downloads_dir = "/path/to/eu/downloads/";
        $templates_compiledir = "/path/to/eu/templates_c/";
        break;

    // if no cookie is set then we pass default us db to our client
    default:
        $db_username = 'usdb_usr';
        $db_password = 'usdb_password';
        $db_name = 'usdb_name';
        $cc_encryption_hash = 'some_random_chars_here';
        $mysql_charset = 'utf8';
        $attachments_dir = "/path/to/us/attachments";
        $downloads_dir = "/path/to/us/downloads/";
        $templates_compiledir = "/path/to/us/templates_c/";
        break;

}
//======= END of configuration.php content

# Then in your public html folder create a new folder and let's call it "us"
# and create .htaccess and index.php files and edit them as follows

#======= BEGIN of .htaccess content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [QSA,PT,L]

#======= END of .htaccess edit

# make sure to change domain in cookie you set and the url of your whmcs installation
//======= BEGIN of index.php content
<?php<?php
setcookie("clientbase", "us",NULL, "/", "xsl.tel", 1, 1);
$args = $_REQUEST;
unset($args['uri']);
$newArgs= array();
foreach($args as $key => $value) {
    $newArgs[] = "{$key}={$value}";
}
$queryargs = implode('&',$newArgs);
header("Location: https://my.
xsl.tel/{$_GET['uri']}?{$queryargs}");
//======= END of index.php content

# now create another folder and let's call it "eu" for our EUR currency create same files except changing index.php as follows (just change cookie value from us to eu so our configuration.php serve eudb_name)
//======= BEGIN of index.php content
<?php
setcookie("clientbase", "eu",NULL, "/", "
xsl.tel", 1, 1);
$args = $_REQUEST;
unset($args['uri']);
$newArgs= array();
foreach($args as $key => $value) {
    $newArgs[] = "{$key}={$value}";
}
$queryargs = implode('&',$newArgs);
header("Location: https://my.
xsl.tel/{$_GET['uri']}?{$queryargs}");//======= END of index.php content

# MySQL work
# now you have to mysqldump/export your current whmcs database (usdb_name) in this tutorial and import it on our (eudb_name) or whatever your new db_name

# now if you navigate to your whmcs.domain/eu/ (change name to something different to see if it differs from whmcs.domain/us/) .
# If everything works as expected let's continue to setting up cron jobs for new systems.

# Looks like we can't pass cookie through command line so we will run cron through wget using this command  (don't forget to change domain)
#======= BEGIN of cron job
*/5     *     *     *     *     wget -O /dev/null https://my.xsl.tel/us/crons/cron.php
> /dev/null 2>&1
*/5     *     *     *     *     wget -O /dev/null https://my.xsl.tel/eu/crons/cron.php
> /dev/null 2>&1
 #======= END of cron job

# Only use if you want to use pop3 import feature to import tickets to your sub-systems
#======= BEGIN of cron job
*/5     *     *     *     *     wget -O /dev/null https://my.xsl.tel/us/crons/pop.php > /dev/null 2>&1
*/5     *     *     *     *     wget -O /dev/null https://my.xsl.tel/eu/crons/pop.php
> /dev/null 2>&1
 #======= END of cron job

# For security reasons you may want to restrict accessing /crons/ folder from your server IP only to avoid bots bombing your cron.php with requests so create .htaccess file with following directives
#======= BEGIN of .htaccess content
Order Allow,Deny
Allow from IP-OF-SERVER 127.0.0.1

#======= END of .htaccess content

# Login to our new /eu/ whmcs admin panel and change currency to EUR and WHMCS system url to corresponding folders we created above and you are done.

# Optional Step merging clients & contacts of main usdb_name and eudb_name RUN this query in mysql as root/permitted user for both databases
#======= BEGIN of MySQL Clients merge
DROP TABLE eudb_name.tblclients;
CREATE VIEW eudb_name.tblclients AS SELECT * FROM usdb_name.tblclients;


 DROP TABLE eudb_name.tblcontacts;
CREATE VIEW eudb_name.tblcontacts AS SELECT * FROM usdb_name.tblcontacts;

#======= END of MySQL Clients merge


No comments:

Post a Comment

How to setup Reverse Proxy account on xsl.tel

Hi there, if you would like to use reverse proxy service to hide your website IP, bypass firewall restrictions (if you have a service runn...