CartToken.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Post;
  8. use ApiPlatform\Metadata\GraphQl\Mutation;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Webkul\BagistoApi\Dto\CartInput;
  11. use Webkul\BagistoApi\State\CartTokenMutationProvider;
  12. use Webkul\BagistoApi\State\CartTokenProcessor;
  13. /**
  14. * CartToken - GraphQL API Resource for Cart Operations
  15. *
  16. * Provides mutations and queries for shopping cart management with token-based authentication.
  17. * Supports both authenticated users and guest users via cart tokens.
  18. *
  19. * Operations:
  20. * - createCartToken: Create new guest cart with unique UUID token
  21. * - updateCartItem: Update cart item quantity
  22. * - removeCartItem: Remove item from cart
  23. * - readCart: Get single cart details
  24. * - cartCollection: Get all customer carts
  25. * - mergeGuestCart: Merge guest cart to customer cart on login
  26. * - applyCouponCode: Apply discount coupon
  27. */
  28. #[ApiResource(
  29. routePrefix: '/api/shop',
  30. shortName: 'CartToken',
  31. paginationEnabled: false,
  32. uriTemplate: '/cart-tokens/{id}',
  33. operations: [
  34. // new Post(
  35. // name: 'create',
  36. // uriTemplate: '/cart-tokens',
  37. // input: CartInput::class,
  38. // output: CartToken::class,
  39. // processor: CartTokenProcessor::class,
  40. // denormalizationContext: [
  41. // 'allow_extra_attributes' => true,
  42. // 'groups' => ['mutation'],
  43. // ],
  44. // normalizationContext: [
  45. // 'groups' => ['mutation'],
  46. // ],
  47. // description: 'Create new guest cart with unique UUID token or get authenticated customer cart.',
  48. // ),
  49. new GetCollection(uriTemplate: '/cart-tokens',
  50. provider: CartTokenMutationProvider::class,
  51. normalizationContext: [
  52. 'groups' => ['query'],
  53. ],
  54. description: 'Get all customer carts',
  55. ),
  56. new Get(uriTemplate: '/cart-tokens/{id}',
  57. provider: CartTokenMutationProvider::class,
  58. normalizationContext: [
  59. 'groups' => ['query'],
  60. ],
  61. description: 'Get single cart by ID',
  62. ),
  63. ],
  64. graphQlOperations: [
  65. new Mutation(
  66. name: 'create',
  67. input: CartInput::class,
  68. output: CartToken::class,
  69. provider: CartTokenMutationProvider::class,
  70. processor: CartTokenProcessor::class,
  71. denormalizationContext: [
  72. 'allow_extra_attributes' => true,
  73. 'groups' => ['mutation'],
  74. ],
  75. normalizationContext: [
  76. 'groups' => ['mutation'],
  77. ],
  78. description: 'Create new guest cart with unique UUID token or get authenticated customer cart. Returns sessionToken for guests.',
  79. ),
  80. ]
  81. )]
  82. class CartToken
  83. {
  84. #[ApiProperty(readable: true, writable: false)]
  85. #[Groups(['query', 'mutation'])]
  86. public ?int $id = null;
  87. #[ApiProperty(readable: true, writable: false)]
  88. #[Groups(['query', 'mutation'])]
  89. public ?string $cartToken = null;
  90. #[ApiProperty(readable: true, writable: false)]
  91. #[Groups(['query', 'mutation'])]
  92. public ?int $customerId = null;
  93. #[ApiProperty(readable: true, writable: false)]
  94. #[Groups(['query', 'mutation'])]
  95. public ?int $channelId = null;
  96. #[ApiProperty(readable: true, writable: false)]
  97. #[Groups(['query', 'mutation'])]
  98. public ?int $itemsCount = null;
  99. #[ApiProperty(readable: true, writable: false)]
  100. #[Groups(['query', 'mutation'])]
  101. public ?array $items = null;
  102. #[ApiProperty(readable: true, writable: false)]
  103. #[Groups(['query', 'mutation'])]
  104. public ?float $subtotal = null;
  105. #[ApiProperty(readable: true, writable: false)]
  106. #[Groups(['query', 'mutation'])]
  107. public ?float $baseSubtotal = null;
  108. #[ApiProperty(readable: true, writable: false)]
  109. #[Groups(['query', 'mutation'])]
  110. public ?float $discountAmount = null;
  111. #[ApiProperty(readable: true, writable: false)]
  112. #[Groups(['query', 'mutation'])]
  113. public ?float $baseDiscountAmount = null;
  114. #[ApiProperty(readable: true, writable: false)]
  115. #[Groups(['query', 'mutation'])]
  116. public ?float $taxAmount = null;
  117. #[ApiProperty(readable: true, writable: false)]
  118. #[Groups(['query', 'mutation'])]
  119. public ?float $baseTaxAmount = null;
  120. #[ApiProperty(readable: true, writable: false)]
  121. #[Groups(['query', 'mutation'])]
  122. public ?float $shippingAmount = null;
  123. #[ApiProperty(readable: true, writable: false)]
  124. #[Groups(['query', 'mutation'])]
  125. public ?float $baseShippingAmount = null;
  126. #[ApiProperty(readable: true, writable: false)]
  127. #[Groups(['query', 'mutation'])]
  128. public ?float $grandTotal = null;
  129. #[ApiProperty(readable: true, writable: false)]
  130. #[Groups(['query', 'mutation'])]
  131. public ?float $baseGrandTotal = null;
  132. #[ApiProperty(readable: true, writable: false)]
  133. #[Groups(['query', 'mutation'])]
  134. public ?string $formattedSubtotal = null;
  135. #[ApiProperty(readable: true, writable: false)]
  136. #[Groups(['query', 'mutation'])]
  137. public ?string $formattedDiscountAmount = null;
  138. #[ApiProperty(readable: true, writable: false)]
  139. #[Groups(['query', 'mutation'])]
  140. public ?string $formattedTaxAmount = null;
  141. #[ApiProperty(readable: true, writable: false)]
  142. #[Groups(['query', 'mutation'])]
  143. public ?string $formattedShippingAmount = null;
  144. #[ApiProperty(readable: true, writable: false)]
  145. #[Groups(['query', 'mutation'])]
  146. public ?string $formattedGrandTotal = null;
  147. #[ApiProperty(readable: true, writable: false)]
  148. #[Groups(['query', 'mutation'])]
  149. public ?string $couponCode = null;
  150. #[ApiProperty(readable: true, writable: false)]
  151. #[Groups(['query', 'mutation'])]
  152. public ?bool $success = null;
  153. #[ApiProperty(readable: true, writable: false)]
  154. #[Groups(['query', 'mutation'])]
  155. public ?string $message = null;
  156. #[ApiProperty(readable: true, writable: false)]
  157. #[Groups(['query', 'mutation'])]
  158. public ?array $carts = null;
  159. #[ApiProperty(readable: true, writable: false)]
  160. #[Groups(['query', 'mutation'])]
  161. public ?string $sessionToken = null;
  162. #[ApiProperty(readable: true, writable: false)]
  163. #[Groups(['query', 'mutation'])]
  164. public ?bool $isGuest = null;
  165. }