Question

i'm trying to add partial payment for magento 2, any suggestion in modifying the order state, or maybe a good tutorial on making module that integrate with the admin sections, thanks.

Was it helpful?

Solution

To create a payment module in magento 2 you can start by magento sample payment gateway module. Make you customizations in it.

If you want to change the order status after payment processing you can use my function to change the statuses:

public function __construct(
    ...
    \Magento\Sales\Model\Order $orderModel,      
    array $data = []
) {
    ...
    $this->orderModel = $orderModel;

}
public function changeOrderStatus(){
    //it is used when payment fails.
    $orderId = 100 // get increment Id of order that you want
    $orderModel = $this->orderModel->loadByIncrementId($orderId);;
    $orderModel->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW, true); //STATE_PENDING_PAYMENT
    $orderModel->setStatus(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW); //STATE_PENDING_PAYMENT
    $orderModel->addStatusToHistory($orderModel->getStatus(), 'Order processed successfully with reference');
    $orderModel->save();
}

Here are the list of the order states and statuses from magento core file. You can use these instead of STATE_PAYMENT_REVIEW:

/**
 * Order states
 */
const STATE_NEW = 'new';

const STATE_PENDING_PAYMENT = 'pending_payment';

const STATE_PROCESSING = 'processing';

const STATE_COMPLETE = 'complete';

const STATE_CLOSED = 'closed';

const STATE_CANCELED = 'canceled';

const STATE_HOLDED = 'holded';

const STATE_PAYMENT_REVIEW = 'payment_review';

/**
 * Order statuses
 */
const STATUS_FRAUD = 'fraud';

/**
 * Order flags
 */
const ACTION_FLAG_CANCEL = 'cancel';

const ACTION_FLAG_HOLD = 'hold';

const ACTION_FLAG_UNHOLD = 'unhold';

const ACTION_FLAG_EDIT = 'edit';

const ACTION_FLAG_CREDITMEMO = 'creditmemo';

const ACTION_FLAG_INVOICE = 'invoice';

const ACTION_FLAG_REORDER = 'reorder';

const ACTION_FLAG_SHIP = 'ship';

const ACTION_FLAG_COMMENT = 'comment';

/**
 * Report date types
 */
const REPORT_DATE_TYPE_CREATED = 'created';

const REPORT_DATE_TYPE_UPDATED = 'updated';

OTHER TIPS

To modify or change the order status you will need to use the OrderRepositoryInterface as since load() and save() are deprecated now.

...
use \Magento\Sales\Api\OrderRepositoryInterface;

class YourClassName{
    ...
    protected $_orderRepository;
    ...
    public function __construct(..., OrderRepositoryInterface $orderRepository, ...){

        $this->_orderRepository = $orderRepository;
        ...
    }
    ...
    public function setOrderStatus($orderID, $statusCode){
        try{
            // obtain the order with the order ID
            $order = $this->_orderRepository->get($orderID);
            $order->setState($statusCode)->setStatus($statusCode);
            $this->_orderRepository->save($order);
            return true;
        } catch (\Exception $e){
            // add some logging here
            return false;
        }
    }
    ...
}

Call the function setOrderStatus with your custom status code like partially paid orders. You can use ready-made Magento2 Partial Payment Extension By Milople.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top