1
|
|
|
<?php namespace BB\Helpers; |
2
|
|
|
|
3
|
|
|
class GoCardlessHelper |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
private $account_details; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var \GoCardlessPro\Client |
10
|
|
|
*/ |
11
|
|
|
private $client; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->setup(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function setup() |
19
|
|
|
{ |
20
|
|
|
$this->account_details = array( |
21
|
|
|
'app_id' => env('GOCARDLESS_APP_ID', ''), |
22
|
|
|
'app_secret' => env('GOCARDLESS_APP_SECRET', ''), |
23
|
|
|
'merchant_id' => env('GOCARDLESS_MERCHANT_ID', ''), |
24
|
|
|
'access_token' => env('GOCARDLESS_ACCESS_TOKEN', ''), |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$this->client = new \GoCardlessPro\Client([ |
28
|
|
|
'access_token' => env('NEW_GOCARDLESS_ACCESS_TOKEN'), |
29
|
|
|
'environment' => (env('NEW_GOCARDLESS_ENV', 'LIVE') == 'LIVE')? \GoCardlessPro\Environment::LIVE: \GoCardlessPro\Environment::SANDBOX, |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getPayment($paymentId) |
34
|
|
|
{ |
35
|
|
|
return $this->client->payments()->get($paymentId); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function newPreAuthUrl($user, $paymentDetails) |
39
|
|
|
{ |
40
|
|
|
$redirectFlow = $this->client->redirectFlows()->create([ |
41
|
|
|
"params" => $paymentDetails |
|
|
|
|
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$user->gocardless_setup_id = $redirectFlow->id; |
45
|
|
|
$user->save(); |
46
|
|
|
|
47
|
|
|
return $redirectFlow->redirect_url; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function confirmResource($user, $confirm_params) |
51
|
|
|
{ |
52
|
|
|
return $this->client->redirectFlows()->complete( |
53
|
|
|
$user->gocardless_setup_id, |
54
|
|
|
["params" => ["session_token" => 'user-token-'.$user->id]] |
|
|
|
|
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function cancelSubscription($id) |
59
|
|
|
{ |
60
|
|
|
return $this->client->subscriptions()->cancel($id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new payment against a preauth |
65
|
|
|
* @param $preauthId |
66
|
|
|
* @param $amount |
67
|
|
|
* @param null|string $name |
68
|
|
|
* @param null|string $description |
69
|
|
|
* @return bool|mixed |
70
|
|
|
*/ |
71
|
|
|
public function newBill($preauthId, $amount, $name = null, $description = null) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
return $this->client->payments()->create([ |
75
|
|
|
"params" => [ |
|
|
|
|
76
|
|
|
"amount" => $amount, // 10 GBP in pence |
|
|
|
|
77
|
|
|
"currency" => "GBP", |
|
|
|
|
78
|
|
|
"links" => [ |
|
|
|
|
79
|
|
|
"mandate" => $preauthId |
|
|
|
|
80
|
|
|
// The mandate ID from last section |
81
|
|
|
], |
82
|
|
|
// Almost all resources in the API let you store custom metadata, |
83
|
|
|
// which you can retrieve later |
84
|
|
|
"metadata" => [ |
|
|
|
|
85
|
|
|
"description" => $name |
|
|
|
|
86
|
|
|
] |
87
|
|
|
], |
88
|
|
|
"headers" => [ |
|
|
|
|
89
|
|
|
//"Idempotency-Key" => $preauthId . ':' . time() |
|
|
|
|
90
|
|
|
] |
91
|
|
|
]); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
\Log::error($e); |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function cancelPreAuth($preauthId) |
99
|
|
|
{ |
100
|
|
|
if (empty($preauthId)) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
try { |
104
|
|
|
$mandate = $this->client->mandates()->cancel($preauthId); |
105
|
|
|
|
106
|
|
|
return ($mandate->status == 'cancelled'); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
\Log::error($e); |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $reason |
115
|
|
|
* @return null|string |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function getNameFromReason($reason) |
118
|
|
|
{ |
119
|
|
|
switch ($reason) { |
120
|
|
|
case 'subscription': |
121
|
|
|
return 'Monthly subscription'; |
122
|
|
|
case 'balance': |
123
|
|
|
return 'Balance top up'; |
124
|
|
|
case 'equipment-fee': |
125
|
|
|
return 'Equipment access fee'; |
126
|
|
|
case 'door-key': |
127
|
|
|
return 'Door key'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.