This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGateway.php
215 lines (192 loc) · 5.07 KB
/
Gateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace Tuurbo\Spreedly;
class Gateway
{
protected $config;
protected $client;
protected $gatewayToken;
/**
* Create a Guzzle instance and set token.
*
* @param \Tuurbo\Spreedly\Client $client
* @param array $config
* @param string $gatewayToken optional
*/
public function __construct(Client $client, $config, $gatewayToken = null)
{
$this->client = $client;
$this->config = $config;
$this->gatewayToken = $gatewayToken ?: $config['gateway'];
}
/**
* Get a list of gateways supported by Spreedly.
*
* <code>
* Spreedly::gateway()->setup();
* </code>
*
* @return \Tuurbo\Spreedly\Client
*/
public function setup()
{
return $this->client->get('v1/gateways_options.json');
}
/**
* Get a list of all gateways you've created on Spreedly.
*
* <code>
* Spreedly::gateway()->all();
* </code>
*
* @param string $gatewayToken optional
*
* @return \Tuurbo\Spreedly\Client
*/
public function all($gatewayToken = null)
{
$append = '';
if ($gatewayToken) {
$append = '?since_token='.$gatewayToken;
}
return $this->client->get('v1/gateways.json'.$append);
}
/**
* Get a specific gateway on Spreedly.
*
* <code>
* Spreedly::gateway($gatewayToken)->show();
* </code>
*
* @return \Tuurbo\Spreedly\Client
*/
public function show()
{
return $this->client->get('v1/gateways/'.$this->gatewayToken.'.json');
}
/**
* Create a new gateway on Spreedly.
*
* <code>
* // Example gateway for testing
* Spreedly::gateway()->create('test');
* </code>
*
* @param string $gateway
* @param array $data optional
*
* @return \Tuurbo\Spreedly\Client
*/
public function create($gateway, array $data = null)
{
$params = [
'gateway' => [
'gateway_type' => $gateway,
],
];
if (is_array($data)) {
$params['gateway'] += $data;
}
return $this->client->post('v1/gateways.json', $params);
}
/**
* Update a gateway on Spreedly.
*
* <code>
* // Example
* Spreedly::gateway($gatewayToken)->update(array('
* password' => '12345'
* ));
* </code>
*
* @param array $data
*
* @return \Tuurbo\Spreedly\Client
*/
public function update(array $data)
{
if (!$this->gatewayToken) {
throw new Exceptions\MissingGatewayTokenException();
}
$params = [
'gateway' => $data,
];
return $this->client->put('v1/gateways/'.$this->gatewayToken.'.json', $params);
}
/**
* Disable a gateway on Spreedly.
*
* <code>
* Spreedly::gateway($gatewayToken)->disable();
* </code>
*
* @return \Tuurbo\Spreedly\Client
*/
public function disable()
{
if (!$this->gatewayToken) {
throw new Exceptions\MissingGatewayTokenException();
}
return $this->client->put('v1/gateways/'.$this->gatewayToken.'/redact.json');
}
/**
* Handle dynamic calls for \Tuurbo\Spreedly\Payment.
*
* Useful when you don't want to use the default gateway.
*
* <code>
* // Charge a payment method on a non-default gateway.
* Spreedly::gateway($gatewayToken)->payment($paymentToken)->purchase();
* </code>
*
* @param string $paymentToken optional
*
* @return Payment
*/
public function payment($paymentToken = null)
{
if (!$this->gatewayToken) {
throw new Exceptions\MissingGatewayTokenException();
}
return new Payment($this->client, $this->config, $paymentToken, $this->gatewayToken);
}
/**
* View all transactions of a specific gateway.
*
* <code>
* Spreedly::gateway($gatewayToken)->transactions();
*
* // Paginate
* Spreedly::gateway($gatewayToken)->transactions($transactionToken);
*
* // Paginate and sort
* Spreedly::gateway($gatewayToken)->transactions($transactionToken, ['order' => 'desc']);
* </code>
*
* @param string $paymentToken optional
* @param array $data optional
*
* @link https://docs.spreedly.com/reference/api/v1/gateways/transactions/
*
* @return \Tuurbo\Spreedly\Client
*/
public function transactions($paymentToken = null, array $data = [])
{
if (!$this->gatewayToken) {
throw new Exceptions\MissingGatewayTokenException();
}
$append = '';
if ($paymentToken) {
$append = '?since_token='.$paymentToken;
}
return $this->client->get('v1/gateways/'.$this->gatewayToken.'/transactions.json'.$append, $data);
}
/**
* Retrieve the gateway token.
*
* @return string
*/
public function getToken()
{
return $this->gatewayToken;
}
}