Author: whmcsisg8

1 Jul

Stripe alipay module for WHMCS

Stripe payment API integration with WHMCS is easy to do. This article covered payment using stripe with alipay.

1) First let’s create a source using the below code

 $response = \Stripe\Source::create(array(
      "type" => "alipay",
      "currency" => "usd",
      "amount" => $params['amount'] * 100,
      "metadata" => array(  
          "invoiceid" => $invoiceid,
          "userid" => $params['clientdetails']['userid'],
          "order_id" => $invoiceid . "_" . date( "YmdHis" )
      ),
      "redirect" => array(
        "return_url" => $systemUrl . "/modules/gateways/callback/stripe_alipay.php?invoiceid=$invoiceid"
      ),
    ));

2)Then we need to make use of the hook feature of stripe.

Hooks we have to use are
surce.chargeable, charge.succeeded.

From surce.chargeable we have to create charge as

 $charge = \Stripe\Charge::create(array(
        "amount" => $amount,
        "currency" => "usd",
        "source" => $source_id,
    ));

And then from charge.succeeded, We have to add transaction details as shown below.

    $source_meta_data = $event_json->data->object->source;
    $source_id = $event_json->data->object->id;
    $amount = ($event_json->data->object->amount) / (100.00);
    $invoiceid = $source_meta_data->metadata->invoiceid;
    $order_id = $source_meta_data->metadata->order_id;
    $userid = $source_meta_data->metadata->userid;
    $invoiceid = checkCbInvoiceID($invoiceid, "stripe_alipay");
    addInvoicePayment($invoiceid, $source_id, $amount, 0, "stripe_alipay");
    logTransaction("stripe_alipay", json_decode($body, true), "Successful");

This is a high level idea of the gateway module development.
If you would like to develop a stripe module or need more info, please contact from the link
Contact Us.
We will get back to you asap.

7 May

Smarty for WHMCS

In MVC, we always use view to display html contents. If we are not using any template engine, then we have to use php tags in the html page to add dynamic contents.

For example, we can use below code to loop through an array without any template engine.

<?php foreach ($users as $user) : ?>
<div>
    <h2><?php echo $user['name']; ?></h2>
    <p><?php echo $user['company']; ?></p>
</div>
<?php endforeach; ?>

Here we are using php tags to loop through the array. Also we are using php function to print it. WHMCS uses Smarty template engine, and we have to do loop in a different manner with Smarty template engine.

{foreach from=$users item=user}
	<h2>{$user.name}</h2>
	<p>{$user.company}</p>
{/foreach}

This is just an example that shows the difference between Smarty and normal view files. For normal view files, we add .php as extension. But for Smarty files, by default .tpl is the extension of the view files.

Now let’s explore more smarty examples.
Let’s fist create a WHMCS page.

For any custom client area page, we have to add the below content at the top of the page.

<?php

//Let's include whmcs classes
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;

define('CLIENTAREA', true);

require __DIR__ . '/init.php';

$ca = new ClientArea();
$ca->initPage();

Let’s name our custom page as custom_page.php and upload it to the root folder of whmcs.
Now , we have the content required for any client area page on custom_page.php.
Now let’s display some html using the php file.
For that we have to create a tpl file.

Before that, let’s discuss about client area theming.

With whmcs, there are many themes available. For each theme,
there will be different css, images etc. Templates are stored in the folder templates/.
WHMCS has a default client area template with the name ‘six’. So the client area .tpl files are stored in the folder templates/six.

Now, back to our custom_page.php. For this page, we have to create a tpl file to add html contents. Let’s name the tpl file as custom_page.tpl, then it should be uploaded to the folder templates/six.

So our tpl file full path is templates/six/custom_page.tpl

Let’s just add content as

<p>Hello world!!!! </p>

To use the above file as tpl file, we have to define that in custom_page.php

That can be one as

$ca->setTemplate('custom_page');  //it will load the templates/six/custom_page.tpl

if our tpl file name was test.tpl, then we could load that as

$ca->setTemplate('test');  //it will load templates/six/test.tpl

Then to print the content, we have to add below line to the custom_page.php

$ca->output();

So the full custom_page.php is

<?php

//Let's include whmcs classes
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;

define('CLIENTAREA', true);

require __DIR__ . '/init.php';

$ca = new ClientArea();
$ca->initPage();

$ca->setTemplate('custom_page'); 
$ca->output();

And our templates/six/custom_page.tpl content is

<p>Hellow world!!!! </p>

Then browse the url
http://whmcssite.com/custom_page.php

And you will see the html content
Hello world!!!!

Now we know how to display static content using smarty.

Now let’s see how to display dynamic contents using smarty.
For that, now let’s just display the logged in user’s email in the tpl.

We have to add the below code to the php file after the line $ca->initPage();

