srmklive /
chargify
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Srmklive\Chargify\Traits\ChargifyAPI; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | |||
| 7 | trait WebHooks |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * List web hooks for a site. |
||
| 11 | * |
||
| 12 | * @param string $start |
||
| 13 | * @param string $end |
||
| 14 | * @param bool $newest |
||
| 15 | * @param int $page |
||
| 16 | * @param int $per_page |
||
| 17 | * @param int $subscription_id |
||
| 18 | * |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | public function webhooks_list($start = '', $end = '', $newest = true, $page = 1, $per_page = 200, $subscription_id = 0): array |
||
| 22 | { |
||
| 23 | $now = Carbon::now(); |
||
| 24 | |||
| 25 | $start_date = empty($start) ? $now->subDays(15)->toDateString() : $start; |
||
| 26 | $end_date = empty($end) ? $now->toDateString() : $end; |
||
| 27 | $order = ($newest === true) ? 'newest_first' : 'oldest_first'; |
||
| 28 | $subscription_field = $subscription_id > 0 ? "subscription_id={$subscription_id}&" : ''; |
||
| 29 | |||
| 30 | $this->apiEndPoint = "/webhooks.json?{$subscription_field}page={$page}&per_page={$per_page}&since_date={$start_date}&until_date={$end_date}&order={$order}"; |
||
| 31 | |||
| 32 | $this->verb = 'get'; |
||
| 33 | |||
| 34 | return $this->doChargifyRequest(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Replay web hooks for the site. |
||
| 39 | * |
||
| 40 | * @param array $webhook_ids |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | public function webhooks_replay(array $webhook_ids): array |
||
| 45 | { |
||
| 46 | $this->apiEndPoint = '/webhooks/replay.json'; |
||
| 47 | |||
| 48 | $this->options['json'] = ['ids' => $webhook_ids]; |
||
|
0 ignored issues
–
show
|
|||
| 49 | |||
| 50 | $this->verb = 'post'; |
||
| 51 | |||
| 52 | return $this->doChargifyRequest(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a new web hook for the site. |
||
| 57 | * |
||
| 58 | * @param string $url |
||
| 59 | * @param array $events |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function webhooks_create(string $url, array $events = []): array |
||
| 64 | { |
||
| 65 | $this->apiEndPoint = '/endpoints.json'; |
||
| 66 | |||
| 67 | $this->options['json'] = collect([ |
||
|
0 ignored issues
–
show
|
|||
| 68 | 'endpoint' => [ |
||
| 69 | 'url' => $url, |
||
| 70 | 'webhook_subscriptions' => $events, |
||
| 71 | ], |
||
| 72 | ])->toArray(); |
||
| 73 | |||
| 74 | $this->verb = 'post'; |
||
| 75 | |||
| 76 | return $this->doChargifyRequest(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Update a new web hook for the site. |
||
| 81 | * |
||
| 82 | * @param string $webhook_id |
||
| 83 | * @param string $url |
||
| 84 | * @param array $events |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function webhooks_update(string $webhook_id, string $url, array $events = []): array |
||
| 89 | { |
||
| 90 | $this->apiEndPoint = "/endpoints/{$webhook_id}.json"; |
||
| 91 | |||
| 92 | $this->options['json'] = collect([ |
||
|
0 ignored issues
–
show
|
|||
| 93 | 'endpoint' => [ |
||
| 94 | 'url' => $url, |
||
| 95 | 'webhook_subscriptions' => $events, |
||
| 96 | ], |
||
| 97 | ])->toArray(); |
||
| 98 | |||
| 99 | $this->verb = 'put'; |
||
| 100 | |||
| 101 | return $this->doChargifyRequest(); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get web hook endpoints for the site. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function webhooks_endpoints(): array |
||
| 110 | { |
||
| 111 | $this->apiEndPoint = '/endpoints.json'; |
||
| 112 | |||
| 113 | $this->verb = 'get'; |
||
| 114 | |||
| 115 | return $this->doChargifyRequest(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Enable web hooks for the site. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function webhooks_enable(): array |
||
| 124 | { |
||
| 125 | $this->apiEndPoint = '/webhooks/settings.json'; |
||
| 126 | |||
| 127 | $this->options['json'] = [ |
||
|
0 ignored issues
–
show
|
|||
| 128 | 'webhooks_enabled' => true, |
||
| 129 | ]; |
||
| 130 | $this->verb = 'put'; |
||
| 131 | |||
| 132 | return $this->doChargifyRequest(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 |