在Symfony中自定义JMSPaymentCoreBundle表单

时间:2013-11-09 23:31:00

标签: php symfony paypal

我已经整合了捆绑包JMSPaymentCoreBundle及其插件JMSPaymentPaypalBundle。然而,第一束给出的形式迫使用户选择他/她希望使用哪种支付方法。在我的情况下,我没有多个选项,只有Paypal。可悲的是,用户仍然需要点击单选按钮确认他希望在重定向到Paypal的网站之前使用Paypal。我想从这个过程中删除这一步。

这是我使用的表格:

$form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
            'amount'   => $orderPrice,
            'currency' => 'CAD',
            'default_method' => 'payment_paypal', // Optional
            'predefined_data' => array(
                'paypal_express_checkout' => array(
                    'return_url' => $this->router->generate('paiement_complete', array(
                        'id' =>$order->getId()
                    ), true),
                    'cancel_url' => $this->router->generate('paiement_annule', array(), true)
                ),
            ),
        ));

有没有办法修改此表单以删除选择,还是需要实现新表单?

谢谢。

1 个答案:

答案 0 :(得分:2)

如果您愿意,可以绕过付款方式选择,并直接直接表达结帐(或您想要的方法)。

而不是在类似于此的动作中显示和接收表单数据:

public function detailsAction(Order $order)
{
    // Your form

    if ('POST' === $this->request->getMethod())
    {
        $form->bindRequest($this->request);

        if ($form->isValid())
        {
            $this->ppc->createPaymentInstruction($instruction = $form->getData());

            $extendedData = $instruction->getExtendedData();

            $extendedData->set(
                'checkout_params', array(
                    'L_PAYMENTREQUEST_0_NAME0' => 'My cool article',
                    'L_PAYMENTREQUEST_0_DESC0' => 'this is a description for my article',
                )
            );

            $order->setPaymentInstruction($instruction);
            $this->em->persist($order);
            $this->em->flush($order);

            return new RedirectResponse($this->router->generate('payment_frontpage'));
         }
      }
}

您可以直接构建付款指示并在操作中继续使用Paypal:

/**
 * Paypal Express Checkout
 *
 * @Route("/{orderNumber}/checkout", name="payment_checkout")
 * @Template
 */
public function checkoutAction(Order $order)
{
    $em = $this->get('doctrine.orm.entity_manager');
    $router = $this->get('router');

    // Create the extended data object
    $extendedData = new ExtendedData();

    // Complete payment return URL
    $extendedData->set('return_url',
        $router->generate('payment_complete', array(
            'orderNumber' => $order->getOrderNumber(),
        ), true)
    );

    // Cancel payment return URL
    $extendedData->set('cancel_url',
        $router->generate('payment_cancel', array(
            'orderNumber' => $order->getOrderNumber(),
        ), true)
    );

    // Checkout parameters
    $checkout_params = $this->generateCheckoutParameters($order);
    $this->get('logger')->info(print_r($checkout_params, 1));

    // Add checkout information to the exended data
    $extendedData->set('checkout_params', $checkout_params);

    // Create the payment instruction object
    $instruction = new PaymentInstruction(
        $order->getAmount(), 'EUR', 'paypal_express_checkout', $extendedData
    );

    // Validate and persist the payment instruction
    $this->get('payment.plugin_controller')->createPaymentInstruction($instruction);

    // Update the order object
    $order->setPaymentInstruction($instruction);
    $em->persist($order);
    $em->flush($order);

    // Continue with payment
    return new RedirectResponse($router->generate('payment_complete', array(
        'orderNumber' => $order->getOrderNumber(),
    )));
}

__

protected function generateCheckoutParameters(Order $order)
{
    // Checkout parameters
    $checkout_params = array();

    // Include items data in the order
    foreach ($order->getItems() as $in=>$item)
    {
        $checkout_params = array_merge(
            $checkout_params,
            array(
                sprintf('L_PAYMENTREQUEST_0_NAME%d', $in)   => $item->getName(),
                sprintf('L_PAYMENTREQUEST_0_DESC%d', $in)   => $item->getDescription(),
                sprintf('L_PAYMENTREQUEST_0_AMT%d', $in)    => $item->getPrice(),
                sprintf('L_PAYMENTREQUEST_0_QTY%d', $in)    => 1,
            )
        );
    }

    // Include payments data in the order
    $checkout_params = array_merge(
        $checkout_params,
        array(
            'PAYMENTREQUEST_0_DESC'     => "Order #".$order->getOrderNumber(),
            'PAYMENTREQUEST_0_INVNUM'   => $order->getOrderNumber(),
        )
    );

    return $checkout_params;
}