onAfterOrderConfirm not triggering

  • Posts: 7
  • Thank you received: 0
11 years 3 months ago #117346

I'm getting there :)

Below is the code I'm working on to integrate Hikashop with Kashflow (accounts).

<?php

defined('_JEXEC') or die('Restricted access');

?><?php

if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }

class plgHikashopkf_insert extends JPlugin

{

	function plgHikashopKashflow(&$subject, $config){

		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('hikashop', 'kf_insert');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}

	}

		function onAfterOrderConfirm(&$order, &$methods, $method_id) {

    	if(!empty($order->order_id)){
		
		$orderClass = hikashop_get('class.order');
		$full_order = $orderClass->loadFullOrder($order->order_id, true, false);
		
require_once("**********.inc.php");
require_once("******.inc.php");

$username = "******";
$password = "*******";

//create variables
		
$order_user_id = $full_order->order_user_id;
		
mysql_connect("localhost", "*************", "**************") or die(mysql_error()); 
mysql_select_db("****************") or die(mysql_error()); 

$email = mysql_result(mysql_query("SELECT user_email FROM nl7xh_hikashop_user WHERE user_id = '".$order_user_id."';"), 0);
		
	foreach($full_order->products as $product){
	$cart = $product->order_product_name;
	$subtotal = $product->order_product_price;
	}
	$inv_name = $full_order->shipping_address->address_firstname . " " . $full_order->shipping_address->address_lastname; //ok
	$inv_addr1 = $full_order->shipping_address->address_street;
	$inv_addr2 = $full_order->shipping_address->address_city;
	$inv_state = $full_order->shipping_address->address_state;
	$inv_zip = $full_order->shipping_address->address_post_code;
	$inv_country = $full_order->shipping_address->address_country;
	$description = $cart;
	$qty = '1';
	$tax_rate = 20;
	$tel = $full_order->shipping_address->address_telephone;
	$shipping = $full_order->shipping->shipping_price;

// Kashflow will check if it's available and increment it if already used.
$invoice_number = 1;

// Uses format yyyy-mm-dd.
$invoice_date = date("Y-m-d"); // current date
$due_date = strtotime(date("Y-m-d", strtotime($invoice_date)) . " +30 days"); //net 30

// below is the nominal id code for Sale of Goods.
$nominal_id = 1236497;
$kashflow_customer_id; //First we will try to get this id from an email if not we will create it

//**************** Test For User ***************//


$kashflow = new Kashflow($username,$password);
$results = $kashflow->getCustomerByEmail($email);
	
//Checks to see if the email exists and creates a new account if not
if($results->StatusDetail == 'There is no customer with that email address'){
	$name = $inv_name;
	$address = $inv_addr1;
	$town = $inv_addr2;
	$state = $inv_state;
	$postcode = $inv_zip;
	$country = $inv_country;
	$tel = $tel;
	$newCustomerResults = $kashflow->insertCustomer($name,$address,$town,$state,$postcode,$country,$email,$telS); 
	$kashflow_customer_id = $newCustomerResults->InsertCustomerResult;	
}else{
	$kashflow_customer_id = $results->GetCustomerByEmailResult->CustomerID;
}

$unit_net = $subtotal + $shipping;

//This should be true unless you use bad data to create account.
if($kashflow_customer_id){
	// Connect to Kashflow.
	$kashflow = new Kashflow($username,$password);

	// Initialise the invoice.
	$invoice  = new KashflowInvoice($kashflow_customer_id,$invoice_number,$invoice_date,$due_date);
	
	// Add a line to the invoice: 1 unit, with a unit price of £10 @ 20% VAT.
	$invoice->addLine($qty,$unit_net,$unit_tax,$tax_rate,$nominal_id,$description);
		// Now insert the invoice on Kashflow.
	$response = $kashflow->insertInvoice($invoice);
	
	// Done - here's the response!
	echo "<pre>";
	print_r($response);
	echo "</pre>";
}else{

	echo "<p>CustomerID is not valid: ".$kashflow_customer_id."</p>";
}
       
    	}
		return true;
    }
	
}

?>

I want to trigger my script onAfterOrderConfirm, but it doesn't trigger. I get the order confirmation sent to the customer, but no invoice in Kashflow. If I change
function onAfterOrderConfirm(&$order, &$methods, $method_id) {
to
function onAfterOrderCreate(&$order,&$send_email){
it works, but this is not what I want. I want to be sure payment has been received.

Have I made a mistake with the trigger code change?

Many thanks
Eleventy

Last edit: 11 years 3 months ago by eleventy. Reason: redaction

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
11 years 3 months ago #117350

This function is triggered at the end of the checkout process after the order has been created so that the payment plugin can redirect the user to the payment platform for the payment.
Please make sure that you're extending from the hikashopPaymentPlugin class or you'll be missing a lot of code from your plugin and it won't work properly during the checkout if you're using HikaShop 2.2 or above.

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

  • Posts: 7
  • Thank you received: 0
11 years 3 months ago #117356

Thanks Nicolas for your continued support :)

What would be the best trigger/function to use? I just need my Kashflow script to execute when the customer has paid.

Last edit: 11 years 3 months ago by eleventy.

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

  • Posts: 7
  • Thank you received: 0
11 years 3 months ago #117364

I think onAfterOrderUpdate will work, if I run my script only when the order is confirmed not created.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
11 years 3 months ago #117375

Then my question is : how and when are your orders confirmed ? What is confirming them ?
Normally, it's the payment plugin itself which confirms the orders. So you should actually do that in the plugin. So asking me where to put your script when the order is confirmed is strange since you should be the one confirming it and thus you should already know where to put your script.


Here is how most payment plugins work:
1. the user finish the checkout
2. the order is created
3. the onAfterOrderConfirm function is called
4. in that function the plugin displays the redirection form to the payment platform and also specifies the "notification URL".
5. the user is redirected to the payment platform and pays the order
6. the payment platform notifies the "notification URL" of the payment being successful
7. at that point the onPaymentNotification function of the plugin is called
8. the plugin checks that the notification is valid
9. the plugin finally confirms the order

So basically, in most payment plugins, the two main functions are onAfterOrderConfirm, in order to redirect the user to the payment gateway, and onPaymentNotification, in order to confirm the order.

The following user(s) said Thank You: lionel75

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

  • Posts: 60
  • Thank you received: 1
10 years 10 months ago #140719

Hi Nicolas,
I need to trigger my own plugin after payment (paypal) confirm. From your workflow I understand I need step
10. onPaymentConfirm
which unfortunately doesn't exists (if I'm not wrong).
Isn't it?
If so, can I asky you why don't you add that function?

Thanks in advance

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
10 years 10 months ago #140727

Hi,

There is already such function. It is called onAfterOrderUpdate which is triggered each time the order is changed.
So simply implement that trigger and in it, you verify that the $order->order_status is confirmed and the the $order->old->order_status is created and you'll know that the order has just been confirmed.
You can read more on that trigger in our developer documentation.

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

  • Posts: 60
  • Thank you received: 1
10 years 10 months ago #140729

Hi Nicolas,
I'm sorry, I've missed it!
Thanks a lot!

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

Time to create page: 0.077 seconds
Powered by Kunena Forum