|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yabacon\Paystack; |
|
3
|
|
|
|
|
4
|
|
|
use Yabacon\Paystack\Http\Request; |
|
5
|
|
|
|
|
6
|
|
|
class Event |
|
7
|
|
|
{ |
|
8
|
|
|
public $raw = ''; |
|
9
|
|
|
protected $signature = ''; |
|
10
|
|
|
public $obj; |
|
11
|
|
|
const SIGNATURE_KEY = 'HTTP_X_PAYSTACK_SIGNATURE'; |
|
12
|
|
|
|
|
13
|
6 |
|
protected function __construct() |
|
14
|
|
|
{ |
|
15
|
6 |
|
} |
|
16
|
|
|
|
|
17
|
3 |
|
public static function capture() |
|
18
|
|
|
{ |
|
19
|
3 |
|
$evt = new Event(); |
|
20
|
2 |
|
$evt->raw = @file_get_contents('php://input'); |
|
21
|
2 |
|
$evt->signature = ( isset($_SERVER[self::SIGNATURE_KEY]) ? $_SERVER[self::SIGNATURE_KEY] : '' ); |
|
22
|
2 |
|
$evt->loadObject(); |
|
23
|
2 |
|
return $evt; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
6 |
|
protected function loadObject() |
|
27
|
|
|
{ |
|
28
|
6 |
|
$this->obj = json_decode($this->raw); |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function discoverOwner(array $keys) |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (!$this->obj || !property_exists($this->obj, 'data')) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
1 |
|
foreach ($keys as $index => $key) { |
|
37
|
1 |
|
if ($this->validFor($key)) { |
|
38
|
1 |
|
return $index; |
|
39
|
|
|
} |
|
40
|
1 |
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function validFor($key) |
|
44
|
|
|
{ |
|
45
|
2 |
|
if ($this->signature === hash_hmac('sha512', $this->raw, $key)) { |
|
46
|
2 |
|
return true; |
|
47
|
|
|
} |
|
48
|
1 |
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function package(array $additional_headers = [], $method = 'POST') |
|
52
|
|
|
{ |
|
53
|
2 |
|
$pack = new Request(); |
|
54
|
2 |
|
$pack->method = $method; |
|
55
|
2 |
|
$pack->headers = $additional_headers; |
|
56
|
2 |
|
$pack->headers["X-Paystack-Signature"] = $this->signature; |
|
57
|
2 |
|
$pack->headers["Content-Type"] = "application/json"; |
|
58
|
2 |
|
$pack->body = $this->raw; |
|
59
|
2 |
|
return $pack; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function forwardTo($url, array $additional_headers = [], $method = 'POST') |
|
63
|
|
|
{ |
|
64
|
1 |
|
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
$packed = $this->package($additional_headers, $method); |
|
68
|
|
|
$packed->endpoint = $url; |
|
69
|
|
|
return $packed->send()->wrapUp(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|