|
| 1 | +<?php |
| 2 | +namespace Acme\PaymentBundle\Controller; |
| 3 | + |
| 4 | +use Acme\PaymentBundle\Entity\PaymentDetails; |
| 5 | +use Payum\Core\Payum; |
| 6 | +use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | +use Symfony\Component\Validator\Constraints\Range; |
| 9 | +use Sensio\Bundle\FrameworkExtraBundle\Configuration as Extra; |
| 10 | + |
| 11 | +class SimplePaytrailController extends Controller |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @Extra\Route("/paytrail/prepare", name="acme_paytrail_prepare") |
| 15 | + * @Extra\Template("AcmePaymentBundle::prepare.html.twig") |
| 16 | + */ |
| 17 | + public function prepareAction(Request $request) |
| 18 | + { |
| 19 | + $gatewayName = 'paytrail'; |
| 20 | + |
| 21 | + $form = $this->createPurchaseForm(); |
| 22 | + $form->handleRequest($request); |
| 23 | + if ($form->isValid()) { |
| 24 | + $data = $form->getData(); |
| 25 | + |
| 26 | + $storage = $this->getPayum()->getStorage(PaymentDetails::class); |
| 27 | + |
| 28 | + $payment = $storage->create(); |
| 29 | + $payment['orderNumber'] = '12345678'; |
| 30 | + $payment['locale'] = 'fi_FI'; |
| 31 | + $payment['price'] = (float) $data['amount']; |
| 32 | + $payment['currency'] = $data['currency']; |
| 33 | + |
| 34 | + $storage->update($payment); |
| 35 | + |
| 36 | + $captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken( |
| 37 | + $gatewayName, |
| 38 | + $payment, |
| 39 | + 'acme_payment_details_view' |
| 40 | + ); |
| 41 | + |
| 42 | + return $this->redirect($captureToken->getTargetUrl()); |
| 43 | + } |
| 44 | + |
| 45 | + return ['form' => $form->createView()]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return \Symfony\Component\Form\Form |
| 50 | + */ |
| 51 | + protected function createPurchaseForm() |
| 52 | + { |
| 53 | + return $this->createFormBuilder() |
| 54 | + ->add('amount', null, array( |
| 55 | + 'data' => 1.1, |
| 56 | + 'constraints' => array(new Range(array('max' => 2))) |
| 57 | + )) |
| 58 | + ->add('currency', null, array('data' => 'EUR')) |
| 59 | + |
| 60 | + ->getForm() |
| 61 | + ; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return Payum |
| 66 | + */ |
| 67 | + protected function getPayum() |
| 68 | + { |
| 69 | + return $this->get('payum'); |
| 70 | + } |
| 71 | +} |
0 commit comments