Completed
Push — master ( 620307...8ff95d )
by Dominik
03:16
created

XmlRequest::getRequestXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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
    protected function prepareRequestXml($requestChild)
60
    {
61
        $fields = $this->getData();
62
63
        foreach ($fields as $key => $value) {
64
            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
                $requestChild->addChild($key, $value);
72
            }
73
        }
74
75
        return $requestChild;
76
    }
77
78
    /**
79
     * Generate XML for request
80
     *
81
     * @return \SimpleXMLElement
82
     */
83
    protected function getRequestXml()
84
    {
85
        $serviceXmlNode = "<" . $this->getServiceName() . "/>";
86
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . $serviceXmlNode);
87
        $xml->addAttribute('version', $this->getServiceVersion());
88
89
        $bodyChild = $xml->addChild('body');
90
        $bodyChild->addAttribute('merchantId', $this->getMerchantId());
91
92
        $transactionChild = $bodyChild->addChild('transaction');
93
        $transactionChild->addAttribute('refno', $this->getTransactionId());
94
95
        $requestChild = $transactionChild->addChild('request');
96
97
        $this->prepareRequestXml($requestChild);
98
99
        $requestChild->addChild('sign', $this->getSign());
100
101
        return $xml;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getServiceName()
108
    {
109
        return $this->serviceName;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getServiceVersion()
116
    {
117
        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
    protected function getHttpMethod()
128
    {
129
        return 'POST';
130
    }
131
132
    /**
133
     * @param mixed $data
134
     * @return XmlResponse
135
     *
136
     * @throws InvalidResponseException
137
     */
138
    public function sendData($data)
139
    {
140
        $httpRequest = $this->httpClient->createRequest(
141
            $this->getHttpMethod(),
142
            $this->getEndpoint(),
143
            array(
144
                'Accept' => 'application/xml',
145
                'Content-type' => 'application/xml',
146
            ),
147
            $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
        );
149
150
        // Might be useful to have some debug code here, PayPal especially can be
151
        // a bit fussy about data formats and ordering.  Perhaps hook to whatever
152
        // logging engine is being used.
153
        // 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...
154
155
        try {
156
            $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
157
            $httpResponse = $httpRequest->send();
158
            // Empty response body should be parsed also as and empty array
159
            $body = $httpResponse->getBody(true);
160
            $xmlResponse = !empty($body) ? $httpResponse->xml() : '';
161
162
            if($xmlResponse instanceof \SimpleXMLElement) {
163
                $response = $xmlResponse->body->transaction;
164
165
                $response = json_decode(json_encode($response), true);
166
167
                return $this->response = $this->createResponse($response);
168
            }
169
170
            throw new InvalidResponseException('Error communicating with payment gateway');
171
        } catch (\Exception $e) {
172
            throw new InvalidResponseException(
173
                'Error communicating with payment gateway: ' . $e->getMessage(),
174
                $e->getCode()
175
            );
176
        }
177
    }
178
179
    /**
180
     * @param $data
181
     *
182
     * @return XmlResponse
183
     */
184
    protected function createResponse($data)
185
    {
186
        return $this->response = new XmlResponse($this, $data);
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    protected function getEndpoint()
193
    {
194
        $base = $this->getTestMode() ? $this->getApiBaseTestUrl() : $this->getApiBaseProdUrl();
195
        return $base . '/' . $this->getApiEndpoint();
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function getApiBaseProdUrl()
202
    {
203
        return $this->apiBaseProdUrl;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getApiBaseTestUrl()
210
    {
211
        return $this->apiBaseTestUrl;
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function getApiEndpoint()
218
    {
219
        return $this->apiEndpoint;
220
    }
221
}
222