Skip to content

Commit 45251b5

Browse files
committed
Issue #64 lower PHP requirement a notch
1 parent 4010f82 commit 45251b5

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ $client = new Client();
122122
// on which is installed. You can explictly create the PSR-7 factory instead and pass that in
123123
// as a third parameter when creating Request\CreateSessionKey.
124124

125-
$keyResponse = $client->sendRequest($keyRequest->message());
125+
$keyResponse = $client->sendRequest($keyRequest->createHttpRequest());
126126

127127
// Capture the result in our local response model.
128128
// Use the ResponseFactory to automatically choose the correct message class.
@@ -169,7 +169,7 @@ $cardIdentifierRequest = new CreateCardIdentifier(
169169
// Send the PSR-7 message.
170170
// The same error handling as shown earlier can be used.
171171

172-
$cardIdentifierResponse = $client->sendRequest($cardIdentifierRequest->message());
172+
$cardIdentifierResponse = $client->sendRequest($cardIdentifierRequest->createHttpRequest());
173173

174174
// Grab the result as a local model.
175175
// If all is well, we will have a Resposne\CardIdentifier that will be valid for use
@@ -293,7 +293,7 @@ $paymentRequest = new CreatePayment(
293293

294294
// Send it to Sage Pay.
295295

296-
$paymentResponse = $client->sendRequest($paymentRequest->message());
296+
$paymentResponse = $client->sendRequest($paymentRequest->createHttpRequest());
297297

298298
// Assuming we got no exceptions, extract the response details.
299299

@@ -348,7 +348,7 @@ $transaction_result = new Request\FetchTransaction(
348348

349349
// Send it to Sage Pay.
350350

351-
$response = $client->sendRequest($transaction_result->message());
351+
$response = $client->sendRequest($transaction_result->createHttpRequest());
352352

353353
// Assuming no exceptions, this gives you the payment or repeat payment record.
354354
// But do check for errors in the usual way (i.e. you could get an error collection here).
@@ -514,7 +514,7 @@ Handling the 3D Secure result involves two steps:
514514

515515
// Send to Sage Pay and get the final 3D Secure result.
516516

517-
$response = $client->send($request->message());
517+
$response = $client->send($request->createHttpRequest());
518518
$secure3dResponse = ResponseFactory::fromHttpResponse($response);
519519

520520
// This will be the result. We are looking for `Authenticated` or similar.
@@ -560,7 +560,7 @@ reported as still being an issue.
560560

561561
// Send the request for the transaction to Sage Pay.
562562

563-
$response = $client->sendRequest($transactionResult->message());
563+
$response = $client->sendRequest($transactionResult->createHttpRequest());
564564

565565
// We should now have the payment, repeat payment, or an error collection.
566566

@@ -610,7 +610,7 @@ $securityCode = new LinkSecurityCode(
610610
// The result will be a `Response\NoContent` if all is well.
611611

612612
$securityCodeResponse = ResponseFactory::fromHttpResponse(
613-
$client->sendRequest($securityCode->message())
613+
$client->sendRequest($securityCode->createHttpRequest())
614614
);
615615

616616
// Should check for errors here:

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "academe/opayo-pi",
33
"license": "MIT",
44
"type": "library",
5-
"description": "A library to handle the message content and structure for the Opayo Pi (ne Sage Pay Integration) payment gateway",
5+
"description": "A library to handle the message content and structure for the Opayo Pi (was Sage Pay Integration) payment gateway",
66
"keywords": [
77
"gateway",
88
"merchant",
@@ -19,10 +19,10 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^7.4|^8.0",
22+
"php": "^7.3|^8.0",
2323
"shrikeh/teapot": "^1.0|^2.0",
2424
"psr/http-message": "^1.0",
25-
"alcohol/iso4217": "^4.0"
25+
"alcohol/iso4217": "^3.0|^4.0"
2626
},
2727
"suggest": {
2828
"moneyphp/money": "^3.0",

src/Factory/DiactorosFactory.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/**
66
* Zend Diactoros Factory for creating PSR-7 objects.
77
* Requires zendframework/zend-diactoros:~1.3
8+
*
9+
* @deprecated 3.0.0 abandoned some time ago https://github.com/zendframework/zend-diactoros
810
*/
911

1012
use Psr\Http\Message\StreamInterface;
@@ -27,7 +29,7 @@ class DiactorosFactory implements RequestFactoryInterface
2729
public function jsonRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
2830
{
2931
// If we are sending a JSON body, then the recipient needs to know.
30-
$headers['Content-type'] = 'application/json';
32+
$headers['Content-Type'] = 'application/json';
3133

3234
// If the body is not already a stream or string of some sort, then JSON encode it for streaming.
3335
if (! is_string($body) && ! $body instanceof StreamInterface && gettype($body) != 'resource') {

src/Factory/GuzzleFactory.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/**
66
* Guzzle Factory for creating PSR-7 objects.
77
* Requires guzzlehttp/guzzle:~6.0
8+
*
9+
* @deprecated use any PSR-17 factory instead
810
*/
911

1012
use Psr\Http\Message\StreamInterface;
@@ -17,6 +19,7 @@ class GuzzleFactory implements RequestFactoryInterface
1719
/**
1820
* Return a new GuzzleHttp\Psr7\Request object.
1921
* The body is to be sent as a JSON request.
22+
*
2023
* @param null|string $method
2124
* @param UriInterface|null|string $uri
2225
* @param array $headers
@@ -27,15 +30,18 @@ class GuzzleFactory implements RequestFactoryInterface
2730
public function jsonRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
2831
{
2932
// If we are sending a JSON body, then the recipient needs to know.
30-
$headers['Content-type'] = 'application/json';
33+
34+
$headers['Content-Type'] = 'application/json';
3135

3236
// If the body is already a stream or string of some sort, then it is
3337
// assumed to already be a JSON stream.
38+
3439
if (! is_string($body) && ! $body instanceof StreamInterface && gettype($body) != 'resource') {
3540
$body = json_encode($body);
3641
}
3742

3843
// Guzzle will accept the body as a string and generate a stream from it.
44+
3945
return new Request(
4046
$method,
4147
$uri,

src/Request/AbstractRequest.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
use Academe\Opayo\Pi\Factory\FactoryInterface;
1414
use Academe\Opayo\Pi\Factory\DiactorosFactory;
1515
use Academe\Opayo\Pi\Factory\GuzzleFactory;
16+
use Academe\Opayo\Pi\Factory\RequestFactoryInterface;
1617
use UnexpectedValueException;
1718
use JsonSerializable;
1819
use Exception;
20+
// use Psr\Http\Message\RequestFactoryInterface;
21+
use Psr\Http\Message\RequestInterface;
1922

2023
abstract class AbstractRequest extends AbstractMessage implements JsonSerializable
2124
{
@@ -36,7 +39,7 @@ abstract class AbstractRequest extends AbstractMessage implements JsonSerializab
3639
protected $resource_path = [];
3740

3841
/**
39-
* @var string Most messages are sent as POST requests, so this is the default
42+
* @var string Most messages are sent with the POST method, so this is the default
4043
*/
4144
protected $method = 'POST';
4245

@@ -122,7 +125,7 @@ public function getResourcePath()
122125
}
123126

124127
/**
125-
* @returns string The full URL of this resource
128+
* @returns string The fully qualified URL of this resource
126129
*/
127130
public function getUrl()
128131
{
@@ -163,6 +166,9 @@ protected function getBasicAuthHeaders()
163166
}
164167

165168
/**
169+
* TODO: can we use the PSR-17 Psr\Http\Message\RequestFactoryInterface
170+
* instead of our custom factory?
171+
*
166172
* @param RequestFactoryInterface $factory
167173
* @return $this
168174
*/
@@ -185,11 +191,12 @@ protected function withFactory(RequestFactoryInterface $factory)
185191
/**
186192
* Get the PSR-7 factory.
187193
* Create a factory if none supplied and relevant libraries are installed.
194+
*
188195
* @param bool $exception
189-
* @return DiactorosFactory|GuzzleFactory
196+
* @return RequestFactoryInterface for example DiactorosFactory or GuzzleFactory
190197
* @throws Exception
191198
*/
192-
public function getFactory($exception = false)
199+
public function getFactory($exception = false): RequestFactoryInterface
193200
{
194201
if (!isset($this->factory) && GuzzleFactory::isSupported()) {
195202
// If the GuzzleFactory is supported (relevant Guzzle package is
@@ -218,13 +225,17 @@ public function getFactory($exception = false)
218225

219226
/**
220227
* Return as a PSR-7 request message.
228+
* TODO: Use a PSR-17 factory to create the basic request, then add the
229+
* headers and body to that.
230+
*
221231
* @return \Psr\Http\Message\RequestInterface
222232
* @throws Exception
223233
*/
224-
public function createHttpRequest()
234+
public function createHttpRequest(): RequestInterface
225235
{
226236
// If the data is protected from accidental serialisation, then
227237
// pull it out through the protected method.
238+
228239
if (method_exists($this, 'jsonSerializePeek')) {
229240
$body = json_encode($this->jsonSerializePeek());
230241
} else {
@@ -239,16 +250,9 @@ public function createHttpRequest()
239250
);
240251
}
241252

242-
/**
243-
* @deprecated Use more appropriately named createHttpRequest()
244-
*/
245-
public function message()
246-
{
247-
return $this->createHttpRequest();
248-
}
249-
250253
/**
251254
* Set various flags - anything with a setFoo() method.
255+
*
252256
* @param array $options
253257
* @return $this
254258
*/
@@ -270,6 +274,7 @@ protected function setOptions(array $options = [])
270274

271275
/**
272276
* Set various flags - anything with a setFoo() method.
277+
*
273278
* @param array $options
274279
* @return AbstractRequest
275280
*/

0 commit comments

Comments
 (0)