Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with omnipay-common:^3.0 #29

Merged
merged 4 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ php:
- "7.1"
- "7.0"
- "5.6"
- "5.5"
- "5.4"
- hhvm

matrix:
include:
- php: "5.3"
dist: precise

before_script:
- composer install -n --dev --prefer-source
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ to your `composer.json` file:
```json
{
"require": {
"omnipay/worldpay": "~2.0"
"omnipay/worldpay": "~3.0"
}
}
```
Expand All @@ -32,6 +32,7 @@ And run composer to update your dependencies:
The following gateways are provided by this package:

* WorldPay
* WorldPay_Json

For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
repository.
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
"psr-4": { "Omnipay\\WorldPay\\" : "src/" }
},
"require": {
"omnipay/common": "~2.0"
"omnipay/common": "~3.0"
},
"require-dev": {
"omnipay/tests": "~2.0"
"omnipay/tests": "~3.0",
"squizlabs/php_codesniffer": "~3.0"
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "3.0.x-dev"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
83 changes: 82 additions & 1 deletion src/JsonGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

namespace Omnipay\WorldPay;

use Http\Adapter\Guzzle6\Client;
use Omnipay\Common\AbstractGateway;

/**
* WorldPay Gateway
*
* @link http://www.worldpay.com/support/kb/bg/htmlredirect/rhtml.html
* @link https://developer.worldpay.com/jsonapi/docs
*/
class JsonGateway extends AbstractGateway
{
/**
* Name of the gateway
*
* @return string
*/
public function getName()
{
return 'WorldPay JSON';
}

/**
* Setup the default parameters
*
* @return string[]
*/
public function getDefaultParameters()
{
return array(
Expand All @@ -25,53 +36,123 @@ public function getDefaultParameters()
);
}

/**
* Get the stored service key
*
* @return string
*/
public function getServiceKey()
{
return $this->getParameter('serviceKey');
}

/**
* Set the stored service key
*
* @param string $value Service key to store
*/
public function setServiceKey($value)
{
return $this->setParameter('serviceKey', $value);
}

/**
* Get the stored merchant ID
*
* @return string
*/
public function getMerchantId()
{
return $this->getParameter('merchantId');
}

/**
* Set the stored merchant ID
*
* @param string $value Merchant ID to store
*/
public function setMerchantId($value)
{
return $this->setParameter('merchantId', $value);
}

/**
* Get the stored client key
*
* @return string
*/
public function getClientKey()
{
return $this->getParameter('clientKey');
}

/**
* Set the stored client key
*
* @param string $value Client key to store
*/
public function setClientKey($value)
{
return $this->setParameter('clientKey', $value);
}

/**
* Create purchase request
*
* @param array $parameters
*
* @return \Omnipay\WorldPay\Message\JsonPurchaseRequest
*/
public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\WorldPay\Message\JsonPurchaseRequest', $parameters);
}

/**
* Create authorize request
*
* @param array $parameters
*
* @return \Omnipay\WorldPay\Message\JsonAuthorizeRequest
*/
public function authorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\WorldPay\Message\JsonAuthorizeRequest', $parameters);
}

/**
* Create refund request
*
* @param array $parameters
*
* @return \Omnipay\WorldPay\Message\JsonRefundRequest
*/
public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\WorldPay\Message\JsonRefundRequest', $parameters);
}

/**
* Create capture request
*
* @param array $parameters
*
* @return \Omnipay\WorldPay\Message\JsonCaptureRequest
*/
public function capture(array $parameters = array())
{
return $this->createRequest('\Omnipay\WorldPay\Message\JsonCaptureRequest', $parameters);
}

protected function getDefaultHttpClient()
{
$guzzleClient = Client::createWithConfig([
'curl.options' => [
CURLOPT_SSLVERSION => 6
]
]);


return new \Omnipay\Common\Http\Client($guzzleClient);
}
}
86 changes: 63 additions & 23 deletions src/Message/JsonAbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,115 @@

namespace Omnipay\WorldPay\Message;

use Guzzle\Http\Message\Response as HttpResponse;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\ResponseInterface;

abstract class JsonAbstractRequest extends AbstractRequest
{
/**
* @var string API endpoint base to connect to
*/
protected $endpoint = 'https://api.worldpay.com/v1';

/**
* Method required to override for getting the specific request endpoint
*
* @return string
*/
abstract public function getEndpoint();

/**
* The HTTP method used to send data to the API endpoint
*
* @return string
*/
public function getHttpMethod()
{
return 'POST';
}

/**
* Get the stored merchant ID
*
* @return string
*/
public function getMerchantId()
{
return $this->getParameter('merchantId');
}

/**
* Set the stored merchant ID
*
* @param string $value Merchant ID to store
*/
public function setMerchantId($value)
{
return $this->setParameter('merchantId', $value);
}

/**
* Get the stored service key
*
* @return string
*/
public function getServiceKey()
{
return $this->getParameter('serviceKey');
}

/**
* Set the stored service key
*
* @param string $value Service key to store
*/
public function setServiceKey($value)
{
return $this->setParameter('serviceKey', $value);
}

/**
* Get the stored client key
*
* @return string
*/
public function getClientKey()
{
return $this->getParameter('clientKey');
}

/**
* Set the stored client key
*
* @param string $value Client key to store
*/
public function setClientKey($value)
{
return $this->setParameter('clientKey', $value);
}


/**
* Make the actual request to WorldPay
*
* @param mixed $data The data to encode and send to the API endpoint
*
* @return \Psr\Http\Message\ResponseInterface HTTP response object
*/
public function sendRequest($data)
{
$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();
}
}
);

$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
null,
[],
json_encode($data)
);

$httpResponse = $httpRequest
->setHeader('Authorization', $this->getServiceKey())
->setHeader('Content-type', 'application/json')
->send();
$httpRequest = $httpRequest
->withHeader('Authorization', $this->getServiceKey())
->withHeader('Content-type', 'application/json');

$httpResponse = $this->httpClient->sendRequest($httpRequest);

return $httpResponse;
}
Expand All @@ -86,7 +124,9 @@ public function getResponseClassName()
}

/**
* @param mixed $data
* Send the request to the API then build the response object
*
* @param mixed $data The data to encode and send to the API endpoint
*
* @return JsonResponse
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Message/JsonAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
class JsonAuthorizeRequest extends JsonPurchaseRequest
{
/**
* Set up the authorize-specific data
*
* @return mixed
*/
public function getData()
{
$data = parent::getData();
Expand Down
Loading