Passed
Pull Request — main (#5)
by Leith
03:40
created

BaseRequest::setCreateToken()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 1
1
<?php
2
3
namespace Omnipay\WindcaveHpp\Message;
4
5
use Omnipay\Common\Message\AbstractRequest;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
8
abstract class BaseRequest extends AbstractRequest
9
{
10
    const ENDPOINT_TEST = 'https://uat.windcave.com/api/v1';
11
    const ENDPOINT_LIVE = 'https://sec.windcave.com/api/v1';
12
13
    // API Credentials
14
    public function setApiUsername($value)
15
    {
16
        return $this->setParameter('apiUsername', $value);
17
    }
18
19
    public function getApiUsername()
20
    {
21
        return $this->getParameter('apiUsername');
22
    }
23
24
    public function setApiKey($value)
25
    {
26
        return $this->setParameter('apiKey', $value);
27
    }
28
29
    public function getApiKey()
30
    {
31
        return $this->getParameter('apiKey');
32
    }
33
34
    protected function getAuthorization()
35
    {
36
        return base64_encode($this->getApiUsername() . ':' . $this->getApiKey());
37
    }
38
39
    public function setCreateToken($value)
40
    {
41
        return $this->setParameter('createToken', $value);
42
    }
43
44
    public function getCreateToken()
45
    {
46
        return $this->getParameter('createToken');
47
    }
48
49
    // Merchant Reference
50
    public function setMerchantReference($value)
51
    {
52
        return $this->setParameter('merchantReference', $value);
53
    }
54
55
    public function getMerchantReference()
56
    {
57
        return $this->getParameter('merchantReference');
58
    }
59
60
    public function setType($value)
61
    {
62
        return $this->setParameter('type', $value);
63
    }
64
65
    public function getType()
66
    {
67
        return $this->getParameter('type') ?? 'purchase';
68
    }
69
70
    public function setLanguage($value)
71
    {
72
        return $this->setParameter('language', $value);
73
    }
74
75
    public function getLanguage()
76
    {
77
        return $this->getParameter('language') ?? 'en';
78
    }
79
80
    /**
81
     * @param $list
82
     * Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac', 'unionpay', 'oxipay', 'visacheckout', 'wechat']
83
     *
84
     * @return PurchaseRequest
85
     */
86
    public function setPaymentMethods($list)
87
    {
88
        $options = [
89
            'card', 'account2account', 'alipay', 'applepay',
90
            'googlepay', 'paypal', 'interac', 'unionpay',
91
            'oxipay', 'visacheckout', 'wechat'
92
        ];
93
94
        foreach ( $list as $method ) {
95
            if ( !in_array($method, $options) ) {
96
                throw new InvalidRequestException("Unknown payment method: {$method}");
97
            }
98
        }
99
100
        return $this->setParameter('paymentMethods', $list);
101
    }
102
103
    public function getPaymentMethods()
104
    {
105
        return $this->getParameter('paymentMethods');
106
    }
107
108
    public function setCardTypes($list)
109
    {
110
        return $this->setParameter('cardTypes', $list);
111
    }
112
113
    public function getCardTypes()
114
    {
115
        return $this->getParameter('cardTypes');
116
    }
117
118
    public function setExpiresAt($value)
119
    {
120
        return $this->setParameter('expiresAt', $value);
121
    }
122
123
    public function getExpiresAt()
124
    {
125
        return $this->getParameter('expiresAt');
126
    }
127
128
    public function setDeclineUrl($url)
129
    {
130
        return $this->setParameter('declineUrl', $url);
131
    }
132
133
    public function getDeclineUrl()
134
    {
135
        return $this->getParameter('declineUrl');
136
    }
137
138
    /**
139
     * @deprecated   Alias. Use standard `setCreateToken()` instead
140
     */
141
    public function setStoreCard($value)
142
    {
143
        return $this->setCreateToken($value);
144
    }
145
146
    /**
147
     * @deprecated   Alias. Use standard `getCreateToken()` instead
148
     */
149
    public function getStoreCard()
150
    {
151
        return $this->getCreateToken();
152
    }
153
154
    public function setStoredCardIndicator($value)
155
    {
156
        $options = [
157
            'single', 'recurringfixed', 'recurringvariable', 'installment',
158
            'recurringnoexpiry', 'recurringinitial', 'installmentinitial', 'credentialonfileinitial',
159
            'unscheduledcredentialonfileinitial', 'credentialonfile', 'unscheduledcredentialonfile', 'incremental',
160
            'resubmission', 'reauthorisation', 'delayedcharges', 'noshow'
161
        ];
162
163
        if ( ! in_array($value, $options) ) {
164
            throw new InvalidRequestException("Invalid option '{$value}' set for StoredCardIndicator.");
165
        }
166
167
        return $this->setParameter('storedCardIndicator', $value);
168
    }
169
170
    public function getStoredCardIndicator()
171
    {
172
        return $this->getParameter('storedCardIndicator');
173
    }
174
175
    public function setMetadata($data)
176
    {
177
        return $this->setParameter('metaData', $data);
178
    }
179
180
    public function getMetadata()
181
    {
182
        return $this->getParameter('metaData');
183
    }
184
185
186
    public function setRecurringFrequency($value)
187
    {
188
        $options = [
189
            'daily', 'weekly', 'every2weeks', 'every4weeks',
190
            'monthly', 'monthly28th', 'monthlylastcalendarday',
191
            'monthlysecondlastcalendarday', 'monthlythirdlastcalendarday',
192
            'twomonthly', 'threemonthly', 'fourmonthly', 'sixmonthly', 'annually'
193
        ];
194
195
        if ( ! in_array($value, $options) ) {
196
            throw new InvalidRequestException("Invalid option '{$value}' set for RecurringFrequency.");
197
        }
198
199
        return $this->setParameter('recurringFrequency', $value);
200
    }
201
202
    public function getRecurringFrequency()
203
    {
204
        return $this->getParameter('recurringFrequency');
205
    }
206
207
    public function setRecurringExpiry($value)
208
    {
209
        // For scenarios where no expiry/end date is established i.e.,
210
        // subscription payments, the merchant web application should use "9999-12-31" as the value.
211
212
        return $this->setParameter('recurringExpiry', $value);
213
    }
214
215
    public function getRecurringExpiry()
216
    {
217
        return $this->getParameter('recurringExpiry');
218
    }
219
220
    // Endpoint selection based on test mode
221
    protected function getEndpoint($path = '')
222
    {
223
        $base = $this->getTestMode() ? self::ENDPOINT_TEST : self::ENDPOINT_LIVE;
224
        return $base . '/' . $path;
225
    }
226
}
227