/**
  * @Extra\Route(
  *   "/prepare_paypal_via_jms_plugin",
  *   name="acme_other_paypal_via_jms_plugin"
  * )
  *
  * @Extra\Template
  */
 public function prepareAction(Request $request)
 {
     $gatewayName = 'paypal_express_checkout_via_jms_plugin';
     $form = $this->createPurchaseForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         $paymentInstruction = new PaymentInstruction($data['amount'], $data['currency'], 'paypal_express_checkout');
         $paymentInstruction->setState(PaymentInstruction::STATE_VALID);
         $payment = new Payment($paymentInstruction, $data['amount']);
         $this->getDoctrine()->getManager()->persist($paymentInstruction);
         $this->getDoctrine()->getManager()->persist($payment);
         $this->getDoctrine()->getManager()->flush();
         $captureToken = $this->getTokenFactory()->createCaptureToken($gatewayName, $payment, 'acme_other_purchase_done_paypal_via_jms_plugin');
         $payment->getPaymentInstruction()->getExtendedData()->set('return_url', $captureToken->getTargetUrl());
         $payment->getPaymentInstruction()->getExtendedData()->set('cancel_url', $captureToken->getTargetUrl());
         //the state manipulations  is needed for saving changes in extended data.
         $oldState = $payment->getPaymentInstruction()->getState();
         $payment->getPaymentInstruction()->setState(PaymentInstruction::STATE_INVALID);
         $this->getDoctrine()->getManager()->persist($paymentInstruction);
         $this->getDoctrine()->getManager()->persist($payment);
         $this->getDoctrine()->getManager()->flush();
         $payment->getPaymentInstruction()->setState($oldState);
         $this->getDoctrine()->getManager()->persist($paymentInstruction);
         $this->getDoctrine()->getManager()->flush();
         return $this->redirect($captureToken->getTargetUrl());
     }
     return array('form' => $form->createView());
 }
 /**
  * @expectedException \InvalidArgumentException
  * @dataProvider getTestAmountsForIndependentCredit
  */
 public function testCreditOnlyAcceptsValidAmountsForIndependentCredits($amount)
 {
     $controller = $this->getController();
     $instruction = new PaymentInstruction(150.0, 'EUR', 'foo', new ExtendedData());
     $instruction->setState(PaymentInstructionInterface::STATE_VALID);
     $credit = new Credit($instruction, 50);
     $instruction->setDepositedAmount(10.0);
     $instruction->setReversingDepositedAmount(0.01);
     $instruction->setCreditedAmount(0.01);
     $instruction->setCreditingAmount(0.01);
     $this->callCredit($controller, array($credit, $amount));
 }