-
Notifications
You must be signed in to change notification settings - Fork 6
/
Notification.php
211 lines (174 loc) · 6.86 KB
/
Notification.php
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace Academe\AuthorizeNet\ServerRequest;
/**
* The notification message sent by a webhook.
*/
use Academe\AuthorizeNet\Response\HasDataTrait;
use Academe\AuthorizeNet\AbstractModel;
use Academe\AuthorizeNet\ServerRequest\AbstractPayload;
use Academe\AuthorizeNet\ServerRequest\Payload\CustomerPaymentProfile;
use Academe\AuthorizeNet\ServerRequest\Payload\Fraud;
use Academe\AuthorizeNet\ServerRequest\Payload\Payment;
use Academe\AuthorizeNet\ServerRequest\Payload\Subscription;
use Academe\AuthorizeNet\ServerRequest\Payload\CustomerProfile;
use Academe\AuthorizeNet\ServerRequest\Payload\Unknown;
class Notification extends AbstractModel
{
use HasDataTrait;
const EVENT_NAMESPACE = 'net.authorize';
/**
* The event name prefix indicates the payload type.
* Note that some prefixes are subsets of others, so be
* careful what order they are checked.
*/
const EVENT_TARGET_PAYMENT = 'payment';
const EVENT_TARGET_CUSTOMER = 'customer';
const EVENT_SUBTARGET_FRAUD = 'fraud';
const EVENT_SUBTARGET_AUTHORIZATION = 'authorization';
const EVENT_SUBTARGET_AUTHCAPTURE = 'authcapture';
const EVENT_SUBTARGET_CAPTURE = 'capture';
const EVENT_SUBTARGET_REFUND = 'refund';
const EVENT_SUBTARGET_PRIORAUTHCAPTURE = 'priorAuthCapture';
const EVENT_SUBTARGET_VOID = 'void';
const EVENT_SUBTARGET_PAYMENTPROFILE = 'paymentProfile';
const EVENT_SUBTARGET_SUBSCRIPTION = 'subscription';
/**
* A list of event actions we know about.
*/
const EVENT_ACTION_CREATED = 'created';
const EVENT_ACTION_UPDATED = 'updated';
const EVENT_ACTION_DELETED = 'deleted';
const EVENT_ACTION_SUSPENDED = 'suspended';
const EVENT_ACTION_TERMINATED = 'terminated';
const EVENT_ACTION_CANCELLED = 'cancelled';
const EVENT_ACTION_EXPIRING = 'expiring';
const EVENT_ACTION_HELD = 'held';
const EVENT_ACTION_APPROVED = 'approved';
const EVENT_ACTION_DECLINED = 'declined';
protected $notificationId;
protected $eventType;
protected $eventDate;
protected $webhookId;
protected $payload;
protected $eventTarget;
protected $eventSubtarget;
protected $eventAction;
// TODO: the deliberyStatus and retryLog is not a part of the
// webhook notifications, but the REST API for managing and
// reporting on the webhooks. Move it there when the management
// API is done.
//
// Fetching past notifications returns the deliveryStatus,
// racking whether the merchant site has received this
// notification. Also the retryLog.
// The past notifications do not include the original payload,
// unless the delivery status shows it has failed to be delivered.
//protected $deliveryStatus; // Failed, Delivered and ??? (maybe not visible until one or the other)
//protected $retryLog;
/**
* Feed in the raw data structure (array or nested objects).
*/
public function __construct($data)
{
$this->setData($data);
$this->setNotificationId($this->getDataValue('notificationId'));
$this->setEventType($this->getDataValue('eventType'));
$this->setEventDate($this->getDataValue('eventDate'));
$this->setWebhookId($this->getDataValue('webhookId'));
$eventType = $this->eventType;
// Parse the eventType.
// We split it up into the following parts:
// {namespace}.{target}[.{sub-target}].{action}
// The namespace is discarded, and the remainder are used
// to determine the payload class.
// Strip off the namespace prefix if it is present.
// If not present, we may want to throw an exception
// or fall back to a default 'unknown' payload.
if (strpos($eventType, static::EVENT_NAMESPACE) === 0) {
$arr = explode('.', $eventType);
foreach (explode('.', static::EVENT_NAMESPACE) as $i) {
array_shift($arr);
}
// The main target is the section after the prefix.
$this->eventTarget = array_shift($arr);
// The action being notified is the last part of the event type.
$this->eventAction = array_pop($arr);
// The sub-target is what's left, which should be one or zero
// elements, i.e. optional.
$this->eventSubtarget = implode('.', $arr);
}
if ($payload = $this->getDataValue('payload')) {
if ($this->eventTarget === static::EVENT_TARGET_PAYMENT) {
if ($this->eventSubtarget === static::EVENT_SUBTARGET_FRAUD) {
// Notification of a payment fraud.
$this->setPayload(new Fraud($payload));
} else {
// Notification of a payment (any other sub-target).
$this->setPayload(new Payment($payload));
}
} elseif ($this->eventTarget === static::EVENT_TARGET_CUSTOMER) {
if ($this->eventSubtarget === static::EVENT_SUBTARGET_PAYMENTPROFILE) {
// Notification of a customer payment profile change.
$this->setPayload(new CustomerPaymentProfile($payload));
} elseif ($this->eventSubtarget === static::EVENT_SUBTARGET_SUBSCRIPTION) {
// Notification of a change to a customer subscription.
$this->setPayload(new Subscription($payload));
} else {
// Notification of a change to a customer profile.
$this->setPayload(new CustomerProfile($payload));
}
} else {
// Fall back to a default payload.
$this->setPayload(new Unknown($payload));
}
}
}
public function jsonSerialize()
{
$data = [
'notificationId' => $this->notificationId,
'eventType' => $this->eventType,
'eventDate' => $this->eventDate,
'webhookId' => $this->webhookId,
];
if ($this->payload) {
$data['payload'] = $this->payload;
}
return $data;
}
protected function setNotificationId($value)
{
$this->notificationId = $value;
}
protected function setEventType($value)
{
$this->eventType = $value;
}
/**
* Example: 2017-10-22T15:09:49.0609961Z
*/
protected function setEventDate($value)
{
$this->eventDate = $value;
}
protected function setWebhookId($value)
{
$this->webhookId = $value;
}
protected function setPayload(AbstractPayload $value)
{
$this->payload = $value;
}
protected function getEventTarget()
{
return $this->eventTarget;
}
protected function getEventSubtarget()
{
return $this->eventSubtarget;
}
protected function getEventAction()
{
return $this->eventAction;
}
}