Stripe::verify()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Clarkeash\Shield\Services;
4
5
use Illuminate\Http\Request;
6
7
class Stripe extends BaseService
8
{
9
    public function verify(Request $request): bool
10
    {
11
        $processed = $this->process($this->header($request, 'Stripe-Signature'));
12
13
        $tolerance = config('shield.services.stripe.tolerance', 60 * 5);
14
15
        if(($tolerance > 0) && ((time() - $processed['t']) > $tolerance)) {
16
            return false;
17
        }
18
19
        $payload = $processed['t'] . '.' . $request->getContent();
20
        $generated = hash_hmac('sha256', $payload, config('shield.services.stripe.token'));
21
22
        return hash_equals($generated, $processed['v1']);
23
    }
24
25
    protected function process(string $header)
26
    {
27
        $sections = explode(',', $header);
28
29
        $data = [];
30
31
        foreach ($sections as $section) {
32
            $parts = explode('=', $section);
33
            $data[$parts[0]] = $parts[1];
34
        }
35
36
        return $data;
37
    }
38
39
    public function headers(): array
40
    {
41
        return ['Stripe-Signature'];
42
    }
43
}
44