Skip to content

Commit 5a52082

Browse files
authored
Merge pull request #80 from mikealmond/feature/transfers
Add Stripe Connect charges/transfer support (fixes #33, #70, #78)
2 parents a88b94d + a24099e commit 5a52082

37 files changed

+2047
-21
lines changed

src/Gateway.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,101 @@ public function fetchBalanceTransaction(array $parameters = array())
277277
return $this->createRequest('\Omnipay\Stripe\Message\FetchBalanceTransactionRequest', $parameters);
278278
}
279279

280+
281+
//
282+
// Transfers
283+
// @link https://stripe.com/docs/api#transfers
284+
//
285+
286+
287+
/**
288+
* Transfer Request.
289+
*
290+
* To send funds from your Stripe account to a connected account, you create
291+
* a new transfer object. Your Stripe balance must be able to cover the
292+
* transfer amount, or you'll receive an "Insufficient Funds" error.
293+
*
294+
* @param array $parameters
295+
*
296+
* @return \Omnipay\Common\Message\AbstractRequest
297+
*/
298+
public function transfer(array $parameters = array())
299+
{
300+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\CreateTransferRequest', $parameters);
301+
}
302+
303+
/**
304+
* @param array $parameters
305+
*
306+
* @return \Omnipay\Common\Message\AbstractRequest
307+
*/
308+
public function fetchTransfer(array $parameters = array())
309+
{
310+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\FetchTransferRequest', $parameters);
311+
}
312+
313+
/**
314+
* @param array $parameters
315+
*
316+
* @return \Omnipay\Common\Message\AbstractRequest
317+
*/
318+
public function updateTransfer(array $parameters = array())
319+
{
320+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\UpdateTransferRequest', $parameters);
321+
}
322+
323+
/**
324+
* List Transfers
325+
*
326+
* @param array $parameters
327+
*
328+
* @return \Omnipay\Common\Message\AbstractRequest|\Omnipay\Stripe\Message\Transfers\ListTransfersRequest
329+
*/
330+
public function listTransfers(array $parameters = array())
331+
{
332+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\ListTransfersRequest', $parameters);
333+
}
334+
335+
/**
336+
* @param array $parameters
337+
*
338+
* @return \Omnipay\Common\Message\AbstractRequest
339+
*/
340+
public function reverseTransfer(array $parameters = array())
341+
{
342+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\CreateTransferReversalRequest', $parameters);
343+
}
344+
345+
/**
346+
* @param array $parameters
347+
*
348+
* @return \Omnipay\Common\Message\AbstractRequest
349+
*/
350+
public function fetchTransferReversal(array $parameters = array())
351+
{
352+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\FetchTransferReversalRequest', $parameters);
353+
}
354+
355+
/**
356+
* @param array $parameters
357+
*
358+
* @return \Omnipay\Common\Message\AbstractRequest
359+
*/
360+
public function updateTransferReversal(array $parameters = array())
361+
{
362+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\UpdateTransferReversalRequest', $parameters);
363+
}
364+
365+
/**
366+
* @param array $parameters
367+
*
368+
* @return \Omnipay\Common\Message\AbstractRequest
369+
*/
370+
public function listTransferReversals(array $parameters = array())
371+
{
372+
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\ListTransferReversalsRequest', $parameters);
373+
}
374+
280375
//
281376
// Cards
282377
// @link https://stripe.com/docs/api#cards

src/Message/AbstractRequest.php

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
/**
44
* Stripe Abstract Request.
55
*/
6+
67
namespace Omnipay\Stripe\Message;
78

9+
use Omnipay\Common\Message\ResponseInterface;
10+
811
/**
912
* Stripe Abstract Request.
1013
*
@@ -29,8 +32,6 @@
2932
*
3033
* @see \Omnipay\Stripe\Gateway
3134
* @link https://stripe.com/docs/api
32-
*
33-
* @method \Omnipay\Stripe\Message\Response send()
3435
*/
3536
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
3637
{
@@ -110,6 +111,46 @@ public function setMetadata($value)
110111
return $this->setParameter('metadata', $value);
111112
}
112113

114+
/**
115+
* Connect only
116+
*
117+
* @return mixed
118+
*/
119+
public function getConnectedStripeAccountHeader()
120+
{
121+
return $this->getParameter('connectedStripeAccount');
122+
}
123+
124+
/**
125+
* @param string $value
126+
*
127+
* @return \Omnipay\Common\Message\AbstractRequest
128+
*/
129+
public function setConnectedStripeAccountHeader($value)
130+
{
131+
return $this->setParameter('connectedStripeAccount', $value);
132+
}
133+
134+
/**
135+
* Connect only
136+
*
137+
* @return mixed
138+
*/
139+
public function getIdempotencyKeyHeader()
140+
{
141+
return $this->getParameter('idempotencyKey');
142+
}
143+
144+
/**
145+
* @param string $value
146+
*
147+
* @return \Omnipay\Common\Message\AbstractRequest
148+
*/
149+
public function setIdempotencyKeyHeader($value)
150+
{
151+
return $this->setParameter('idempotencyKey', $value);
152+
}
153+
113154
abstract public function getEndpoint();
114155

