AbstractRequest::sendData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
use Omnipay\Common\Message\ResponseInterface;
7
use SimpleXMLElement;
8
9
/**
10
 * BillPay Abstract Request.
11
 *
12
 * @link      https://techdocs.billpay.de/en/For_developers/Introduction.html
13
 *
14
 * @author    Andreas Lange <[email protected]>
15
 * @copyright 2016, Connox GmbH
16
 * @license   MIT
17
 */
18
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
19
{
20
    const API_VERSION = '1.5.10';
21
22
    protected $liveEndpoint = 'https://api.billpay.de/xml';
23
    protected $testEndpoint = 'https://test-api.billpay.de/xml/offline';
24
25
    /**
26
     * @var string
27
     */
28
    protected $rawLastHttpRequest;
29
30
    /**
31
     * @var string
32
     */
33
    protected $rawLastHttpResponse;
34
35
    /**
36
     * @param string $country
37
     *
38
     * @return string|null ISO-3166-1 Alpha3
39
     */
40 12
    public function getCountryCode($country)
41
    {
42
        $countries = [
43 11
            'germany' => 'DEU',
44 11
            'deu' => 'DEU',
45 11
            'de' => 'DEU',
46 11
            'austria' => 'AUT',
47 12
            'aut' => 'AUT',
48 11
            'at' => 'AUT',
49 11
            'switzerland' => 'CHE',
50 11
            'swiss' => 'CHE',
51 11
            'che' => 'CHE',
52 12
            'ch' => 'CHE',
53 11
            'netherlands' => 'NLD',
54 11
            'the netherlands' => 'NLD',
55 11
            'nld' => 'NLD',
56 11
            'nl' => 'NLD',
57 11
        ];
58
59 11
        return array_key_exists(strtolower($country), $countries) ? $countries[strtolower($country)] : null;
60
    }
61
62
    /**
63
     * @return int Merchant ID
64
     */
65 22
    public function getMerchantId()
66
    {
67 22
        return $this->getParameter('merchantId');
68
    }
69
70
    /**
71
     * @return int Portal ID
72
     */
73 22
    public function getPortalId()
74
    {
75 22
        return $this->getParameter('portalId');
76
    }
77
78
    /**
79
     * Gets the raw http request of the last request including header lines.
80
     *
81
     * @return string
82
     */
83 1
    public function getRawLastHttpRequest()
84
    {
85 1
        return $this->rawLastHttpRequest;
86
    }
87
88
    /**
89
     * Gets the raw http response of the last request including header lines.
90
     *
91
     * @return string
92
     */
93 1
    public function getRawLastHttpResponse()
94
    {
95 1
        return $this->rawLastHttpResponse;
96 1
    }
97
98
    /**
99
     * @return string MD5 hash of the security key generated for this portal. (generated and delivered by BillPay)
100
     */
101 22
    public function getSecurityKey()
102
    {
103 22
        return $this->getParameter('securityKey');
104
    }
105
106
    /**
107
     * Send the request with specified data.
108
     *
109
     * @param SimpleXMLElement $data The data to send
110
     *
111
     * @throws InvalidRequestException
112
     *
113
     * @return ResponseInterface
114
     */
115 12
    public function sendData($data)
116
    {
117 12
        if (! $data instanceof SimpleXMLElement) {
118 1
            throw new InvalidRequestException('Data must be XML.');
119
        }
120
121 11
        $httpRequest = $this->httpClient->post($this->getEndpoint(), null, (string)$data->asXML());
122 11
        $this->rawLastHttpRequest = (string)$httpRequest;
123
124 11
        $httpResponse = $httpRequest->send();
125 11
        $this->rawLastHttpResponse = $httpResponse->getMessage();
126
127 11
        return $this->createResponse($httpResponse->xml());
128
    }
129
130
    /**
131
     * @param int $value Merchant ID
132
     *
133
     * @return AbstractRequest
134
     */
135 19
    public function setMerchantId($value)
136
    {
137 19
        return $this->setParameter('merchantId', $value);
138
    }
139
140
    /**
141
     * @param int $value Portal ID
142
     *
143
     * @return AbstractRequest
144
     */
145 19
    public function setPortalId($value)
146
    {
147 19
        return $this->setParameter('portalId', $value);
148
    }
149
150
    /**
151
     * @param string $value MD5 hash of the security key generated for this portal. (generated and delivered by BillPay)
152
     *
153
     * @return AbstractRequest
154
     */
155 19
    public function setSecurityKey($value)
156
    {
157 19
        return $this->setParameter('securityKey', $value);
158
    }
159
160
    /**
161
     * @param SimpleXMLElement $response
162
     *
163
     * @return ResponseInterface
164
     *
165
     * @codeCoverageIgnore
166
     */
167
    abstract protected function createResponse($response);
168
169
    /**
170
     * @return SimpleXMLElement
171
     */
172 18
    protected function getBaseData()
173
    {
174 18
        $data = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><data/>');
175 18
        $data['api_version'] = self::API_VERSION;
176 18
        $data[0]->default_params['mid'] = $this->getMerchantId();
177 18
        $data[0]->default_params['pid'] = $this->getPortalId();
178 18
        $data[0]->default_params['bpsecure'] = md5($this->getSecurityKey());
179
180 18
        return $data;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 11
    protected function getEndpoint()
187
    {
188 11
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
189
    }
190
}
191