Skip to content

Commit

Permalink
Merge branch 'master' into XOL-4975
Browse files Browse the repository at this point in the history
  • Loading branch information
anush committed Mar 10, 2020
2 parents 62f39bf + b292b86 commit b972a8d
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 243 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ php:
- 7.1
- 7.2

sudo: false

cache:
directories:
- $HOME/.composer/cache

before_script:
- composer install -n --dev --prefer-source
- composer install --prefer-dist --no-interaction

script: vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpunit --coverage-text
19 changes: 6 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@
"homepage": "https://github.com/thephpleague/omnipay-stripe/contributors"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/xola/omnipay-common"
}
],
"autoload": {
"psr-4": { "Omnipay\\Stripe\\" : "src/" }
},
"require": {
"omnipay/common": "^2.5.4"
"ext-json": "*",
"omnipay/common": "^3"
},
"require-dev": {
"omnipay/tests": "~2.0"
"omnipay/tests": "^3",
"squizlabs/php_codesniffer": "^3"
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
"minimum-stability": "dev",
"prefer-stable": true
}
8 changes: 4 additions & 4 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ public function setApiKey($value)
*
* @return Gateway provides a fluent interface
*/
public function setApiVersion($value)
public function setStripeVersion($value)
{
return $this->setParameter('apiVersion', $value);
return $this->setParameter('stripeVersion', $value);
}

/**
* Returns the API version configured for this gateway
*
* @return string
*/
public function getApiVersion()
public function getStripeVersion()
{
return $this->getParameter('apiVersion');
return $this->getParameter('stripeVersion');
}

/**
Expand Down
86 changes: 21 additions & 65 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

namespace Omnipay\Stripe\Message;

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

use Omnipay\Common\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -65,14 +62,14 @@ public function setApiKey($value)
return $this->setParameter('apiKey', $value);
}

public function getApiVersion()
public function getStripeVersion()
{
return $this->getParameter('apiVersion');
return $this->getParameter('stripeVersion');
}

public function setApiVersion($value)
public function setStripeVersion($value)
{
return $this->setParameter('apiVersion', $value);
return $this->setParameter('stripeVersion', $value);
}

/**
Expand Down Expand Up @@ -147,7 +144,7 @@ public function getConnectedStripeAccountHeader()
/**
* @param string $value
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function setConnectedStripeAccountHeader($value)
{
Expand All @@ -167,7 +164,7 @@ public function getIdempotencyKeyHeader()
/**
* @param string $value
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function setIdempotencyKeyHeader($value)
{
Expand Down Expand Up @@ -220,10 +217,10 @@ public function getHeaders()
$headers['Idempotency-Key'] = $this->getIdempotencyKeyHeader();
}

$apiVersion = $this->getApiVersion();
if (!empty($apiVersion)) {
$stripeVersion = $this->getStripeVersion();
if (!empty($stripeVersion)) {
// If user has set an API version use that https://stripe.com/docs/api#versioning
$headers['Stripe-Version'] = $this->getApiVersion();
$headers['Stripe-Version'] = $this->getStripeVersion();
}

return $headers;
Expand All @@ -247,16 +244,20 @@ public function send()

public function sendData($data, array $headers = null)
{
$httpRequest = $this->createClientRequest($data, $headers);
$httpResponse = $httpRequest->send();
$headers = array_merge(
$this->getHeaders(),
array('Authorization' => 'Basic ' . base64_encode($this->getApiKey() . ':'))
);

$this->response = new Response($this, $httpResponse->json());
$body = $data ? http_build_query($data, '', '&') : null;
$httpResponse = $this->httpClient->request($this->getHttpMethod(), $this->getEndpoint(), $headers, $body);

if ($httpResponse->hasHeader('Request-Id')) {
$this->response->setRequestId((string) $httpResponse->getHeader('Request-Id'));
}
return $this->createResponse($httpResponse->getBody()->getContents(), $httpResponse->getHeaders());
}

return $this->response;
protected function createResponse($data, $headers = [])
{
return $this->response = new Response($this, $data, $headers);
}

/**
Expand All @@ -277,51 +278,6 @@ 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 @@ -362,7 +318,7 @@ protected function getCardData()
$data['address_zip'] = $card->getPostcode();
$data['address_state'] = $card->getState();
$data['address_country'] = $card->getCountry();
$data['email'] = $card->getEmail();
$data['email'] = $card->getEmail();

return $data;
}
Expand Down
32 changes: 30 additions & 2 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Omnipay\Stripe\Message;

use Omnipay\Common\ItemBag;
use Omnipay\Stripe\StripeItemBag;

/**
* Stripe Authorize Request.
*
Expand Down Expand Up @@ -207,6 +210,30 @@ public function setReceiptEmail($email)
return $this;
}

/**
* A list of items in this order
*
* @return ItemBag|null A bag containing items in this order
*/
public function getItems()
{
return $this->getParameter('items');
}

