PurchaseRequest::setPaymentMethods()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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