Skip to content

Commit

Permalink
Merge pull request #10 from srishaharidas/XOL-3558
Browse files Browse the repository at this point in the history
XOL-3558 Fetch balance_transaction when creating charge on Stripe
  • Loading branch information
anush authored Jul 17, 2017
2 parents fe4603b + 343a21f commit 0697e5f
Show file tree
Hide file tree
Showing 55 changed files with 2,495 additions and 107 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- composer install -n --dev --prefer-source
Expand Down
97 changes: 96 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,101 @@ public function fetchBalanceTransaction(array $parameters = array())
return $this->createRequest('\Omnipay\Stripe\Message\FetchBalanceTransactionRequest', $parameters);
}


//
// Transfers
// @link https://stripe.com/docs/api#transfers
//


/**
* Transfer Request.
*
* To send funds from your Stripe account to a connected account, you create
* a new transfer object. Your Stripe balance must be able to cover the
* transfer amount, or you'll receive an "Insufficient Funds" error.
*
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function transfer(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\CreateTransferRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function fetchTransfer(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\FetchTransferRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function updateTransfer(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\UpdateTransferRequest', $parameters);
}

/**
* List Transfers
*
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest|\Omnipay\Stripe\Message\Transfers\ListTransfersRequest
*/
public function listTransfers(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\ListTransfersRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function reverseTransfer(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\CreateTransferReversalRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function fetchTransferReversal(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\FetchTransferReversalRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function updateTransferReversal(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\UpdateTransferReversalRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function listTransferReversals(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\Transfers\ListTransferReversalsRequest', $parameters);
}

//
// Cards
// @link https://stripe.com/docs/api#cards
Expand Down Expand Up @@ -543,7 +638,7 @@ public function deletePlan(array $parameters = array())
*/
public function listPlans(array $parameters = array())
{
return $this->createRequest('App\Lib\Omnipay\Stripe\Message\ListPlansRequest', $parameters);
return $this->createRequest('\Omnipay\Stripe\Message\ListPlansRequest', $parameters);
}

/**
Expand Down
176 changes: 147 additions & 29 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
/**
* Stripe Abstract Request.
*/

namespace Omnipay\Stripe\Message;

use Guzzle\Common\Event;
use Omnipay\Stripe\Util\StripeQueryAggregator;

use Omnipay\Common\Message\ResponseInterface;

/**
* Stripe Abstract Request.
*
Expand All @@ -29,8 +35,6 @@
*
* @see \Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api
*
* @method \Omnipay\Stripe\Message\Response send()
*/
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
Expand Down Expand Up @@ -130,6 +134,46 @@ public function setMetadata($value)
return $this->setParameter('metadata', $value);
}

/**
* Connect only
*
* @return mixed
*/
public function getConnectedStripeAccountHeader()
{
return $this->getParameter('connectedStripeAccount');
}

/**
* @param string $value
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function setConnectedStripeAccountHeader($value)
{
return $this->setParameter('connectedStripeAccount', $value);
}

/**
* Connect only
*
* @return mixed
*/
public function getIdempotencyKeyHeader()
{
return $this->getParameter('idempotencyKey');
}

/**
* @param string $value
*
* @return \Omnipay\Common\Message\AbstractRequest
*/
public function setIdempotencyKeyHeader($value)
{
return $this->setParameter('idempotencyKey', $value);
}

abstract public function getEndpoint();

/**
Expand All @@ -144,41 +188,70 @@ public function getHttpMethod()
return 'POST';
}

public function sendData($data)
/**
* Set the expand params for this request
*
* Use this to specify which fields should be returned in expanded form in the response
*
* @return AbstractRequest provides a fluent interface.
*/
public function setExpand($value)
{
// Stripe only accepts TLS >= v1.2, so make sure Curl is told
$config = $this->httpClient->getConfig();
$curlOptions = $config->get('curl.options');
$curlOptions[CURLOPT_SSLVERSION] = 6;
$config->set('curl.options', $curlOptions);
$this->httpClient->setConfig($config);

// don't throw exceptions for 4xx errors
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function ($event) {
if ($event['response']->isClientError()) {
$event->stopPropagation();
}
}
);
return $this->setParameter('expand', $value);
}

public function getExpand()
{
return $this->getParameter('expand');
}

/**
* @return array
*/
public function getHeaders()
{
$headers = array();

if ($this->getConnectedStripeAccountHeader()) {
$headers['Stripe-Account'] = $this->getConnectedStripeAccountHeader();
}

if ($this->getIdempotencyKeyHeader()) {
$headers['Idempotency-Key'] = $this->getIdempotencyKeyHeader();
}

$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
null,
$data
);
$httpRequest->setHeader('Authorization', 'Basic '.base64_encode($this->getApiKey().':'));
$apiVersion = $this->getApiVersion();
if (!empty($apiVersion)) {
// If user has set an API version use that https://stripe.com/docs/api#versioning
$httpRequest->setHeader('Stripe-Version', $this->getApiVersion());
$headers['Stripe-Version'] = $this->getApiVersion();
}

return $headers;
}

/**
* Send the request
*
* @return ResponseInterface
*/
public function send()
{
$data = $this->getData();
$headers = array_merge(
$this->getHeaders(),
array('Authorization' => 'Basic ' . base64_encode($this->getApiKey() . ':'))
);

return $this->sendData($data, $headers);
}

public function sendData($data, array $headers = null)
{
$httpRequest = $this->createClientRequest($data, $headers);
$httpResponse = $httpRequest->send();

$this->response = new Response($this, $httpResponse->json());

if ($httpResponse->hasHeader('Request-Id')) {
$this->response->setRequestId((string) $httpResponse->getHeader('Request-Id'));
}
Expand All @@ -204,6 +277,50 @@ public function setSource($value)
return $this->setParameter('source', $value);
}

/**
* @param $data
* @param array $headers
*
* @return \Guzzle\Http\Message\RequestInterface
*/
protected function createClientRequest($data, array $headers = null)
{
// Stripe only accepts TLS >= v1.2, so make sure Curl is told
$config = $this->httpClient->getConfig();
$curlOptions = $config->get('curl.options');
$curlOptions[CURLOPT_SSLVERSION] = 6;
$config->set('curl.options', $curlOptions);
$this->httpClient->setConfig($config);

// For query params with an array value, Stripe accepts only one format for aggregating these values. This type
// of query aggregation is not supported by default by Guzzle so we have to use a custom query aggregator
$this->httpClient->getEventDispatcher()->addListener('request.before_send', function (Event $event) {
$request = $event['request'];
if ($request->getMethod() === 'POST') {
$request->getQuery()->setAggregator(new StripeQueryAggregator());
}
});

// don't throw exceptions for 4xx errors
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function ($event) {
if ($event['response']->isClientError()) {
$event->stopPropagation();
}
}
);

$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
$headers,
$data
);

return $httpRequest;
}


/**
* Get the card data.
Expand Down Expand Up @@ -242,6 +359,7 @@ protected function getCardData()
$data['address_line1'] = $card->getAddress1();
$data['address_line2'] = $card->getAddress2();
$data['address_city'] = $card->getCity();
$data['address_zip'] = $card->getPostcode();
$data['address_state'] = $card->getState();
$data['address_country'] = $card->getCountry();
$data['email'] = $card->getEmail();
Expand Down
Loading

0 comments on commit 0697e5f

Please sign in to comment.