Magento – How to create observer to change order status after it was created

magento-2.0magento2order-statusorders

I am creating a custom payment method.

Before calling the payment gateway, I am calling PlaceOrderAction javascript function in mypay-method.js. that way, order is created in Processing status.

placeOrder = placeOrderAction(this.getData(), false, this.messageContainer);

The fact is that I need the order to be created with Payment Pending status, so, I have to change order status accordingly after it was created.

How can I do it?

Best Answer

Observe the sales_order_place_after event, then check the order from the observer if it has your payment method, do your logic and save the order again.