Jump to content

Gateway fees module


Recommended Posts

So ive come across this module that is open source and thought I would share it and if anyone can improve the code that would be awesome!

 

[h=1]Gateway Fees Plugin for WHMCS[/h] Version: 1.0.1

This is an open-source version of the Gateway Fees plugin for WHMCS, it will allow you to set percentage fees on prices of your product in WHMCS.

To install this addon module, upload the gateway_fees directory to /modules/addons/.

Go to Setup -> Addon Modules, active and configure price ($ and/or % per transaction).

Your activated payment gateways will be automaticly shown there. You can set fixed fee or/and some percent of the total.

 

 

/modules/addons/gateway_fees/gateway_fees.php

<?php                       
                      if (!defined("WHMCS")) die("This file cannot be accessed directly");                       
                      function gateway_fees_config()                       {                       	$configarray = array(                       		"name" => "Gateway Fees for WHMCS",                       		"description" => "Add fees based on the gateway being used.",                       		"version" => "1.0.1",                       		"author" => "Open Source"                       	);                       	$result = select_query("tblpaymentgateways", "", "", "", "");                       	while ($data = mysql_fetch_array($result)) {                       		$configarray['fields']["fee_1_" . $data['gateway']] = array(                       			"FriendlyName" => $data['gateway'],                       			"Type" => "text",                       			"Default" => "0.00",                       			"Description" => "$"                       		);                       		$configarray['fields']["fee_2_" . $data['gateway']] = array(                       			"FriendlyName" => $data['gateway'],                       			"Type" => "text",                       			"Default" => "0.00",                       			"Description" => "%<br />"                       		);                       	}                       
                      	return $configarray;                       }                       
                      function gateway_fees_activate()                       {                       	$result = mysql_query('select * from tblpaymentgateways group by gateway');                       	while ($data = mysql_fetch_array($result)) {                       		$query2 = "insert into `tbladdonmodules` (module,setting,value) value ('gateway_fees','fee_1_" . $data['gateway'] . "','0.00' )";                       		$result2 = mysql_query($query2);                       		$query3 = "insert into `tbladdonmodules` (module,setting,value) value ('gateway_fees','fee_2_" . $data['gateway'] . "','0.00' )";                       		$result3 = mysql_query($query3);                       	}                       }                       
                      ?>

 

/modules/addons/gateway_fees/hooks.php

 

