Completed
Push — master ( 7009b3...8b82fe )
by Dominik
03:21
created

XmlRequest::getServiceVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * w-vision
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the GNU General Public License version 3 (GPLv3)
8
 * For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
9
 * files that are distributed with this source code.
10
 *
11
 * @copyright  Copyright (c) 2016 Woche-Pass AG (http://www.w-vision.ch)
12
 * @license    GNU General Public License version 3 (GPLv3)
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 8
    protected function prepareRequestXml($requestChild)
60
    {
61 8
        $fields = $this->getData();
62
63 8
        foreach ($fields as $key => $value) {
64 8
            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 8
                $requestChild->addChild($key, $value);
72
            }
73 8
        }
74
75 8
        return $requestChild;
76
    }
77
78
    /**
79
     * Generate XML for request
80
     *
81
     * @return \SimpleXMLElement
82
     */
83 8
    protected function getRequestXml()
84
    {
85 8
        $serviceXmlNode = "<" . $this->getServiceName() . "/>";
86 8
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . $serviceXmlNode);
87 8
        $xml->addAttribute('version', $this->getServiceVersion());
88
89 8
        $bodyChild = $xml->addChild('body');
90 8
        $bodyChild->addAttribute('merchantId', $this->getMerchantId());
91
92 8
        $transactionChild = $bodyChild->addChild('transaction');
93 8
        $transactionChild->addAttribute('refno', $this->getTransactionId());
94
95 8
        $requestChild = $transactionChild->addChild('request');
96
97 8
        $this->prepareRequestXml($requestChild);
98
99 8
        $requestChild->addChild('sign', $this->getSign());
100
101 8
        return $xml;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 8
    public function getServiceName()
108
    {
109 8
        return $this->serviceName;
110
    }
111
112
    /**
113
     * @return int
114
     */
115 8
    public function getServiceVersion()
116
    {
117 8
        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 8
    protected function getHttpMethod()
128
    {
129 8
        return 'POST';
130
    }
131
132
    /**
133
     * @param mixed $data
134
     * @return XmlResponse
135
     *
136
     * @throws InvalidResponseException
137
     */
138 8
    public function sendData($data)
139
    {
140 8
        $httpRequest = $this->httpClient->createRequest(
141 8
            $this->getHttpMethod(),
142 8
            $this->getEndpoint(),
143
            array(
144 8
                'Accept' => 'application/xml',
145 8
                'Content-type' => 'application/xml',
146 8
            ),
147 8
            $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 8
        );
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 8
            $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
158 8
            $httpResponse = $httpRequest->send();
159
            // Empty response body should be parsed also as and empty array
160 8
            $body = $httpResponse->getBody(true);
161 8
            $xmlResponse = !empty($body) ? $httpResponse->xml() : '';
162
            
163 8
            if ($xmlResponse instanceof \SimpleXMLElement) {
164 8
                $response = $xmlResponse->body->transaction;
165
166 8
                $response = json_decode(json_encode($response), true);
167
168 8
                return $this->response = $this->createResponse($response);
169
            }
170
171 1
            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 8
    protected function createResponse($data)
186
    {
187 8
        return $this->response = new XmlResponse($this, $data);
188
    }
189
190
    /**
191
     * @return string
192
     */
193 8
    protected function getEndpoint()
194
    {
195 8
        $base = $this->getTestMode() ? $this->getApiBaseTestUrl() : $this->getApiBaseProdUrl();
196 8
        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 8
    public function getApiBaseTestUrl()
211
    {
212 8
        return $this->apiBaseTestUrl;
213
    }
214
215
    /**
216
     * @return string
217
     */
218 8
    public function getApiEndpoint()
219
    {
220 8
        return $this->apiEndpoint;
221
    }
222
}
223