Ajax call to custom payment plugin

  • Posts: 2
  • Thank you received: 0
1 year 6 months ago #325526

-- HikaShop version -- : HikaShop Starter 4.3.0
-- Joomla version -- : 3.9
-- PHP version -- : 7.3.0

Hi,

I'm trying to develope a REST paypal payment process. The payment process works fine in sandbox and I can see the transactions between accounts.
When Paypal return me a success, I made a pretty simpre AJAX call to my plugin to test ajax url and it works fine

jQuery("#ok").on("click", function(){
		var formdata = new FormData();
		formdata.append('cmd','payment');
		
		jQuery.ajax({
			url: 'index.php?option=com_hikashop&ctrl=checkout&task=notify&notif_payment=paypal_api&tmpl=component&lang=IT',
			
			async: false,
			data: formdata,
			type: 'POST',
			contentType: false,
			processData: false,
			success: function(response) {
				console.log(response);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("Status: " + textStatus); alert("Error: " + errorThrown);  console.log(XMLHttpRequest);
			}
		});
	});

but now there's the trick, when i'm inside my plugin I can't access hikashop environment/method/variable/class in any way

I read ther doc, and I found out that I need this line
if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }

now I tried to access to the user_id using the helper method
$user = hikashop_loadUser();
return($user);

but, when I log the response into the console I get an empty string

I'm a novice developing plugin, I had more experience developing joomla modules and components. I read about the trigger topic for plugin but thats a topic to study later. I need now to get data from cart && user in order to create an order and create a payment

Would be a easier choise to get data from dbo? I can easily access joomla's environment/methods and so on from my custom plugin
<?php
defined('_JEXEC') or die('Restricted access');
class plgHikashoppaymentpaypal_api extends hikashopPaymentPlugin {
...
function onPaymentNotification(&$statuses) {
		include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php');
		$user = hikashop_loadUser();
		return($user); //return empty string on response
}
...
}


thanks in advance

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

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
1 year 6 months ago #325541

Hi,

The onPaymentNotification function is called by the checkout controller which himself is called by the helper.php file.
So the helper.php file of HikaShop is already loaded in memory when the onPaymentNotification starts.
And, thus all the HikaShop functions and environment are already loaded and available for you to use.
That's not your problem here.
The problem is that in the checkout controller, for the notification mechanism, a buffer is started before the onPaymentNotification function is called and after the function is called, it get the information in the buffer and stores it in the payment log file (that you can access in the Files section of the HikaShop configuration).
That's because the code run in the onPaymentNotification function is normally a server to server HTTP request and thus when you output something, it would go to the payment gateway server, which you normally do not want. So the output gets logged instead.
So you should find your $user variable content in the payment log file.
If you want to output something back to the browser in the onPaymentNotifiation, you should echo it, and before returning, you should add an exit; or die() so that the checkout controller doesn't log the output but PHP directly sends it back to the browser.

The following user(s) said Thank You: Ramio91

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

  • Posts: 2
  • Thank you received: 0
1 year 6 months ago #325570

Thank you very much for your reply, it was really useful. Dealing with ajax call is always a pain because you cannot see well what's happening server side

Can I ask you which are the major steps to records a purchase on my system?

By abstraction I can think them as:

Order demand
Payment
Payment and Order insertion on database
Order creation
Notification

I'm I missing some steps inside hikashop environment?

//EDIT

return_url, cancel_url, and similar can be dealt easily with jQuery with a proper response to AJAX call so I don't need them as step

Last edit: 1 year 6 months ago by Ramio91.

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

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
1 year 6 months ago #325613

Hi,

I'm not sure what you mean by each point in

Order demand
Payment
Payment and Order insertion on database
Order creation
Notification

I'm actually not even sure of what you mean by "the major steps to records a purchase on my system".
Because, to record an order in HikaShop, all you need is to call the save function of class.order with the necessary $order object properly formatted as it is done in the createFromCart function in class.order.
HikaShop will then handle the rest for you.

Now regarding the general workflow when an order is created at the end of the checkout, here is how it goes:
- the customer clicks on the "finish" button of the checkout
- the checkout system checks that everything has been properly entered/selected in the checkout
- it loads the cart and converts it into an order object
- the order creation process starts
- plugins are called with the onBeforeOrderCreate trigger
- if there was a credit card form on the checkout, the payment plugin will usually do the payment at that point and if the payment fails, it will cancel the order creation. If it succeeds, it ill usually change the status of the order to confirmed
- the order is added in the database
- the stock is updated for each order
- plugins are called with the onAfterOrderCreate trigger
- the order creation notification email is sent
- the onAfterOrderConfirm trigger of the selected payment plugin is called
- the plugin usually returns a redirect HTML form so that HikaShop displays it to the customer to be redirected to the payment gateway
- the customer fill his credit card information on the payment gateway
- the payment gateway sends a notification to the onPaymentNotification trigger of the payment plugin so that it can change the status of the order to confirmed and HikaShop will then send the order status notification email to the customer.
- the customer is redirected back to the website with the thank you page.

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

Time to create page: 0.054 seconds
Powered by Kunena Forum