create Ajax function in hikashop payment plugin

  • Posts: 23
  • Thank you received: 1
6 years 2 months ago #285590

-- HikaShop version -- : 3.2.1
-- Joomla version -- : 3.7
-- PHP version -- : 7.0

I'm trying to create a payment plugin that uses ajax, but when I create the function onAjax[plugin] it not register in the trigger array, there is any solution for that?

function created in the braintree.php file in the Hikashoppayment group plugin

class plgHikashoppaymentBraintree extends hikashopPaymentPlugin
{
...
   public function onAjaxBraintree(){
        echo '<script> alert("onAjaxBraintree") </script>';
    }
...
}
call to the function in the javascript
function brain_execute(action){
    var w_data = {
        'option' : 'com_ajax',
        'group'  : 'Hikashoppayment',
        'plugin' : 'Braintree',
        'format' : 'json',
        'token'  : w_card_activ,
        'nonce'  : w_card_nonce,
        'action' : action
    };
    jQuery.ajax({
        type      : 'POST',
        data      : w_data,
        success   : function (response) { console.log(response);},
        error     : function (response) { console.log(response);}
    });
};

Last edit: 6 years 2 months ago by Jerome. Reason: [code] tag is so nice !

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
6 years 2 months ago #285592

Hi,

Payment plugins are standard Joomla plugins so you can use com_ajax with them the same way you would for any joomla plugin.
There is nothing particular to do.
However, I see that you have :
'group' : 'Hikashoppayment',
but the first character should be lowercase.
Also, I see that you didn't set any "url" parameter when calling jQuery.ajax

Please Log in or Create an account to join the conversation.

  • Posts: 23
  • Thank you received: 1
6 years 2 months ago #285632

hello Nicolas,

I have tried with the hikashoppayment all in lowercase and result is the same. The problem is in the

class plgHikashoppaymentBraintree extends hikashopPaymentPlugin
{
...
public function onAjaxBraintree(){
echo '<script> alert("onAjaxBraintree") </script>';
}
...
}
the function onAjaxBraintree is not registred in the triggers array of Joomla and I do not understand why.
I have debugged the array and the onajaxbraintree entry does not exist.
if you verify in the
$dispatcher = JEventDispatcher::getInstance();
at line 140 of com_ajax\ajax.php the array _methods of the $dispatcher do not include the onajaxbraintree so it's not executed.

I have hacked the system hikashoppayment plugin and add at the end the following code:
public function onAjaxHikashoppayment() {
if(!include_once(rtrim(JPATH_PLUGINS,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'Hikashoppayment'.DIRECTORY_SEPARATOR.$_POST["dir"].DIRECTORY_SEPARATOR.$_POST["file"]))
            return;
        $manageToken = new $_POST['class'];
        $func=$_POST['func'];
        $manageToken->{$func}();
    }
and I have used the following javascript function, and it works.
function brain_execute(action){
    var w_data = {
        'option' : 'com_ajax',
        'group'  : 'system',
        'plugin' : 'Hikashoppayment',
        'format' : 'json',
        'dir'    : 'Braintree',
        'file'   : 'helper.php',
        'class'  : 'plgHikashoppaymentBraintreeHelper',
        'func'   : 'onAjaxBraintree',
        'token'  : w_card_activ,
        'nonce'  : w_card_nonce,
        'action' : action
    };
    jQuery.ajax({
        type      : 'POST',
        data      : w_data,
        success   : function (response) { console.log(response);},
        error     : function (response) { console.log(response);}
    });
};
but if it's possible I prefer the solution to include the ajax function directly in the plugin.

If I have understood correctly, with the new Joomla router and JSON format there is no need to specify the url, we only have to define the option, the group and the plugin in the data to be passed in the jQuery call.

Last edit: 6 years 2 months ago by Jerome. Reason: [code] tag is awesome !

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
6 years 2 months ago #285637

Hi,

Functions are automatically registered in the dispatcher. Make sure that you don't have a __construct function and if you do, make sure that you call the parent::_construct function from it so that Joomla can do its thing.

Please Log in or Create an account to join the conversation.

  • Posts: 23
  • Thank you received: 1
6 years 2 months ago #285693

I have a construct function, and I call the parent::__construct function

function __construct(&$subject, $config) {
        // Calling the parent Constructor
		parent::__construct($subject, $config);

        // Do some extra initialisation in this constructor if required
		$this->manageToken = new plgHikashoppaymentBraintreeHelper;
	}

Last edit: 6 years 2 months ago by plnabais.

Please Log in or Create an account to join the conversation.

  • Posts: 25994
  • Thank you received: 4004
  • MODERATOR
6 years 2 months ago #285695

Hello,

Why not simply using the notification URL and add some parameters in order to "dispatch" the processings ?
Hacking the HikaShop core with such kind of specific code will not be good for the long run because you cannot update anymore (without loosing your modifications).

Regards,

PS :The plugin group "hikashoppayment" do not have any uppercase.


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 23
  • Thank you received: 1
6 years 2 months ago #285720

Hello Jerome
I agree totally with you, hacking the hikashop core is not good for upgrading, but I need an ajax function to interact with the user when he chooses the payments methods saved in the braintree vault.
I have spent 1-week trying com_ajax directly in the plugin with all the variant of upper and lowercases, and It not works by any means. So the only solution I found was this, change the system plugin of hikashop. I would like to find a workaround to have no concerns regarding future updates, and eventually be able to sell the plugin.

Best regards

Last edit: 6 years 2 months ago by plnabais.

Please Log in or Create an account to join the conversation.

  • Posts: 25994
  • Thank you received: 4004
  • MODERATOR
6 years 2 months ago #285724

Hello,

As I wrote in my previous message : The work around is to use the notify URL, there are already several plugins doing that.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 23
  • Thank you received: 1
6 years 2 months ago #286060

Hello,
Is there any code example of using AJAX in a plugin for hikashop?

Regards

Please Log in or Create an account to join the conversation.

  • Posts: 81379
  • Thank you received: 13037
  • MODERATOR
6 years 2 months ago #286063

Hi,

No.
But there is nothing complex.
Just call in Ajax the notification URL of the plugin with an extra parameter.
For example:

index.php?option=com_hikashop&ctrl=checkout&task=notify&notif_payment=braintree&tmpl=component&specialparam=1
And in your onPaymentNotification function of the plugin, you can do such code at the beginning:
if(!empty($_REQUEST['specialparam'])){
 //PHP ajax process... echo stuff you want for your javascript...
 exit;
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.069 seconds
Powered by Kunena Forum