JMS payment-core and payment-paypal integration in Symfony2

1.6k views Asked by At

I have installed these bundle for paypal integration in my Symfony2.3 project.

"jms/payment-core-bundle": "1.0.*@dev" "jms/payment-paypal-bundle": "dev-master"

I have followed this link http://jmsyst.com/bundles/JMSPaymentPaypalBundle for configuration. I got entity and database, but I couldnt get forms and views.

My question is how I can get the payment page with these bundles ? Is there any forms for that ? If so, how could I get it ?

1

There are 1 answers

0
Nawfal Serrar On BEST ANSWER

You need a payment action to render a form with something like this :

 * @Route("/{id}", name="paiement")
     * @Template()
     */
    public function paymentAction($id=0) // this is a personnal ID i pass to the controler to identify the previous shopping cart
    {
        $form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
            'amount'   => $order->getPrice(),
            'currency' => 'EUR',
            'default_method' => 'payment_paypal', // Optional
            'predefined_data' => array()
        ));

        if ('POST' === $this->request->getMethod()) {
            $form->bindRequest($this->request);
            $order = new Order();
            $this->em->persist( $order);
            $this->em->flush( $order);

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

            $form->bindRequest($this->request);

    // Once the Form is validate, you update the order with payment instruction
            if ($form->isValid()) {
                $instruction = $form->getData();
                $this->ppc->createPaymentInstruction($instruction);
                $order->setPaymentInstruction($instruction);
                $this->em->persist($order);
                $this->em->flush($order);
                // now, let's redirect to payment_complete with the order id
                return new RedirectResponse($this->router->generate('payment_complete', array('id' => $order->getId() )));
            }
        }
        return $this->render('YourBundle:Paiement:paymentChooseTemplate.html.twig',array('form' => $form->createView() , 'id' => $id));
    }

The important part in this code is the form creation which show the paypal choice, you can embed this form in your payment page by rendering it and then in your payment action check for its validity, and go on with the code.

I am doing this differently in our current website now without using the default form it is also possible.

give you some link where you can get more informations :

http://symfony2.ylly.fr/how-to-set-up-jmspayment-bundle-and-add-the-paypal-plugin-sebastien/

http://jmsyst.com/bundles/JMSPaymentCoreBundle/master/usage

Its sad that the official JMS website lakes documentation about this... but still easy to understand how it works so i guess that's why they didn't bother.