Search code examples
phpmagentocheckoutobservers

auto invoice on email confirmation


I need to programmatically invoice orders in Magento on certain product types when an order has been successfully placed. This is for a virtual giftcard, and in order for the giftcard number to be emailed out the order has to be invoiced in the admin backend.

I think that there is functionality like this for "virtual goods" already, but my product has the type 'giftcard', not 'virtual' and I therefore had to do this myself.

So far I've managed to make sure that the product can only be ordered alone, so no physical products mixed in into the order. Also I have been able to detect the giftcard at checkout to hide any non-direct or non-assured payment methods.

Now all I had to do was to add an observer to check the order status change to "payment received (reserved on account)" or just "payment recieved" from any previous order statuses, like "payment pending". It's just that there is no such event in Magento - at least as far as I can tell by searching.

So I figured that I'd connect the auto-invoice code to where Magento sends out the order confirmation email - but I simply can not find where that is.

Any suggestions on where I can insert my code to auto invoice the order? Preferably as "safe" as possible in regards to payment received - but I'd settle with anything and can always go back manually and block giftcards connected to suspected frauds and orders where the payment is somehow not received.

Thanks in advance for any hints on how to accomplish this!


Solution

  • I think the best option is to observe event sales_order_place_after, sales_order_payment_place_end, which are fired when an order payment is placed (see here). To be 100% sure add a code to your observer to check that the order due is equal zero $order->getTotalDue() == 0