选择离线付款后如何将订单状态更改为已付款?

emi*_*mix 4 symfony sylius symfony4

使用稳定的Sylius 1.2.0,在选择了离线付款方式后,如何将订单标记为已付款?

尝试使用sylius_order_payment状态机的回调,但在任何过渡上似乎都不会触发:

winzou_state_machine:
    sylius_order_payment:
        callbacks:
            after:
                set_order_paid:
                    on: ['complete']
                    do: ['@AppBundle\Payment\StateMachine\Callback\CallbackClass', 'updateOrder']
                    args: ['object']
Run Code Online (Sandbox Code Playgroud)

是否使用了状态机?也许我使用的是错误的。欢迎任何建议。感谢您的耐心等待。

更新1

明天我将尝试使用文档中的“ 使用状态机转换完成付款”一章。我正在考虑将这段代码放在侦听Order创建的资源事件的事件侦听器中,尽管状态机回调听起来像是更好的解决方案。

emi*_*mix 5

终于使它工作了:

state_machine.yml

winzou_state_machine:
    sylius_order_checkout:
        callbacks:
            after:
                app_order_complete_set_paid:
                    on: ['complete']
                    do: ['@AppBundle\Order\StateMachine\Callback\OrderCompleteSetPaidCallback', 'setPaid']
                    args: ['object']

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: true

    AppBundle\Order\StateMachine\Callback\OrderCompleteSetPaidCallback: ~
Run Code Online (Sandbox Code Playgroud)

OrderCompleteSetPaidCallback.php

<?php

namespace AppBundle\Order\StateMachine\Callback;

use SM\Factory\FactoryInterface;
use AppBundle\Infrastructure\CommandBus\CommandBus;
use AppBundle\Order\SetPaid\SetPaidCommand;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Payment\PaymentTransitions;

final class OrderCompleteSetPaidCallback
{
    private $stateMachineFactory;

    public function __construct(FactoryInterface $stateMachineFactory)
    {
        $this->stateMachineFactory = $stateMachineFactory;
    }

    public function setPaid(OrderInterface $order): void
    {
        if (!($lastPayment = $order->getLastPayment())) {
            return;
        }

        if ('cash_on_delivery' === $lastPayment->getMethod()->getCode()) {
            $this->transition($order);
        }
    }

    private function transition(OrderInterface $order): void
    {
        $stateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);
        $stateMachine->apply(OrderPaymentTransitions::TRANSITION_PAY);

        $payment = $order->getLastPayment();

        $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
        $stateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE);
    }
}
Run Code Online (Sandbox Code Playgroud)