【问题标题】:How to change order status to paid after the offline payment was selected?选择线下支付后如何将订单状态更改为已支付?
【发布时间】:2018-07-04 14:27:42
【问题描述】:

使用稳定的Sylius1.2.0,选择线下支付方式后如何将订单标记为已付款?

尝试使用 sylius_order_payment state-machine` 的后回调,但它似乎不会在任何转换时触发:

winzou_state_machine:
    sylius_order_payment:
        callbacks:
            after:
                set_order_paid:
                    on: ['complete']
                    do: ['@AppBundle\Payment\StateMachine\Callback\CallbackClass', 'updateOrder']
                    args: ['object']

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

更新 1

明天我将尝试文档中的Completing a Payment with a state machine transition 章节。我正在考虑将这段代码放在一个事件监听器中,监听订单创建的资源事件,尽管状态机回调听起来是一个更好的解决方案。

【问题讨论】:

    标签: symfony symfony4 sylius


    【解决方案1】:

    终于成功了:

    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: ~
    

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2020-03-06
      • 2013-07-09
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多