1
|
|
|
<?php namespace GeneaLabs\LaravelMixpanel\Http\Requests; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use GeneaLabs\LaravelMixpanel\Events\MixpanelEvent; |
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
|
8
|
|
|
class RecordStripeEvent extends FormRequest |
9
|
|
|
{ |
10
|
|
|
public function authorize() : bool |
11
|
|
|
{ |
12
|
|
|
return true; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function rules() : array |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
// |
19
|
|
|
]; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function process() |
23
|
|
|
{ |
24
|
|
|
$data = $this->json()->all(); |
25
|
|
|
|
26
|
|
|
if (! $data || ! ($data['data'] ?? false)) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$transaction = $data['data']['object']; |
31
|
|
|
$originalValues = array_key_exists('previous_attributes', $data['data']) |
32
|
|
|
? $data['data']['previous_attributes'] |
33
|
|
|
: []; |
34
|
|
|
$stripeCustomerId = $this->findStripeCustomerId($transaction); |
35
|
|
|
$authModel = config('auth.providers.users.model') ?? config('auth.model'); |
36
|
|
|
$user = app($authModel)->where('stripe_id', $stripeCustomerId)->first(); |
37
|
|
|
|
38
|
|
|
if (! $user) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
app('mixpanel')->identify($user->id); |
43
|
|
|
|
44
|
|
|
if ($transaction['object'] === 'charge' && ! count($originalValues)) { |
45
|
|
|
$this->recordCharge($transaction, $user); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($transaction['object'] === 'subscription') { |
49
|
|
|
$this->recordSubscription($transaction, $user, $originalValues); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function recordCharge(array $transaction, $user) |
54
|
|
|
{ |
55
|
|
|
$charge = 0; |
56
|
|
|
$amount = $transaction['amount'] / 100; |
57
|
|
|
$status = 'Failed'; |
58
|
|
|
|
59
|
|
|
if ($transaction['paid']) { |
60
|
|
|
$status = 'Authorized'; |
61
|
|
|
|
62
|
|
|
if ($transaction['captured']) { |
63
|
|
|
$status = 'Successful'; |
64
|
|
|
|
65
|
|
|
if ($transaction['refunded']) { |
66
|
|
|
$status = 'Refunded'; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$trackingData = [ |
72
|
|
|
'Payment', |
73
|
|
|
[ |
74
|
|
|
'Status' => $status, |
75
|
|
|
'Amount' => $amount, |
76
|
|
|
], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
event(new MixpanelEvent($user, $trackingData, $charge)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function recordSubscription(array $transaction, $user, array $originalValues = []) |
83
|
|
|
{ |
84
|
|
|
$planStatus = array_key_exists('status', $transaction) ? $transaction['status'] : null; |
85
|
|
|
$planName = isset($transaction['plan']['name']) ? $transaction['plan']['name'] : null; |
86
|
|
|
$planStart = array_key_exists('start', $transaction) ? $transaction['start'] : null; |
87
|
|
|
$planAmount = isset($transaction['plan']['amount']) ? $transaction['plan']['amount'] : null; |
88
|
|
|
$oldPlanName = isset($originalValues['plan']['name']) ? $originalValues['plan']['name'] : null; |
89
|
|
|
$oldPlanAmount = isset($originalValues['plan']['amount']) ? $originalValues['plan']['amount'] : null; |
90
|
|
|
|
91
|
|
|
if ($planStatus === 'canceled') { |
92
|
|
|
$profileData = [ |
93
|
|
|
'Subscription' => 'None', |
94
|
|
|
'Churned' => (new Carbon($transaction['canceled_at']))->format('Y-m-d\Th:i:s'), |
95
|
|
|
'Plan When Churned' => $planName, |
96
|
|
|
'Paid Lifetime' => (new Carbon)->createFromTimestampUTC($planStart) |
97
|
|
|
->diffInDays((new Carbon(timestamp($transaction['ended_at']))) |
98
|
|
|
->timezone('UTC')) . ' days' |
99
|
|
|
]; |
100
|
|
|
$trackingData = [ |
101
|
|
|
['Subscription', ['Status' => 'Canceled', 'Upgraded' => false]], |
102
|
|
|
['Churn! :-('], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (count($originalValues)) { |
107
|
|
|
if ($planAmount && $oldPlanAmount) { |
108
|
|
|
if ($planAmount < $oldPlanAmount) { |
109
|
|
|
$profileData = [ |
110
|
|
|
'Subscription' => $planName, |
111
|
|
|
'Churned' => (new Carbon($transaction['ended_at'])) |
112
|
|
|
->timezone('UTC') |
113
|
|
|
->format('Y-m-d\Th:i:s'), |
114
|
|
|
'Plan When Churned' => $oldPlanName, |
115
|
|
|
]; |
116
|
|
|
$trackingData = [ |
117
|
|
|
['Subscription', [ |
118
|
|
|
'Upgraded' => false, |
119
|
|
|
'FromPlan' => $oldPlanName, |
120
|
|
|
'ToPlan' => $planName, |
121
|
|
|
]], |
122
|
|
|
['Churn! :-('], |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($planAmount > $oldPlanAmount) { |
127
|
|
|
$profileData = [ |
128
|
|
|
'Subscription' => $planName, |
129
|
|
|
]; |
130
|
|
|
$trackingData = [ |
131
|
|
|
['Subscription', [ |
132
|
|
|
'Upgraded' => true, |
133
|
|
|
'FromPlan' => $oldPlanName, |
134
|
|
|
'ToPlan' => $planName, |
135
|
|
|
]], |
136
|
|
|
['Unchurn! :-)'], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
} else { |
140
|
|
|
if ($planStatus === 'trialing' && ! $oldPlanName) { |
141
|
|
|
$profileData = [ |
142
|
|
|
'Subscription' => $planName, |
143
|
|
|
]; |
144
|
|
|
$trackingData = [ |
145
|
|
|
['Subscription', [ |
146
|
|
|
'Upgraded' => true, |
147
|
|
|
'FromPlan' => 'Trial', |
148
|
|
|
'ToPlan' => $planName, |
149
|
|
|
]], |
150
|
|
|
['Unchurn! :-)'], |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} else { |
155
|
|
|
if ($planStatus === 'active') { |
156
|
|
|
$profileData = [ |
157
|
|
|
'Subscription' => $planName, |
158
|
|
|
]; |
159
|
|
|
$trackingData = [ |
160
|
|
|
['Subscription', ['Status' => 'Created']], |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($planStatus === 'trialing') { |
165
|
|
|
$profileData = [ |
166
|
|
|
'Subscription' => 'Trial', |
167
|
|
|
]; |
168
|
|
|
$trackingData = [ |
169
|
|
|
['Subscription', ['Status' => 'Trial']], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
event(new MixpanelEvent($user, $trackingData, 0, $profileData)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function findStripeCustomerId(array $transaction) |
178
|
|
|
{ |
179
|
|
|
if (array_key_exists('customer', $transaction)) { |
180
|
|
|
return $transaction['customer']; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if (array_key_exists('object', $transaction) && $transaction['object'] === 'customer') { |
184
|
|
|
return $transaction['id']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (array_key_exists('subscriptions', $transaction) |
188
|
|
|
&& array_key_exists('data', $transaction['subscriptions']) |
189
|
|
|
&& array_key_exists(0, $transaction['subscriptions']['data']) |
190
|
|
|
&& array_key_exists('customer', $transaction['subscriptions']['data'][0]) |
191
|
|
|
) { |
192
|
|
|
return $transaction['subscriptions']['data'][0]['customer']; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|