|
1
|
|
|
<?php |
|
2
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use Loevgaard\AltaPay\Payload\PaymentRequest as PaymentRequestPayload; |
|
5
|
|
|
use Loevgaard\AltaPay\Payload\OrderLine as OrderLinePayload; |
|
6
|
|
|
use Loevgaard\AltaPay\Payload\PaymentRequest\Config as ConfigPayload; |
|
7
|
|
|
use Loevgaard\Dandomain\Pay\Handler; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
|
|
15
|
|
|
class PaymentController extends Controller { |
|
16
|
|
|
/** |
|
17
|
|
|
* @Method("POST") |
|
18
|
|
|
* @Route("/{terminal}") |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $terminal |
|
21
|
|
|
* @param Request $request |
|
22
|
|
|
* @return RedirectResponse |
|
23
|
|
|
*/ |
|
24
|
|
|
public function newAction($terminal, Request $request) |
|
25
|
|
|
{ |
|
26
|
|
|
$terminalManager = $this->container->get('loevgaard_dandomain_altapay.terminal_manager'); |
|
27
|
|
|
$paymentManager = $this->container->get('loevgaard_dandomain_altapay.payment_manager'); |
|
28
|
|
|
$terminalEntity = $terminalManager->findTerminalBySlug($terminal); |
|
29
|
|
|
|
|
30
|
|
|
if(!$terminalEntity) { |
|
31
|
|
|
throw new \RuntimeException('Terminal `'.$terminal.'` not found'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$handler = new Handler( |
|
35
|
|
|
$request, |
|
36
|
|
|
$this->container->getParameter('loevgaard_dandomain_altapay.shared_key_1'), |
|
37
|
|
|
$this->container->getParameter('loevgaard_dandomain_altapay.shared_key_2') |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
if(!$handler->checksumMatches()) { |
|
41
|
|
|
// @todo what should happen here? |
|
42
|
|
|
throw new \RuntimeException('Checksum mismatch. Try again'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$paymentRequest = $handler->getPaymentRequest(); |
|
46
|
|
|
|
|
47
|
|
|
$paymentEntity = $paymentManager->createPaymentFromDandomainPaymentRequest($paymentRequest); |
|
48
|
|
|
$paymentManager->updatePayment($paymentEntity); |
|
49
|
|
|
|
|
50
|
|
|
$paymentRequestPayload = new PaymentRequestPayload( |
|
51
|
|
|
$terminalEntity->getTitle(), |
|
52
|
|
|
$paymentRequest->getOrderId(), |
|
53
|
|
|
$paymentRequest->getTotalAmount(), |
|
54
|
|
|
$paymentRequest->getCurrencySymbol() |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($paymentRequest->getOrderLines() as $orderLine) { |
|
58
|
|
|
$orderLinePayload = new OrderLinePayload( |
|
59
|
|
|
$orderLine->getName(), |
|
60
|
|
|
$orderLine->getProductNumber(), |
|
61
|
|
|
$orderLine->getQuantity(), |
|
62
|
|
|
$orderLine->getPrice(), |
|
63
|
|
|
$orderLine->getVat() |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$configPayload = new ConfigPayload($this->generateUrl('loevgaard_dandomain_altapay_callback_form', [], UrlGeneratorInterface::ABSOLUTE_URL)); // @todo set the callback urls |
|
70
|
|
|
$paymentRequestPayload->setConfig($configPayload); |
|
71
|
|
|
|
|
72
|
|
|
//$paymentRequestPayload->setCookie(); @todo set payment request entity id in the cookie so we can use this in callbacks |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$altapay = $this->container->get('loevgaard_dandomain_altapay.altapay_client'); |
|
76
|
|
|
$response = $altapay->createPaymentRequest($paymentRequestPayload); |
|
77
|
|
|
|
|
78
|
|
|
if(!$response->isSuccessful()) { |
|
79
|
|
|
// @todo fix this |
|
80
|
|
|
throw new \RuntimeException('An error occurred during payment request.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
echo $response->getUrl();exit; |
|
|
|
|
|
|
84
|
|
|
return new RedirectResponse($response->getUrl()); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.