Completed
Push — master ( e6924a...b3599f )
by Dominik
02:35
created

XmlRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 200
ccs 51
cts 64
cp 0.7969
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareRequestXml() 0 18 4
A getRequestXml() 0 20 1
A getServiceName() 0 4 1
A getServiceVersion() 0 4 1
A getHttpMethod() 0 4 1
A createResponse() 0 4 1
A getEndpoint() 0 5 2
A getApiBaseProdUrl() 0 4 1
A getApiBaseTestUrl() 0 4 1
A getApiEndpoint() 0 4 1
B sendData() 0 41 4
1
<?php
2
/**
3
 * w-vision
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the MIT License
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that are distributed with this source code.
10
 *
11
 * @copyright  Copyright (c) 2016 Woche-Pass AG (http://www.w-vision.ch)
12
 * @license    MIT License
13
 */
14
15
namespace Omnipay\Datatrans\Message;
16
17
use Omnipay\Common\Exception\InvalidResponseException;
18
19
/**
20
 * Datatrans abstract request.
21
 * Implements all property setters and getters.
22
 */
23
abstract class XmlRequest extends AbstractRequest
24
{
25
    /**
26
     * The XML API Endpoint Base URL
27
     *
28
     * @var string
29
     */
30
    protected $apiBaseProdUrl = 'https://payment.datatrans.biz/upp/jsp';
31
    /**
32
     * The XML API Endpoint Base URL
33
     *
34
     * @var string
35
     */
36
    protected $apiBaseTestUrl = 'https://pilot.datatrans.biz/upp/jsp';
37
38
    /**
39
     * defines the endpoint for a specific api
40
     *
41
     * @var string
42
     */
43
    protected $apiEndpoint = null;
44
45
    /**
46
     * @var string
47
     */
48
    protected $serviceName = null;
49
50
    /**
51
     * @var int
52
     */
53
    protected $serviceVersion = null;
54
55
    /**
56
     * @param $requestChild
57
     * @return mixed
58
     */
59 10
    protected function prepareRequestXml($requestChild)
60
    {
61 10
        $fields = $this->getData();
62
63 10
        foreach ($fields as $key => $value) {
64 10
            if (is_array($value)) {
65
                $array = $requestChild->addChild($key);
66
67
                foreach ($value as $subKey => $subValue) {
68
                    $array->addChild($subKey, $subValue);
69
                }
70
            } else {
71 10
                $requestChild->addChild($key, $value);
72
            }
73 10
        }
74
75 10
        return $requestChild;
76
    }
77
78
    /**
79
     * Generate XML for request
80
     *
81
     * @return \SimpleXMLElement
82
     */
83 10
    protected function getRequestXml()
84
    {
85 10
        $serviceXmlNode = "<" . $this->getServiceName() . "/>";
86 10
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . $serviceXmlNode);
87 10
        $xml->addAttribute('version', $this->getServiceVersion());
88
89 10
        $bodyChild = $xml->addChild('body');
90 10
        $bodyChild->addAttribute('merchantId', $this->getMerchantId());
91
92 10
        $transactionChild = $bodyChild->addChild('transaction');
93 10
        $transactionChild->addAttribute('refno', $this->getTransactionId());
94
95 10
        $requestChild = $transactionChild->addChild('request');
96
97 10
        $this->prepareRequestXml($requestChild);
98
99 10
        $requestChild->addChild('sign', $this->getSign());
100
101 10
        return $xml;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 10
    public function getServiceName()
108
    {
109 10
        return $this->serviceName;
110
    }
111
112
    /**
113
     * @return int
114
     */
115 10
    public function getServiceVersion()
116
    {
117 10
        return $this->serviceVersion;
118
    }
119
120
    /**
121
     * Get HTTP Method.
122
     *
123
     * This is nearly always POST but can be over-ridden in sub classes.
124
     *
125
     * @return string
126
     */
127 10
    protected function getHttpMethod()
128
    {
129 10
        return 'POST';
130
    }
131
132
    /**
133
     * @param mixed $data
134
     * @return XmlResponse
135
     *
136
     * @throws InvalidResponseException
137
     */
138 10
    public function sendData($data)
139
    {
140 10
        $httpRequest = $this->httpClient->createRequest(
141 10
            $this->getHttpMethod(),
142 10
            $this->getEndpoint(),
143
            array(
144 10
                'Accept' => 'application/xml',
145 10
                'Content-type' => 'application/xml',
146 10
            ),
147 10
            $this->getRequestXml()->asXML()
0 ignored issues
show
Security Bug introduced by
It seems like $this->getRequestXml()->asXML() targeting SimpleXMLElement::asXML() can also be of type false; however, Guzzle\Http\ClientInterface::createRequest() does only seem to accept string|resource|array|ob...tityBodyInterface>|null, did you maybe forget to handle an error condition?
Loading history...
148 10
        );
149
150
151
        // Might be useful to have some debug code here, PayPal especially can be
152
        // a bit fussy about data formats and ordering.  Perhaps hook to whatever
153
        // logging engine is being used.
154
        // echo "Data == " . json_encode($data) . "\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
156
        try {
157 10
            $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
158 10
            $httpResponse = $httpRequest->send();
159
            // Empty response body should be parsed also as and empty array
160 10
            $body = $httpResponse->getBody(true);
161 10
            $xmlResponse = !empty($body) ? $httpResponse->xml() : '';
162
            
163 10
            if ($xmlResponse instanceof \SimpleXMLElement) {
164 10
                $response = $xmlResponse->body->transaction;
165
166 10
                $response = json_decode(json_encode($response), true);
167
168 10
                return $this->response = $this->createResponse($response);
169
            }
170
171
            throw new InvalidResponseException('Error communicating with payment gateway');
172
        } catch (\Exception $e) {
173
            throw new InvalidResponseException(
174
                'Error communicating with payment gateway: ' . $e->getMessage(),
175
                $e->getCode()
176
            );
177
        }
178
    }
179
180
    /**
181
     * @param $data
182
     *
183
     * @return XmlResponse
184
     */
185 10
    protected function createResponse($data)
186
    {
187 10
        return $this->response = new XmlResponse($this, $data);
188
    }
189
190
    /**
191
     * @return string
192
     */
193 10
    protected function getEndpoint()
194
    {
195 10
        $base = $this->getTestMode() ? $this->getApiBaseTestUrl() : $this->getApiBaseProdUrl();
196 10
        return $base . '/' . $this->getApiEndpoint();
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getApiBaseProdUrl()
203
    {
204
        return $this->apiBaseProdUrl;
205
    }
206
207
    /**
208
     * @return string
209
     */
210 10
    public function getApiBaseTestUrl()
211
    {
212 10
        return $this->apiBaseTestUrl;
213
    }
214
215
    /**
216
     * @return string
217
     */
218 10
    public function getApiEndpoint()
219
    {
220 10
        return $this->apiEndpoint;
221
    }
222
}
223