付款失败后,在sylius中启动付款步骤

时间:2015-01-28 12:31:37

标签: php symfony sylius

如果在Sylius 0.13中下订单后的尝试失败,是否有一种干净的方式让用户能够重试付款?

我为拥有订单列表的用户提供了前端管理功能,我希望为每个未成功付款的订单添加链接,以便将用户转发到/payment/capture/<hash>网址,例如下订单。

感谢。

1 个答案:

答案 0 :(得分:0)

我通过常规控制器解决了问题,该控制器自己处理付款。我们在前端有订单详细信息,其中可以看到付款表单(如果未付款):

public function showAction($number)
{
    $order = $this->findOrderOr404($number);
    $form = $this->getPaymentFormIfOrderPayable($order);

    $view = $this
        ->view()
        ->setTemplate('SyliusWebBundle:Frontend/Account:Order/show.html.twig')
        ->setData(array(
                'order' => $order,
                'form' => $form,
            ))
    ;

    return $this->handleView($view);
}

新付款始终是数据库中的最后一个方法。之后,它会生成类似于完成订单后的形式。

/**
 * @param OrderInterface $order
 * @return bool|FormInterface form if payable otherwise false
 */
protected function getPaymentFormIfOrderPayable(OrderInterface $order) {
    if($order->getState() === OrderInterface::STATE_PENDING) {
        /** @var PaymentInterface $paymentMethod */
        $paymentMethod = $order->getPayments()->last()->getMethod()->getGateway();
        switch ($paymentMethod) {
            case 'gpe':
                $form = $this->createGPEPaymentRequest($order);
                break;

            default:
                $form = false;
        }
    } else {
        $form = false;
    }

    return $form;
}

付款后,用户将被转发到Payum中的CaptureAction和PaymentStatusAction之类的另一个操作:

/**
 * @param Request $request
 * @param string $number Order Id
 * @return \Symfony\Component\HttpFoundation\RedirectResponse
 * @throws NotFoundHttpException
 * @throws AccessDeniedException
 */
public function payAction(Request $request, $number) {
    $order = $this->findOrderOr404($number);

    /** @var PaymentInterface $paymentMethod */
    $paymentMethod = $order->getPayments()->last()->getMethod()->getGateway();
    switch ($paymentMethod) {
        case 'gpe':
            $this->resolveGPEPaymentRequest($request, $order);
            break;

    $this->get('sylius.cart_provider')->abandonCart();
    $this->get('event_dispatcher')->dispatch('sylius.checkout.purchase.complete', new PurchaseCompleteEvent($order->getPayments()->last()));

    return $this->redirect($this->generateUrl('sylius_account_order_show', array('number' => $order->getNumber())));
}