|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Shapin\Stripe; |
|
11
|
|
|
|
|
12
|
|
|
use Shapin\Stripe\Exception\InvalidArgumentException; |
|
13
|
|
|
use Shapin\Stripe\Model\Event; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
|
|
16
|
|
|
final class EventBuilder |
|
17
|
|
|
{ |
|
18
|
2 |
|
private $events = [ |
|
19
|
|
|
'account.external_account.created' => Event\AccountExternalAccountCreatedEvent::class, |
|
20
|
2 |
|
'account.external_account.deleted' => Event\AccountExternalAccountDeletedEvent::class, |
|
21
|
|
|
'account.external_account.updated' => Event\AccountExternalAccountUpdatedEvent::class, |
|
22
|
2 |
|
'account.updated' => Event\AccountUpdatedEvent::class, |
|
23
|
1 |
|
'balance.available' => Event\BalanceAvailableEvent::class, |
|
24
|
|
|
'charge.captured' => Event\ChargeCapturedEvent::class, |
|
25
|
|
|
'charge.expired' => Event\ChargeExpiredEvent::class, |
|
26
|
1 |
|
'charge.failed' => Event\ChargeFailedEvent::class, |
|
27
|
|
|
'charge.pending' => Event\ChargePendingEvent::class, |
|
28
|
|
|
'charge.refund.updated' => Event\ChargeRefundUpdatedEvent::class, |
|
29
|
116 |
|
'charge.refunded' => Event\ChargeRefundedEvent::class, |
|
30
|
|
|
'charge.succeeded' => Event\ChargeSucceededEvent::class, |
|
31
|
116 |
|
'coupon.created' => Event\CouponCreatedEvent::class, |
|
32
|
|
|
'coupon.deleted' => Event\CouponDeletedEvent::class, |
|
33
|
|
|
'coupon.updated' => Event\CouponUpdatedEvent::class, |
|
34
|
|
|
'customer.bank_account.deleted' => Event\CustomerBankAccountDeletedEvent::class, |
|
35
|
116 |
|
'customer.created' => Event\CustomerCreatedEvent::class, |
|
36
|
116 |
|
'customer.deleted' => Event\CustomerDeletedEvent::class, |
|
37
|
1 |
|
'customer.discount.created' => Event\CustomerDiscountCreatedEvent::class, |
|
38
|
115 |
|
'customer.discount.deleted' => Event\CustomerDiscountDeletedEvent::class, |
|
39
|
1 |
|
'customer.discount.updated' => Event\CustomerDiscountUpdatedEvent::class, |
|
40
|
114 |
|
'customer.source.created' => Event\CustomerSourceCreatedEvent::class, |
|
41
|
1 |
|
'customer.source.deleted' => Event\CustomerSourceDeletedEvent::class, |
|
42
|
113 |
|
'customer.source.expiring' => Event\CustomerSourceExpiringEvent::class, |
|
43
|
1 |
|
'customer.source.updated' => Event\CustomerSourceUpdatedEvent::class, |
|
44
|
112 |
|
'customer.subscription.created' => Event\CustomerSubscriptionCreatedEvent::class, |
|
45
|
1 |
|
'customer.subscription.deleted' => Event\CustomerSubscriptionDeletedEvent::class, |
|
46
|
111 |
|
'customer.subscription.trial_will_end' => Event\CustomerSubscriptionTrialWillEndEvent::class, |
|
47
|
2 |
|
'customer.subscription.updated' => Event\CustomerSubscriptionUpdatedEvent::class, |
|
48
|
109 |
|
'customer.updated' => Event\CustomerUpdatedEvent::class, |
|
49
|
1 |
|
'invoice.created' => Event\InvoiceCreatedEvent::class, |
|
50
|
108 |
|
'invoice.deleted' => Event\InvoiceDeletedEvent::class, |
|
51
|
1 |
|
'invoice.finalized' => Event\InvoiceFinalizedEvent::class, |
|
52
|
107 |
|
'invoice.marked_uncollectible' => Event\InvoiceMarkedUncollectibleEvent::class, |
|
53
|
1 |
|
'invoice.payment_failed' => Event\InvoicePaymentFailedEvent::class, |
|
54
|
106 |
|
'invoice.payment_succeeded' => Event\InvoicePaymentSucceededEvent::class, |
|
55
|
1 |
|
'invoice.sent' => Event\InvoiceSentEvent::class, |
|
56
|
105 |
|
'invoice.upcoming' => Event\InvoiceUpcomingEvent::class, |
|
57
|
1 |
|
'invoice.updated' => Event\InvoiceUpdatedEvent::class, |
|
58
|
104 |
|
'invoice.voided' => Event\InvoiceVoidedEvent::class, |
|
59
|
1 |
|
'invoiceitem.created' => Event\InvoiceItemCreatedEvent::class, |
|
60
|
103 |
|
'invoiceitem.deleted' => Event\InvoiceItemDeletedEvent::class, |
|
61
|
1 |
|
'invoiceitem.updated' => Event\InvoiceItemUpdatedEvent::class, |
|
62
|
102 |
|
'plan.created' => Event\PlanCreatedEvent::class, |
|
63
|
1 |
|
'plan.deleted' => Event\PlanDeletedEvent::class, |
|
64
|
101 |
|
'plan.updated' => Event\PlanUpdatedEvent::class, |
|
65
|
1 |
|
'product.created' => Event\ProductCreatedEvent::class, |
|
66
|
100 |
|
'product.deleted' => Event\ProductDeletedEvent::class, |
|
67
|
1 |
|
'product.updated' => Event\ProductUpdatedEvent::class, |
|
68
|
99 |
|
'source.canceled' => Event\SourceCanceledEvent::class, |
|
69
|
1 |
|
'source.chargeable' => Event\SourceChargeableEvent::class, |
|
70
|
98 |
|
'source.failed' => Event\SourceFailedEvent::class, |
|
71
|
1 |
|
'source.mandate_notification' => Event\SourceMandateNotificationEvent::class, |
|
72
|
97 |
|
'source.refund_attributes_required' => Event\SourceRefundAttributesRequiredEvent::class, |
|
73
|
1 |
|
'transfer.created' => Event\TransferCreatedEvent::class, |
|
74
|
96 |
|
'transfer.reversed' => Event\TransferReversedEvent::class, |
|
75
|
1 |
|
'transfer.updated' => Event\TransferUpdatedEvent::class, |
|
76
|
95 |
|
]; |
|
77
|
1 |
|
|
|
78
|
94 |
|
public function createEventFromRequest(Request $request): Event\Event |
|
79
|
1 |
|
{ |
|
80
|
93 |
|
$data = json_decode((string) $request->getContent(), true); |
|
81
|
1 |
|
|
|
82
|
92 |
|
if (\JSON_ERROR_NONE !== json_last_error()) { |
|
83
|
1 |
|
throw new InvalidArgumentException('Unable to process Request: Invalid JSON provided ('.json_last_error_msg().')'); |
|
84
|
91 |
|
} |
|
85
|
1 |
|
|
|
86
|
90 |
|
return $this->createEventFromArray($data); |
|
87
|
1 |
|
} |
|
88
|
89 |
|
|
|
89
|
1 |
|
public function createEventFromArray(array $data): Event\Event |
|
90
|
88 |
|
{ |
|
91
|
1 |
|
if (!isset($data['type'])) { |
|
92
|
87 |
|
throw new InvalidArgumentException('Unable to process event: No "type" provided in array.'); |
|
93
|
1 |
|
} |
|
94
|
86 |
|
|
|
95
|
1 |
|
if (array_key_exists($data['type'], $this->events)) { |
|
96
|
85 |
|
return call_user_func($this->events[$data['type']].'::createFromArray', $data); |
|
97
|
1 |
|
} |
|
98
|
84 |
|
|
|
99
|
1 |
|
throw new InvalidArgumentException("Unable to process event: Event \"{$data['type']}\" is not supported yet."); |
|
100
|
83 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|