|
| 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