onAfterOrderUpdate custom plugin

  • Posts: 17
  • Thank you received: 0
6 years 5 months ago #267474

-- HikaShop version -- : 3.0.1
-- Joomla version -- : 3.6.5
-- PHP version -- : 7.0.10

I'm trying to write a plugin to perform additional tasks after PayPal payment is recieved. For instance assign points in Jomsocial.

I took the original points plugin and started modifing it. Trying to use the two events:

public function onAfterOrderCreate(&$order, &$send_email) {
		
		$app = JFactory::getApplication();
		
		if($app->isAdmin())
		{
			
			return true;
		
		}
		
		if(!isset($order->order_status))
		{
			
			return true;
			
		}
		
		if(!empty($order->order_type) && $order->order_type != 'sale')
		{
			
			return true;
			
		}
		
		$this->awardDonationRewards($order);
		
		unset($this->fullOrder);
		
		return true;
	
	}

This event works and the '$this->awardDonationRewards($order)' gets pharsed before I get javascript redirected to PayPal.
public function onAfterOrderUpdate(&$order, &$send_email) {
		
		echo 'success';
		jexit();
}

Doesn't work as expected. Offcourse this is test code. Ultimately I go to $this->awardDonationRewards($order) again. I had the impression to get a blank page with just one word as result when returning from PayPal. Instead it gives me the full thank you page from the link 'index.php?option=com_hikashop&ctrl=checkout&task=after_end&order_id=103&Itemid=580'.

In the administrator I can see the order being changed from 'created' to 'confirmed'.

When I manually edit the order status in the administrator this event gets triggered.

Any ideas how to achieve extra functions after payment and return from PayPal back to my site?

Last edit: 6 years 5 months ago by mindgem.

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

  • Posts: 17
  • Thank you received: 0
6 years 5 months ago #267497

Do I understand correct you need to write a custom plugin for manual editing of order status and a custom payment plugin to handle actions after PayPal return status?

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

  • Posts: 25855
  • Thank you received: 3986
  • MODERATOR
6 years 5 months ago #267478

Hello,

Most of payment methods (Paypal included) is using a server-to-server notification for the order confirmation.
So it is not the customer who confirm the order but Paypal itself.

So if you are displaying something and performing an "exit", you might interact with the paypal notification, not with the customer return. And thus, you won't see anything displayed on the screen as customer. You will only display that back at the PayPal server who contacts HikaShop to provide the payment notification, and he doesn't care about it and just ignores it.

And no, you should only write a plugin of the type "hikashop" and implement the onAfterOrderUpdate trigger.
I would first recommend to test your code with the changing of the order status in the backend of HikaShop in the orders listing.
Once that's working, it should work the same with the PayPal automatic notifications.

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.
Last edit: 6 years 5 months ago by nicolas.

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

  • Posts: 17
  • Thank you received: 0
6 years 5 months ago #267510

So I created the plugin as attached. I can see it working in the backend (adding the customer to the group and add some points).

According to your answer this should work for reallife payment by PayPal? As according to my tests it doesn't,

Last edit: 6 years 5 months ago by mindgem.

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

  • Posts: 17
  • Thank you received: 0
6 years 5 months ago #267515

Added

JLoader::register('JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php');

JFile::append(HKASHOP_ROOT.'mylogfile.txt','onAfterOrderUpdate: started');

to test some more

Last edit: 6 years 5 months ago by mindgem.

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

  • Posts: 17
  • Thank you received: 0
6 years 5 months ago #267532

Got this figured out. Small coding issue from my part. As the event is excecuted not by the user itself I needed to pass the right userID.

Paypal earned many points :P

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

  • Posts: 25855
  • Thank you received: 3986
  • MODERATOR
6 years 5 months ago #267535

Hello,

Good to know that you have fixed your issue.

Do not hesitate to read the user_id from the order object itself ( order_user_id ).

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: 4
4 weeks 1 day ago #354792

Hi,

sorry to necropost, but I'm having a similar issue.

I also use the onAfterOrderUpdate($order, $send_email) function - to update some values in a CSV file.

Everything works fine if I manually change the order status in the backend.

But when PayPal confirms payment and an order status is automatically changed to "Confirmed" the function doesn't seem to trigger at all.

public function onAfterOrderUpdate($order, $send_email)
	{
		// Only process PayPal orders
		if ($order->order_payment_id != 1) return;
		
		$orderFile = $this->params->get('path') . '/infounit_export_orders.csv';
		// Only run if there are orders waiting to be imported
		if (!is_file($orderFile)) return;
		
		$newRows = [];
		// Read CSV
		if (($stream = fopen($orderFile, 'r')) !== false)
		{
			$index = 0;
			$indexZahlungsweg;
			$indexBestellzeichen;
			while (($row = fgetcsv($stream, 1000, ';')) !== false)
			{
				// Look up which column contains the payment information and which column contains the order number
				if ($index == 0)
				{
					$indexZahlungsweg = array_search('Zahlungsweg', $row);
					$indexBestellzeichen = array_search('Bestellzeichen', $row);
				}
				
				// Use the order number to find the right order
				if ($row[$indexBestellzeichen] == $order->order_number)
				{
					// Update the order status
					switch ($order->order_status)
					{
						case 'confirmed':
						$row[$indexZahlungsweg] = 'PayPal';
						break;
						case 'cancelled':
						$row[$indexZahlungsweg] = 'PayPal (storniert!)';
						break;
						case 'created':
						$row[$indexZahlungsweg] = 'PayPal (nicht bezahlt!)';
						break;
					}
				}
				$newRows[] = $row;
				$index++;
			}
			fclose($stream);
		}
		
		// Write back to CSV
		if (($stream = fopen($orderFile, 'w')) !== false)
		{
			foreach($newRows as $row)
			{
				fputcsv($stream, $row, ';');
			}
			fclose($stream);
		}
	}

Where's the issue?

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

  • Posts: 79891
  • Thank you received: 12703
  • MODERATOR
4 weeks 23 hours ago #354801

Hi,

The issue is that in an "update" event, only the new data is provided in the main object.
So in onAfterOrderUpdate, the $order variable might not contain all the data of an order.
And with payment plugins changing the status of an order, $order usually only contains the order_id and the order_status.
So the first line

if ($order->order_payment_id != 1) return;
of your plugin stops the processing since order_payment_id is not set in that case.
You'll want check the data in $order->old->order_payment_id
And you'll have the same issue with the order_number in the code after that.

The following user(s) said Thank You: NTV

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

  • Posts: 23
  • Thank you received: 4
2 weeks 1 day ago #355074

Working perfectly now - cheers!

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

Time to create page: 0.081 seconds
Powered by Kunena Forum