-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProcessor.php
More file actions
192 lines (168 loc) · 5.31 KB
/
Copy pathProcessor.php
File metadata and controls
192 lines (168 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
* Copyright 2023 Cloud Creativity Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace CloudCreativity\LaravelStripe\Webhooks;
use CloudCreativity\LaravelStripe\Config;
use CloudCreativity\LaravelStripe\Contracts\Connect\AccountInterface;
use CloudCreativity\LaravelStripe\Contracts\Connect\AdapterInterface;
use CloudCreativity\LaravelStripe\Contracts\Webhooks\ProcessorInterface;
use CloudCreativity\LaravelStripe\Jobs\ProcessWebhook;
use CloudCreativity\LaravelStripe\Models\StripeEvent;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Illuminate\Contracts\Events\Dispatcher as Events;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Stripe\Event;
class Processor implements ProcessorInterface
{
public const EVENT_PREFIX = 'stripe.webhooks';
public const CONNECT_EVENT_PREFIX = 'stripe.connect.webhooks';
/**
* @var Bus
*/
private $queue;
/**
* @var Events
*/
private $events;
/**
* @var AdapterInterface
*/
private $accounts;
/**
* @var Model
*/
private $model;
/**
* Processor constructor.
*/
public function __construct(
Bus $queue,
Events $events,
AdapterInterface $accounts,
Model $model,
) {
$this->queue = $queue;
$this->events = $events;
$this->accounts = $accounts;
$this->model = $model;
}
public function receive(Event $event)
{
$data = $event->jsonSerialize();
/**
* Create the model, giving it the option of filling the account as `account_id`.
*/
$model = $this->model->create(
collect($data)->put('account_id', Arr::get($data, 'account'))->all(),
);
/** Get the queue config for this specific event. */
$accountId = $event['account'] ?? null;
$queue = Config::webhookQueue($event->type, !!$accountId);
/** Dispatch a job to asynchronously process the webhook. */
$job = new ProcessWebhook($model, $data);
$job->onConnection($queue['connection'])->onQueue($queue['queue']);
$this->queue->dispatch($job);
}
public function didReceive(Event $event)
{
return $this->model->newQuery()->whereKey($event->id)->exists();
}
/**
* Dispatch a processed webhook.
*
* For each webhook, we dispatch events to give applications options
* as to how they want to bind.
*
* E.g. if the Stripe event name is `payment_intent.succeeded`, we dispatch:
*
* - `stripe.webhooks`
* - `stripe.webhooks:payment_intent`
* - `stripe.webhooks:payment_intent.succeeded`
*
* However, if the event is a Connect webhook, we dispatch:
*
* - `stripe.connect.webhooks`
* - `stripe.connect.webhooks:payment_intent`
* - `stripe.connect.webhooks:payment_intent.succeeded`
*
* @param mixed|StripeEvent $model
* @return void
*/
public function dispatch(Event $webhook, $model)
{
if ($accountId = Arr::get($webhook, 'account')) {
$this->dispatchConnect($webhook, $this->accounts->find($accountId), $model);
} else {
$this->dispatchAccount($webhook, $model);
}
/** Update the timestamps on the stored event */
if ($model instanceof Model) {
$model->touch();
}
}
/**
* Dispatch a webhook for the application's Stripe account.
*
* @return void
*/
protected function dispatchAccount(Event $event, $model)
{
$webhook = new Webhook(
$event,
$model,
Config::webhookQueue($event->type),
);
foreach ($this->eventsFor($event->type) as $name) {
$this->events->dispatch($name, $webhook);
}
}
/**
* Dispatch a webhook for a Stripe Connect account.
*
* @param mixed|StripeEvent $model
* @return void
* @todo change method signature for PHP7.
*/
protected function dispatchConnect(Event $event, ?AccountInterface $account = null, $model = null)
{
$webhook = new ConnectWebhook(
$event,
$account,
$model,
Config::webhookQueue($event->type, true),
);
foreach ($this->eventsFor($event->type, true) as $name) {
$this->events->dispatch($name, $webhook);
}
}
/**
* Get event names for the specified webhook.
*
* @param string $type
* @param bool $connect
* @return string[]
*/
protected function eventsFor($type, $connect = false)
{
$prefix = $connect ? static::CONNECT_EVENT_PREFIX : static::EVENT_PREFIX;
return [
$prefix,
sprintf('%s:%s', $prefix, explode('.', $type)[0]),
sprintf('%s:%s', $prefix, $type),
];
}
}