payment-helpers.ts 4.0 KB

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