Passed
Push — master ( c14871...b4b2a6 )
by Antonio
04:24 queued 42s
created

CaptureOffsiteAction::checkAndUpdateResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Locastic\SyliusHTPayWayPlugin\Action;
4
5
use Locastic\TcomPayWay\AuthorizeForm\Model\Payment as Api;
6
use Locastic\TcomPayWay\Helpers\CardTypeInterpreter;
7
use Locastic\TcomPayWay\Helpers\ResponseCodeInterpreter;
8
use Payum\Core\Action\ActionInterface;
9
use Payum\Core\ApiAwareInterface;
10
use Payum\Core\ApiAwareTrait;
11
use Payum\Core\Bridge\Spl\ArrayObject;
12
use Payum\Core\Exception\RequestNotSupportedException;
13
use Payum\Core\GatewayAwareInterface;
14
use Payum\Core\GatewayAwareTrait;
15
use Payum\Core\Reply\HttpResponse;
16
use Payum\Core\Request\Capture;
17
use Payum\Core\Request\GetHttpRequest;
18
use Payum\Core\Request\RenderTemplate;
19
20
/**
21
 * @property Api $api
22
 */
23
class CaptureOffsiteAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
24
{
25
    use ApiAwareTrait;
26
    use GatewayAwareTrait;
27
28
    public function __construct()
29
    {
30
        $this->apiClass = Api::class;
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     *
36
     * @param Capture $request
37
     */
38
    public function execute($request)
39
    {
40
        RequestNotSupportedException::assertSupports($this, $request);
41
42
        $model = ArrayObject::ensureArrayObject($request->getModel());
43
44
        $httpRequest = new GetHttpRequest();
45
        $this->gateway->execute($httpRequest);
46
47
        //we are back from ht payway site so we have to just update model and complete action
48
        if (isset($httpRequest->request['pgw_trace_ref'])) {
49
            $model['tcompayway_response'] = $this->checkAndUpdateResponse($httpRequest->request);
50
51
            return;
52
        }
53
54
        $this->api->setPgwAmount($model['pgwAmount']);
55
        $this->api->setPgwOrderId($model['pgwOrderId']);
56
        $this->api->setPgwEmail($model['pgwEmail']);
57
        $this->api->setPgwSuccessUrl($request->getToken()->getTargetUrl());
58
        $this->api->setPgwFailureUrl($request->getToken()->getTargetUrl());
59
60
        $this->api->setPgwFirstName($model['pgwFirstName']);
61
        $this->api->setPgwLastName($model['pgwLastName']);
62
        $this->api->setPgwStreet($model['pgwStreet']);
63
        $this->api->setPgwCity($model['pgwCity']);
64
        $this->api->setPgwPostCode($model['pgwPostCode']);
65
        $this->api->setPgwCountry($model['pgwCountry']);
66
        $this->api->setPgwPhoneNumber($model['pgwPhoneNumber']);
67
68
        $this->api->setPgwLanguage($model['pgwLanguage']);
69
        $this->api->setPgwMerchantData($model['pgwMerchantData']);
70
        $this->api->setPgwOrderInfo($model['pgwOrderInfo']);
71
        $this->api->setPgwOrderItems($model['pgwOrderItems']);
72
73
        $renderTemplate = new RenderTemplate(
74
            '@LocasticSyliusHTPayWayPlugin/Offsite/capture.html.twig', array(
75
                'payment' => $this->api,
76
            )
77
        );
78
        $this->gateway->execute($renderTemplate);
79
80
        throw new HttpResponse($renderTemplate->getResult());
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function supports($request)
87
    {
88
        return $request instanceof Capture && $request->getModel() instanceof \ArrayAccess;
89
    }
90
91
    protected function checkAndUpdateResponse($pgwResponse)
92
    {
93
        if (!$this->api->isPgwResponseValid($pgwResponse)) {
94
            throw new RequestNotSupportedException('Not valid PGW Response');
95
        }
96
97
        // tcompayway request failed
98
        if (isset($pgwResponse['pgw_result_code'])) {
99
            $pgwResponse['error'] = ResponseCodeInterpreter::getPgwResultCode($pgwResponse['pgw_result_code']);
100
101
            return $pgwResponse;
102
        }
103
104
        // tcom request success, add status code 0 manually
105
        $pgwResponse['credit_card'] = CardTypeInterpreter::getPgwCardType($pgwResponse['pgw_card_type_id']);
106
        $pgwResponse['pgw_result_code'] = 0;
107
108
        return $pgwResponse;
109
    }
110
}