Passed
Push — main ( 536cb4...ad2370 )
by
unknown
08:47
created

PurchaseRequest::getEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
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\Exception\InvalidRequestException;
7
use Omnipay\Common\Message\AbstractRequest;
8
use Symfony\Component\HttpFoundation\ParameterBag;
9
10
/**
11
 * Windcave HPP Purchase Request
12
 */
13
14
class PurchaseRequest extends AbstractRequest {
15
16
    const endpointTest = 'https://uat.windcave.com/api/v1';
17
    const endpointLive = 'https://sec.windcave.com/api/v1';
18
19
    public function initialize(array $parameters = []) 
20
    {
21
        return parent::initialize($parameters);
22
    }
23
24
    public function setApiUsername($value)
25
    {
26
        return $this->setParameter('apiUsername', $value);
27
    }
28
29
    public function getApiUsername()
30
    {
31
        return $this->getParameter('apiUsername');
32
    }
33
34
    public function setApiKey($value)
35
    {
36
        return $this->setParameter('apiKey', $value);
37
    }
38
39
    public function getApiKey()
40
    {
41
        return $this->getParameter('apiKey');
42
    }
43
44
    public function setMerchantReference($value)
45
    {
46
        return $this->setParameter('merchantReference', $value);
47
    }
48
49
    public function getMerchantReference()
50
    {
51
        return $this->getParameter('merchantReference');
52
    }
53
54
    public function setType($value)
55
    {
56
        return $this->setParameter('type', $value);
57
    }
58
59
    public function getType()
60
    {
61
        return $this->getParameter('type') ?? 'purchase';
62
    }
63
64
    public function setLanguage($value)
65
    {
66
        return $this->setParameter('language', $value);
67
    }
68
69
    public function getLanguage()
70
    {
71
        return $this->getParameter('language') ?? 'en';
72
    }
73
74
    /**
75
     * @param $list
76
     * Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac', 'unionpay', 'oxipay', 'visacheckout', 'wechat']
77
     *
78
     * @return PurchaseRequest
79
     */
80
    public function setPaymentMethods($list)
81
    {
82
        $options = [
83
            'card', 'account2account', 'alipay', 'applepay',
84
            'googlepay', 'paypal', 'interac', 'unionpay',
85
            'oxipay', 'visacheckout', 'wechat'
86
        ];
87
88
        foreach ( $list as $method ) {
89
            if ( !in_array($method, $options) ) {
90
                throw new InvalidRequestException("Unknown payment method: {$method}");
91
            }
92
        }
93
94
        return $this->setParameter('paymentMethods', $list);
95
    }
96
97
    public function getPaymentMethods()
98
    {
99
        return $this->getParameter('paymentMethods');
100
    }
101
102
    public function setCardTypes($list)
103
    {
104
        return $this->setParameter('cardTypes', $list);
105
    }
106
107
    public function getCardTypes()
108
    {
109
        return $this->getParameter('cardTypes');
110
    }
111
112
    public function setExpiresAt($value)
113
    {
114
        return $this->setParameter('expiresAt', $value);
115
    }
116
117
    public function getExpiresAt()
118
    {
119
        return $this->getParameter('expiresAt');
120
    }
121
122
    public function setDeclineUrl($url)
123
    {
124
        return $this->setParameter('declineUrl', $url);
125
    }
126
127
    public function getDeclineUrl()
128
    {
129
        return $this->getParameter('declineUrl');
130
    }
131
132
    public function setStoreCard($value)
133
    {
134
        return $this->setParameter('storeCard', $value);
135
    }
136
137
    public function getStoreCard()
138
    {
139
        return $this->getParameter('storeCard');
140
    }
141
142
    public function setStoredCardIndicator($value)
143
    {
144
        $options = [
145
            'single', 'recurringfixed', 'recurringvariable', 'installment',
146
            'recurringnoexpiry', 'recurringinitial', 'installmentinitial', 'credentialonfileinitial',
147
            'unscheduledcredentialonfileinitial', 'credentialonfile', 'unscheduledcredentialonfile', 'incremental',
148
            'resubmission', 'reauthorisation', 'delayedcharges', 'noshow'
149
        ];
150
151
        if ( ! in_array($value, $options) ) {
152
            throw new InvalidRequestException("Invalid option '{$value}' set for StoredCardIndicator.");
153
        }
154
155
        return $this->setParameter('storeCardIndicator', $value);
156
    }
157
158
    public function getStoredCardIndicator()
159
    {
160
        return $this->getParameter('storeCardIndicator');
161
    }
162
163
    public function setMetadata($data)
164
    {
165
        return $this->setParameter('metaData', $data);
166
    }
167
168
    public function getMetadata()
169
    {
170
        return $this->getParameter('metaData');
171
    }
172
173
    public function setRecurringFrequency($value)
174
    {
175
        $options = [
176
            'daily', 'weekly', 'every2weeks', 'every4weeks',
177
            'monthly', 'monthly28th', 'monthlylastcalendarday',
178
            'monthlysecondlastcalendarday', 'monthlythirdlastcalendarday',
179
            'twomonthly', 'threemonthly', 'fourmonthly', 'sixmonthly', 'annually'
180
        ];
181
182
        if ( ! in_array($value, $options) ) {
183
            throw new InvalidRequestException("Invalid option '{$value}' set for RecurringFrequency.");
184
        }
185
186
        return $this->setParameter('recurringFrequency', $value);
187
    }
188
189
    public function getRecurringFrequency()
190
    {
191
        return $this->getParameter('recurringFrequency');
192
    }
193
194
    public function getData()
195
    {
196
        $this->validate('apiUsername', 'apiKey', 'amount', 'currency');
197
198
        $data = [];
199
200
        $data['type'] = $this->getType();
201
        $data['amount'] = $this->getAmount();
202
        $data['currency'] = $this->getCurrency();
203
        $data['storeCard'] = (bool) $this->getStoreCard() ?? false;
204
        $data['callbackUrls'] = [];
205
206
        if ( is_array($this->getPaymentMethods()) ) {
207
            $data['methods'] = $this->getPaymentMethods();
208
        }
209
210
        if ( is_array($this->getCardTypes()) ) {
211
            $data['cardTypes'] = $this->getCardTypes();
212
        }
213
214
        if ( is_array($this->getMetadata()) ) {
215
            $data['metaData'] = $this->getMetadata();
216
        }
217
218
        if ( $this->getMerchantReference() ) {
219
            $data['merchantReference'] = $this->getMerchantReference();
220
        }
221
222
        if ( $this->getReturnUrl() ) {
223
            $data['callbackUrls']['approved'] = $this->getReturnUrl();
224
        }
225
226
        if ( $this->getDeclineUrl() ) {
227
            $data['callbackUrls']['declined'] = $this->getDeclineUrl();
228
        }
229
230
        if ( $this->getCancelUrl() ) {
231
            $data['callbackUrls']['cancelled'] = $this->getCancelUrl();
232
        }
233
234
        if ( $this->getNotifyUrl() ) {
235
            $data['notificationUrl'] = $this->getNotifyUrl();
236
        }
237
238
        return $data;
239
    }
240
241
    public function sendData($data)
242
    {
243
        $headers = [
244
            'Accept' => 'application/json',
245
            'Content-Type' => 'application/json',
246
            'Authorization' => 'Basic ' . $this->getAuthorization()
247
        ];
248
249
        $httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
250
251
        try {
252
            $responseData = json_decode($httpResponse->getBody()->getContents());
253
        } catch (\Exception $exception) {
254
            $responseData = [];
255
        }
256
257
        return $this->response = new PurchaseResponse($this, $responseData ?? []);
258
    }
259
260
    /**
261
     * Get endpoint
262
     *
263
     * Returns endpoint depending on test mode
264
     *
265
     * @access protected
266
     * @return string
267
     */
268
    protected function getEndpoint($path = '')
269
    {
270
        return ($this->getTestMode() ? self::endpointTest : self::endpointLive) . '/' . $path;
271
    }
272
273
    protected function getAuthorization()
274
    {
275
        return base64_encode($this->getApiUsername().':'.$this->getApiKey());
276
    }
277
}