1
|
|
|
<?php namespace BB\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use BB\Entities\User; |
4
|
|
|
|
5
|
|
|
class GoCardlessPaymentController extends Controller |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \BB\Repo\PaymentRepository |
9
|
|
|
*/ |
10
|
|
|
private $paymentRepository; |
11
|
|
|
/** |
12
|
|
|
* @var \BB\Helpers\GoCardlessHelper |
13
|
|
|
*/ |
14
|
|
|
private $goCardless; |
15
|
|
|
|
16
|
|
|
function __construct(\BB\Repo\PaymentRepository $paymentRepository, \BB\Helpers\GoCardlessHelper $goCardless) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$this->paymentRepository = $paymentRepository; |
19
|
|
|
|
20
|
|
|
$this->middleware('role:member', array('only' => ['create', 'store'])); |
21
|
|
|
$this->goCardless = $goCardless; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Main entry point for all gocardless payments - not subscriptions |
26
|
|
|
* @param $userId |
27
|
|
|
* @return mixed |
28
|
|
|
* @throws \BB\Exceptions\AuthenticationException |
29
|
|
|
*/ |
30
|
|
|
public function create($userId) |
31
|
|
|
{ |
32
|
|
|
$user = User::findWithPermission($userId); |
33
|
|
|
|
34
|
|
|
$requestData = \Request::only(['reason', 'amount', 'return_path']); |
35
|
|
|
|
36
|
|
|
$reason = $requestData['reason']; |
37
|
|
|
$amount = ($requestData['amount'] * 1) / 100; |
38
|
|
|
$returnPath = $requestData['return_path']; |
39
|
|
|
$ref = $this->getReference($reason); |
40
|
|
|
|
41
|
|
|
if ($user->payment_method == 'gocardless-variable') { |
42
|
|
|
|
43
|
|
|
return $this->handleBill($amount, $reason, $user, $ref, $returnPath); |
44
|
|
|
|
45
|
|
|
} elseif ($user->payment_method == 'gocardless') { |
46
|
|
|
|
47
|
|
|
return $this->ddMigratePrompt($returnPath); |
48
|
|
|
|
49
|
|
|
} else { |
50
|
|
|
|
51
|
|
|
abort(500, 'Not supported'); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function ddMigratePrompt($returnPath) |
57
|
|
|
{ |
58
|
|
|
if (\Request::wantsJson()) { |
59
|
|
|
return \Response::json(['error' => 'Please visit the "Your Membership" page and migrate your Direct Debit first, then return and make the payment'], 400); |
60
|
|
|
} |
61
|
|
|
\Notification::error("Please visit the \"Your Membership\" page and migrate your Direct Debit first, then return and make the payment"); |
|
|
|
|
62
|
|
|
return \Redirect::to($returnPath); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Process a direct debit payment when we have a preauth |
67
|
|
|
* |
68
|
|
|
* @param $amount |
69
|
|
|
* @param $reason |
70
|
|
|
* @param User $user |
71
|
|
|
* @param $ref |
72
|
|
|
* @param $returnPath |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
private function handleBill($amount, $reason, $user, $ref, $returnPath) |
76
|
|
|
{ |
77
|
|
|
if (is_null($ref)) { |
78
|
|
|
$ref = ''; |
79
|
|
|
} |
80
|
|
|
$bill = $this->goCardless->newBill($user->subscription_id, $amount * 100, $this->goCardless->getNameFromReason($reason)); |
81
|
|
|
//dd($bill); |
82
|
|
|
|
83
|
|
|
if ($bill) { |
84
|
|
|
//Store the payment |
85
|
|
|
$fee = 0; |
86
|
|
|
$paymentSourceId = $bill->id; |
87
|
|
|
$amount = $bill->amount / 100; |
88
|
|
|
$status = $bill->status; |
89
|
|
|
if ($status == 'pending_submission') { |
90
|
|
|
$status = 'pending'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//The record payment process will make the necessary record updates |
94
|
|
|
$this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', $paymentSourceId, $amount, $status, $fee, $ref); |
95
|
|
|
|
96
|
|
|
if (\Request::wantsJson()) { |
97
|
|
|
return \Response::json(['message' => 'The payment was submitted successfully']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
\Notification::success("The payment was submitted successfully"); |
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
//something went wrong - we still have the pre auth though |
103
|
|
|
|
104
|
|
|
if (\Request::wantsJson()) { |
105
|
|
|
return \Response::json(['error' => 'There was a problem charging your account'], 400); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
\Notification::error("There was a problem charging your account"); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return \Redirect::to($returnPath); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
private function getDescription($reason) |
117
|
|
|
{ |
118
|
|
|
if ($reason == 'subscription') { |
119
|
|
|
return "Monthly Subscription Fee - Manual"; |
|
|
|
|
120
|
|
|
} elseif ($reason == 'induction') { |
121
|
|
|
return strtoupper(\Input::get('induction_key')) . " Induction Fee"; |
|
|
|
|
122
|
|
|
} elseif ($reason == 'door-key') { |
123
|
|
|
return "Door Key Deposit"; |
|
|
|
|
124
|
|
|
} elseif ($reason == 'storage-box') { |
125
|
|
|
return "Storage Box Deposit"; |
|
|
|
|
126
|
|
|
} elseif ($reason == 'balance') { |
127
|
|
|
return "BB Credit Payment"; |
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
throw new \BB\Exceptions\NotImplementedException(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function getName($reason, $userId) |
134
|
|
|
{ |
135
|
|
|
if ($reason == 'subscription') { |
136
|
|
|
return strtoupper("BBSUB" . $userId . ":MANUAL"); |
|
|
|
|
137
|
|
|
} elseif ($reason == 'induction') { |
138
|
|
|
return strtoupper("BBINDUCTION" . $userId . ":" . \Request::get('induction_key')); |
|
|
|
|
139
|
|
|
} elseif ($reason == 'door-key') { |
140
|
|
|
return strtoupper("BBDOORKEY" . $userId); |
|
|
|
|
141
|
|
|
} elseif ($reason == 'storage-box') { |
142
|
|
|
return strtoupper("BBSTORAGEBOX" . $userId); |
|
|
|
|
143
|
|
|
} elseif ($reason == 'balance') { |
144
|
|
|
return strtoupper("BBBALANCE" . $userId); |
|
|
|
|
145
|
|
|
} else { |
146
|
|
|
throw new \BB\Exceptions\NotImplementedException(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function getReference($reason) |
151
|
|
|
{ |
152
|
|
|
if ($reason == 'induction') { |
153
|
|
|
return \Request::get('ref'); |
154
|
|
|
} elseif ($reason == 'balance') { |
155
|
|
|
return \Request::get('reference'); |
156
|
|
|
} |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.