Skip to content

Commit ad2745b

Browse files
committed
wip
1 parent 1cbb525 commit ad2745b

File tree

3 files changed

+38
-113
lines changed

3 files changed

+38
-113
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,23 @@ This provides an OpenAPI schema and an auto-generated PHP client as a composer p
2424

2525
```php
2626
$client = new Cone\SimplePay\Client('MERCHANT', 'SECRET_KEY');
27+
28+
$client->api()->start(...);
29+
$client->api()->finish(...);
30+
$client->api()->refund(...);
31+
$client->api()->query(...);
32+
$client->api()->do(...);
33+
$client->api()->transactionCancel(...);
34+
$client->api()->doRecurring(...);
35+
$client->api()->cardQuery(...);
36+
$client->api()->cardCancel(...);
37+
$client->api()->tokenQuery(...);
38+
$client->api()->tokenCancel(...);
39+
$client->api()->startEam(...);
40+
$client->api()->startApplePay(...);
41+
$client->api()->doApplePay(...);
2742
```
43+
44+
> [!NOTE]
45+
> The client automatically adds the `merchant`, `salt` and `sdkVersion` parameters to the body as well as the `Signature` header to the request.
46+

openapi.json

Lines changed: 9 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
"summary": "Start a transaction",
3131
"description": "Start a transaction using the given parameters",
3232
"operationId": "start",
33-
"parameters": [
34-
{
35-
"$ref": "#/components/parameters/Signature"
36-
}
37-
],
3833
"requestBody": {
3934
"description": "The transaction object you would like to start.",
4035
"required": true,
@@ -112,11 +107,6 @@
112107
"summary": "Finish a two-step transaction",
113108
"description": "Finish an authorized two-step transaction.",
114109
"operationId": "finish",
115-
"parameters": [
116-
{
117-
"$ref": "#/components/parameters/Signature"
118-
}
119-
],
120110
"requestBody": {
121111
"description": "The authorized transaction object you would like to finish.",
122112
"required": true,
@@ -177,11 +167,6 @@
177167
"summary": "Refund a transaction",
178168
"description": "Refund a transaction fully or partially.",
179169
"operationId": "refund",
180-
"parameters": [
181-
{
182-
"$ref": "#/components/parameters/Signature"
183-
}
184-
],
185170
"requestBody": {
186171
"description": "The refund object.",
187172
"required": true,
@@ -249,11 +234,6 @@
249234
"summary": "Query a set transactions",
250235
"description": "Get the given transactions.",
251236
"operationId": "query",
252-
"parameters": [
253-
{
254-
"$ref": "#/components/parameters/Signature"
255-
}
256-
],
257237
"requestBody": {
258238
"description": "The query parameters.",
259239
"required": true,
@@ -449,12 +429,7 @@
449429
],
450430
"summary": "Cancel a transaction",
451431
"description": "Cancel an init-ed transaction.",
452-
"operationId": "transactioncancel",
453-
"parameters": [
454-
{
455-
"$ref": "#/components/parameters/Signature"
456-
}
457-
],
432+
"operationId": "transactionCancel",
458433
"requestBody": {
459434
"description": "The cancel object.",
460435
"required": true,
@@ -511,11 +486,6 @@
511486
"summary": "Do a one-click transaction",
512487
"description": "Do a one-click transaction with a saved card",
513488
"operationId": "do",
514-
"parameters": [
515-
{
516-
"$ref": "#/components/parameters/Signature"
517-
}
518-
],
519489
"requestBody": {
520490
"description": "The transaction object you would init.",
521491
"required": true,
@@ -580,12 +550,7 @@
580550
],
581551
"summary": "Do a recurring transaction",
582552
"description": "Do a recurring transaction with a saved token",
583-
"operationId": "dorecurring",
584-
"parameters": [
585-
{
586-
"$ref": "#/components/parameters/Signature"
587-
}
588-
],
553+
"operationId": "doRecurring",
589554
"requestBody": {
590555
"description": "The transaction object you would init.",
591556
"required": true,
@@ -645,12 +610,7 @@
645610
],
646611
"summary": "Query a saved card",
647612
"description": "Get the info about a saved bankcard",
648-
"operationId": "cardquery",
649-
"parameters": [
650-
{
651-
"$ref": "#/components/parameters/Signature"
652-
}
653-
],
613+
"operationId": "cardQuery",
654614
"requestBody": {
655615
"description": "The card object you would query.",
656616
"required": true,
@@ -735,12 +695,7 @@
735695
],
736696
"summary": "Cancel a saved card",
737697
"description": "Cancel a saved bankcard",
738-
"operationId": "cardcancel",
739-
"parameters": [
740-
{
741-
"$ref": "#/components/parameters/Signature"
742-
}
743-
],
698+
"operationId": "cardCancel",
744699
"requestBody": {
745700
"description": "The card object you would cancel.",
746701
"required": true,
@@ -798,12 +753,7 @@
798753
],
799754
"summary": "Query a saved token",
800755
"description": "Get the info about a saved token",
801-
"operationId": "tokenquery",
802-
"parameters": [
803-
{
804-
"$ref": "#/components/parameters/Signature"
805-
}
806-
],
756+
"operationId": "tokenQuery",
807757
"requestBody": {
808758
"description": "The token object you would query.",
809759
"required": true,
@@ -861,12 +811,7 @@
861811
],
862812
"summary": "Cancel a saved token",
863813
"description": "Cancel a saved token",
864-
"operationId": "tokencancel",
865-
"parameters": [
866-
{
867-
"$ref": "#/components/parameters/Signature"
868-
}
869-
],
814+
"operationId": "tokenCancel",
870815
"requestBody": {
871816
"description": "The token object you would cancel.",
872817
"required": true,
@@ -924,12 +869,7 @@
924869
],
925870
"summary": "Start an EAM transaction",
926871
"description": "Start an EAM transaction using the given parameters",
927-
"operationId": "starteam",
928-
"parameters": [
929-
{
930-
"$ref": "#/components/parameters/Signature"
931-
}
932-
],
872+
"operationId": "startEam",
933873
"requestBody": {
934874
"description": "The EAM transaction object you would like to start.",
935875
"required": true,
@@ -1006,12 +946,7 @@
1006946
],
1007947
"summary": "Start an ApplePay session",
1008948
"description": "Start an ApplePay session using the given parameters",
1009-
"operationId": "startapplepay",
1010-
"parameters": [
1011-
{
1012-
"$ref": "#/components/parameters/Signature"
1013-
}
1014-
],
949+
"operationId": "startApplePay",
1015950
"requestBody": {
1016951
"description": "The ApplePay session object you would like to start.",
1017952
"required": true,
@@ -1123,12 +1058,7 @@
11231058
],
11241059
"summary": "Start an ApplePay transaction",
11251060
"description": "Start an ApplePay transaction using the given parameters",
1126-
"operationId": "doapplepay",
1127-
"parameters": [
1128-
{
1129-
"$ref": "#/components/parameters/Signature"
1130-
}
1131-
],
1061+
"operationId": "doApplePay",
11321062
"requestBody": {
11331063
"description": "The ApplePay transaction object you would like to start.",
11341064
"required": true,
@@ -1195,18 +1125,6 @@
11951125
}
11961126
}
11971127
},
1198-
"parameters": {
1199-
"Signature": {
1200-
"name": "Signature",
1201-
"in": "header",
1202-
"description": "The signature.",
1203-
"required": true,
1204-
"schema": {
1205-
"type": "string",
1206-
"example": "3WGqCnWJArhA224xVdUY1fPh91tpd6va6JvBrPNuHK449TZTgsRn3DBu5UBGbcTn"
1207-
}
1208-
}
1209-
},
12101128
"schemas": {
12111129
"Transaction": {
12121130
"required": [

src/Client.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Client
2323
/**
2424
* The transaction API instance.
2525
*/
26-
protected ?TransactionApi $transactions = null;
26+
protected ?TransactionApi $api = null;
2727

2828
/**
2929
* Create a new SimplePay Client instance.
@@ -43,33 +43,25 @@ public function __construct(
4343
/**
4444
* Sign the given data.
4545
*/
46-
public function sign(string $data): string
46+
protected function sign(string $data): string
4747
{
4848
return base64_encode(
4949
hash_hmac('sha384', $data, $this->secretKey, true)
5050
);
5151
}
5252

53-
/**
54-
* Validate the signature.
55-
*/
56-
public function validateSignature(string $hash, string $data): bool
57-
{
58-
return hash_equals($hash, $this->sign($data));
59-
}
60-
6153
/**
6254
* Get the configuration instance.
6355
*/
64-
public function config(): Configuration
56+
protected function config(): Configuration
6557
{
6658
return Configuration::getDefaultConfiguration();
6759
}
6860

6961
/**
7062
* Create a new Guzzle Client instance.
7163
*/
72-
public function client(): ClientInterface
64+
protected function client(): ClientInterface
7365
{
7466
$stack = HandlerStack::create();
7567

@@ -97,7 +89,7 @@ public function client(): ClientInterface
9789
$stack->push(Middleware::mapResponse(function (ResponseInterface $response): ResponseInterface {
9890
$body = (string) $response->getBody();
9991

100-
if (! $this->validateSignature($response->getHeader('Signature')[0] ?? '', $body)) {
92+
if (! hash_equals($response->getHeader('Signature')[0] ?? '', $this->sign($body))) {
10193
throw new ApiException('Invalid Signature.', 999, $response->getHeaders(), $response->getBody());
10294
}
10395

@@ -123,15 +115,11 @@ public function client(): ClientInterface
123115
/**
124116
* Get the transaction API.
125117
*/
126-
public function transactions(): TransactionApi
118+
public function api(): TransactionApi
127119
{
128-
if (is_null($this->transactions)) {
129-
$this->transactions = new TransactionApi(
130-
$this->client(),
131-
$this->config()
132-
);
133-
}
134-
135-
return $this->transactions;
120+
return $this->api ??= new TransactionApi(
121+
$this->client(),
122+
$this->config()
123+
);
136124
}
137125
}

0 commit comments

Comments
 (0)