Completed
Push — master ( 677329...636a3e )
by Joachim
14:34
created

PaymentController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 57
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A newAction() 0 47 3
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
14
class PaymentController extends Controller {
15
    /**
16
     * @Method("POST")
17
     * @Route("/{terminal}")
18
     *
19
     * @param string $terminal
20
     * @param Request $request
21
     * @return RedirectResponse
22
     */
23
    public function newAction($terminal, Request $request)
24
    {
25
        $handler = new Handler(
26
            $request,
27
            $this->container->getParameter('loevgaard_dandomain_altapay.shared_key_1'),
28
            $this->container->getParameter('loevgaard_dandomain_altapay.shared_key_2')
29
        );
30
31
        if(!$handler->checksumMatches()) {
32
            // @todo what should happen here?
33
            throw new \RuntimeException('Checksum mismatch. Try again');
34
        }
35
36
        $paymentRequest = $handler->getPaymentRequest();
37
38
        // @todo log payment request to database
39
40
        $paymentRequestPayload = new PaymentRequestPayload(
41
            $terminal->getTitle(),
0 ignored issues
show
Bug introduced by
The method getTitle cannot be called on $terminal (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
42
            $paymentRequest->getOrderId(),
43
            $paymentRequest->getTotalAmount(),
44
            $paymentRequest->getCurrencySymbol()
45
        );
46
47
        foreach ($paymentRequest->getOrderLines() as $orderLine) {
48
            $orderLinePayload = new OrderLinePayload(
49
                $orderLine->getName(),
50
                $orderLine->getProductNumber(),
51
                $orderLine->getQuantity(),
52
                $orderLine->getPrice(),
53
                $orderLine->getVat()
54
            );
55
56
            $paymentRequestPayload->addOrderLine($orderLinePayload);
57
        }
58
59
        $configPayload = new ConfigPayload(); // @todo set the callback urls
60
        $paymentRequestPayload->setConfig($configPayload);
61
62
        //$paymentRequestPayload->setCookie(); @todo set payment request entity id in the cookie so we can use this in callbacks
63
64
65
        $altapay = $this->container->get('loevgaard_dandomain_altapay.altapay_client');
66
        $response = $altapay->createPaymentRequest($paymentRequestPayload);
67
68
        return new RedirectResponse($response->getDynamicJavascriptUrl());
69
    }
70
}