//get the logged in user id
$loggedin_user_id =  $_SESSION['uid'];
$client = Capsule::table('tblclients')
        ->where('id', '=', $loggedin_user_id)
        ->first();
$ca->assign('client_email', $client->email);

And below code to the tpl

<p>
        Client email is: {$client_email}
</p>

So the trick is, we havee to fetch what ever data to be displayed using a query and then pass that to the
tpl as

$ca->assign('client_email', $client->email);

Then we can just access that from the tpl as {$client_email}

If we were passing the email as

$ca->assign('clientMail', $client->email);

Then we can access it from the tpl as

{$clientMail}

We can do a lot more with smarty, Some of them are explained in the below links.
https://www.smarty.net/docsv2/en/language.function.if.tpl
https://www.smarty.net/docs/en/language.function.foreach.tpl
https://www.smarty.net/docs/en/language.variables.tpl
https://www.smarty.net/docs/en/language.function.include.tpl
https://www.smarty.net/docs/en/language.syntax.variables.tpl

~

31 Mar

Useful Laravel Commands

#Reload the database
php artisan migrate:fresh –seed

#Install new packages and the compile
`composer install` & `npm install`

#Create a seeder file
php artisan make:seeder NewModelSeeder

#Run a single seeder file
php artisan db:seed –class=NewModelSeeder

#Create a model
php artisan make:model MyNewModel

#Create a controller
php artisan make:controller MyNewController –resource

#Create a migration file
php artisan make:migration create_my_new_table –create=my_new_table

#Run migration
php artisan migrate

20 Dec

Install Laravel on Ubuntu with XAMPP

The steps to install Laravel on Ubuntu with XAMPP are.

Step 1:
Install composer using the below command.
sudo curl -s https://getcomposer.org/installer | /opt/lampp/bin/php

Step 2:
Create a soft link
sudo ln -s /opt/lampp/bin/php /usr/local/bin/php

Step 3:
Move the composer.phar to the bin folder by using the below command.
mv composer.phar /usr/local/bin/composer

Step 4:
Run the below command to install the Laravel.
composer global require "laravel/installer"

Step 5:
Create a Laravel project.
/home/username/.config/composer/vendor/bin/laravel new test_1

Step 6:
Go to the project folder
cd /home/username/test_1

Step 7:
Start the Laravel app.
/opt/lampp/bin/php artisan serve

You will get response as
Laravel development server started:

11 Dec

How to get WHMCS date format?

To know the date format is set in the WHMCS, we can use the below query.

$date_format =  Capsule::table('tblconfiguration')
              ->where('setting', '=', 'DateFormat')
              ->value('value');

Some of the example date formats are MM-DD-YYYY, MM/DD/YYYY, DD-MM-YYYY etc.

We can convert that to the classical d m Y format by using the below code.

            $date_format = str_replace("DD", "d", $date_format);
            $date_format = str_replace("MM", "m", $date_format);
            $date_format = str_replace("YYYY", "Y", $date_format);
3 Oct

WHMCS Merchant Gateway Development

This type of WHMCS gateways can be used for payment using credit card. Let’s create a payment gateway with name test. For that we have to create a file named test.php and then we have to upload it to the folder modules/gateways.

Live mode and test mode.

Each payment gateway provides some test credit card details, we can use that for test payment.
In test mode, for any payment there won’t be any actual transaction.
In live mode, we have to use real credit card details and real transaction will happen in this mode. Payment gateway will give us both live url and test url for real and test payments respectively.

Our gateway module will decide weather we have to do real or test transactions based on the settings in the module config.

'testMode' => array(
            'FriendlyName' => 'Test Mode',
            'Type' => 'yesno',
            'Description' => 'Tick to enable test mode',
        ),

If we select testMode, then only test payments will work. If it’s unselected real transaction will happen.

Initially the test.php file looks like

<?php

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

function test_MetaData()
{
    return array(
        'DisplayName' => 'Test payment gateway',
        'APIVersion' => '1.0',
        'DisableLocalCredtCardInput' => true,
        'TokenisedStorage' => false,
    );
}

function test_config()
{
    return array(
        'FriendlyName' => array(
            'Type' => 'System',
            'Value' => 'Test payment gateway',
        ),
        'live_url' => array(
            'FriendlyName' => 'Live URL',
            'Type' => 'text',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter your live url here',
        ),
        'test_url' => array(
            'FriendlyName' => 'Test URL',
            'Type' => 'text',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter your test url here',
        ),
        'testMode' => array(
            'FriendlyName' => 'Test Mode',
            'Type' => 'yesno',
            'Description' => 'Tick to enable test mode',
        ),
        'user_id' => array(
            'FriendlyName' => 'User Id',
            'Type' => 'text',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter your user id here',
        ),
    );
}


function test_capture($params) {

        if($params['testMode'] == 'on') { //testmode
          $url = $params['test_url'];
        } else { // live mode
          $url = $params['live_url'];
        }
}
?>

