Skip to content

Commit f2df190

Browse files
[API] Add custom payment-related endpoints to open api docs
1 parent 7a305f8 commit f2df190

File tree

4 files changed

+185
-3
lines changed

4 files changed

+185
-3
lines changed

config/services/api.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@
5858
<argument type="service" id="sylius_abstraction.state_machine" />
5959
<argument type="service" id="sylius_mollie.logger.mollie_logger_action" />
6060
</service>
61+
62+
<service id="sylius_mollie.api.open_api.mollie_documentation_modifier" class="Sylius\MolliePlugin\Api\OpenApi\MollieDocumentationModifier">
63+
<argument>%sylius.security.api_shop_route%</argument>
64+
65+
<tag name="sylius.open_api.modifier" />
66+
</service>
6167
</services>
6268
</container>

src/Api/Controller/GetMollieStatusAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public function __invoke(string $tokenValue): JsonResponse
7979
}
8080

8181
return new JsonResponse([
82-
'orderNumber' => $order->getNumber(),
8382
'orderToken' => $tokenValue,
8483
'paymentState' => $payment->getState(),
8584
]);

src/Api/Controller/SelectMollieMethodAction.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ public function __invoke(string $tokenValue, Request $request): JsonResponse
164164
$this->entityManager->flush();
165165