115156
/**
@@ -124,15 +165,55 @@ public function getHttpMethod()
124165
return 'POST';
125166
}
126167

127-
public function sendData($data)
168+
/**
169+
* @return array
170+
*/
171+
public function getHeaders()
172+
{
173+
$headers = array();
174+
175+
if ($this->getConnectedStripeAccountHeader()) {
176+
$headers['Stripe-Account'] = $this->getConnectedStripeAccountHeader();
177+
}
178+
179+
if ($this->getIdempotencyKeyHeader()) {
180+
$headers['Idempotency-Key'] = $this->getIdempotencyKeyHeader();
181+
}
182+
183+
return $headers;
184+
}
185+
186+
/**
187+
* Send the request
188+
*
189+
* @return ResponseInterface
190+
*/
191+
public function send()
192+
{
193+
$data = $this->getData();
194+
$headers = array_merge(
195+
$this->getHeaders(),
196+
array('Authorization' => 'Basic ' . base64_encode($this->getApiKey() . ':'))
197+
);
198+
199+
return $this->sendData($data, $headers);
200+
}
201+
202+
/**
203+
* @param $data
204+
* @param array $headers
205+
*
206+
* @return \Guzzle\Http\Message\RequestInterface
207+
*/
208+
protected function createClientRequest($data, array $headers = null)
128209
{
129210
// Stripe only accepts TLS >= v1.2, so make sure Curl is told
130-
$config = $this->httpClient->getConfig();
131-
$curlOptions = $config->get('curl.options');
211+
$config = $this->httpClient->getConfig();
212+
$curlOptions = $config->get('curl.options');
132213
$curlOptions[CURLOPT_SSLVERSION] = 6;
133214
$config->set('curl.options', $curlOptions);
134215
$this->httpClient->setConfig($config);
135-
216+
136217
// don't throw exceptions for 4xx errors
137218
$this->httpClient->getEventDispatcher()->addListener(
138219
'request.error',
@@ -146,15 +227,20 @@ function ($event) {
146227
$httpRequest = $this->httpClient->createRequest(
147228
$this->getHttpMethod(),
148229
$this->getEndpoint(),
149-
null,
230+
$headers,
150231
$data
151232
);
152-
$httpResponse = $httpRequest
153-
->setHeader('Authorization', 'Basic '.base64_encode($this->getApiKey().':'))
154-
->send();
155-
233+
234+
return $httpRequest;
235+
}
236+
237+
public function sendData($data, array $headers = null)
238+
{
239+
$httpRequest = $this->createClientRequest($data, $headers);
240+
$httpResponse = $httpRequest->send();
241+
156242
$this->response = new Response($this, $httpResponse->json());
157-
243+
158244
if ($httpResponse->hasHeader('Request-Id')) {
159245
$this->response->setRequestId((string) $httpResponse->getHeader('Request-Id'));
160246
}
@@ -180,7 +266,6 @@ public function setSource($value)
180266
return $this->setParameter('source', $value);
181267
}
182268

183-
184269
/**
185270
* Get the card data.
186271
*

src/Message/AuthorizeRequest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setDestination($value)
9191
}
9292

9393
/**
94-
* @return mixedgi
94+
* @return mixed
9595
*/
9696
public function getSource()
9797
{
@@ -108,6 +108,46 @@ public function setSource($value)
108108
return $this->setParameter('source', $value);
109109
}
110110

111+
/**
112+
* Connect only
113+
*
114+
* @return mixed
115+
*/
116+
public function getTransferGroup()
117+
{
118+
return $this->getParameter('transferGroup');
119+
}
120+
121+
/**
122+
* @param string $value
123+
*
124+
* @return AbstractRequest provides a fluent interface.
125+
*/
126+
public function setTransferGroup($value)
127+
{
128+
return $this->setParameter('transferGroup', $value);
129+
}
130+
131+
/**
132+
* Connect only
133+
*
134+
* @return mixed
135+
*/
136+
public function getOnBehalfOf()
137+
{
138+
return $this->getParameter('onBehalfOf');
139+
}
140+
141+
/**
142+
* @param string $value
143+
*
144+
* @return AbstractRequest provides a fluent interface.
145+
*/
146+
public function setOnBehalfOf($value)
147+
{
148+
return $this->setParameter('onBehalfOf', $value);
149+
}
150+
111151
/**
112152
* @return float
113153
*/
@@ -184,10 +224,18 @@ public function getData()
184224
$data['destination'] = $this->getDestination();
185225
}
186226

227+
if ($this->getOnBehalfOf()) {
228+
$data['on_behalf_of'] = $this->getOnBehalfOf();
229+
}
230+
187231
if ($this->getApplicationFee()) {
188232
$data['application_fee'] = $this->getApplicationFeeInteger();
189233
}
190234

235+
if ($this->getTransferGroup()) {
236+
$data['transfer_group'] = $this->getTransferGroup();
237+
}
238+
191239
if ($this->getReceiptEmail()) {
192240
$data['receipt_email'] = $this->getReceiptEmail();
193241
}

src/Message/DeleteInvoiceItemRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getInvoiceItemReference()
2525
/**
2626
* Set the set invoice-item reference.
2727
*
28-
* @return FetchInvoiceItemLinesRequest provides a fluent interface.
28+
* @return DeleteInvoiceItemRequest provides a fluent interface.
2929
*/
3030
public function setInvoiceItemReference($value)
3131
{

src/Message/FetchChargeRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Stripe Fetch Charge Request.
10-
*
10+
*
1111
* @deprecated 2.3.3:3.0.0 functionality provided by \Omnipay\Stripe\Message\FetchTransactionRequest
1212
* @see \Omnipay\Stripe\Message\FetchTransactionRequest
1313
* @link https://stripe.com/docs/api#retrieve_charge

src/Message/FetchInvoiceItemRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getInvoiceItemReference()
2525
/**
2626
* Set the set invoice-item reference.
2727
*
28-
* @return FetchInvoiceItemLinesRequest provides a fluent interface.
28+
* @return FetchInvoiceItemRequest provides a fluent interface.
2929
*/
3030
public function setInvoiceItemReference($value)
3131
{

0 commit comments

Comments
 (0)