payment-helpers.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { ID } from '@vendure/common/lib/shared-types';
  2. import { ChannelService, OrderService, PaymentService, RequestContext } from '@vendure/core';
  3. import { SimpleGraphQLClient, TestServer } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import { REFUND_ORDER } from './graphql/admin-queries';
  6. import { RefundFragment, RefundOrderMutation, RefundOrderMutationVariables } from './graphql/generated-admin-types';
  7. import {
  8. GetShippingMethodsQuery,
  9. SetShippingMethodMutation,
  10. SetShippingMethodMutationVariables,
  11. TestOrderFragmentFragment,
  12. TransitionToStateMutation,
  13. TransitionToStateMutationVariables,
  14. } from './graphql/generated-shop-types';
  15. import {
  16. GET_ELIGIBLE_SHIPPING_METHODS,
  17. SET_SHIPPING_ADDRESS,
  18. SET_SHIPPING_METHOD,
  19. TRANSITION_TO_STATE,
  20. } from './graphql/shop-queries';
  21. export async function setShipping(shopClient: SimpleGraphQLClient): Promise<void> {
  22. await shopClient.query(SET_SHIPPING_ADDRESS, {
  23. input: {
  24. fullName: 'name',
  25. streetLine1: '12 the street',
  26. city: 'Leeuwarden',
  27. postalCode: '123456',
  28. countryCode: 'AT',
  29. },
  30. });
  31. const { eligibleShippingMethods } = await shopClient.query<GetShippingMethodsQuery>(
  32. GET_ELIGIBLE_SHIPPING_METHODS,
  33. );
  34. await shopClient.query<SetShippingMethodMutation, SetShippingMethodMutationVariables>(SET_SHIPPING_METHOD, {
  35. id: eligibleShippingMethods[1].id,
  36. });
  37. }
  38. export async function proceedToArrangingPayment(shopClient: SimpleGraphQLClient): Promise<ID> {
  39. await setShipping(shopClient);
  40. const { transitionOrderToState } = await shopClient.query<
  41. TransitionToStateMutation,
  42. TransitionToStateMutationVariables
  43. >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
  44. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  45. return (transitionOrderToState as TestOrderFragmentFragment)!.id;
  46. }
  47. export async function refundOrderLine(
  48. adminClient: SimpleGraphQLClient,
  49. orderLineId: string,
  50. quantity: number,
  51. paymentId: string,
  52. adjustment: number,
  53. ): Promise<RefundFragment> {
  54. const { refundOrder } = await adminClient.query<RefundOrderMutation, RefundOrderMutationVariables>(
  55. REFUND_ORDER,
  56. {
  57. input: {
  58. lines: [{ orderLineId, quantity }],
  59. shipping: 0,
  60. adjustment,
  61. paymentId,
  62. },
  63. },
  64. );
  65. return refundOrder as RefundFragment;
  66. }
  67. /**
  68. * Add a partial payment to an order. This happens, for example, when using Gift cards
  69. */
  70. export async function addManualPayment(server: TestServer, orderId: ID, amount: number): Promise<void> {
  71. const ctx = new RequestContext({
  72. apiType: 'admin',
  73. isAuthorized: true,
  74. authorizedAsOwnerOnly: false,
  75. channel: await server.app.get(ChannelService).getDefaultChannel(),
  76. });
  77. const order = await server.app.get(OrderService).findOne(ctx, orderId);
  78. // tslint:disable-next-line:no-non-null-assertion
  79. await server.app.get(PaymentService).createManualPayment(ctx, order!, amount, {
  80. method: 'Gift card',
  81. // tslint:disable-next-line:no-non-null-assertion
  82. orderId: order!.id,
  83. metadata: {
  84. bogus: 'test',
  85. },
  86. });
  87. }
  88. export const CREATE_MOLLIE_PAYMENT_INTENT = gql`
  89. mutation createMolliePaymentIntent($input: MolliePaymentIntentInput!) {
  90. createMolliePaymentIntent(input: $input) {
  91. ... on MolliePaymentIntent {
  92. url
  93. }
  94. ... on MolliePaymentIntentError {
  95. errorCode
  96. message
  97. }
  98. }
  99. }
  100. `;
  101. export const CREATE_STRIPE_PAYMENT_INTENT = gql`
  102. mutation createStripePaymentIntent{
  103. createStripePaymentIntent
  104. }`;
  105. export const GET_MOLLIE_PAYMENT_METHODS = gql`
  106. query molliePaymentMethods($input: MolliePaymentMethodsInput!) {
  107. molliePaymentMethods(input: $input) {
  108. id
  109. code
  110. description
  111. minimumAmount {
  112. value
  113. currency
  114. }
  115. maximumAmount {
  116. value
  117. currency
  118. }
  119. image {
  120. size1x
  121. size2x
  122. svg
  123. }
  124. }
  125. }
  126. `;