Skip to content

Commit be0b5c3

Browse files
authored
Merge pull request #22 from lukeholder/master
Support for new Worldpay JSON API
2 parents db1b379 + 6c88b13 commit be0b5c3

21 files changed

+751
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
composer.phar
44
phpunit.xml
5+
.idea

.travis.yml

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
language: php
22

3+
dist: trusty
34
php:
4-
- 5.3
5-
- 5.4
6-
- 5.5
7-
- 5.6
5+
- "7.1"
6+
- "7.0"
7+
- "5.6"
8+
- "5.5"
9+
- "5.4"
810
- hhvm
911

12+
matrix:
13+
include:
14+
- php: "5.3"
15+
dist: precise
16+
1017
before_script:
1118
- composer install -n --dev --prefer-source
1219

src/JsonGateway.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay;
4+
5+
use Omnipay\Common\AbstractGateway;
6+
7+
/**
8+
* WorldPay Gateway
9+
*
10+
* @link http://www.worldpay.com/support/kb/bg/htmlredirect/rhtml.html
11+
*/
12+
class JsonGateway extends AbstractGateway
13+
{
14+
public function getName()
15+
{
16+
return 'WorldPay JSON';
17+
}
18+
19+
public function getDefaultParameters()
20+
{
21+
return array(
22+
'merchantId' => '',
23+
'serviceKey' => '',
24+
'clientKey' => '',
25+
);
26+
}
27+
28+
public function getServiceKey()
29+
{
30+
return $this->getParameter('serviceKey');
31+
}
32+
33+
public function setServiceKey($value)
34+
{
35+
return $this->setParameter('serviceKey', $value);
36+
}
37+
38+
public function getMerchantId()
39+
{
40+
return $this->getParameter('merchantId');
41+
}
42+
43+
public function setMerchantId($value)
44+
{
45+
return $this->setParameter('merchantId', $value);
46+
}
47+
48+
public function getClientKey()
49+
{
50+
return $this->getParameter('clientKey');
51+
}
52+
53+
public function setClientKey($value)
54+
{
55+
return $this->setParameter('clientKey', $value);
56+
}
57+
58+
public function purchase(array $parameters = array())
59+
{
60+
return $this->createRequest('\Omnipay\WorldPay\Message\JsonPurchaseRequest', $parameters);
61+
}
62+
63+
public function authorize(array $parameters = array())
64+
{
65+
return $this->createRequest('\Omnipay\WorldPay\Message\JsonAuthorizeRequest', $parameters);
66+
}
67+
68+
public function refund(array $parameters = array())
69+
{
70+
return $this->createRequest('\Omnipay\WorldPay\Message\JsonRefundRequest', $parameters);
71+
}
72+
73+
public function capture(array $parameters = array())
74+
{
75+
return $this->createRequest('\Omnipay\WorldPay\Message\JsonCaptureRequest', $parameters);
76+
}
77+
}

src/Message/JsonAbstractRequest.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay\Message;
4+
5+
use Omnipay\Common\Message\AbstractRequest;
6+
7+
abstract class JsonAbstractRequest extends AbstractRequest
8+
{
9+
protected $endpoint = 'https://api.worldpay.com/v1';
10+
11+
public function getHttpMethod()
12+
{
13+
return 'POST';
14+
}
15+
16+
public function getMerchantId()
17+
{
18+
return $this->getParameter('merchantId');
19+
}
20+
21+
public function setMerchantId($value)
22+
{
23+
return $this->setParameter('merchantId', $value);
24+
}
25+
26+
public function getServiceKey()
27+
{
28+
return $this->getParameter('serviceKey');
29+
}
30+
31+
public function setServiceKey($value)
32+
{
33+
return $this->setParameter('serviceKey', $value);
34+
}
35+
36+
public function getClientKey()
37+
{
38+
return $this->getParameter('clientKey');
39+
}
40+
41+
public function setClientKey($value)
42+
{
43+
return $this->setParameter('clientKey', $value);
44+
}
45+
46+
47+
public function sendRequest($data)
48+
{
49+
$config = $this->httpClient->getConfig();
50+
$curlOptions = $config->get('curl.options');
51+
$curlOptions[CURLOPT_SSLVERSION] = 6;
52+
$config->set('curl.options', $curlOptions);
53+
$this->httpClient->setConfig($config);
54+
55+
// don't throw exceptions for 4xx errors
56+
$this->httpClient->getEventDispatcher()->addListener(
57+
'request.error',
58+
function ($event) {
59+
if ($event['response']->isClientError()) {
60+
$event->stopPropagation();
61+
}
62+
}
63+
);
64+
65+
$httpRequest = $this->httpClient->createRequest(
66+
$this->getHttpMethod(),
67+
$this->getEndpoint(),
68+
null,
69+
json_encode($data)
70+
);
71+
72+
$httpResponse = $httpRequest
73+
->setHeader('Authorization', $this->getServiceKey())
74+
->setHeader('Content-type', 'application/json')
75+
->send();
76+
77+
return $httpResponse;
78+
}
79+
80+
/**
81+
* @return string
82+
*/
83+
public function getResponseClassName()
84+
{
85+
return '\Omnipay\WorldPay\Message\JsonResponse';
86+
}
87+
88+
/**
89+
* @param mixed $data
90+
*
91+
* @return JsonResponse
92+
*/
93+
public function sendData($data)
94+
{
95+
$httpResponse = $this->sendRequest($data);
96+
97+
$responseClass = $this->getResponseClassName();
98+
return $this->response = new $responseClass($this, $httpResponse);
99+
}
100+
}

