选择离线支付后,如何将订单状态更改为已支付?
How to change order status to paid after the offline payment was selected?
使用稳定的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']
是否使用了状态机?也许我用错了。欢迎提出任何建议。感谢您的耐心等待。
更新 1
明天我将尝试文档中的 Completing a Payment with a state machine transition 章节。我正在考虑将此代码放入事件侦听器中,侦听 Order created resource event,尽管状态机回调听起来是更好的解决方案。
终于成功了:
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);
}
}
使用稳定的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']
是否使用了状态机?也许我用错了。欢迎提出任何建议。感谢您的耐心等待。
更新 1
明天我将尝试文档中的 Completing a Payment with a state machine transition 章节。我正在考虑将此代码放入事件侦听器中,侦听 Order created resource event,尽管状态机回调听起来是更好的解决方案。
终于成功了:
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);
}
}