<?php                       
                      function update_gateway_fee3($vars)                       {                           $id = $vars['invoiceid'];                           updateInvoiceTotal($id);                       }                       
                      function update_gateway_fee1($vars)                       {                           $id = $vars['invoiceid'];                           $result = select_query("tblinvoices", '', "id='" . $id . "'", "", "");                           $data = mysql_fetch_array($result);                           update_gateway_fee2(array(                               'paymentmethod' => $data['paymentmethod'],                               "invoiceid" => $data['id']                           ));                       }                       
                      function update_gateway_fee2($vars)                       {                           $paymentmethod = $vars['paymentmethod'];                           delete_query("tblinvoiceitems", "invoiceid='" . $vars[invoiceid] . "' and notes='gateway_fees'");                           $result = select_query("tbladdonmodules", "setting,value", "setting='fee_2_" . $vars['paymentmethod'] . "' or setting='fee_1_" . $vars[paymentmethod] . "'");                           while ($data = mysql_fetch_array($result)) {                               $params[$data[0]] = $data[1];                           }                       
                          $fee1 = ($params['fee_1_' . $paymentmethod]);                           $fee2 = ($params['fee_2_' . $paymentmethod]);                           $total = InvoiceTotal($vars['invoiceid']);                           if ($total > 0) {                               $amountdue = $fee1 + $total * $fee2 / 100;                               if ($fee1 > 0 & $fee2 > 0) {                                   $d = $fee1 . '+' . $fee2 . "%";                               }                               elseif ($fee2 > 0) {                                   $d = $fee2 . "%";                               }                               elseif ($fee1 > 0) {                                   $d = $fee1;                               }                           }                       
                          if ($d) {                               insert_query("tblinvoiceitems", array(                                   "userid" => $_SESSION['uid'],                                   "invoiceid" => $vars[invoiceid],                                   "type" => "Fee",                                   "notes" => "gateway_fees",                                   "description" => getGatewayName2($vars['paymentmethod']) . " Fees ($d)",                                   "amount" => $amountdue,                                   "taxed" => "0",                                   "duedate" => "now()",                                   "paymentmethod" => $vars[paymentmethod]                               ));                           }                       
                          updateInvoiceTotal($vars[invoiceid]);                       }                       
                      add_hook("InvoiceChangeGateway", 1, "update_gateway_fee2");                       add_hook("InvoiceCreated", 1, "update_gateway_fee1");                       add_hook("AdminInvoicesControlsOutput", 2, "update_gateway_fee3");                       add_hook("AdminInvoicesControlsOutput", 1, "update_gateway_fee1");                       add_hook("InvoiceCreationAdminArea", 1, "update_gateway_fee1");                       add_hook("InvoiceCreationAdminArea", 2, "update_gateway_fee3");                       
                      function InvoiceTotal($id)                       {                           global $CONFIG;                           $result = select_query("tblinvoiceitems", "", array(                               "invoiceid" => $id                           ));                           while ($data = mysql_fetch_array($result)) {                               if ($data['taxed'] == "1") {                                   $taxsubtotal+= $data['amount'];                               }                               else {                                   $nontaxsubtotal+= $data['amount'];                               }                           }                       
                          $subtotal = $total = $nontaxsubtotal + $taxsubtotal;                           $result = select_query("tblinvoices", "userid,credit,taxrate,taxrate2", array(                               "id" => $id                           ));                           $data = mysql_fetch_array($result);                           $userid = $data['userid'];                           $credit = $data['credit'];                           $taxrate = $data['taxrate'];                           $taxrate2 = $data['taxrate2'];                           if (!function_exists("getClientsDetails")) {                               require_once (dirname(__FILE__) . "/clientfunctions.php");                       
                          }                       
                          $clientsdetails = getClientsDetails($userid);                           $tax = $tax2 = 0;                           if ($CONFIG['TaxEnabled'] == "on" && !$clientsdetails['taxexempt']) {                               if ($taxrate != "0.00") {                                   if ($CONFIG['TaxType'] == "Inclusive") {                                       $taxrate = $taxrate / 100 + 1;                                       $calc1 = $taxsubtotal / $taxrate;                                       $tax = $taxsubtotal - $calc1;                                   }                                   else {                                       $taxrate = $taxrate / 100;                                       $tax = $taxsubtotal * $taxrate;                                   }                               }                       
                              if ($taxrate2 != "0.00") {                                   if ($CONFIG['TaxL2Compound']) {                                       $taxsubtotal+= $tax;                                   }                       
                                  if ($CONFIG['TaxType'] == "Inclusive") {                                       $taxrate2 = $taxrate2 / 100 + 1;                                       $calc1 = $taxsubtotal / $taxrate2;                                       $tax2 = $taxsubtotal - $calc1;                                   }                                   else {                                       $taxrate2 = $taxrate2 / 100;                                       $tax2 = $taxsubtotal * $taxrate2;                                   }                               }                       
                              $tax = round($tax, 2);                               $tax2 = round($tax2, 2);                           }                       
                          if ($CONFIG['TaxType'] == "Inclusive") {                               $subtotal = $subtotal - $tax - $tax2;                           }                           else {                               $total = $subtotal + $tax + $tax2;                           }                       
                          if (0 < $credit) {                               if ($total < $credit) {                                   $total = 0;                                   $remainingcredit = $total - $credit;                               }                               else {                                   $total-= $credit;                               }                           }                       
                          $subtotal = format_as_currency($subtotal);                           $tax = format_as_currency($tax);                           $total = format_as_currency($total);                           return $total;                       }                       
                      function getGatewayName2($modulename)                       {                           $result = select_query("tblpaymentgateways", "value", array(                               "gateway" => $modulename,                               "setting" => "name"                           ));                           $data = mysql_fetch_array($result);                           return $data["value"];                       }                       
                      ?>

 

- - - Updated - - -

 

https://github.com/delta360/WHMCS-Gateway-Fees

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