From fe324bd2bc1ee4b5b8a3778420016689c37619c1 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Mon, 9 Jul 2012 16:27:21 +1000 Subject: [PATCH] Add a default_method option to the Choose form --- Form/ChoosePaymentMethodType.php | 4 +- Resources/doc/usage.rst | 77 ++++++++++++++++---------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/Form/ChoosePaymentMethodType.php b/Form/ChoosePaymentMethodType.php index 690a040..0896868 100755 --- a/Form/ChoosePaymentMethodType.php +++ b/Form/ChoosePaymentMethodType.php @@ -64,6 +64,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder->add('method', 'choice', array( 'choices' => $this->buildChoices($options['available_methods']), 'expanded' => true, + 'data' => $options['default_method'], )); foreach ($options['available_methods'] as $method) { @@ -161,8 +162,9 @@ public function validate(FormInterface $form, array $options) public function getDefaultOptions() { return array( - 'currency' => null, 'amount' => null, + 'currency' => null, + 'default_method' => null, 'predefined_data' => array(), ); } diff --git a/Resources/doc/usage.rst b/Resources/doc/usage.rst index 5dc7359..90df873 100755 --- a/Resources/doc/usage.rst +++ b/Resources/doc/usage.rst @@ -10,55 +10,55 @@ object or equivalent. This could look like: .. code-block :: php amount = $amount; $this->orderNumber = $orderNumber; } - + public function getOrderNumber() { return $this->orderNumber; } - + public function getAmount() { return $this->amount; } - + public function getPaymentInstruction() { return $this->paymentInstruction; } - + public function setPaymentInstruction(PaymentInstruction $instruction) { $this->paymentInstruction = $instruction; } - + // ... } .. note :: - An order object, or the like is not strictly necessary, but since it is + An order object, or the like is not strictly necessary, but since it is regularly available, we will be using it in this chapter for demonstration purposes. @@ -76,9 +76,9 @@ which we will leverage. .. warning :: - We have completely left out any security considerations, in a real-world + We have completely left out any security considerations, in a real-world scenario, you have to make sure the following actions are sufficiently - covered by access rules, for example by using @PreAuthorize from + covered by access rules, for example by using @PreAuthorize from JMSSecurityExtraBundle_. .. code-block :: php @@ -93,7 +93,7 @@ which we will leverage. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\RedirectResponse; - + /** * @Route("/payments") */ @@ -103,14 +103,14 @@ which we will leverage. private $request; /** @DI\Inject */ - private $router; - + private $router; + /** @DI\Inject("doctrine.orm.entity_manager") */ private $em; - + /** @DI\Inject("payment.plugin_controller") */ private $ppc; - + /** * @Route("/{orderNumber}/details", name = "payment_details") * @Template @@ -118,8 +118,9 @@ which we will leverage. public function detailsAction(Order $order) { $form = $this->getFormFactory()->create('jms_choose_payment_method', null, array( - 'currency' => 'EUR', 'amount' => $order->getAmount(), + 'currency' => 'EUR', + 'default_method' => 'payment_paypal', // Optional 'predefined_data' => array( 'paypal_express_checkout' => array( 'return_url' => $this->router->generate('payment_complete', array( @@ -131,30 +132,30 @@ which we will leverage. ), ), )); - + if ('POST' === $this->request->getMethod()) { $form->bindRequest($this->request); - + if ($form->isValid()) { $this->ppc->createPaymentInstruction($instruction = $form->getData()); - + $order->setPaymentInstruction($instruction); $this->em->persist($order); $this->em->flush($order); - + return new RedirectResponse($this->router->generate('payment_complete', array( 'orderNumber' => $order->getOrderNumber(), ))); } } - + return array( 'form' => $form->createView() ); } - + // ... - + /** @DI\LookupMethod("form.factory") */ protected function getFormFactory() { } } @@ -162,7 +163,7 @@ which we will leverage. The ``jms_choose_payment_method`` form type will automatically render a form with all available payment methods. Upon binding, the form type will validate the data for the chosen payment method, and on success will give us a valid -``PaymentInstruction`` instance back. +``PaymentInstruction`` instance back. Depositing Money ---------------- @@ -183,7 +184,7 @@ route for which we will now create the corresponding action in our controller: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\RedirectResponse; - + /** * @Route("/payments") */ @@ -197,12 +198,12 @@ route for which we will now create the corresponding action in our controller: /** @DI\Inject("doctrine.orm.entity_manager") */ private $em; - + /** @DI\Inject("payment.plugin_controller") */ private $ppc; - + // ... see previous section - + /** * @Route("/{orderNumber}/complete", name = "payment_complete") */ @@ -214,25 +215,25 @@ route for which we will now create the corresponding action in our controller: } else { $payment = $pendingTransaction->getPayment(); } - + $result = $this->ppc->approveAndDeposit($payment->getId(), $payment->getTargetAmount()); if (Result::STATUS_PENDING === $result->getStatus()) { $ex = $result->getPluginException(); - + if ($ex instanceof ActionRequiredException) { $action = $ex->getAction(); - + if ($action instanceof VisitUrl) { return new RedirectResponse($action->getUrl()); } - + throw $ex; } } else if (Result::STATUS_SUCCESS !== $result->getStatus()) { throw new \RuntimeException('Transaction was not successful: '.$result->getReasonCode()); } - - // payment was successful, do something interesting with the order + + // payment was successful, do something interesting with the order } }