1 | <?php |
||
2 | |||
3 | namespace Srmklive\PayPal\Traits\PayPalAPI; |
||
4 | |||
5 | trait WebHooks |
||
6 | { |
||
7 | /** |
||
8 | * Create a new web hook. |
||
9 | * |
||
10 | * @param string $url |
||
11 | * @param array $events |
||
12 | * |
||
13 | * @throws \Throwable |
||
14 | * |
||
15 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
16 | * |
||
17 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post |
||
18 | */ |
||
19 | public function createWebHook(string $url, array $events) |
||
20 | { |
||
21 | $this->apiEndPoint = 'v1/notifications/webhooks'; |
||
22 | |||
23 | $data = ['url' => $url]; |
||
24 | $data['event_types'] = collect($events)->map(function ($item) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
25 | return ['name' => $item]; |
||
26 | })->toArray(); |
||
27 | |||
28 | $this->options['json'] = $data; |
||
29 | |||
30 | $this->verb = 'post'; |
||
31 | |||
32 | return $this->doPayPalRequest(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * List all web hooks. |
||
37 | * |
||
38 | * @throws \Throwable |
||
39 | * |
||
40 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
41 | * |
||
42 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_list |
||
43 | */ |
||
44 | public function listWebHooks() |
||
45 | { |
||
46 | $this->apiEndPoint = 'v1/notifications/webhooks'; |
||
47 | |||
48 | $this->verb = 'get'; |
||
49 | |||
50 | return $this->doPayPalRequest(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Delete a web hook. |
||
55 | * |
||
56 | * @param string $web_hook_id |
||
57 | * |
||
58 | * @throws \Throwable |
||
59 | * |
||
60 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
61 | * |
||
62 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_delete |
||
63 | */ |
||
64 | public function deleteWebHook(string $web_hook_id) |
||
65 | { |
||
66 | $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}"; |
||
67 | |||
68 | $this->verb = 'delete'; |
||
69 | |||
70 | return $this->doPayPalRequest(false); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Update an existing web hook. |
||
75 | * |
||
76 | * @param string $web_hook_id |
||
77 | * @param array $data |
||
78 | * |
||
79 | * @throws \Throwable |
||
80 | * |
||
81 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
82 | * |
||
83 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_update |
||
84 | */ |
||
85 | public function updateWebHook(string $web_hook_id, array $data) |
||
86 | { |
||
87 | $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}"; |
||
88 | |||
89 | $this->options['json'] = $data; |
||
90 | |||
91 | $this->verb = 'patch'; |
||
92 | |||
93 | return $this->doPayPalRequest(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Show details for an existing web hook. |
||
98 | * |
||
99 | * @param string $web_hook_id |
||
100 | * |
||
101 | * @throws \Throwable |
||
102 | * |
||
103 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
104 | * |
||
105 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get |
||
106 | */ |
||
107 | public function showWebHookDetails(string $web_hook_id) |
||
108 | { |
||
109 | $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}"; |
||
110 | |||
111 | $this->verb = 'get'; |
||
112 | |||
113 | return $this->doPayPalRequest(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * List events for an existing web hook. |
||
118 | * |
||
119 | * @param string $web_hook_id |
||
120 | * |
||
121 | * @throws \Throwable |
||
122 | * |
||
123 | * @return array|\Psr\Http\Message\StreamInterface|string |
||
124 | * |
||
125 | * @see https://developer.paypal.com/docs/api/webhooks/v1/#webhooks_get |
||
126 | */ |
||
127 | public function listWebHookEvents($web_hook_id) |
||
128 | { |
||
129 | $this->apiEndPoint = "v1/notifications/webhooks/{$web_hook_id}/event-types"; |
||
130 | |||
131 | $this->verb = 'get'; |
||
132 | |||
133 | return $this->doPayPalRequest(); |
||
134 | } |
||
135 | } |
||
136 |