/**
* Set the items in this order
*
* @param array $items An array of items in this order
* @return AuthorizeRequest
*/
public function setItems($items)
{
if ($items && !$items instanceof ItemBag) {
$items = new StripeItemBag($items);
}
return $this->setParameter('items', $items);
}

public function getData()
{
$this->validate('amount', 'currency');
Expand All @@ -219,8 +246,9 @@ public function getData()
$data['metadata'] = $this->getMetadata();
$data['capture'] = 'false';

$apiVersion = $this->getApiVersion();
if (is_null($apiVersion) || (!is_null($apiVersion) && $apiVersion >= self::API_VERSION_STATEMENT_DESCRIPTOR)) {
$stripeVersion = $this->getStripeVersion();
if (is_null($stripeVersion) || (!is_null($stripeVersion)
&& $stripeVersion >= self::API_VERSION_STATEMENT_DESCRIPTOR)) {
$data['statement_descriptor_suffix'] = $this->getStatementDescriptor();
} else {
$data['statement_description'] = $this->getStatementDescriptor();
Expand Down
34 changes: 20 additions & 14 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Omnipay\Stripe\Message;

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RequestInterface;

/**
* Stripe Response.
Expand All @@ -22,7 +23,19 @@ class Response extends AbstractResponse
* @var string URL
*/
protected $requestId = null;


/**
* @var array
*/
protected $headers = [];

public function __construct(RequestInterface $request, $data, $headers = [])
{
$this->request = $request;
$this->data = json_decode($data, true);
$this->headers = $headers;
}

/**
* Is the transaction successful?
*
Expand Down Expand Up @@ -113,7 +126,6 @@ public function getCustomerReference()
public function getCardReference()
{
if (isset($this->data['object']) && 'customer' === $this->data['object']) {

if (isset($this->data['default_card']) && !empty($this->data['default_card'])) {
return $this->data['default_card'];
}
Expand Down Expand Up @@ -359,22 +371,16 @@ public function getType()

return null;
}

/**
* @return string
* @return string|null
*/
public function getRequestId()
{
return $this->requestId;
}
if (isset($this->headers['Request-Id'])) {
return $this->headers['Request-Id'][0];
}

/**
* Set request id
*
* @return AbstractRequest provides a fluent interface.
*/
public function setRequestId($requestId)
{
$this->requestId = $requestId;
return null;
}
}
36 changes: 36 additions & 0 deletions src/StripeItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Stripe Item
*/

namespace Omnipay\Stripe;

use Omnipay\Common\Item;

/**
* Class StripeItem
*
* @package Omnipay\Stripe
*/
class StripeItem extends Item
{
public function getTaxes()
{
return $this->getParameter('taxes');
}

public function setTaxes($value)
{
$this->setParameter('taxes', $value);
}

public function getDiscount()
{
return $this->getParameter('discount');
}

public function setDiscount($value)
{
$this->setParameter('discount', $value);
}
}
Loading

0 comments on commit b972a8d

Please sign in to comment.