Magento Paid Workflow and Event Order

For one of my Magento modules, I need to catch the event "any order is defined as paid." So I found that the related event is sales_order_payment_pay. Ok, it seems to fit my needs perfectly.

Im a Magento dev, not a merchant, and at the moment Ive never put a copy of Magento online and into production. Thus, the payment process for me is quite dark. I used to select "Check / Money Order" as a payment method during my tests.

The problem is this: I need to catch this event (sales_order_payment_pay), but it looks like I cannot run it and never enter the observer function. I have 2 payment methods, check / money transfer and credit card. For the first, the merchant must confirm the payment to the backend, and I will edge his moment when he creates the invoice. So I tried, but the event never fires. For the second, this is more complicated: since I am in dev mode, I do not know how to verify the payment with a credit card. I found a number for a MasterCard online card that allows me to run tests in Magento, but in the end the total balance is zero. Therefore, after sending the order, no event is triggered (it seems quite logical), and when I create an invoice, the event is still not triggered.

Can you help me understand a little more of the payment process and catch any order? I can not find information or documentation about this.

PS: The purpose of my module is to catch a place order or pay a fee and create an XML file for exporting data. The first with the customer has already been developed.

+4
source share
2 answers

This question seems a little complicated, so I will share my vision of payment methods and the life cycle of the order.

Vision of Alan Storm

First of all, I invite you to familiarize yourself with these topics with one of Magento's most famous bloggers, Alan Storm.

This is how I think it works:

Case 1: order with online payment (Paypal, credit card, etc.), payment accepted

Customer Confirmed Order β†’ Magento Created Order β†’ pending_payment Status
An order paid online by the Customer β†’ payment confirmed by the Payment service β†’ payment OK β†’ invoice created by Magento β†’ status pending_payment (not sure about this).
Order shipped by seller β†’ shippig coupon created by Magento β†’ status completed

Case 2: an order with online payment (Paypal, credit card, etc.), payment was refused

Customer Confirmed Order β†’ Magento Created Order β†’ pending_payment Status
Order paid online by the Client β†’ payment refused by the Payment Service β†’ Order canceled Magento β†’ status canceled

Case 3: an order with an offline payment (check / money order, coupon for sale, etc.). Seller invoices before shipping

Order confirmed by the Customer β†’ Order created by Magento β†’ Status pending confirmation Order paid offline by the Customer β†’ payment received by Merchant β†’ order issued by Merchant β†’ invoice created by Magento β†’ status processing
Order shipped by Merchant β†’ coupon created by Magento β†’ status completed

Case 4: an order with an offline payment (check / money order, coupon for sale, etc.). Merchant ships before billing

An order confirmed by the Customer β†’ An order created by Magento β†’ Status pending confirmation An order paid by an offline client β†’ an order sent by the seller β†’ a delivery coupon created by Magento β†’ status processing
Order placed by Merchant β†’ invoice created by Magento β†’ status processing

Hope this topic helps someone in the future.
Let me know if you need more information.

+2
source

I am using the following:

1 - app / code / loca / Packagename / Modulename / etc / config.xml

<config> <global> <models> <sales> <rewrite> <order>Packagename_Modulename_Model_Rewrite_Order</order> </rewrite> </sales> <sales_resource> <rewrite> <quote>Packagename_Modulename_Model_Rewrite_Resource_Sales_Quote</quote> </rewrite> </sales_resource> <modelrewrite> <class>Packagename_Modulename_Model_Rewrite</class> </modelrewrite> </models> <events> <packagename_modulename_order_status_change> <observers> <packagename_modulename_model_rewrite> <type>singleton</type> <class>modelrewrite/observer</class> <method>changeStateOrder</method> </packagename_modulename_model_rewrite> </observers> </packagename_modulename_order_status_change> </events> <resources> <modulename_setup> <setup> <module>Packagename_Modulename</module> <class>Mage_Sales_Model_Resource_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </modulename_setup> </resources> </global> <crontab> <!-- here I am listing only major routines --> <jobs> <Packagename_Modulename_Model_Rewrite_SendOrderToERP> <schedule> <cron_expr>12 * * * *</cron_expr> </schedule> <run> <model>modelrewrite/observer::runSendOrderToERP</model> </run> </Packagename_Modulename_Model_Rewrite_SendOrderToERP> <Packagename_Modulename_Model_Rewrite_ReceiveERPStatusOrder> <schedule> <cron_expr>*/5 * * * *</cron_expr> </schedule> <run> <model>modelrewrite/observer::runReceiveERPStatusOrder</model> </run> </Packagename_Modulename_Model_Rewrite_ReceiveERPStatusOrder> </jobs> </crontab> 

2 - app / code / loca / Packagename / Modulename / Model / Rewrite / Order.php

class Packagename_Modulename_Model_Rewrite_Order extends Mage_Sales_Model_Order {

 public function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false){ // attempt to set the specified state // tenta definir o status especificado if ($shouldProtectState) { if ($this->isStateProtected($state)) { Mage::throwException( Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state) ); } } $this->setData('state', $state); // add status history if ($status) { if ($status === true) { $status = $this->getConfig()->getStateDefaultStatus($state); } $this->setStatus($status); $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility } Mage::dispatchEvent('packagename_modulename_order_status_change', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified)); Mage::log("Packagename_Modulename_Model_Rewrite_Order Changing order to STATE ".$state." STATUS ".$status); return $this; } 

}

3 - app / code / loca / Packagename / Modulename / Model / Rewrite / Observer.php

 class Packagename_modulename_Model_Rewrite_Observer{ public function implementOrderStatus($event){ $order = $event->getOrder(); $state = $event->getState(); $status = $event->getStatus(); return $this; } public function changeStateOrder($observer){ Mage::log('changeStateOrder'); $order = $observer->getEvent()->getOrder(); if($order->getStatus() == "processing_paid"){ $this->autoInvoicing($order); } else if($order->getStatus() == "processing_shipped"){ $this->autoShipment($order); } // use your necessary ifs $this->sendTransactionalEmail($order); return $this; } public function autoShipment(Mage_Sales_Model_Order $order){ //... } public function autoInvoicing(Mage_Sales_Model_Order $order){ //... //return $this; } public function sendTransactionalEmail(Mage_Sales_Model_Order $order){ switch ($order->getStatus()) { case "processing": $templateId = 1; break; case "pending_payment": $templateId = 0; break; //... default: $templateId = 0; break; } //... } public function runSendOrderToERP($schedule){ //... } public function runReceiveERPStatusOrder($schedule){ //... } //And much more another methods can be used after change state / status order: refund, calcel, etc 
0
source

Source: https://habr.com/ru/post/1490850/


All Articles