Getting UserState in onAfterOrderCreated

  • Posts: 167
  • Thank you received: 12
  • Hikashop Business
2 weeks 2 days ago #319082

-- HikaShop version -- : 4.3.0
-- Joomla version -- : 3.9.11
-- PHP version -- : 7.2.30
-- Error-message(debug-mod must be tuned on) -- : This is specific to my application - Braintree payment plugin

We have developed the Braintree Payment Plugin. Some JS on the checkout page takes parameters from the authorization transaction and sends them via AJAX call which ends up in onPaymentNotification. There the setUserState (or JFactory::getSession()->set() saves the array of parameters as the user state / session variable.

Once the user then clicks the Finish button in checkout, the onAfterOrderCreate (or onBeforeOrderCreate or onPaymentDisplay) when I try to get the parameters from the user state or session (same thing?) they are not there.

The above happens on the first attempt at checkout by a logged in user in native or legacy checkout mode. Guest users are able to checkout fine.

If you have any ideas why the onAfterOrderCreate, onBeforeOrderCreate, or onPaymentDisplay functions would NOT be able to immediately read user state values please let us know. Of if you have suggestions on another secure way to store information per order (without custom fields) please let us know that also.

Thanks for reading. Paul @ 3by400.com

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

  • Posts: 69451
  • Thank you received: 10401
  • MODERATOR
2 weeks 2 days ago #319087

Hi,

I don't see why the data in the user session would be lost after the creation of the order and only for logged in users.
The user session is not reset and we don't touch session parameters we're not aware of in the HikaShop code.
I would recommend to try to add calls to hikashop_writeToLog($var) in a onBeforeOrderCreate function of your plugin so that you can check that the variable is there before the order creation by checking the HikaShop payment log file (setting in the Hikashop configuration where hikashop_writeToLog will dump your $var). And also add it at the beginning of your onAfterOrderCreate function.

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

  • Posts: 167
  • Thank you received: 12
  • Hikashop Business
2 weeks 1 day ago #319181

I am extensively logging information in the various plugin functions / methods. When I save things in user state variables within the onPaymentNotification method I can read back from the user state and get what was written to it. But after that I get nothing from the user state in. the class constructor or other functions (on other triggers). I have also verified that the session id is the same when read inside the onPaymentNotification method and all other methods.

All I can conclude at this point is that either the user state / session is being cleared after the onPaymentNotification method by something other than my code, or there is an instance situation where the variables I set in the user state / session are not in the same scope as those in other methods.

Perhaps the solution would be to drop using the onPaymentNotification in favor of using the Joomla com_ajax capability.

Any. thoughts or ideas would be appreciated.
Thanks, Paul

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

  • Posts: 69451
  • Thank you received: 10401
  • MODERATOR
2 weeks 18 hours ago #319183

Hi,

Well, without looking at the code and debugging it myself I can't say more on the issue that what you and I already said.

Regarding doing it another way, If you add your parameters in hidden input fields with the correct name, you can have them saved in the session by HikaShop for you.
For example, if you add in the custom_html attribute of your $method a hidden field with the name checkout[payment][custom][XXX][YYY] (where XXX is the id of the payment method and YYY the name of your variable) you can then fill it in the javascript, and then, in onBeforeOrderCreate you can us such code to retrieve the data:

$app = JFactory::getApplication();
$checkout_custom = $app->getUserState(HIKASHOP_COMPONENT.'.checkout_custom', null);
if(!empty($checkout_custom))
	$checkout_custom = json_decode(base64_decode($checkout_custom), true);
echo $checkout_custom[(int)$order->order_payment_id]['YYY'];
That's the mechanism several payment plugins use when they have their own credit card form on the checkout and want to pass data from the payment gateway to the payment plugin. So I'm sure this works.
I actually already talked to you about that in an email conversation and provided an example with the authorize.js plugin (which uses that exact method).
So maybe you didn't get the email and it went to your spam folder or something ?

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

  • Posts: 167
  • Thank you received: 12
  • Hikashop Business
1 week 4 days ago #319352

Nicolas, We very much appreciate your suggestions. Yes, we did get the previous one, but had not followed up with that, but will now. I've downloaded the plugin you referenced and am reviewing how that works.

Is checkPaymentDisplay() a newer HikaShop function? It is not listed in the available documentation. But it is a public function in that plugin. If it is basic to HikaShop now, at what point is that event triggered?

Thanks, Paul

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

  • Posts: 69451
  • Thank you received: 10401
  • MODERATOR
1 week 4 days ago #319379

Hi,

Yes, it's a relatively new function.
It's basically the same as the onPaymentDisplay function but with less code to add as it's only called for your payment plugin while onPaymentDisplay is called upon all the plugins implementing it.

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

  • Posts: 167
  • Thank you received: 12
  • Hikashop Business
1 week 3 days ago #319441

The checkPaymentDisplay event is not firing under any circumstances for me. Are there any specific steps I need to take to set up for that? Does it require native checkout? (I am going back and forth from legacy to native, so maybe I've just missed it.)

So far I have been able to successfully use the custom_html and related capabilities you've mentioned to get data from hidden fields back to the onBeforeOrderCreate method.

Also, are the custom_html and checkout_custom capabilities limited to native checkout?

Thanks, Paul

Last edit: 1 week 3 days ago by 3by400.

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

  • Posts: 69451
  • Thank you received: 10401
  • MODERATOR
1 week 3 days ago #319445

Hi,

If you still have the onPaymentDisplay function in your plugin, and that you don't call parent::onPaymentDisplay in your function, then checkPaymentDisplay won't be called as it's called from the onPaymentDisplay function of the class hikashopPlugin your plugin is supposed to extend from.

The custom_html will be displayed by the legacy checkout. However, there is no mechanism to get the data from the POST and store it in the session so that checkout_custom is available later on. It's up to you to get the data from the POST in the onPaymentSave function of your plugin:

function onPaymentSave(&$cart, &$rates, &$payment_id) {
 $result = parent::onPaymentSave($cart, $rates, $payment_id);
 $config = hikashop_config();
 if($config->get('checkout_legacy')) {
  // get data from the POST and store it in the session
 }
 return $result;
}

Also, there is no javascript mechanism to plug yourself before the submit of the checkout page with the registerAjax function like there is with the new checkout. So it's up to you to add an event listener on the next button to run your javascript.

The Authorize.js plugin is actually not compatible with the legacy checkout.

Last edit: 1 week 3 days ago by nicolas.

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

Time to create page: 0.063 seconds
Powered by Kunena Forum