• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / PayWay / 5212705470

pending completion
5212705470

push

github

mlocati
Initial version

1803 of 1803 new or added lines in 47 files covered. (100.0%)

1313 of 1803 relevant lines covered (72.82%)

1.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/src/Service/BaseRequestTrait.php
1
<?php
2

3
namespace MLocati\PayWay\Service;
4

5
use DateTime;
6
use DateTimeInterface;
7
use MLocati\PayWay\Exception;
8
use ValueError;
9

10
trait BaseRequestTrait
11
{
12
    use JsonCleanupTrait;
13

14
    /**
15
     * Undocumented.
16
     *
17
     * @var string
18
     */
19
    private $apiVersion = '';
20

21
    /**
22
     * The merchant terminal code.
23
     *
24
     * @var string
25
     */
26
    private $tid = '';
27

28
    /**
29
     * Undocumented.
30
     *
31
     * @var string
32
     */
33
    private $merID = '';
34

35
    /**
36
     * Undocumented.
37
     *
38
     * @var string
39
     */
40
    private $payInstr = '';
41

42
    /**
43
     * @var \DateTimeInterface|null
44
     */
45
    private $reqTime;
46

47
    /**
48
     * Undocumented.
49
     *
50
     * @return string
51
     */
52
    public function getApiVersion()
53
    {
54
        return $this->apiVersion;
5✔
55
    }
56

57
    /**
58
     * Undocumented.
59
     *
60
     * @param string $value
61
     *
62
     * @return $this
63
     */
64
    public function setApiVersion($value)
65
    {
66
        $this->apiVersion = (string) $value;
×
67

68
        return $this;
×
69
    }
70

71
    /**
72
     * Get the merchant terminal code.
73
     *
74
     * @return string
75
     */
76
    public function getTID()
77
    {
78
        return $this->tid;
5✔
79
    }
80

81
    /**
82
     * Set the merchant terminal code.
83
     *
84
     * @param string $value
85
     *
86
     * @return $this
87
     */
88
    public function setTID($value)
89
    {
90
        $this->tid = (string) $value;
2✔
91

92
        return $this;
2✔
93
    }
94

95
    /**
96
     * Undocumented.
97
     *
98
     * @return string
99
     */
100
    public function getMerID()
101
    {
102
        return $this->merID;
5✔
103
    }
104

105
    /**
106
     * Undocumented.
107
     *
108
     * @param string $value
109
     *
110
     * @return $this
111
     */
112
    public function setMerID($value)
113
    {
114
        $this->merID = (string) $value;
×
115

116
        return $this;
×
117
    }
118

119
    /**
120
     * Undocumented.
121
     *
122
     * @return string
123
     */
124
    public function getPayInstr()
125
    {
126
        return $this->payInstr;
5✔
127
    }
128

129
    /**
130
     * Undocumented.
131
     *
132
     * @param string $value
133
     *
134
     * @return $this
135
     */
136
    public function setPayInstr($value)
137
    {
138
        $this->payInstr = (string) $value;
×
139

140
        return $this;
×
141
    }
142

143
    /**
144
     * Undocumented.
145
     *
146
     * @return \DateTimeInterface|null
147
     */
148
    public function getReqTime()
149
    {
150
        return $this->reqTime;
5✔
151
    }
152

153
    /**
154
     * Undocumented.
155
     *
156
     * @return $this
157
     */
158
    public function setReqTime(DateTimeInterface $value = null)
159
    {
160
        $this->reqTime = $value;
×
161

162
        return $this;
×
163
    }
164

165
    /**
166
     * Get the HMAC-SHA256 signature.
167
     *
168
     * @param string $key
169
     *
170
     * @throws \MLocati\PayWay\Exception\UnableToCreateSignature
171
     *
172
     * @return string
173
     */
174
    public function getSignature($key)
175
    {
176
        $data = implode('', $this->getSignatureFields());
5✔
177

178
        try {
179
            $rawSignature = hash_hmac('sha256', $data, $key, true);
5✔
180
        } catch (ValueError $x) {
×
181
            throw new Exception\UnableToCreateSignature($x->getMessage());
×
182
        }
183
        if ($rawSignature === false) {
5✔
184
            throw new Exception\UnableToCreateSignature();
×
185
        }
186

187
        return base64_encode($rawSignature);
5✔
188
    }
189

190
    /**
191
     * {@inheritdoc}
192
     *
193
     * @see \JsonSerializable::jsonSerialize()
194
     */
195
    #[\ReturnTypeWillChange]
196
    public function jsonSerialize()
197
    {
198
        return $this->cleanupJson([
2✔
199
            'apiVersion' => $this->apiVersion,
2✔
200
            'tid' => $this->tid,
2✔
201
            'merID' => $this->merID,
2✔
202
            'payInstr' => $this->payInstr,
2✔
203
            'reqTime' => $this->reqTime === null ? '' : $this->reqTime->format(DateTime::RFC3339),
2✔
204
        ]);
2✔
205
    }
206

207
    /**
208
     * @return string[]
209
     *
210
     * @private
211
     */
212
    abstract protected function getSignatureFields();
213

214
    /**
215
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
216
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
217
     * @throws \MLocati\PayWay\Exception\FieldValueOutOfRange
218
     * @throws \MLocati\PayWay\Exception\InvalidFieldUrl
219
     */
220
    private function checkBaseRequest()
221
    {
222
        $this->checkStringField('tid', true, 16);
5✔
223
    }
224

225
    /**
226
     * @param string $fieldName
227
     * @param bool $required
228
     * @param int $maxLength
229
     *
230
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
231
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
232
     */
233
    private function checkStringField($fieldName, $required, $maxLength)
234
    {
235
        $value = $this->{$fieldName};
5✔
236
        if ($value === '') {
5✔
237
            if ($required) {
4✔
238
                throw new Exception\MissingRequiredField($fieldName);
4✔
239
            }
240
        } elseif (strlen($value) > $maxLength) {
5✔
241
            throw new Exception\FieldValueTooLong($fieldName, $maxLength);
×
242
        }
243
    }
244

245
    /**
246
     * @param string $fieldName
247
     * @param bool $required
248
     * @param string[] $allowedValues
249
     *
250
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
251
     * @throws \MLocati\PayWay\Exception\FieldValueOutOfRange
252
     */
253
    private function checkEnumField($fieldName, $required, array $allowedValues)
254
    {
255
        $value = $this->{$fieldName};
4✔
256
        if ($value === '') {
4✔
257
            if ($required) {
4✔
258
                throw new Exception\MissingRequiredField($fieldName);
4✔
259
            }
260
        } elseif (!in_array($value, $allowedValues, true)) {
4✔
261
            throw new Exception\FieldValueOutOfRange($fieldName, $allowedValues);
×
262
        }
263
    }
264

265
    /**
266
     * @param string $fieldName
267
     * @param bool $required
268
     * @param int $maxLength
269
     *
270
     * @throws \MLocati\PayWay\Exception\MissingRequiredField
271
     * @throws \MLocati\PayWay\Exception\FieldValueTooLong
272
     * @throws \MLocati\PayWay\Exception\InvalidFieldUrl
273
     */
274
    private function checkUrlField($fieldName, $required, $maxLength)
275
    {
276
        $value = $this->{$fieldName};
4✔
277
        if ($value === '') {
4✔
278
            if ($required) {
4✔
279
                throw new Exception\MissingRequiredField($fieldName);
4✔
280
            }
281
        } elseif (strlen($value) > $maxLength) {
4✔
282
            throw new Exception\FieldValueTooLong($fieldName, $maxLength);
×
283
        } elseif (!filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
4✔
284
            throw new Exception\InvalidFieldUrl($fieldName);
×
285
        }
286
    }
287
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc