Skip to content

Commit 797699a

Browse files
committed
add live and test endpoints to backend request
1 parent 96d3f19 commit 797699a

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/Messages/BackendPurchaseRequest.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class BackendPurchaseRequest extends AbstractRequest
88
{
9+
protected $liveEndpoint = 'https://gw.every-pay.eu';
10+
protected $testEndpoint = 'https://gw-demo.every-pay.com';
11+
912
public function getData()
1013
{
1114
$data = $this->getBaseData();
@@ -35,24 +38,42 @@ public function setEmail($value)
3538
return $this->setParameter('email', $value);
3639
}
3740

38-
public function sendData($data)
41+
protected function signData($envelope, $data)
3942
{
40-
// TODO: Production
41-
// $endpoint = 'https://gw.every-pay.eu/';
42-
$endpoint = 'https://gw-demo.every-pay.com/';
43-
44-
$payload = [
45-
'charge' => SignedData::make(
43+
return [
44+
$envelope => SignedData::make(
4645
$data,
4746
SignedDataOptions::backend($this->getSecret())
4847
)
4948
];
49+
}
5050

51-
$response = $this->httpClient->request('POST', sprintf('%s%s', $endpoint, 'charges'), [
51+
protected function createResponse($response)
52+
{
53+
$status = $response->getStatusCode();
54+
$body = $response->getBody()->getContents();
55+
$body = @json_decode($body, true);
56+
57+
$data = compact('status', 'body');
58+
return $this->response = new BackendPurchaseResponse($this, $data);
59+
}
60+
61+
public function sendData($data)
62+
{
63+
$headers = [
5264
'Accept' => 'application/json',
5365
'Content-Type' => 'application/json',
54-
], json_encode($payload));
66+
];
67+
68+
$data = $this->signData('charge', $data);
69+
70+
$response = $this->httpClient->request(
71+
'POST',
72+
$this->getEndpoint() . '/charges',
73+
$headers,
74+
json_encode($data)
75+
);
5576

56-
return $this->response = new BackendPurchaseResponse($this, $response->getBody()->getContents());
77+
return $this->createResponse($response);
5778
}
5879
}

src/Messages/BackendPurchaseResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class BackendPurchaseResponse extends AbstractResponse implements RedirectRespon
1717

1818
public function __construct(RequestInterface $request, $data)
1919
{
20+
$this->data = $data;
2021
$this->request = $request;
21-
$this->data = @json_decode($data, true);
2222
}
2323

2424
public function isSuccessful()
2525
{
26-
$response = $this->data;
26+
$response = $this->data['body'];
2727

2828
if (!is_array($response) || (!isset($response['errors']) && !isset($response['charge']))) {
2929
$this->message = 'Unrecognized response format';
@@ -57,7 +57,7 @@ public function isSuccessful()
5757

5858
public function getTransactionReference()
5959
{
60-
return $this->data['charge']['payment_reference'];
60+
return $this->data['body']['charge']['payment_reference'];
6161
}
6262

6363
public function isRedirect()

0 commit comments

Comments
 (0)