Passed
Push — main ( 007e00...f82f47 )
by
unknown
09:50
created

PurchaseRequest::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Omnipay\WindcaveHpp\Message;
4
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Omnipay\Common\Message\AbstractRequest;
7
8
/**
9
 * Windcave HPP Purchase Request
10
 */
11
12
class PurchaseRequest extends AbstractRequest {
13
14
    const endpointTest = 'https://uat.windcave.com/api/v1';
15
    const endpointLive = 'https://sec.windcave.com/api/v1';
16
17
    public function initialize(array $parameters = []) 
18
    {
19
        return parent::initialize($parameters);
20
    }
21
22
    public function setApiUsername($value)
23
    {
24
        return $this->setParameter('apiUsername', $value);
25
    }
26
27
    public function getApiUsername()
28
    {
29
        return $this->getParameter('apiUsername');
30
    }
31
32
    public function setApiKey($value)
33
    {
34
        return $this->setParameter('apiKey', $value);
35
    }
36
37
    public function getApiKey()
38
    {
39
        return $this->getParameter('apiKey');
40
    }
41
42
    public function setMerchantReference($value)
43
    {
44
        return $this->setParameter('merchantReference', $value);
45
    }
46
47
    public function getMerchantReference()
48
    {
49
        return $this->getParameter('merchantReference');
50
    }
51
52
    public function setType($value)
53
    {
54
        return $this->setParameter('type', $value);
55
    }
56
57
    public function getType()
58
    {
59
        return $this->getParameter('type') ?? 'purchase';
60
    }
61
62
    public function setLanguage($value)
63
    {
64
        return $this->setParameter('language', $value);
65
    }
66
67
    public function getLanguage()
68
    {
69
        return $this->getParameter('language') ?? 'en';
70
    }
71
72
    public function setPaymentMethods($list)
73
    {
74
        return $this->setParameter('paymentMethods', $list);
75
    }
76
77
    public function getPaymentMethods()
78
    {
79
        return $this->getParameter('paymentMethods');
80
    }
81
82
    public function setCardTypes($list)
83
    {
84
        return $this->setParameter('cardTypes', $list);
85
    }
86
87
    public function getCardTypes()
88
    {
89
        return $this->getParameter('cardTypes');
90
    }
91
92
    public function setExpiresAt($value)
93
    {
94
        return $this->setParameter('expiresAt', $value);
95
    }
96
97
    public function getExpiresAt()
98
    {
99
        return $this->getParameter('expiresAt');
100
    }
101
102
    public function setDeclineUrl($url)
103
    {
104
        return $this->setParameter('declineUrl', $url);
105
    }
106
107
    public function getDeclineUrl()
108
    {
109
        return $this->getParameter('declineUrl');
110
    }
111
112
    public function setStoreCard($value)
113
    {
114
        return $this->setParameter('storeCard', $value);
115
    }
116
117
    public function getStoreCard()
118
    {
119
        return $this->getParameter('storeCard');
120
    }
121
122
    public function getData()
123
    {
124
        $this->validate('apiUsername', 'apiKey', 'amount', 'currency');
125
126
        $data = [];
127
128
        $data['type'] = $this->getType();
129
        $data['amount'] = $this->getAmount();
130
        $data['currency'] = $this->getCurrency();
131
        $data['storeCard'] = (bool) $this->getStoreCard() ?? false;
132
        $data['callbackUrls'] = [];
133
134
        if ( $this->getMerchantReference() ) {
135
            $data['merchantReference'] = $this->getMerchantReference();
136
        }
137
138
        if ( $this->getReturnUrl() ) {
139
            $data['callbackUrls']['approved'] = $this->getReturnUrl();
140
        }
141
142
        if ( $this->getDeclineUrl() ) {
143
            $data['callbackUrls']['declined'] = $this->getDeclineUrl();
144
        }
145
146
        if ( $this->getCancelUrl() ) {
147
            $data['callbackUrls']['cancelled'] = $this->getCancelUrl();
148
        }
149
150
        if ( $this->getNotifyUrl() ) {
151
            $data['notificationUrl'] = $this->getNotifyUrl();
152
        }
153
154
        return $data;
155
    }
156
157
    public function sendData($data)
158
    {
159
//        echo '<pre>'; print_r($data); echo '</pre>';
160
161
        $headers = [
162
            'Accept' => 'application/json',
163
            'Content-Type' => 'application/json',
164
            'Authorization' => 'Basic ' . $this->getAuthorization()
165
        ];
166
167
        $httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
168
169
        try {
170
            $responseData = json_decode($httpResponse->getBody()->getContents());
171
        } catch (\Exception $exception) {
172
            $responseData = [];
173
        }
174
175
        return $this->response = new PurchaseResponse($this, $responseData ?? []);
176
    }
177
178
    /**
179
     * Get endpoint
180
     *
181
     * Returns endpoint depending on test mode
182
     *
183
     * @access protected
184
     * @return string
185
     */
186
    protected function getEndpoint($path = '')
187
    {
188
        return ($this->getTestMode() ? self::endpointTest : self::endpointLive) . '/' . $path;
189
    }
190
191
    protected function getAuthorization()
192
    {
193
        return base64_encode($this->getApiUsername().':'.$this->getApiKey());
194
    }
195
}