1
|
|
|
<?php namespace BB\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use BB\Entities\User; |
4
|
|
|
use BB\Repo\SubscriptionChargeRepository; |
5
|
|
|
|
6
|
|
|
class SubscriptionController extends Controller |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var SubscriptionChargeRepository |
12
|
|
|
*/ |
13
|
|
|
private $subscriptionChargeRepository; |
14
|
|
|
/** |
15
|
|
|
* @var \BB\Repo\UserRepository |
16
|
|
|
*/ |
17
|
|
|
private $userRepository; |
18
|
|
|
|
19
|
|
|
function __construct(\BB\Helpers\GoCardlessHelper $goCardless, SubscriptionChargeRepository $subscriptionChargeRepository, \BB\Repo\UserRepository $userRepository) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$this->goCardless = $goCardless; |
|
|
|
|
22
|
|
|
$this->subscriptionChargeRepository = $subscriptionChargeRepository; |
23
|
|
|
$this->userRepository = $userRepository; |
24
|
|
|
|
25
|
|
|
$this->middleware('role:member', array('only' => ['create', 'destroy'])); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Setup a new pre auth |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Http\RedirectResponse |
32
|
|
|
*/ |
33
|
|
|
public function create($userId) |
34
|
|
|
{ |
35
|
|
|
$user = User::findWithPermission($userId); |
36
|
|
|
$payment_details = array( |
37
|
|
|
"description" => "Build Brighton", |
|
|
|
|
38
|
|
|
'success_redirect_url' => route('account.subscription.store', $user->id), |
|
|
|
|
39
|
|
|
"session_token" => 'user-token-'.$user->id, |
|
|
|
|
40
|
|
|
'prefilled_customer' => [ |
41
|
|
|
'given_name' => $user->given_name, |
42
|
|
|
'family_name' => $user->family_name, |
43
|
|
|
'email' => $user->email, |
44
|
|
|
'address_line1' => $user->address->line_1, |
|
|
|
|
45
|
|
|
'address_line2' => $user->address->line_2, |
|
|
|
|
46
|
|
|
'city' => $user->address->line_3, |
|
|
|
|
47
|
|
|
'postal_code' => $user->address->postcode, |
|
|
|
|
48
|
|
|
'country_code' => 'GB' |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return \Redirect::to($this->goCardless->newPreAuthUrl($user, $payment_details)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Store a newly created resource in storage. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Http\RedirectResponse |
59
|
|
|
*/ |
60
|
|
|
public function store($userId) |
61
|
|
|
{ |
62
|
|
|
$confirm_params = array( |
63
|
|
|
'resource_id' => \Request::get('resource_id'), |
64
|
|
|
'resource_type' => \Request::get('resource_type'), |
65
|
|
|
'resource_uri' => \Request::get('resource_uri'), |
66
|
|
|
'signature' => \Request::get('signature'), |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
// State is optional |
70
|
|
|
if (\Request::get('state')) { |
71
|
|
|
$confirm_params['state'] = \Request::get('state'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$user = User::findWithPermission($userId); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$confirmed_resource = $this->goCardless->confirmResource($user, $confirm_params); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
\Notification::error($e->getMessage()); |
80
|
|
|
return \Redirect::route('account.show', $user->id); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
if (!isset($confirmed_resource->links->mandate) || empty($confirmed_resource->links->mandate)) { |
85
|
|
|
\Notification::error('Something went wrong, you can try again or get in contact'); |
86
|
|
|
return \Redirect::route('account.show', $user->id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->userRepository->recordGoCardlessVariableDetails($user->id, $confirmed_resource->links->mandate); |
90
|
|
|
|
91
|
|
|
//all we need for a valid member is an active dd so make sure the user account is active |
92
|
|
|
$this->userRepository->ensureMembershipActive($user->id); |
93
|
|
|
|
94
|
|
|
return \Redirect::route('account.show', [$user->id]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove the specified resource from storage. |
100
|
|
|
* |
101
|
|
|
* @param int $id |
102
|
|
|
* @return Illuminate\Http\RedirectResponse |
103
|
|
|
*/ |
104
|
|
|
public function destroy($userId, $id = null) |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* TODO: Check for and cancel pending sub charges |
109
|
|
|
*/ |
110
|
|
|
$user = User::findWithPermission($userId); |
111
|
|
|
if ($user->payment_method == 'gocardless') { |
112
|
|
|
try { |
113
|
|
|
$subscription = $this->goCardless->cancelSubscription($user->subscription_id); |
114
|
|
|
if ($subscription->status == 'cancelled') { |
115
|
|
|
$user->cancelSubscription(); |
|
|
|
|
116
|
|
|
\Notification::success('Your subscription has been cancelled'); |
117
|
|
|
return \Redirect::back(); |
118
|
|
|
} |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
$user->cancelSubscription(); |
|
|
|
|
121
|
|
|
\Notification::success('Your subscription has been cancelled'); |
122
|
|
|
return \Redirect::back(); |
123
|
|
|
} |
124
|
|
|
} elseif ($user->payment_method == 'gocardless-variable') { |
125
|
|
|
$status = $this->goCardless->cancelPreAuth($user->subscription_id); |
126
|
|
|
if ($status) { |
127
|
|
|
$user->subscription_id = null; |
128
|
|
|
$user->payment_method = ''; |
129
|
|
|
$user->save(); |
130
|
|
|
|
131
|
|
|
$user->setLeaving(); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->subscriptionChargeRepository->cancelOutstandingCharges($userId); |
134
|
|
|
|
135
|
|
|
\Notification::success('Your direct debit has been cancelled'); |
136
|
|
|
return \Redirect::back(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
\Notification::error('Sorry, we were unable to cancel your subscription, please get in contact'); |
140
|
|
|
return \Redirect::back(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
public function listCharges() |
145
|
|
|
{ |
146
|
|
|
$charges = $this->subscriptionChargeRepository->getChargesPaginated(); |
147
|
|
|
return \View::make('payments.sub-charges')->with('charges', $charges); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function updatePaymentMethod($id) |
151
|
|
|
{ |
152
|
|
|
$user = User::findWithPermission($id); |
153
|
|
|
$paymentMethod = \Input::get('payment_method'); |
154
|
|
|
|
155
|
|
|
if ($paymentMethod === 'balance' && $user->payment_method != $paymentMethod) { |
156
|
|
|
$status = $this->goCardless->cancelPreAuth($user->subscription_id); |
157
|
|
|
if ($status) { |
158
|
|
|
$user->subscription_id = null; |
159
|
|
|
$user->payment_method = 'balance'; |
160
|
|
|
$user->save(); |
161
|
|
|
|
162
|
|
|
// If we don't cancel the sub charge the balance process may pick it up |
163
|
|
|
//$this->subscriptionChargeRepository->cancelOutstandingCharges($userId); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
\Notification::success('Details Updated'); |
168
|
|
|
return \Redirect::route('account.show', [$user->id]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.