src/Message/JsonAuthorizeRequest.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay\Message;
4+
5+
/**
6+
* WorldPay Purchase Request
7+
*/
8+
class JsonAuthorizeRequest extends JsonPurchaseRequest
9+
{
10+
public function getData()
11+
{
12+
$data = parent::getData();
13+
14+
$data['authorizeOnly'] = true;
15+
16+
return $data;
17+
}
18+
19+
/**
20+
* @return string
21+
*/
22+
public function getResponseClassName()
23+
{
24+
return '\Omnipay\WorldPay\Message\JsonAuthorizeResponse';
25+
}
26+
}

src/Message/JsonAuthorizeResponse.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
use Omnipay\Common\Message\RequestInterface;
7+
8+
/**
9+
* WorldPay Purchase Request
10+
*/
11+
class JsonAuthorizeResponse extends JsonResponse
12+
{
13+
14+
/**
15+
* Is the response successful?
16+
*
17+
* @return bool
18+
*/
19+
public function isSuccessful()
20+
{
21+
$isHttpSuccess = parent::isSuccessful();
22+
$isPurchaseSuccess = false;
23+
24+
if (isset($this->data['paymentStatus']) && $this->data['paymentStatus'] == 'AUTHORIZED') {
25+
$isPurchaseSuccess = true;
26+
}
27+
28+
return ($isHttpSuccess && $isPurchaseSuccess);
29+
}
30+
}

src/Message/JsonCaptureRequest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay\Message;
4+
5+
/**
6+
* WorldPay Capture Request
7+
*/
8+
class JsonCaptureRequest extends JsonAbstractRequest
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function getData()
14+
{
15+
$data = array();
16+
17+
$data['captureAmount'] = $this->getAmountInteger();
18+
19+
return $data;
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getEndpoint()
26+
{
27+
return $this->endpoint.'/orders/'.$this->getTransactionReference().'/capture';
28+
}
29+
}

src/Message/JsonPurchaseRequest.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Omnipay\WorldPay\Message;
4+
5+
/**
6+
* WorldPay Purchase Request
7+
*/
8+
class JsonPurchaseRequest extends JsonAbstractRequest
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function getData()
14+
{
15+
$this->validate('amount', 'token');
16+
17+
$data = array();
18+
$data['token'] = $this->getToken();
19+
$data['amount'] = $this->getAmountInteger();
20+
$data['currencyCode'] = $this->getCurrency();
21+
$data['orderDescription'] = $this->getDescription();
22+
$data['customerOrderCode'] = $this->getTransactionId();
23+
$data['currency'] = $this->getCurrency();
24+
25+
$card = $this->getCard();
26+
27+
$data['billingAddress'] = array();
28+
29+
if ($card) {
30+
$data['billingAddress']['address1'] = $card->getBillingAddress1();
31+
$data['billingAddress']['address2'] = $card->getBillingAddress2();
32+
$data['billingAddress']['city'] = $card->getBillingCity();
33+
$data['billingAddress']['state'] = $card->getBillingState();
34+
$data['billingAddress']['countryCode'] = $card->getBillingCountry();
35+
$data['billingAddress']['postalCode'] = $card->getBillingPostcode();
36+
$data['billingAddress']['telephoneNumber'] = $card->getBillingPhone();
37+
38+
$data['name'] = $card->getName();
39+
40+
$data['deliveryAddress'] = array();
41+
42+
$data['deliveryAddress']['firstName'] = $card->getShippingFirstName();
43+
$data['deliveryAddress']['lastName'] = $card->getShippingLastName();
44+
$data['deliveryAddress']['address1'] = $card->getShippingAddress1();
45+
$data['deliveryAddress']['address2'] = $card->getShippingAddress2();
46+
$data['deliveryAddress']['city'] = $card->getShippingCity();
47+
$data['deliveryAddress']['state'] = $card->getShippingState();
48+
$data['deliveryAddress']['countryCode'] = $card->getShippingCountry();
49+
$data['deliveryAddress']['postalCode'] = $card->getShippingPostcode();
50+
$data['deliveryAddress']['telephoneNumber'] = $card->getBillingPhone();
51+
52+
$data['shopperEmailAddress'] = $card->getEmail();
53+
54+
}
55+
56+
$data['shopperIpAddress'] = $this->getClientIp();
57+
58+
// Omnipay does not support recurring at the moment
59+
$data['orderType'] = 'ECOM';
60+
61+
return $data;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getEndpoint()
68+
{
69+
return $this->endpoint.'/orders';
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
public function getResponseClassName()
76+
{
77+
return '\Omnipay\WorldPay\Message\JsonResponse';
78+
}
79+
}

0 commit comments

Comments
 (0)