2013-11-09 41 views
2

我已經整合了JMSPaymentCoreBundle和它的插件JMSPaymentPaypalBundle。但是,由第一個包提供的表單強制用戶選擇他/她希望使用哪種付款方式。在我的情況下,我沒有多個選項,只有Paypal。可悲的是,用戶仍然需要點擊一個單選按鈕來確認他希望在重定向到PayPal網站之前使用PayPal。我想從這個過程中刪除這一步。定製Symfony中的JMSPaymentCoreBundle表單

這是形式使用:

$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) 
       ), 
      ), 
     )); 

有什麼辦法來修改這個形式刪除選擇,或者我需要實現一個新的形式?

謝謝。

回答

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 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; 
} 
+1

非常感謝。我只是想指出,我確實需要更改線$ '$ em-> flush();' 到 '$ em-> flush($ order);' – wrenchimp

+0

快速的問題。如果我在訂單上打折,那麼結帳參數似乎有問題。在這種情況下是否有特定的參數? – wrenchimp