| 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 |  |  |             'redirect_uri'      => route('account.subscription.store', $user->id), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'user'              => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'first_name'        =>  $user->given_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 'last_name'         =>  $user->family_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'billing_address1'  =>  $user->address->line_1, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 'billing_address2'  =>  $user->address->line_2, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'billing_town'      =>  $user->address->line_3, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'billing_postcode'  =>  $user->address->postcode, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'country_code'      => 'GB' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return \Redirect::to($this->goCardless->newPreAuthUrl($payment_details)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Store a newly created resource in storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function store($userId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $confirm_params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'resource_id'    => \Request::get('resource_id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'resource_type'  => \Request::get('resource_type'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'resource_uri'   => \Request::get('resource_uri'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'signature'      => \Request::get('signature'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // State is optional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (\Request::get('state')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $confirm_params['state'] = \Request::get('state'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $user = User::findWithPermission($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $confirmed_resource = $this->goCardless->confirmResource($confirm_params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             \Notification::error($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return \Redirect::route('account.show', $user->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if (strtolower($confirmed_resource->status) != 'active') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             \Notification::error('Something went wrong, you can try again or get in contact'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return \Redirect::route('account.show', $user->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->userRepository->recordGoCardlessVariableDetails($user->id, $confirmed_resource->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         //all we need for a valid member is an active dd so make sure the user account is active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->userRepository->ensureMembershipActive($user->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return \Redirect::route('account.show', [$user->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Remove the specified resource from storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param  int  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function destroy($userId, $id = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |          * TODO: Check for and cancel pending sub charges | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $user = User::findWithPermission($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if ($user->payment_method == 'gocardless') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $subscription = $this->goCardless->cancelSubscription($user->subscription_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 if ($subscription->status == 'cancelled') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     $user->cancelSubscription(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     \Notification::success('Your subscription has been cancelled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     return \Redirect::back(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } catch (\GoCardless_ApiException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 if ($e->getCode() == 404) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     $user->cancelSubscription(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     \Notification::success('Your subscription has been cancelled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     return \Redirect::back(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } elseif ($user->payment_method == 'gocardless-variable') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $status = $this->goCardless->cancelPreAuth($user->subscription_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             if ($status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $user->subscription_id = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $user->payment_method = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $user->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 $user->setLeaving(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $this->subscriptionChargeRepository->cancelOutstandingCharges($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 \Notification::success('Your direct debit has been cancelled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 return \Redirect::back(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         \Notification::error('Sorry, we were unable to cancel your subscription, please get in contact'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return \Redirect::back(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function listCharges() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $charges = $this->subscriptionChargeRepository->getChargesPaginated(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return \View::make('payments.sub-charges')->with('charges', $charges); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |     public function updatePaymentMethod($id) | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |         $user = User::findWithPermission($id); | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |         $paymentMethod = \Input::get('payment_method'); | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |         if ($paymentMethod === 'balance' && $user->payment_method == 'gocardless-variable') { | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |             $user->payment_method  = 'balance'; | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |             $user->secondary_payment_method = 'gocardless-variable'; | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |             $user->save(); | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |         if ($paymentMethod === 'gocardless-variable' && $user->payment_method == 'balance') { | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |             if (empty($user->subscription_id)) { | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |                 $user->payment_method = null; | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |                 $user->payment_method = 'gocardless-variable'; | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |             $user->secondary_payment_method = null; | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |             $user->save(); | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         \Notification::success('Details Updated'); | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |         return \Redirect::route('account.show', [$user->id]); | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  | 
            
                        
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.