diff --git a/en-GB.plg_payperdownloadplus_paystackpay.ini b/en-GB.plg_payperdownloadplus_paystackpay.ini index 4e1037f..fd2fbb8 100755 --- a/en-GB.plg_payperdownloadplus_paystackpay.ini +++ b/en-GB.plg_payperdownloadplus_paystackpay.ini @@ -23,4 +23,10 @@ COM_PAYSTACKPAY_EXTRACHARGE_VALUEDESC="The value of the extra charges" ; front -PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_PAYWITHPAYSTACK="Pay with PayStack" \ No newline at end of file +PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_PAYWITHPAYSTACK="Pay with PayStack" + +PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_ADDITIONALFEEWILLAPPLY="A payment charge of %s %s will apply" + +PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGPUBLICKEY="Missing public key for PayStackPay" +PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGSECRETKEY="Missing secret key for PayStackPay" +PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_USERMUSTBELOGGEDIN="User must be logged in" diff --git a/paystackpay.php b/paystackpay.php index 4364322..f51e20c 100755 --- a/paystackpay.php +++ b/paystackpay.php @@ -44,9 +44,10 @@ function log_transaction_success($trx_ref){ } } - class plgPayperDownloadPlusPaystackPay extends JPlugin { + protected static $tax_percentage; + public function __construct(&$subject, $config = array()) { parent::__construct($subject, $config); @@ -54,159 +55,168 @@ public function __construct(&$subject, $config = array()) $this->loadLanguage(); } - public function onRenderPaymentForm($user, $license, $resource, - $returnUrl, $thankyouUrl) + public function onRenderPaymentForm($user, $license, $resource, $returnUrl, $thankyouUrl) { - $siteUrl = JUri::base(); $package_price = array(); - $theuser = JFactory::getUser(); $pmntinfo = $this->getPaystackPaymentInfo(); $public_key = $pmntinfo->public_key; - $user_id = 0; - if($user) - { - $user_id = $user->id; $customer_email = $user->email; - $thename = explode(" ",$user->name); $firstname = $thename[0]; - // if(count($thename) > 1){$lastname = $thename[1];} - } - else{ - $user_id = $theuser->id; $customer_email = $theuser->email; - $thename = explode(" ",$theuser->name); $firstname = $thename[0]; - } - if($public_key && $customer_email) + if (empty($public_key)) { + echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGPUBLICKEY'); + return; + } + + $secret_key = $pmntinfo->secret_key; + + if (empty($secret_key)) { + echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_MISSINGSECRETKEY'); + return; + } + + if (!isset($user)) { + echo JText::_('PLG_PAYPERDOWNLOADPLUS_PAYSTACKPAY_USERMUSTBELOGGEDIN'); + return; + } + + $user_id = $user->id; + $customer_email = $user->email; +// $thename = explode(" ",$user->name); +// $firstname = $thename[0]; // that's a leap of faith +// if (count($thename) > 1) { +// $lastname = $thename[1]; +// } + + if($license || $resource) { $item_id = 0; $name = ""; $damount = 0; $currency = ""; $description = ""; - $type = ""; $task = ""; $download_id = 0; $amount = 0; - if($license || $resource) + $type = ""; $task = ""; $download_id = ''; $amount = 0; + + if($resource) { - if($resource) - { - $damount = $resource->resource_price; - $currency = $resource->resource_price_currency; //$currency = $pmntinfo->currency; - $item_id = $resource->resource_license_id; - $name = $resource->resource_name; - $download_id = $resource->download_id; - if($resource->alternate_resource_description) - $description = $resource->alternate_resource_description; - else - $description = $resource->resource_description; - $task = "confirmres"; - $type = "resource"; - } + $damount = $resource->resource_price; + $currency = $resource->resource_price_currency; + $item_id = $resource->resource_license_id; + $name = $resource->resource_name; + $download_id = $resource->download_id; + if($resource->alternate_resource_description) + $description = $resource->alternate_resource_description; else - { - $damount = $license->price; - $currency = $license->currency_code; //$currency = $pmntinfo->currency; - $item_id = $license->license_id; - $name = $license->license_name; - $description = $license->description; - $task = "confirm"; - $type = "license"; - } - $package_price = $this->calcFinalAmount($damount, $pmntinfo->ps_extra, $pmntinfo->ps_extratype, $pmntinfo->ps_extraval); - $amount = $package_price[0] * 100; - $amount = (int)$amount; - $returnBase64Coded = base64_encode($returnUrl); - - $callbackurl = JURI::root() . 'index.php?option=com_payperdownload&gateway=paystack&task=' . $task; - $callbackurl .= '&item_id=' . htmlentities($item_id); - $callbackurl .= '&item_type=' . $type; - $callbackurl .= '&user_id=' . htmlentities($user_id); - $callbackurl .= '&amount=' . htmlentities($damount); - $callbackurl .= '¤cy=' . htmlentities($currency); - $callbackurl .= '&r=' . htmlentities($returnBase64Coded); - - if ($thankyouUrl) { - $returnParams = ''; - if ($type == 'license') { - if (strstr($thankyouUrl, "?") === false) { - $returnParams = '?lid=' . (int)$license->license_id; - } else { - $returnParams = '&lid=' . (int)$license->license_id; - } - } - $callbackurl .= '&redirect=' . htmlentities(base64_encode($thankyouUrl . $returnParams)); - } + $description = $resource->resource_description; + $task = "confirmres"; + $type = "resource"; + } + else + { + $damount = $license->price; + $currency = $license->currency_code; + $item_id = $license->license_id; + $name = $license->license_name; + $description = $license->description; + $task = "confirm"; + $type = "license"; + } - if ($download_id) { - $callbackurl .= '&download_id=' . (int)$download_id; - } + $tax = 0; + $tax_percentage = $this->_getTaxPercentage(); + if ($tax_percentage > 0) { + $tax = round($damount * $tax_percentage / 100, 2); + $damount = $damount + $tax; + } - $the_reference = 'ppdp'.floor(mt_rand()*10 + 1);//''+Math.floor((Math.random() * 1000000000) + 1); - //put the reference in a session - $session = JFactory::getSession(); - $session->set('session_transaction_id',$the_reference); //dump($the_reference,"the created reference"); - - //construct the variable containing the form - - JFactory::getDocument()->addScript('https://js.paystack.co/v1/inline.js'); - - if (count($thename) > 1) { - $lastname = $thename[1]; - } - - $js_declaration = <<< JS - function payWithPaystack() { - var handler = PaystackPop.setup({ - key: "$public_key", - email: "$customer_email", - amount: "$amount", - ref: "$the_reference", - currency: "$currency", - firstname: "$firstname", - lastname: "$lastname", - metadata: { - "ecommerce_platform":"Joomla 3", - "payment_plugin": "Paystackpay for PayPerDownload", - "author": "Daydah", - custom_fields: [ - { - display_name: "Plugin", - variable_name: "plugin", - value: "Paystackpay for PayPerDownload" - } - ] - }, - callback: function(response){ - document.getElementById("paystack-payment_form").submit(); - }, - onClose: function(){ - /* cancelling by the buyer */ - } - }); - handler.openIframe(); - } - - window.addEventListener('load', function() { - document.querySelector('#paystack-pay-btn').addEventListener('click', function () { - payWithPaystack(); - }); - }, false); + // apply the tax before or after the additonnal fee ? + + $package_price = $this->calcFinalAmount($damount, $pmntinfo->ps_extra, $pmntinfo->ps_extratype, $pmntinfo->ps_extraval); + $amount = $package_price[0] * 100; + $amount = (int)$amount; + + $callbackurl = JURI::root() . 'index.php?option=com_payperdownload&gateway=paystack&task=' . $task; + + $the_reference = 'ppdp'.floor(mt_rand()*10 + 1);//''+Math.floor((Math.random() * 1000000000) + 1); + //put the reference in a session + $session = JFactory::getSession(); + $session->set('session_transaction_id',$the_reference); //dump($the_reference,"the created reference"); + + //construct the variable containing the form + + JFactory::getDocument()->addScript('https://js.paystack.co/v1/inline.js'); + + $js_declaration = <<< JS + function payWithPaystack() { + var handler = PaystackPop.setup({ + key: "$public_key", + email: "$customer_email", + amount: "$amount", + ref: "$the_reference", + currency: "$currency", + metadata: { + "ecommerce_platform":"Joomla 3", + "payment_plugin": "Paystackpay for PayPerDownload", + "author": "Daydah", + custom_fields: [ + { + display_name: "Plugin", + variable_name: "plugin", + value: "Paystackpay for PayPerDownload" + } + ] + }, + callback: function(response){ + document.getElementById("paystack-payment_form").submit(); + }, + onClose: function(){ + /* cancelling by the buyer */ + } + }); + handler.openIframe(); + } + + window.addEventListener('load', function() { + document.querySelector('#paystack-pay-btn').addEventListener('click', function () { + payWithPaystack(); + }); + }, false); JS; - JFactory::getDocument()->addScriptDeclaration($js_declaration); - - //$tosend ='