1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\User; |
6
|
|
|
use App\Notifications\SubscribeSuccessfully; |
7
|
|
|
use App\Notifications\UnsubscribeSuccessfully; |
8
|
|
|
use Stripe; |
9
|
|
|
|
10
|
|
|
class StripeController extends Controller |
11
|
|
|
{ |
12
|
|
|
protected $plans; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
Stripe\Stripe::setApiKey(\Config::get('services.stripe.secret')); |
17
|
|
|
$this->plans = Stripe\Plan::all(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getPlans() |
21
|
|
|
{ |
22
|
|
|
foreach ($this->plans as $plan) { |
23
|
|
|
switch ($plan->nickname) { |
24
|
|
|
case 'UTY': |
25
|
|
|
$plan->title = '50GBP for unlimited trees yearly'; |
26
|
|
|
break; |
27
|
|
|
case 'UTM': |
28
|
|
|
$plan->title = '5GBP for unlimited trees monthly'; |
29
|
|
|
break; |
30
|
|
|
case 'TTY': |
31
|
|
|
$plan->title = '25GBP for 10 trees yearly'; |
32
|
|
|
break; |
33
|
|
|
case 'TTM': |
34
|
|
|
$plan->title = '2.50GBP for 10 trees monthly'; |
35
|
|
|
break; |
36
|
|
|
case 'OTY': |
37
|
|
|
$plan->title = '10GBP for 1 tree yearly'; |
38
|
|
|
break; |
39
|
|
|
case 'OTM': |
40
|
|
|
$plan->title = '1GBP for 1 tree monthly'; |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
$plan->subscribed = false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->plans; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getCurrentSubscription() |
50
|
|
|
{ |
51
|
|
|
$user = auth()->user(); |
52
|
|
|
$data = []; |
53
|
|
|
$data['has_payment_method'] = $user->hasDefaultPaymentMethod(); |
54
|
|
|
if ($user->subscribed('default')) { |
55
|
|
|
$data['subscribed'] = true; |
56
|
|
|
$data['plan_id'] = $user->subscription()->stripe_plan; |
57
|
|
|
} else { |
58
|
|
|
$data['subscribed'] = false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getIntent() |
65
|
|
|
{ |
66
|
|
|
$user = auth()->user(); |
67
|
|
|
|
68
|
|
|
return ['intent' => $user->createSetupIntent()]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function subscribe() |
72
|
|
|
{ |
73
|
|
|
$user = auth()->user(); |
74
|
|
|
$user->syncRoles('OTY'); |
75
|
|
|
$plan_id = request()->plan_id; |
76
|
|
|
if (request()->has('payment_method')) { |
77
|
|
|
$paymentMethod = request()->payment_method; |
78
|
|
|
$user->newSubscription('default', $plan_id)->create($paymentMethod, ['name' => request()->card_holder_name, 'address' => ['country' => 'GB', 'state' => 'England', 'city' => 'Abberley', 'postal_code' => 'WR6', 'line1' => 'test', 'line2' => '']]); |
79
|
|
|
$user->notify(new SubscribeSuccessfully($plan_id)); |
80
|
|
|
} elseif ($user->hasDefaultPaymentMethod()) { |
81
|
|
|
$paymentMethod = $user->defaultPaymentMethod(); |
82
|
|
|
$user->newSubscription('default', $plan_id)->create($paymentMethod->id); |
83
|
|
|
$user->notify(new SubscribeSuccessfully($plan_id)); |
84
|
|
|
} else { |
85
|
|
|
$user->subscription('default')->swap($plan_id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return ['success' => true]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function unsubscribe() |
92
|
|
|
{ |
93
|
|
|
$user = auth()->user(); |
94
|
|
|
$user->subscription('default')->cancel(); |
95
|
|
|
// $user->role_id = 3; //expired role |
96
|
|
|
$user->save(); |
97
|
|
|
$user->notify(new UnsubscribeSuccessfully($user->subscription()->stripe_plan)); |
98
|
|
|
|
99
|
|
|
return ['success' => true]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function webhook() |
103
|
|
|
{ |
104
|
|
|
$data = request()->all(); |
105
|
|
|
$user = User::where('stripe_id', $data['data']['object']['customer'])->first(); |
106
|
|
|
if ($user) { |
107
|
|
|
$plan_nickname = $data['data']['object']['items']['data'][0]['plan']['nickname']; |
108
|
|
|
foreach ($this->plans as $plan) { |
109
|
|
|
if ($plan->nickname == $plan_nickname) { |
110
|
|
|
switch ($plan->nickname) { |
111
|
|
|
case 'UTY': |
112
|
|
|
$user->syncRoles('UTY'); |
113
|
|
|
break; |
114
|
|
|
case 'UTM': |
115
|
|
|
$user->syncRoles('UTM'); |
116
|
|
|
break; |
117
|
|
|
case 'TTY': |
118
|
|
|
$user->syncRoles('TTY'); |
119
|
|
|
break; |
120
|
|
|
case 'TTM': |
121
|
|
|
$user->syncRoles('TTM'); |
122
|
|
|
break; |
123
|
|
|
case 'OTY': |
124
|
|
|
$user->syncRoles('OTY'); |
125
|
|
|
break; |
126
|
|
|
case 'OTM': |
127
|
|
|
$user->syncRoles('OTM'); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} else { |
133
|
|
|
echo 'User not found!'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|