0

I'm trying to find an event/observer which will fire once an order is paid in Magento (2.2.8).

  • checkout_onepage_controller_success_action - seems limited to frontend orders (ie not called for admin created orders)
  • sales_order_invoice_pay / sales_order_invoice_register - don't seem to be called for PayPal Express

Ideally I want to try to get something fairly universal which will cover any orders paid. Even better if we knew it was only going to fire once (ie not on sales order save, but worst case can get an order field).

0

If the sales_order_invoice_pay event doesn't work, you may try to look at changes of the state of the invoice: If an invoice gets paid it has to change the value of it's state to 'paid'.

There are more approaches for that:

  1. You can create a plugin for Magento\Sales\Model\Order\Invoice, for example a beforeSetState like the following (comparing the actual value of the state with the former value from the database):

    public function beforeSetState(\Magento\Sales\Model\Order\Invoice $subject, $state)
    {
        if ($subject->getState() != $subject::STATE_PAID && $state == $subject::STATE_PAID){
            //you have a state change of the invoice to 'paid' ->do whatever you need here
    
        }
    
        return $state;
    }
    

Depending on your requirement this may be to early in the flow. If that's the case you can try the second approach.

  1. Observe the event sales_invoice_save_before (or even sales_order_invoice_save_after) and check there the state change like this:

    // Event sales_order_invoice_save_before
    public function doWhateverForPaidInvoice ($observer)
    {
        $invoice = $observer->getEvent()->getInvoice();
        if ($invoice->getState() == Magento\Sales\Model\Order\Invoice::STATE_PAID 
                && $invoice->getOrigData('state') != Magento\Sales\Model\Order\Invoice::STATE_PAID) {
            //state has changed to 'paid', execute your code here 
            //...
        }
        return $this;
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.