166166
return new JsonResponse([
167-
'success' => true,
168167
'methodId' => $methodId,
169168
'checkoutUrl' => $checkoutUrl,
170-
'paymentId' => $molliePayment->id,
171169
]);
172170
}
173171

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius Mollie Plugin package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\MolliePlugin\Api\OpenApi;
15+
16+
use ApiPlatform\OpenApi\Model\Operation;
17+
use ApiPlatform\OpenApi\Model\Parameter;
18+
use ApiPlatform\OpenApi\Model\PathItem;
19+
use ApiPlatform\OpenApi\Model\Paths;
20+
use ApiPlatform\OpenApi\Model\RequestBody;
21+
use ApiPlatform\OpenApi\Model\Response as ResponseModel;
22+
use ApiPlatform\OpenApi\OpenApi;
23+
use Sylius\Bundle\ApiBundle\OpenApi\Documentation\DocumentationModifierInterface;
24+
use Symfony\Component\HttpFoundation\Response;
25+
26+
final readonly class MollieDocumentationModifier implements DocumentationModifierInterface
27+
{
28+
public function __construct(
29+
private string $shopApiRoute,
30+
) {
31+
}
32+
33+
public function modify(OpenApi $docs): OpenApi
34+
{
35+
$paths = $docs->getPaths();
36+
$schemas = $docs->getComponents()->getSchemas();
37+
38+
$schemas = $this->addSchemas($schemas);
39+
$this->addPaths($paths);
40+
41+
return $docs
42+
->withPaths($paths)
43+
->withComponents($docs->getComponents()->withSchemas($schemas))
44+
;
45+
}
46+
47+
private function addPaths(Paths $paths): void
48+
{
49+
$tokenParameter = new Parameter(
50+
name: 'tokenValue',
51+
in: 'path',
52+
description: 'The token of the order',
53+
required: true,
54+
schema: ['type' => 'string'],
55+
);
56+
57+
$methodsPath = sprintf('%s/orders/{tokenValue}/mollie-methods', $this->shopApiRoute);
58+
$paths->addPath($methodsPath, new PathItem(
59+
ref: 'MollieMethods',
60+
get: new Operation(
61+
operationId: 'sylius_mollie_api_shop_order_mollie_methods',
62+
tags: ['Mollie'],
63+
responses: [
64+
Response::HTTP_OK => new ResponseModel(
65+
description: 'List of available Mollie payment methods',
66+
content: new \ArrayObject([
67+
'application/json' => [
68+
'schema' => [
69+
'type' => 'array',
70+
'items' => ['$ref' => '#/components/schemas/MollieMethod'],
71+
],
72+
],
73+
]),
74+
),
75+
],
76+
summary: 'Get available Mollie payment methods for an order',
77+
parameters: [$tokenParameter],
78+
),
79+
post: new Operation(
80+
operationId: 'sylius_mollie_api_shop_order_mollie_select_method',
81+
tags: ['Mollie'],
82+
responses: [
83+
Response::HTTP_OK => new ResponseModel(
84+
description: 'Mollie payment created successfully',
85+
content: new \ArrayObject([
86+
'application/json' => [
87+
'schema' => ['$ref' => '#/components/schemas/MollieSelectMethodResponse'],
88+
],
89+
]),
90+
),
91+
],
92+
summary: 'Select a Mollie payment method and create a payment',
93+
parameters: [$tokenParameter],
94+
requestBody: new RequestBody(
95+
content: new \ArrayObject([
96+
'application/json' => [
97+
'schema' => ['$ref' => '#/components/schemas/MollieSelectMethodRequest'],
98+
],
99+
]),
100+
required: true,
101+
),
102+
),
103+
));
104+
105+
$statusPath = sprintf('%s/orders/{tokenValue}/mollie-status', $this->shopApiRoute);
106+
$paths->addPath($statusPath, new PathItem(
107+
ref: 'MolliePaymentStatus',
108+
get: new Operation(
109+
operationId: 'sylius_mollie_api_shop_order_mollie_payment_status',
110+
tags: ['Mollie'],
111+
responses: [
112+
Response::HTTP_OK => new ResponseModel(
113+
description: 'Current payment status',
114+
content: new \ArrayObject([
115+
'application/json' => [
116+
'schema' => ['$ref' => '#/components/schemas/MolliePaymentStatus'],
117+
],
118+
]),
119+
),
120+
],
121+
summary: 'Get Mollie payment status and update Sylius payment state',
122+
parameters: [$tokenParameter],
123+
),
124+
));
125+
}
126+
127+
/**
128+
* @param array<string, mixed>|\ArrayObject<string, mixed> $schemas
129+
*
130+
* @return array<string, mixed>|\ArrayObject<string, mixed>
131+
*/
132+
private function addSchemas(array|\ArrayObject $schemas): array|\ArrayObject
133+
{
134+
$schemas['MollieMethod'] = [
135+
'type' => 'object',
136+
'properties' => [
137+
'id' => ['type' => 'string', 'example' => 'klarna'],
138+
'label' => ['type' => 'string', 'example' => 'Klarna'],
139+
'image' => ['type' => 'string', 'nullable' => true, 'example' => 'https://www.mollie.com/external/icons/payment-methods/klarna.svg'],
140+
'paymentFee' => [
141+
'type' => 'object',
142+
'nullable' => true,
143+
'properties' => [
144+
'type' => ['type' => 'string', 'nullable' => true],
145+
'fixedAmount' => ['type' => 'number', 'nullable' => true],
146+
'percentage' => ['type' => 'number', 'nullable' => true],
147+
'surchargeLimit' => ['type' => 'number', 'nullable' => true],
148+
],
149+
],
150+
],
151+
];
152+
153+
$schemas['MollieSelectMethodRequest'] = [
154+
'type' => 'object',
155+
'required' => ['methodId'],
156+
'properties' => [
157+
'methodId' => ['type' => 'string', 'example' => 'ideal'],
158+
],
159+
];
160+
161+
$schemas['MollieSelectMethodResponse'] = [
162+
'type' => 'object',
163+
'properties' => [
164+
'methodId' => ['type' => 'string'],
165+
'checkoutUrl' => ['type' => 'string'],
166+
],
167+
];
168+
169+
$schemas['MolliePaymentStatus'] = [
170+
'type' => 'object',
171+
'properties' => [
172+
'orderToken' => ['type' => 'string'],
173+
'paymentState' => ['type' => 'string'],
174+
],
175+
];
176+
177+
return $schemas;
178+
}
179+
}

0 commit comments

Comments
 (0)