We have to add payment code into the function, test_capture($params).
Inside that we have to use payment transfer code. Each gateway API will be diffrent, so the payment transfer code will be different for each gateway.

Let’s write a payment transfer code for an arbitrary gateway.

function test_capture($params) {
     $postfields = [
        'invoiceid' => $params['invoiceid'],
        'amount' => $params['amount'],
        'currency' => $params['currency'],
        'cardnumber' => $params['cardnum'],
        'cardexpiry' => $params['cardexp'],
        'cardcvv' => $params['cccvv'],
    ];

    if($params['testMode'] == 'on') { //testmode
          $url = 'https://test.myarbitrarygateway/api/payment';
    } else { // live mode
          $url = 'https://myarbitrarygateway/api/payment';
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_docode($response);

    return array(
        'status' => ($data->success == 1) ? 'success' : 'declined',
        'rawdata' => $data,
        'transid' => $data->transaction_id,
        'fees' => $data->fees,
    ); 
}
25 Sep

What is mean by WHMCS hook?

Standard way of modifying WHMCS features is using hook. WHMCS code is encrypted and we couldn’t change it. In addition to it, modifying core file is a bad idea. When we update WHMCS to the latest version all of our core file changes will be removed. So hook is a nice way to customize WHMCS features.

There are many hooks available in WHMCS. Available WHMCS hooks can be found in the link
https://developers.whmcs.com/hooks/hook-index/

Some of them can be used to run a piece of code just after an action is happened(ClientAdd executes just after a client is added to WHMCS). Some of them can be used used to run a piece of code just before an action(PreDeleteClient executes just before a client is deleted). There are hooks that can be used to run on certain pages(ClientAreaPageHome executes on the client area homepage).

Hooks can be include in the WHMCS in two ways.
1)We can create a php file with any name and can upload to the folder
/includes/hooks/.
2)We can create a file named hooks.php as a part of the addon module.

To get a better idea, let’s go through some examples now.
For now let’s just upload sample files to the folder /includes/hooks/.

Let’s add a hook ‘ClientAreaPage’ to a file named sample.php,
then let’s upload to the folder /includes/hooks/.
(see https://developers.whmcs.com/hooks-reference/client-area-interface/#clientareapage)

<?php
add_hook('ClientAreaPage', 1, function($vars) {
    echo "this is from clieant area hook";
    exit;
});

Then take any page in the client side, you will get a blank page with content
this is from clieant area hook. As we have exit in the code, nothing else will be displayed.

Now let’s modify the hook as shown below.

<?php
add_hook('ClientAreaPage', 1, function($vars) {
    echo "<pre>"; print_r($vars);
    exit;
});

Now the page will display an array.

To remove the hook, just delete the file from the folder /hooks/hook-index/.

Now let’s see another hook, ClientAreaPageCart.
Add the below code to a file named cart_hook.php and then upload to the folder
/includes/hooks/.

<?php
add_hook('ClientAreaPageCart', 1, function($vars) {
        echo "<pre>"; print_r($vars);
        exit;
});

This hook won’t run in all of the client are pages. But it will run in all of the
cart pages. For example take the url /cart.php then you will see an array printed in it.

21 Sep

How to use Smarty if else?

Let’s assign a discount amount from PHP to the tpl file as shown below.

$smarty->assign('discount', 0); 
{if $discount gt 0}
   You have a discount and discount amount is: {$discount}
{else}
	You don't have any discount.
{/if}

Now let’s assign some string to the tpl file as shown below.
Here we compare two strings equality using ‘eq’

$smarty->assign('category', 'Electronics'); 

Then in the tpl file, we can check the category as shown below.

{if $category eq 'Electronics'}
    As you are booking an electronics product, you will get a discount of 15%.
{else}
    For this product, we have a discount of 5%.
{/if}

For detailed explanation regarding the usage of if else in tpl,
go to the link https://www.smarty.net/docsv2/en/language.function.if.tpl

21 Sep

How to use Smarty foreach?

You can pass an array to the smarty tpl file as shown below.

<?php
$user_details = array(
	"name" => "x",
	"address" => "Y",
        "phone" => "Z"
);
$smarty->assign('my_array', $user_details);  //my_array can be replaced with anything

Then in the tpl file you can loop through the array as shown below

<ul>
{*    
   Note: We used my_array here because we passed array as
   assign('my_array', $user_details);
*}

{foreach from=$my_array key=k item=v}
   <li>{$k}: {$v}</li>
{/foreach}
</ul>

If we are passing an array without key in it say

$user_details = array(
	"x", "y", "z"
);
$smarty->assign('my_array', $user_details); 

Then we can display the values in the tpl as shown below

<ul>
{foreach from=$my_array item=v}
   <li>{$v}</li>
{/foreach}
</ul>