|
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
|
|
|
|
|
82
|
|
|
if ($bill) { |
|
83
|
|
|
//Store the payment |
|
84
|
|
|
$fee = 0; |
|
85
|
|
|
$paymentSourceId = $bill->id; |
|
86
|
|
|
$amount = $bill->amount / 100; |
|
87
|
|
|
$status = $bill->status; |
|
88
|
|
|
if ($status == 'pending_submission') { |
|
89
|
|
|
$status = 'pending'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
//The record payment process will make the necessary record updates |
|
93
|
|
|
$this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', $paymentSourceId, $amount, $status, $fee, $ref); |
|
94
|
|
|
|
|
95
|
|
|
if (\Request::wantsJson()) { |
|
96
|
|
|
return \Response::json(['message' => 'The payment was submitted successfully']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
\Notification::success("The payment was submitted successfully"); |
|
|
|
|
|
|
100
|
|
|
} else { |
|
101
|
|
|
//something went wrong - we still have the pre auth though |
|
102
|
|
|
|
|
103
|
|
|
if (\Request::wantsJson()) { |
|
104
|
|
|
return \Response::json(['error' => 'There was a problem charging your account'], 400); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
\Notification::error("There was a problem charging your account"); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return \Redirect::to($returnPath); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
private function getDescription($reason) |
|
116
|
|
|
{ |
|
117
|
|
|
if ($reason == 'subscription') { |
|
118
|
|
|
return "Monthly Subscription Fee - Manual"; |
|
|
|
|
|
|
119
|
|
|
} elseif ($reason == 'induction') { |
|
120
|
|
|
return strtoupper(\Input::get('induction_key')) . " Induction Fee"; |
|
|
|
|
|
|
121
|
|
|
} elseif ($reason == 'door-key') { |
|
122
|
|
|
return "Door Key Deposit"; |
|
|
|
|
|
|
123
|
|
|
} elseif ($reason == 'storage-box') { |
|
124
|
|
|
return "Storage Box Deposit"; |
|
|
|
|
|
|
125
|
|
|
} elseif ($reason == 'balance') { |
|
126
|
|
|
return "BB Credit Payment"; |
|
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
|
|
throw new \BB\Exceptions\NotImplementedException(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function getName($reason, $userId) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($reason == 'subscription') { |
|
135
|
|
|
return strtoupper("BBSUB" . $userId . ":MANUAL"); |
|
|
|
|
|
|
136
|
|
|
} elseif ($reason == 'induction') { |
|
137
|
|
|
return strtoupper("BBINDUCTION" . $userId . ":" . \Request::get('induction_key')); |
|
|
|
|
|
|
138
|
|
|
} elseif ($reason == 'door-key') { |
|
139
|
|
|
return strtoupper("BBDOORKEY" . $userId); |
|
|
|
|
|
|
140
|
|
|
} elseif ($reason == 'storage-box') { |
|
141
|
|
|
return strtoupper("BBSTORAGEBOX" . $userId); |
|
|
|
|
|
|
142
|
|
|
} elseif ($reason == 'balance') { |
|
143
|
|
|
return strtoupper("BBBALANCE" . $userId); |
|
|
|
|
|
|
144
|
|
|
} else { |
|
145
|
|
|
throw new \BB\Exceptions\NotImplementedException(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function getReference($reason) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($reason == 'induction') { |
|
152
|
|
|
return \Request::get('ref'); |
|
153
|
|
|
} elseif ($reason == 'balance') { |
|
154
|
|
|
return \Request::get('reference'); |
|
155
|
|
|
} |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
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.