CheckoutAddress.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\Metadata\GraphQl\Mutation;
  8. use ApiPlatform\Metadata\GraphQl\Query;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use ApiPlatform\Metadata\Post;
  11. use Webkul\BagistoApi\Dto\CheckoutAddressInput;
  12. use Webkul\BagistoApi\Dto\CheckoutAddressOutput;
  13. use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
  14. use Webkul\BagistoApi\State\CheckoutAddressProvider;
  15. use Webkul\BagistoApi\State\CheckoutProcessor;
  16. use ApiPlatform\OpenApi\Model\Operation;
  17. /**
  18. * CheckoutAddress - GraphQL API Resource for Checkout Address
  19. *
  20. * Provides mutation for saving billing and shipping addresses during checkout
  21. */
  22. #[ApiResource(
  23. routePrefix: '/api/shop',
  24. shortName: 'CheckoutAddress',
  25. uriTemplate: '/checkout-addresses',
  26. operations: [
  27. new Get(
  28. uriTemplate: '/checkout-addresses/{id}',
  29. openapi: new Operation(tags: ['Customer Order']),
  30. ),
  31. new GetCollection(
  32. uriTemplate: '/checkout-addresses',
  33. openapi: new Operation(tags: ['Customer Order']),
  34. ),
  35. new Post(
  36. uriTemplate: '/checkout-addresses',
  37. output: CheckoutAddressOutput::class,
  38. processor: CheckoutProcessor::class,
  39. normalizationContext: [
  40. 'groups' => ['mutation'],
  41. 'skip_null_values' => false,
  42. ],
  43. denormalizationContext: [
  44. 'allow_extra_attributes' => true,
  45. 'groups' => ['mutation'],
  46. ],
  47. openapi: new Operation(
  48. tags: ['Checkout'],
  49. summary: 'Save billing and shipping addresses for checkout',
  50. description: 'Saves billing and shipping addresses to the current cart. Use `useForShipping: true` to copy billing as shipping, or provide shipping fields for a different shipping address.',
  51. requestBody: new \ApiPlatform\OpenApi\Model\RequestBody(
  52. required: true,
  53. content: new \ArrayObject([
  54. 'application/json' => [
  55. 'schema' => [
  56. 'type' => 'object',
  57. 'required' => [
  58. 'billingFirstName',
  59. 'billingLastName',
  60. 'billingEmail',
  61. 'billingAddress',
  62. 'billingCity',
  63. 'billingCountry',
  64. 'billingState',
  65. 'billingPostcode',
  66. 'billingPhoneNumber',
  67. ],
  68. 'properties' => [
  69. 'billingFirstName' => ['type' => 'string', 'example' => 'John'],
  70. 'billingLastName' => ['type' => 'string', 'example' => 'Doe'],
  71. 'billingEmail' => ['type' => 'string', 'example' => 'john@example.com'],
  72. 'billingCompanyName' => ['type' => 'string', 'example' => ''],
  73. 'billingAddress' => ['type' => 'string', 'example' => '123 Main St'],
  74. 'billingCity' => ['type' => 'string', 'example' => 'Los Angeles'],
  75. 'billingCountry' => ['type' => 'string', 'example' => 'US'],
  76. 'billingState' => ['type' => 'string', 'example' => 'CA'],
  77. 'billingPostcode' => ['type' => 'string', 'example' => '90001'],
  78. 'billingPhoneNumber' => ['type' => 'string', 'example' => '2125551234'],
  79. 'useForShipping' => ['type' => 'boolean', 'example' => true],
  80. 'shippingFirstName' => ['type' => 'string', 'example' => 'Jane'],
  81. 'shippingLastName' => ['type' => 'string', 'example' => 'Doe'],
  82. 'shippingEmail' => ['type' => 'string', 'example' => 'jane@example.com'],
  83. 'shippingCompanyName' => ['type' => 'string', 'example' => ''],
  84. 'shippingAddress' => ['type' => 'string', 'example' => '456 Oak Ave'],
  85. 'shippingCity' => ['type' => 'string', 'example' => 'San Francisco'],
  86. 'shippingCountry' => ['type' => 'string', 'example' => 'US'],
  87. 'shippingState' => ['type' => 'string', 'example' => 'CA'],
  88. 'shippingPostcode' => ['type' => 'string', 'example' => '94102'],
  89. 'shippingPhoneNumber' => ['type' => 'string', 'example' => '4155559876'],
  90. ],
  91. ],
  92. ],
  93. ]),
  94. ),
  95. ),
  96. ),
  97. ],
  98. graphQlOperations: [
  99. new Query(
  100. name: 'read',
  101. output: CheckoutAddressOutput::class,
  102. provider: CheckoutAddressProvider::class,
  103. resolver: BaseQueryItemResolver::class,
  104. normalizationContext: [
  105. 'groups' => ['query'],
  106. ],
  107. description: 'Get billing and shipping addresses for a cart by token',
  108. ),
  109. new Mutation(
  110. name: 'create',
  111. input: CheckoutAddressInput::class,
  112. output: CheckoutAddressOutput::class,
  113. processor: CheckoutProcessor::class,
  114. denormalizationContext: [
  115. 'allow_extra_attributes' => true,
  116. 'groups' => ['mutation'],
  117. ],
  118. normalizationContext: [
  119. 'groups' => ['mutation'],
  120. ],
  121. description: 'Save billing and shipping addresses for checkout. Returns the created address.',
  122. ),
  123. ]
  124. )]
  125. class CheckoutAddress
  126. {
  127. #[ApiProperty(readable: true, writable: false)]
  128. #[Groups(['query', 'mutation'])]
  129. public ?int $id = null;
  130. #[ApiProperty(readable: true, writable: false)]
  131. #[Groups(['query', 'mutation'])]
  132. public ?string $cartToken = null;
  133. }