test-payment-methods.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { Payment, PaymentMethodHandler, TransactionalConnection } from '@vendure/core';
  2. import { vi } from 'vitest';
  3. import { LanguageCode } from '../graphql/generated-e2e-admin-types';
  4. export const testSuccessfulPaymentMethod = new PaymentMethodHandler({
  5. code: 'test-payment-method',
  6. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  7. args: {},
  8. createPayment: (ctx, order, amount, args, metadata) => {
  9. return {
  10. amount,
  11. state: 'Settled',
  12. transactionId: '12345',
  13. metadata: { public: metadata },
  14. };
  15. },
  16. settlePayment: () => ({
  17. success: true,
  18. }),
  19. });
  20. export const onTransitionSpy = vi.fn();
  21. export const onCancelPaymentSpy = vi.fn();
  22. /**
  23. * A two-stage (authorize, capture) payment method, with no createRefund method.
  24. */
  25. export const twoStagePaymentMethod = new PaymentMethodHandler({
  26. code: 'authorize-only-payment-method',
  27. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  28. args: {},
  29. createPayment: (ctx, order, amount, args, metadata) => {
  30. return {
  31. amount,
  32. state: 'Authorized',
  33. transactionId: '12345-' + order.code,
  34. metadata: { public: metadata },
  35. };
  36. },
  37. settlePayment: () => {
  38. return {
  39. success: true,
  40. metadata: {
  41. moreData: 42,
  42. },
  43. };
  44. },
  45. cancelPayment: (...args) => {
  46. onCancelPaymentSpy(...args);
  47. return {
  48. success: true,
  49. metadata: {
  50. cancellationCode: '12345',
  51. },
  52. };
  53. },
  54. onStateTransitionStart: (fromState, toState, data) => {
  55. onTransitionSpy(fromState, toState, data);
  56. },
  57. });
  58. /**
  59. * A method that can be used to pay for only part of the order (allowing us to test multiple payments
  60. * per order).
  61. */
  62. export const partialPaymentMethod = new PaymentMethodHandler({
  63. code: 'partial-payment-method',
  64. description: [{ languageCode: LanguageCode.en, value: 'Partial Payment Method' }],
  65. args: {},
  66. createPayment: (ctx, order, amount, args, metadata) => {
  67. return {
  68. amount: metadata.amount,
  69. state: metadata.authorizeOnly ? 'Authorized' : 'Settled',
  70. transactionId: '12345',
  71. metadata: { public: metadata },
  72. };
  73. },
  74. settlePayment: () => {
  75. return {
  76. success: true,
  77. };
  78. },
  79. });
  80. /**
  81. * A payment method which includes a createRefund method.
  82. */
  83. export const singleStageRefundablePaymentMethod = new PaymentMethodHandler({
  84. code: 'single-stage-refundable-payment-method',
  85. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  86. args: {},
  87. createPayment: (ctx, order, amount, args, metadata) => {
  88. return {
  89. amount,
  90. state: 'Settled',
  91. transactionId: '12345',
  92. metadata,
  93. };
  94. },
  95. settlePayment: () => {
  96. return { success: true };
  97. },
  98. createRefund: (ctx, input, amount, order, payment, args) => {
  99. return {
  100. state: 'Settled',
  101. transactionId: 'abc123',
  102. };
  103. },
  104. });
  105. let connection: TransactionalConnection;
  106. /**
  107. * A payment method where a Refund attempt will fail the first time
  108. */
  109. export const singleStageRefundFailingPaymentMethod = new PaymentMethodHandler({
  110. code: 'single-stage-refund-failing-payment-method',
  111. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  112. args: {},
  113. init: injector => {
  114. connection = injector.get(TransactionalConnection);
  115. },
  116. createPayment: (ctx, order, amount, args, metadata) => {
  117. return {
  118. amount,
  119. state: 'Settled',
  120. transactionId: '12345',
  121. metadata,
  122. };
  123. },
  124. settlePayment: () => {
  125. return { success: true };
  126. },
  127. createRefund: async (ctx, input, amount, order, payment, args) => {
  128. const paymentWithRefunds = await connection
  129. .getRepository(ctx, Payment)
  130. .findOne({ where: { id: payment.id }, relations: ['refunds'] });
  131. const isFirstRefundAttempt = paymentWithRefunds?.refunds.length === 0;
  132. const metadata = isFirstRefundAttempt ? { errorMessage: 'Service temporarily unavailable' } : {};
  133. return {
  134. state: isFirstRefundAttempt ? 'Failed' : 'Settled',
  135. metadata,
  136. };
  137. },
  138. });
  139. /**
  140. * A payment method where calling `settlePayment` always fails.
  141. */
  142. export const failsToSettlePaymentMethod = new PaymentMethodHandler({
  143. code: 'fails-to-settle-payment-method',
  144. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  145. args: {},
  146. createPayment: (ctx, order, amount, args, metadata) => {
  147. return {
  148. amount,
  149. state: 'Authorized',
  150. transactionId: '12345-' + order.code,
  151. metadata: {
  152. privateCreatePaymentData: 'secret',
  153. public: {
  154. publicCreatePaymentData: 'public',
  155. },
  156. },
  157. };
  158. },
  159. settlePayment: () => {
  160. return {
  161. success: false,
  162. state: 'Cancelled',
  163. errorMessage: 'Something went horribly wrong',
  164. metadata: {
  165. privateSettlePaymentData: 'secret',
  166. public: {
  167. publicSettlePaymentData: 'public',
  168. },
  169. },
  170. };
  171. },
  172. });
  173. /**
  174. * A payment method where calling `settlePayment` always fails.
  175. */
  176. export const failsToCancelPaymentMethod = new PaymentMethodHandler({
  177. code: 'fails-to-cancel-payment-method',
  178. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  179. args: {},
  180. createPayment: (ctx, order, amount, args, metadata) => {
  181. return {
  182. amount,
  183. state: 'Authorized',
  184. transactionId: '12345-' + order.code,
  185. };
  186. },
  187. settlePayment: () => {
  188. return {
  189. success: true,
  190. };
  191. },
  192. cancelPayment: (ctx, order, payment) => {
  193. return {
  194. success: false,
  195. errorMessage: 'something went horribly wrong',
  196. state: payment.state !== 'Cancelled' ? payment.state : undefined,
  197. metadata: {
  198. cancellationData: 'foo',
  199. },
  200. };
  201. },
  202. });
  203. export const testFailingPaymentMethod = new PaymentMethodHandler({
  204. code: 'test-failing-payment-method',
  205. description: [{ languageCode: LanguageCode.en, value: 'Test Failing Payment Method' }],
  206. args: {},
  207. createPayment: (ctx, order, amount, args, metadata) => {
  208. return {
  209. amount,
  210. state: 'Declined',
  211. errorMessage: 'Insufficient funds',
  212. metadata: { public: metadata },
  213. };
  214. },
  215. settlePayment: () => ({
  216. success: true,
  217. }),
  218. });
  219. export const testErrorPaymentMethod = new PaymentMethodHandler({
  220. code: 'test-error-payment-method',
  221. description: [{ languageCode: LanguageCode.en, value: 'Test Error Payment Method' }],
  222. args: {},
  223. createPayment: (ctx, order, amount, args, metadata) => {
  224. return {
  225. amount,
  226. state: 'Error',
  227. errorMessage: 'Something went horribly wrong',
  228. metadata,
  229. };
  230. },
  231. settlePayment: () => ({
  232. success: true,
  233. }),
  234. });