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