Skip to content

Commit 5e0df8f

Browse files
committed
add paytral example
1 parent 58eb0ef commit 5e0df8f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

src/Acme/PaymentBundle/Resources/views/Default/index.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
<a href="{{ path("acme_klarna_prepare_invoice") }}">Reserve&Activate Invoice</a>
107107
</li>
108108

109+
<h3>Paytrail</h3>
110+
<li>
111+
<a href="{{ path("acme_paytrail_prepare") }}">Prepare</a>
112+
</li>
113+
109114
<h3>Redsys\Sermepa</h3>
110115
<li>
111116
<a href="{{ path("acme_redsys_prepare") }}">Simple purchase</a>

0 commit comments

Comments
 (0)