Skip to content

Commit 522fbac

Browse files
authored
Merge pull request #26 from PatronBase/master
Bundle of fixes/updates for WorldPay JSON Gateway
2 parents be0b5c3 + 31cec5a commit 522fbac

18 files changed

+311
-60
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ And run composer to update your dependencies:
3232
The following gateways are provided by this package:
3333

3434
* WorldPay
35+
* WorldPay_Json
3536

3637
For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
3738
repository.

src/JsonGateway.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77
/**
88
* WorldPay Gateway
99
*
10-
* @link http://www.worldpay.com/support/kb/bg/htmlredirect/rhtml.html
10+
* @link https://developer.worldpay.com/jsonapi/docs
1111
*/
1212
class JsonGateway extends AbstractGateway
1313
{
14+
/**
15+
* Name of the gateway
16+
*
17+
* @return string
18+
*/
1419
public function getName()
1520
{
1621
return 'WorldPay JSON';
1722
}
1823

24+
/**
25+
* Setup the default parameters
26+
*
27+
* @return string[]
28+
*/
1929
public function getDefaultParameters()
2030
{
2131
return array(
@@ -25,51 +35,109 @@ public function getDefaultParameters()
2535
);
2636
}
2737

38+
/**
39+
* Get the stored service key
40+
*
41+
* @return string
42+
*/
2843
public function getServiceKey()
2944
{
3045
return $this->getParameter('serviceKey');
3146
}
3247

48+
/**
49+
* Set the stored service key
50+
*
51+
* @param string $value Service key to store
52+
*/
3353
public function setServiceKey($value)
3454
{
3555
return $this->setParameter('serviceKey', $value);
3656
}
3757

58+
/**
59+
* Get the stored merchant ID
60+
*
61+
* @return string
62+
*/
3863
public function getMerchantId()
3964
{
4065
return $this->getParameter('merchantId');
4166
}
4267

68+
/**
69+
* Set the stored merchant ID
70+
*
71+
* @param string $value Merchant ID to store
72+
*/
4373
public function setMerchantId($value)
4474
{
4575
return $this->setParameter('merchantId', $value);
4676
}
4777

78+
/**
79+
* Get the stored client key
80+
*
81+
* @return string
82+
*/
4883
public function getClientKey()
4984
{
5085
return $this->getParameter('clientKey');
5186
}
5287

88+
/**
89+
* Set the stored client key
90+
*
91+
* @param string $value Client key to store
92+
*/
5393
public function setClientKey($value)
5494
{
5595
return $this->setParameter('clientKey', $value);
5696
}
5797

98+
/**
99+
* Create purchase request
100+
*
101+
* @param array $parameters
102+
*
103+
* @return \Omnipay\WorldPay\Message\JsonPurchaseRequest
104+
*/
58105
public function purchase(array $parameters = array())
59106
{
60107
return $this->createRequest('\Omnipay\WorldPay\Message\JsonPurchaseRequest', $parameters);
61108
}
62109

110+
/**
111+
* Create authorize request
112+
*
113+
* @param array $parameters
114+
*
115+
* @return \Omnipay\WorldPay\Message\JsonAuthorizeRequest
116+
*/
63117
public function authorize(array $parameters = array())
64118
{
65119
return $this->createRequest('\Omnipay\WorldPay\Message\JsonAuthorizeRequest', $parameters);
66120
}
67121

122+
/**
123+
* Create refund request
124+
*
125+
* @param array $parameters
126+
*
127+
* @return \Omnipay\WorldPay\Message\JsonRefundRequest
128+
*/
68129
public function refund(array $parameters = array())
69130
{
70131
return $this->createRequest('\Omnipay\WorldPay\Message\JsonRefundRequest', $parameters);
71132
}
72133

134+
/**
135+
* Create capture request
136+
*
137+
* @param array $parameters
138+
*
139+
* @return \Omnipay\WorldPay\Message\JsonCaptureRequest
140+
*/
73141
public function capture(array $parameters = array())
74142
{
75143
return $this->createRequest('\Omnipay\WorldPay\Message\JsonCaptureRequest', $parameters);

src/Message/JsonAbstractRequest.php

+56-2
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,100 @@
22

33
namespace Omnipay\WorldPay\Message;
44

5+
use Guzzle\Http\Message\Response as HttpResponse;
56
use Omnipay\Common\Message\AbstractRequest;
67

78
abstract class JsonAbstractRequest extends AbstractRequest
89
{
10+
/**
11+
* @var string API endpoint base to connect to
12+
*/
913
protected $endpoint = 'https://api.worldpay.com/v1';
1014

15+
/**
16+
* Method required to override for getting the specific request endpoint
17+
*
18+
* @return string
19+
*/
20+
abstract public function getEndpoint();
21+
22+
/**
23+
* The HTTP method used to send data to the API endpoint
24+
*
25+
* @return string
26+
*/
1127
public function getHttpMethod()
1228
{
1329
return 'POST';
1430
}
1531

32+
/**
33+
* Get the stored merchant ID
34+
*
35+
* @return string
36+
*/
1637
public function getMerchantId()
1738
{
1839
return $this->getParameter('merchantId');
1940
}
2041

42+
/**
43+
* Set the stored merchant ID
44+
*
45+
* @param string $value Merchant ID to store
46+
*/
2147
public function setMerchantId($value)
2248
{
2349
return $this->setParameter('merchantId', $value);
2450
}
2551

52+
/**
53+
* Get the stored service key
54+
*
55+
* @return string
56+
*/
2657
public function getServiceKey()
2758
{
2859
return $this->getParameter('serviceKey');
2960
}
3061

62+
/**
63+
* Set the stored service key
64+
*
65+
* @param string $value Service key to store
66+
*/
3167
public function setServiceKey($value)
3268
{
3369
return $this->setParameter('serviceKey', $value);
3470
}
3571

72+
/**
73+
* Get the stored client key
74+
*
75+
* @return string
76+
*/
3677
public function getClientKey()
3778
{
3879
return $this->getParameter('clientKey');
3980
}
4081

82+
/**
83+
* Set the stored client key
84+
*
85+
* @param string $value Client key to store
86+
*/
4187
public function setClientKey($value)
4288
{
4389
return $this->setParameter('clientKey', $value);
4490
}
4591

46-
92+
/**
93+
* Make the actual request to WorldPay
94+
*
95+
* @param mixed $data The data to encode and send to the API endpoint
96+
*
97+
* @return HttpResponse HTTP response object
98+
*/
4799
public function sendRequest($data)
48100
{
49101
$config = $this->httpClient->getConfig();
@@ -86,7 +138,9 @@ public function getResponseClassName()
86138
}
87139

88140
/**
89-
* @param mixed $data
141+
* Send the request to the API then build the response object
142+
*
143+
* @param mixed $data The data to encode and send to the API endpoint
90144
*
91145
* @return JsonResponse
92146
*/

src/Message/JsonAuthorizeRequest.php

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
class JsonAuthorizeRequest extends JsonPurchaseRequest
99
{
10+
/**
11+
* Set up the authorize-specific data
12+
*
13+
* @return mixed
14+
*/
1015
public function getData()
1116
{
1217
$data = parent::getData();

src/Message/JsonAuthorizeResponse.php

+4-20
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22

33
namespace Omnipay\WorldPay\Message;
44

5-
use Omnipay\Common\Message\AbstractResponse;
6-
use Omnipay\Common\Message\RequestInterface;
7-
85
/**
9-
* WorldPay Purchase Request
6+
* WorldPay Authorize Request
107
*/
11-
class JsonAuthorizeResponse extends JsonResponse
8+
class JsonAuthorizeResponse extends JsonPurchaseResponse
129
{
13-
1410
/**
15-
* Is the response successful?
16-
*
17-
* @return bool
11+
* @var string Payment status that determines success
1812
*/
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-
}
13+
protected $successfulPaymentStatus = 'AUTHORIZED';
3014
}

src/Message/JsonPurchaseRequest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
class JsonPurchaseRequest extends JsonAbstractRequest
99
{
1010
/**
11-
* @return array
11+
* Set up the base data for a purchase request
12+
*
13+
* @return mixed[]
1214
*/
1315
public function getData()
1416
{
@@ -74,6 +76,6 @@ public function getEndpoint()
7476
*/
7577
public function getResponseClassName()
7678
{
77-
return '\Omnipay\WorldPay\Message\JsonResponse';
79+
return '\Omnipay\WorldPay\Message\JsonPurchaseResponse';
7880
}
7981
}

src/Message/JsonPurchaseResponse.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Omnipay\WorldPay\Message;
44

5-
use Omnipay\Common\Message\AbstractResponse;
6-
use Omnipay\Common\Message\RequestInterface;
7-
85
/**
96
* WorldPay Purchase Request
107
*/
118
class JsonPurchaseResponse extends JsonResponse
129
{
10+
/**
11+
* @var string Payment status that determines success
12+
*/
13+
protected $successfulPaymentStatus = 'SUCCESS';
1314

1415
/**
1516
* Is the response successful?
@@ -21,10 +22,36 @@ public function isSuccessful()
2122
$isHttpSuccess = parent::isSuccessful();
2223
$isPurchaseSuccess = false;
2324

24-
if (isset($this->data['paymentStatus']) && $this->data['paymentStatus'] == 'SUCCESS') {
25+
if (isset($this->data['paymentStatus']) && $this->data['paymentStatus'] == $this->successfulPaymentStatus) {
2526
$isPurchaseSuccess = true;
2627
}
2728

2829
return ($isHttpSuccess && $isPurchaseSuccess);
2930
}
31+
32+
/**
33+
* What is the relevant description of the transaction response?
34+
*
35+
* @todo Sometimes the 'description' field is more user-friendly (see simulated eror) - how do we decide which one?
36+
*
37+
* @return string|null
38+
*/
39+
public function getMessage()
40+
{
41+
// check for HTTP failure response first
42+
$httpMessage = parent::getMessage();
43+
if ($httpMessage !== null) {
44+
return $httpMessage;
45+
}
46+
47+
// check if descriptive failure reason is available
48+
if (!$this->isSuccessful() && isset($this->data['paymentStatusReason'])) {
49+
return $this->data['paymentStatusReason'];
50+
}
51+
52+
// check if general payment status is available
53+
if (isset($this->data['paymentStatus'])) {
54+
return $this->data['paymentStatus'];
55+
}
56+
}
3057
}

src/Message/JsonRefundRequest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
*/
88
class JsonRefundRequest extends JsonAbstractRequest
99
{
10-
10+
/**
11+
* Set up the refund-specific data
12+
*
13+
* @return mixed
14+
*/
1115
public function getData()
1216
{
1317
$this->validate('amount');

0 commit comments

Comments
 (0)