@adonis-agora/payments 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/CHANGELOG.md +2347 -0
  2. package/dist/assets/spa/assets/index-CT5Zon6P.css +1 -0
  3. package/dist/assets/spa/assets/index-CvEbJEnq.js +41 -0
  4. package/dist/assets/spa/assets/index-CvEbJEnq.js.map +1 -0
  5. package/dist/assets/spa/index.html +20 -0
  6. package/dist/commands/main.d.ts +4 -3
  7. package/dist/commands/main.d.ts.map +1 -1
  8. package/dist/commands/main.js +9 -2
  9. package/dist/commands/main.js.map +1 -1
  10. package/dist/commands/payments_health.d.ts +31 -0
  11. package/dist/commands/payments_health.d.ts.map +1 -0
  12. package/dist/commands/payments_health.js +113 -0
  13. package/dist/commands/payments_health.js.map +1 -0
  14. package/dist/commands/payments_sync.d.ts +18 -4
  15. package/dist/commands/payments_sync.d.ts.map +1 -1
  16. package/dist/commands/payments_sync.js +136 -40
  17. package/dist/commands/payments_sync.js.map +1 -1
  18. package/dist/configure.d.ts +12 -5
  19. package/dist/configure.d.ts.map +1 -1
  20. package/dist/configure.js +18 -5
  21. package/dist/configure.js.map +1 -1
  22. package/dist/providers/dashboard_provider.d.ts +97 -0
  23. package/dist/providers/dashboard_provider.d.ts.map +1 -0
  24. package/dist/providers/dashboard_provider.js +430 -0
  25. package/dist/providers/dashboard_provider.js.map +1 -0
  26. package/dist/providers/payments_client_provider.d.ts +41 -0
  27. package/dist/providers/payments_client_provider.d.ts.map +1 -0
  28. package/dist/providers/payments_client_provider.js +95 -0
  29. package/dist/providers/payments_client_provider.js.map +1 -0
  30. package/dist/providers/payments_provider.d.ts +35 -0
  31. package/dist/providers/payments_provider.d.ts.map +1 -1
  32. package/dist/providers/payments_provider.js +250 -36
  33. package/dist/providers/payments_provider.js.map +1 -1
  34. package/dist/src/billing/billing_health.d.ts +85 -0
  35. package/dist/src/billing/billing_health.d.ts.map +1 -0
  36. package/dist/src/billing/billing_health.js +155 -0
  37. package/dist/src/billing/billing_health.js.map +1 -0
  38. package/dist/src/billing/billing_overview.d.ts +4 -3
  39. package/dist/src/billing/billing_overview.d.ts.map +1 -1
  40. package/dist/src/billing/billing_overview.js +14 -5
  41. package/dist/src/billing/billing_overview.js.map +1 -1
  42. package/dist/src/billing/billing_store.d.ts +634 -5
  43. package/dist/src/billing/billing_store.d.ts.map +1 -1
  44. package/dist/src/billing/billing_store.js +18 -1
  45. package/dist/src/billing/billing_store.js.map +1 -1
  46. package/dist/src/billing/index.d.ts +2 -1
  47. package/dist/src/billing/index.d.ts.map +1 -1
  48. package/dist/src/billing/index.js +1 -0
  49. package/dist/src/billing/index.js.map +1 -1
  50. package/dist/src/billing/list_query.d.ts +16 -0
  51. package/dist/src/billing/list_query.d.ts.map +1 -0
  52. package/dist/src/billing/list_query.js +28 -0
  53. package/dist/src/billing/list_query.js.map +1 -0
  54. package/dist/src/billing/lucid_billing_store.d.ts +122 -4
  55. package/dist/src/billing/lucid_billing_store.d.ts.map +1 -1
  56. package/dist/src/billing/lucid_billing_store.js +811 -13
  57. package/dist/src/billing/lucid_billing_store.js.map +1 -1
  58. package/dist/src/billing/mixins/index.d.ts +3 -0
  59. package/dist/src/billing/mixins/index.d.ts.map +1 -1
  60. package/dist/src/billing/mixins/index.js +3 -0
  61. package/dist/src/billing/mixins/index.js.map +1 -1
  62. package/dist/src/billing/mixins/with_audit_event.d.ts +48 -0
  63. package/dist/src/billing/mixins/with_audit_event.d.ts.map +1 -0
  64. package/dist/src/billing/mixins/with_audit_event.js +77 -0
  65. package/dist/src/billing/mixins/with_audit_event.js.map +1 -0
  66. package/dist/src/billing/mixins/with_customer.d.ts +39 -0
  67. package/dist/src/billing/mixins/with_customer.d.ts.map +1 -0
  68. package/dist/src/billing/mixins/with_customer.js +68 -0
  69. package/dist/src/billing/mixins/with_customer.js.map +1 -0
  70. package/dist/src/billing/mixins/with_dispute.d.ts +68 -0
  71. package/dist/src/billing/mixins/with_dispute.d.ts.map +1 -0
  72. package/dist/src/billing/mixins/with_dispute.js +83 -0
  73. package/dist/src/billing/mixins/with_dispute.js.map +1 -0
  74. package/dist/src/billing/mixins/with_payment.d.ts +42 -0
  75. package/dist/src/billing/mixins/with_payment.d.ts.map +1 -1
  76. package/dist/src/billing/mixins/with_payment.js +29 -3
  77. package/dist/src/billing/mixins/with_payment.js.map +1 -1
  78. package/dist/src/billing/mixins/with_subscription.d.ts +7 -0
  79. package/dist/src/billing/mixins/with_subscription.d.ts.map +1 -1
  80. package/dist/src/billing/mixins/with_subscription.js +15 -1
  81. package/dist/src/billing/mixins/with_subscription.js.map +1 -1
  82. package/dist/src/billing/mixins/with_usage_event.d.ts +7 -0
  83. package/dist/src/billing/mixins/with_usage_event.d.ts.map +1 -1
  84. package/dist/src/billing/mixins/with_usage_event.js +15 -1
  85. package/dist/src/billing/mixins/with_usage_event.js.map +1 -1
  86. package/dist/src/billing/mixins/with_webhook_event.d.ts +18 -0
  87. package/dist/src/billing/mixins/with_webhook_event.d.ts.map +1 -1
  88. package/dist/src/billing/mixins/with_webhook_event.js +18 -1
  89. package/dist/src/billing/mixins/with_webhook_event.js.map +1 -1
  90. package/dist/src/billing/resolve_dispatch.d.ts +22 -0
  91. package/dist/src/billing/resolve_dispatch.d.ts.map +1 -0
  92. package/dist/src/billing/resolve_dispatch.js +44 -0
  93. package/dist/src/billing/resolve_dispatch.js.map +1 -0
  94. package/dist/src/billing/resolve_store.d.ts +18 -0
  95. package/dist/src/billing/resolve_store.d.ts.map +1 -0
  96. package/dist/src/billing/resolve_store.js +35 -0
  97. package/dist/src/billing/resolve_store.js.map +1 -0
  98. package/dist/src/billing/schema.d.ts +108 -0
  99. package/dist/src/billing/schema.d.ts.map +1 -0
  100. package/dist/src/billing/schema.js +350 -0
  101. package/dist/src/billing/schema.js.map +1 -0
  102. package/dist/src/billing/webhook_dispatcher.d.ts +95 -16
  103. package/dist/src/billing/webhook_dispatcher.d.ts.map +1 -1
  104. package/dist/src/billing/webhook_dispatcher.js +186 -33
  105. package/dist/src/billing/webhook_dispatcher.js.map +1 -1
  106. package/dist/src/billing/webhook_events.d.ts +67 -1
  107. package/dist/src/billing/webhook_events.d.ts.map +1 -1
  108. package/dist/src/billing/webhook_events.js +15 -0
  109. package/dist/src/billing/webhook_events.js.map +1 -1
  110. package/dist/src/billing/webhook_processor.d.ts.map +1 -1
  111. package/dist/src/billing/webhook_processor.js +325 -5
  112. package/dist/src/billing/webhook_processor.js.map +1 -1
  113. package/dist/src/client/define_config.d.ts +150 -0
  114. package/dist/src/client/define_config.d.ts.map +1 -0
  115. package/dist/src/client/define_config.js +98 -0
  116. package/dist/src/client/define_config.js.map +1 -0
  117. package/dist/src/client/handlers.d.ts +71 -0
  118. package/dist/src/client/handlers.d.ts.map +1 -0
  119. package/dist/src/client/handlers.js +119 -0
  120. package/dist/src/client/handlers.js.map +1 -0
  121. package/dist/src/client/index.d.ts +16 -0
  122. package/dist/src/client/index.d.ts.map +1 -0
  123. package/dist/src/client/index.js +13 -0
  124. package/dist/src/client/index.js.map +1 -0
  125. package/dist/src/client/statuses.d.ts +38 -0
  126. package/dist/src/client/statuses.d.ts.map +1 -0
  127. package/dist/src/client/statuses.js +42 -0
  128. package/dist/src/client/statuses.js.map +1 -0
  129. package/dist/src/dashboard/actions.d.ts +143 -0
  130. package/dist/src/dashboard/actions.d.ts.map +1 -0
  131. package/dist/src/dashboard/actions.js +112 -0
  132. package/dist/src/dashboard/actions.js.map +1 -0
  133. package/dist/src/dashboard/auth.d.ts +136 -0
  134. package/dist/src/dashboard/auth.d.ts.map +1 -0
  135. package/dist/src/dashboard/auth.js +152 -0
  136. package/dist/src/dashboard/auth.js.map +1 -0
  137. package/dist/src/dashboard/define_config.d.ts +80 -0
  138. package/dist/src/dashboard/define_config.d.ts.map +1 -0
  139. package/dist/src/dashboard/define_config.js +83 -0
  140. package/dist/src/dashboard/define_config.js.map +1 -0
  141. package/dist/src/dashboard/handlers.d.ts +281 -0
  142. package/dist/src/dashboard/handlers.d.ts.map +1 -0
  143. package/dist/src/dashboard/handlers.js +960 -0
  144. package/dist/src/dashboard/handlers.js.map +1 -0
  145. package/dist/src/dashboard/index.d.ts +18 -0
  146. package/dist/src/dashboard/index.d.ts.map +1 -0
  147. package/dist/src/dashboard/index.js +14 -0
  148. package/dist/src/dashboard/index.js.map +1 -0
  149. package/dist/src/dashboard/login_page.d.ts +23 -0
  150. package/dist/src/dashboard/login_page.d.ts.map +1 -0
  151. package/dist/src/dashboard/login_page.js +170 -0
  152. package/dist/src/dashboard/login_page.js.map +1 -0
  153. package/dist/src/dashboard/period.d.ts +31 -0
  154. package/dist/src/dashboard/period.d.ts.map +1 -0
  155. package/dist/src/dashboard/period.js +44 -0
  156. package/dist/src/dashboard/period.js.map +1 -0
  157. package/dist/src/dashboard/session_cookie.d.ts +42 -0
  158. package/dist/src/dashboard/session_cookie.d.ts.map +1 -0
  159. package/dist/src/dashboard/session_cookie.js +97 -0
  160. package/dist/src/dashboard/session_cookie.js.map +1 -0
  161. package/dist/src/dashboard/spa.d.ts +24 -0
  162. package/dist/src/dashboard/spa.d.ts.map +1 -0
  163. package/dist/src/dashboard/spa.js +52 -0
  164. package/dist/src/dashboard/spa.js.map +1 -0
  165. package/dist/src/define_config.d.ts +244 -9
  166. package/dist/src/define_config.d.ts.map +1 -1
  167. package/dist/src/define_config.js +97 -1
  168. package/dist/src/define_config.js.map +1 -1
  169. package/dist/src/diagnostics.d.ts +287 -7
  170. package/dist/src/diagnostics.d.ts.map +1 -1
  171. package/dist/src/diagnostics.js +317 -1
  172. package/dist/src/diagnostics.js.map +1 -1
  173. package/dist/src/driver.d.ts +139 -5
  174. package/dist/src/driver.d.ts.map +1 -1
  175. package/dist/src/drivers/abacate.d.ts +12 -2
  176. package/dist/src/drivers/abacate.d.ts.map +1 -1
  177. package/dist/src/drivers/abacate.js +112 -19
  178. package/dist/src/drivers/abacate.js.map +1 -1
  179. package/dist/src/drivers/adyen.d.ts +164 -0
  180. package/dist/src/drivers/adyen.d.ts.map +1 -0
  181. package/dist/src/drivers/adyen.js +678 -0
  182. package/dist/src/drivers/adyen.js.map +1 -0
  183. package/dist/src/drivers/asaas.d.ts +52 -2
  184. package/dist/src/drivers/asaas.d.ts.map +1 -1
  185. package/dist/src/drivers/asaas.js +364 -29
  186. package/dist/src/drivers/asaas.js.map +1 -1
  187. package/dist/src/drivers/dodo.d.ts +135 -0
  188. package/dist/src/drivers/dodo.d.ts.map +1 -0
  189. package/dist/src/drivers/dodo.js +864 -0
  190. package/dist/src/drivers/dodo.js.map +1 -0
  191. package/dist/src/drivers/efi.d.ts +98 -0
  192. package/dist/src/drivers/efi.d.ts.map +1 -0
  193. package/dist/src/drivers/efi.js +628 -0
  194. package/dist/src/drivers/efi.js.map +1 -0
  195. package/dist/src/drivers/infinitepay.d.ts +156 -0
  196. package/dist/src/drivers/infinitepay.d.ts.map +1 -0
  197. package/dist/src/drivers/infinitepay.js +321 -0
  198. package/dist/src/drivers/infinitepay.js.map +1 -0
  199. package/dist/src/drivers/lemonsqueezy.d.ts +125 -0
  200. package/dist/src/drivers/lemonsqueezy.d.ts.map +1 -0
  201. package/dist/src/drivers/lemonsqueezy.js +632 -0
  202. package/dist/src/drivers/lemonsqueezy.js.map +1 -0
  203. package/dist/src/drivers/mercadopago.d.ts +97 -0
  204. package/dist/src/drivers/mercadopago.d.ts.map +1 -0
  205. package/dist/src/drivers/mercadopago.js +844 -0
  206. package/dist/src/drivers/mercadopago.js.map +1 -0
  207. package/dist/src/drivers/mollie.d.ts +125 -0
  208. package/dist/src/drivers/mollie.d.ts.map +1 -0
  209. package/dist/src/drivers/mollie.js +812 -0
  210. package/dist/src/drivers/mollie.js.map +1 -0
  211. package/dist/src/drivers/paddle.d.ts +148 -0
  212. package/dist/src/drivers/paddle.d.ts.map +1 -0
  213. package/dist/src/drivers/paddle.js +738 -0
  214. package/dist/src/drivers/paddle.js.map +1 -0
  215. package/dist/src/drivers/pagarme.d.ts +97 -0
  216. package/dist/src/drivers/pagarme.d.ts.map +1 -0
  217. package/dist/src/drivers/pagarme.js +829 -0
  218. package/dist/src/drivers/pagarme.js.map +1 -0
  219. package/dist/src/drivers/pagbank.d.ts +57 -0
  220. package/dist/src/drivers/pagbank.d.ts.map +1 -0
  221. package/dist/src/drivers/pagbank.js +499 -0
  222. package/dist/src/drivers/pagbank.js.map +1 -0
  223. package/dist/src/drivers/paypal.d.ts +120 -0
  224. package/dist/src/drivers/paypal.d.ts.map +1 -0
  225. package/dist/src/drivers/paypal.js +891 -0
  226. package/dist/src/drivers/paypal.js.map +1 -0
  227. package/dist/src/drivers/polar.d.ts +118 -0
  228. package/dist/src/drivers/polar.d.ts.map +1 -0
  229. package/dist/src/drivers/polar.js +604 -0
  230. package/dist/src/drivers/polar.js.map +1 -0
  231. package/dist/src/drivers/razorpay.d.ts +103 -0
  232. package/dist/src/drivers/razorpay.d.ts.map +1 -0
  233. package/dist/src/drivers/razorpay.js +867 -0
  234. package/dist/src/drivers/razorpay.js.map +1 -0
  235. package/dist/src/drivers/shared.d.ts +33 -0
  236. package/dist/src/drivers/shared.d.ts.map +1 -0
  237. package/dist/src/drivers/shared.js +37 -0
  238. package/dist/src/drivers/shared.js.map +1 -0
  239. package/dist/src/drivers/square.d.ts +133 -0
  240. package/dist/src/drivers/square.d.ts.map +1 -0
  241. package/dist/src/drivers/square.js +905 -0
  242. package/dist/src/drivers/square.js.map +1 -0
  243. package/dist/src/drivers/stripe.d.ts +40 -3
  244. package/dist/src/drivers/stripe.d.ts.map +1 -1
  245. package/dist/src/drivers/stripe.js +806 -25
  246. package/dist/src/drivers/stripe.js.map +1 -1
  247. package/dist/src/drivers/woovi.d.ts +10 -3
  248. package/dist/src/drivers/woovi.d.ts.map +1 -1
  249. package/dist/src/drivers/woovi.js +162 -29
  250. package/dist/src/drivers/woovi.js.map +1 -1
  251. package/dist/src/ensure_customer.d.ts +34 -1
  252. package/dist/src/ensure_customer.d.ts.map +1 -1
  253. package/dist/src/ensure_customer.js +29 -4
  254. package/dist/src/ensure_customer.js.map +1 -1
  255. package/dist/src/http.d.ts +37 -3
  256. package/dist/src/http.d.ts.map +1 -1
  257. package/dist/src/http.js +69 -5
  258. package/dist/src/http.js.map +1 -1
  259. package/dist/src/index.d.ts +40 -10
  260. package/dist/src/index.d.ts.map +1 -1
  261. package/dist/src/index.js +22 -4
  262. package/dist/src/index.js.map +1 -1
  263. package/dist/src/invoice/emit_invoice.d.ts.map +1 -1
  264. package/dist/src/invoice/emit_invoice.js +31 -11
  265. package/dist/src/invoice/emit_invoice.js.map +1 -1
  266. package/dist/src/money.d.ts +23 -5
  267. package/dist/src/money.d.ts.map +1 -1
  268. package/dist/src/money.js +72 -7
  269. package/dist/src/money.js.map +1 -1
  270. package/dist/src/payments_manager.d.ts +8 -1
  271. package/dist/src/payments_manager.d.ts.map +1 -1
  272. package/dist/src/payments_manager.js +168 -6
  273. package/dist/src/payments_manager.js.map +1 -1
  274. package/dist/src/services/main.d.ts +38 -0
  275. package/dist/src/services/main.d.ts.map +1 -1
  276. package/dist/src/services/main.js +130 -0
  277. package/dist/src/services/main.js.map +1 -1
  278. package/dist/src/telescope/index.d.ts +2 -0
  279. package/dist/src/telescope/index.d.ts.map +1 -1
  280. package/dist/src/telescope/index.js +1 -0
  281. package/dist/src/telescope/index.js.map +1 -1
  282. package/dist/src/telescope/payments_watcher.d.ts +10 -4
  283. package/dist/src/telescope/payments_watcher.d.ts.map +1 -1
  284. package/dist/src/telescope/payments_watcher.js +65 -13
  285. package/dist/src/telescope/payments_watcher.js.map +1 -1
  286. package/dist/src/telescope/register.d.ts +62 -0
  287. package/dist/src/telescope/register.d.ts.map +1 -0
  288. package/dist/src/telescope/register.js +48 -0
  289. package/dist/src/telescope/register.js.map +1 -0
  290. package/dist/src/testing/fake_payments.d.ts +62 -0
  291. package/dist/src/testing/fake_payments.d.ts.map +1 -0
  292. package/dist/src/testing/fake_payments.js +82 -0
  293. package/dist/src/testing/fake_payments.js.map +1 -0
  294. package/dist/src/testing/in_memory_billing_store.d.ts +151 -2
  295. package/dist/src/testing/in_memory_billing_store.d.ts.map +1 -1
  296. package/dist/src/testing/in_memory_billing_store.js +517 -4
  297. package/dist/src/testing/in_memory_billing_store.js.map +1 -1
  298. package/dist/src/testing/index.d.ts +2 -1
  299. package/dist/src/testing/index.d.ts.map +1 -1
  300. package/dist/src/testing/index.js +1 -0
  301. package/dist/src/testing/index.js.map +1 -1
  302. package/dist/src/types.d.ts +161 -5
  303. package/dist/src/types.d.ts.map +1 -1
  304. package/dist/src/types.js +30 -1
  305. package/dist/src/types.js.map +1 -1
  306. package/dist/src/webhook_handlers.d.ts +96 -2
  307. package/dist/src/webhook_handlers.d.ts.map +1 -1
  308. package/dist/src/webhook_handlers.js +91 -10
  309. package/dist/src/webhook_handlers.js.map +1 -1
  310. package/dist/src/webhook_security.d.ts +94 -3
  311. package/dist/src/webhook_security.d.ts.map +1 -1
  312. package/dist/src/webhook_security.js +173 -9
  313. package/dist/src/webhook_security.js.map +1 -1
  314. package/dist/stubs/config/payments.stub +13 -0
  315. package/dist/stubs/config/payments_client.stub +42 -0
  316. package/dist/stubs/config/payments_dashboard.stub +38 -0
  317. package/dist/stubs/database/migrations/create_billing_tables.stub +35 -77
  318. package/dist/stubs/make/webhook_handler/main.stub +29 -11
  319. package/package.json +60 -25
@@ -1,8 +1,16 @@
1
+ import { PAYMENT_METHOD_NAMES } from './types.js';
1
2
  export class PaymentsManager {
2
3
  #drivers;
3
4
  #invoices;
4
5
  #methods;
5
6
  #defaultName;
7
+ /**
8
+ * Method-bound driver proxies, so repeated `driver('pix')` calls return the same object.
9
+ * Keyed by `name:method`, not method alone: the proxy closes over the name the routing
10
+ * check compares against, so caching by method would hand a second provider the first
11
+ * one's identity.
12
+ */
13
+ #boundDrivers = new Map();
6
14
  constructor(options) {
7
15
  this.#drivers = options.drivers;
8
16
  this.#invoices = options.invoices;
@@ -13,6 +21,15 @@ export class PaymentsManager {
13
21
  }
14
22
  this.#defaultName = options.defaultName ?? names[0];
15
23
  }
24
+ /**
25
+ * Every configured driver, by the name it was configured under.
26
+ *
27
+ * Read-only, and there for the checks that have to look at all of them at once — the
28
+ * boot-time webhook-verification refusal is the one that needed it.
29
+ */
30
+ get drivers() {
31
+ return this.#drivers;
32
+ }
16
33
  /**
17
34
  * Resolve a driver by payment method (e.g. `'pix'`), by provider name, or by the
18
35
  * configured default when nothing is given.
@@ -24,7 +41,7 @@ export class PaymentsManager {
24
41
  * ```
25
42
  */
26
43
  driver(methodOrName) {
27
- const { name: resolved, method } = this.#resolveName(methodOrName);
44
+ const { name: resolved, method, unrouted } = this.#resolveName(methodOrName);
28
45
  const driver = this.#drivers.get(resolved);
29
46
  if (!driver) {
30
47
  throw new Error(`[payments] Driver "${resolved}" is not configured. ` +
@@ -37,8 +54,118 @@ export class PaymentsManager {
37
54
  `Supported methods: ${[...driver.supportedMethods].join(', ')}. ` +
38
55
  `Route "${method}" to a different provider in config.methods.`);
39
56
  }
57
+ // Routing picked the provider; it must also reach the charge. Without this the method
58
+ // is only ever set when the CALLER repeats it — `driver('pix').charge({ method: 'pix' })`
59
+ // — and every driver that varies by method (Stripe's `payment_method_types`, Asaas' and
60
+ // AbacatePay's `billingType`) silently fell back to the gateway's dashboard default. A
61
+ // charge routed as Pix could come back a card.
62
+ // `resolved` is the CONFIG KEY, not `driver.provider` — `config.methods` is written in
63
+ // terms of the keys under `config.providers`, and the two differ whenever an app names a
64
+ // provider something of its own (`providers: { primary: payments.stripe(…) }`). Passing
65
+ // the driver's own name here made the routing check compare "stripe" against "primary"
66
+ // and refuse a charge that was routed exactly right.
67
+ if (method !== undefined)
68
+ return this.#bound(driver, method, resolved);
69
+ // `driver()` with no argument used to skip `config.methods` entirely, which is the same
70
+ // trap one level up: an app that configured pix/credit_card/boleto routing and then
71
+ // calls `driver()` got a driver bound to NOTHING, and every charge fell back to the
72
+ // gateway's dashboard default unless the caller happened to repeat `method:`.
73
+ if (unrouted !== undefined)
74
+ return this.#unrouted(driver, resolved, unrouted);
75
+ // (`method === undefined` and no routing map entry: nothing to bind, nothing to check.)
40
76
  return driver;
41
77
  }
78
+ /**
79
+ * The default driver when `config.methods` is configured but this call named no method.
80
+ *
81
+ * Three cases, and only the first can be answered silently:
82
+ *
83
+ * - **Exactly one method routes here.** The routing is unambiguous, so it is applied — the
84
+ * same binding `driver('pix')` would have produced.
85
+ * - **Several methods route here.** There is no honest answer: `methods` says this provider
86
+ * takes pix AND boleto AND card, and picking one would be inventing the charge's payment
87
+ * method. The charge is refused with the two ways to say what you meant. It refuses at
88
+ * `charge`/`createSubscription` rather than at `driver()`, so the app that already passes
89
+ * `method:` on every call — which is what makes this correct today — keeps working
90
+ * unchanged.
91
+ * - **A method is passed that `methods` routes ELSEWHERE.** The routing map and the call
92
+ * disagree about where the money goes; that is a config error either way, and running it
93
+ * would send a card charge to the provider configured for Pix.
94
+ */
95
+ #unrouted(driver, name, candidates) {
96
+ if (candidates.length === 1) {
97
+ return this.#bound(driver, candidates[0], name);
98
+ }
99
+ return new Proxy(driver, {
100
+ get: (target, property, _receiver) => {
101
+ const value = Reflect.get(target, property, target);
102
+ if (typeof value !== 'function')
103
+ return value;
104
+ if (property !== 'charge' && property !== 'createSubscription') {
105
+ return value.bind(target);
106
+ }
107
+ // `async`, so the refusal below is a rejected promise rather than a synchronous
108
+ // throw: every caller of `charge` awaits it, and a sync throw out of an awaited call
109
+ // skips a `.catch()` the caller reasonably attached.
110
+ return async (input, ...rest) => {
111
+ const method = input?.method;
112
+ if (method === undefined) {
113
+ throw new Error([
114
+ `[payments] driver() resolved "${name}", and config.methods routes`,
115
+ `${candidates.join(', ')} to it — so this ${String(property)} has no payment`,
116
+ 'method and would fall back to whatever the gateway dashboard defaults to.',
117
+ `Name it: driver('${candidates[0]}') or`,
118
+ `${String(property)}({ method: '${candidates[0]}' }).`,
119
+ ].join(' '));
120
+ }
121
+ this.#assertRoutedHere(method, name, String(property));
122
+ return value.call(target, input, ...rest);
123
+ };
124
+ },
125
+ });
126
+ }
127
+ /**
128
+ * The driver, with the routed method filled in on the inputs that carry one.
129
+ *
130
+ * A Proxy rather than a hand-written wrapper for one reason: the driver contract has
131
+ * optional members (`findDispute`, `submitDisputeEvidence`, `capabilities`), and callers
132
+ * test for them with `typeof driver.x === 'function'`. A wrapper that defines every method
133
+ * turns "this gateway cannot do that" into "it can, until you call it" — which is the class
134
+ * of bug this package keeps finding. A Proxy passes absence through as absence.
135
+ *
136
+ * Cached per method so `driver('pix') === driver('pix')` still holds. Note that a driver
137
+ * resolved by NAME is returned untouched: `driver('stripe')` routed nothing, so there is no
138
+ * method to thread and no reason to wrap.
139
+ */
140
+ #bound(driver, method, name) {
141
+ const cacheKey = `${name ?? driver.provider}:${method}`;
142
+ const cached = this.#boundDrivers.get(cacheKey);
143
+ if (cached !== undefined)
144
+ return cached;
145
+ const bound = new Proxy(driver, {
146
+ // An arrow, so `this` is still the manager: the routing check below is its business.
147
+ get: (target, property, _receiver) => {
148
+ // Bound to `target`, never to the proxy: driver methods read `#private` fields, and
149
+ // a proxy is not the instance those belong to.
150
+ const value = Reflect.get(target, property, target);
151
+ if (typeof value !== 'function')
152
+ return value;
153
+ if (property !== 'charge' && property !== 'createSubscription') {
154
+ return value.bind(target);
155
+ }
156
+ return async (input, ...rest) => {
157
+ // An explicit method on the input wins. Routing is a default, not an override:
158
+ // `driver('pix')` picks the provider, and a caller who then names a method meant it
159
+ // — as long as the routing map does not send that method somewhere else.
160
+ const chosen = input?.method ?? method;
161
+ this.#assertRoutedHere(chosen, name ?? driver.provider, String(property));
162
+ return value.call(target, { ...input, method: chosen }, ...rest);
163
+ };
164
+ },
165
+ });
166
+ this.#boundDrivers.set(cacheKey, bound);
167
+ return bound;
168
+ }
42
169
  /**
43
170
  * Resolve an invoice provider by name (falls back to the configured default). Throws
44
171
  * when no invoice provider is configured — a charge with `invoice: true` needs one.
@@ -56,14 +183,26 @@ export class PaymentsManager {
56
183
  assertCapability(driver, capability) {
57
184
  if (driver.capabilities?.[capability] === false ||
58
185
  driver.capabilities?.[capability] === undefined) {
186
+ // Only the capabilities actually set to `true`. Listing every KEY meant a driver that
187
+ // spells out `{ invoices: false }` — as the newer ones do — had the capability it was
188
+ // just refused named back to it as supported.
189
+ const supported = Object.entries(driver.capabilities ?? {})
190
+ .filter(([, enabled]) => enabled === true)
191
+ .map(([name]) => name);
59
192
  throw new Error(`[payments] Driver "${driver.provider}" does not support ${capability}. ` +
60
- `Supported capabilities: ${Object.keys(driver.capabilities ?? {}).join(', ') || '(none)'}.`);
193
+ `Supported capabilities: ${supported.join(', ') || '(none)'}.`);
61
194
  }
62
195
  }
63
196
  #resolveName(methodOrName) {
64
- if (methodOrName === undefined)
65
- return { name: this.#defaultName };
66
- // If it names a configured provider, use it directly.
197
+ if (methodOrName === undefined) {
198
+ const candidates = this.#methodsRoutedTo(this.#defaultName);
199
+ return candidates.length > 0
200
+ ? { name: this.#defaultName, unrouted: candidates }
201
+ : { name: this.#defaultName };
202
+ }
203
+ // If it names a configured provider, use it directly. Naming the provider is an
204
+ // explicit choice, so `config.methods` is deliberately not consulted — it says which
205
+ // provider a METHOD goes to, and this call already answered that question.
67
206
  if (this.#drivers.has(methodOrName))
68
207
  return { name: methodOrName };
69
208
  // Otherwise treat it as a payment method and route via config.methods.
@@ -73,7 +212,30 @@ export class PaymentsManager {
73
212
  return { name: routed, method };
74
213
  // Unknown method/name: throw a helpful error pointing at the routing config.
75
214
  const providers = [...this.#drivers.keys()].join(', ') || '(none)';
76
- throw new Error(`[payments] "${methodOrName}" is neither a configured provider nor a method routed in config.methods. Configured providers: ${providers}. Known methods: pix, credit_card, debit_card, boleto, undefined.`);
215
+ throw new Error(`[payments] "${methodOrName}" is neither a configured provider nor a method routed in config.methods. Configured providers: ${providers}. Known methods: ${PAYMENT_METHOD_NAMES.join(', ')}.`);
216
+ }
217
+ /**
218
+ * Refuse a call whose payment method `config.methods` routes to a DIFFERENT provider.
219
+ *
220
+ * The routing map and the call disagree about where the money goes. Running it anyway sends
221
+ * a card charge to the provider configured for Pix — the same class of silence as an
222
+ * unbound driver, one level up.
223
+ */
224
+ #assertRoutedHere(method, name, operation) {
225
+ const routed = this.#methods[method];
226
+ if (routed === undefined || routed === name)
227
+ return;
228
+ throw new Error([
229
+ `[payments] config.methods routes "${method}" to "${routed}", but this ${operation} is`,
230
+ `going to "${name}". Call driver('${method}') to use the routed provider, or name the`,
231
+ `provider explicitly (driver('${name}')) if the override is deliberate.`,
232
+ ].join(' '));
233
+ }
234
+ /** Which payment methods `config.methods` routes to a given provider. */
235
+ #methodsRoutedTo(name) {
236
+ return Object.entries(this.#methods)
237
+ .filter(([, provider]) => provider === name)
238
+ .map(([method]) => method);
77
239
  }
78
240
  }
79
241
  /** Build the driver map from config, resolving every configured factory. */
@@ -1 +1 @@
1
- {"version":3,"file":"payments_manager.js","sourceRoot":"","sources":["../../src/payments_manager.ts"],"names":[],"mappings":"AAcA,MAAM,OAAO,eAAe;IAC1B,QAAQ,CAA8B;IACtC,SAAS,CAA6B;IACtC,QAAQ,CAA6C;IACrD,YAAY,CAAS;IAErB,YAAY,OAA+B;QACzC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC;QAChC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;QACxC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,yEAAyE,CAAC,CAAC;QAC7F,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,WAAW,IAAI,KAAK,CAAC,CAAC,CAAE,CAAC;IACvD,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,YAAyC;QAC9C,MAAM,EAA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AC"}
@@ -1,6 +1,44 @@
1
+ import type { BillingStore } from '../billing/billing_store.js';
2
+ import type { WebhookDispatcher } from '../billing/webhook_dispatcher.js';
3
+ import type { PaymentsDriver } from '../driver.js';
1
4
  import { PaymentsManager } from '../payments_manager.js';
5
+ import type { PaymentMethodName } from '../types.js';
2
6
  export declare function getPayments(): PaymentsManager;
7
+ /** The manager if there is one, without the throw — for callers that can handle absence. */
8
+ export declare function findPayments(): PaymentsManager | undefined;
3
9
  /** Set by the provider once the manager is built. */
4
10
  export declare function setPayments(manager: PaymentsManager): void;
11
+ export declare function getBillingStore(): BillingStore;
12
+ /** The store if there is one, without the throw. */
13
+ export declare function findBillingStore(): BillingStore | undefined;
14
+ /** Set by the provider once the store is resolved. */
15
+ export declare function setBillingStore(store: BillingStore): void;
16
+ export declare function getWebhookDispatcher(): WebhookDispatcher;
17
+ /** The dispatcher if there is one, without throwing — for callers that can do nothing. */
18
+ export declare function findWebhookDispatcher(): WebhookDispatcher | undefined;
19
+ /** Set by the provider once the billing layer is built. */
20
+ export declare function setWebhookDispatcher(dispatcher: WebhookDispatcher | undefined): void;
21
+ /** A {@link PaymentsManager} that resolves on first use. Safe in a field initializer. */
22
+ export declare function lazyPayments(): PaymentsManager;
23
+ /** The billing store, resolved on first use. Safe in a field initializer. */
24
+ export declare function lazyBillingStore(): BillingStore;
25
+ /**
26
+ * One driver — by method or by provider name — resolved on first use.
27
+ *
28
+ * The shape the ecosystem app actually wanted: `private driver = lazyPaymentsDriver('pix')`.
29
+ * Note the routing is resolved lazily too, so a driver named in `config.methods` does not
30
+ * have to exist yet when the field is initialized.
31
+ */
32
+ export declare function lazyPaymentsDriver(methodOrName?: PaymentMethodName | string): PaymentsDriver;
33
+ /**
34
+ * Forget the resolved singletons.
35
+ *
36
+ * Exists for tests: `@adonis-agora/payments/testing`'s `swapPayments()` restores what was
37
+ * there before, and "before" is legitimately *nothing at all* — the app under test may never
38
+ * have booted a provider. Without this, a restore had to call `setPayments` with a value it
39
+ * did not have.
40
+ */
41
+ export declare function resetPayments(): void;
42
+ export declare function resetBillingStore(): void;
5
43
  export default getPayments;
6
44
  //# sourceMappingURL=main.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/services/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAQzD,wBAAgB,WAAW,IAAI,eAAe,CAO7C;AAED,qDAAqD;AACrD,wBAAgB,WAAW,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAE1D;AAED,eAAe,WAAW,CAAC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/services/main.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAQrD,wBAAgB,WAAW,IAAI,eAAe,CAO7C;AAED,4FAA4F;AAC5F,wBAAgB,YAAY,IAAI,eAAe,GAAG,SAAS,CAE1D;AAED,qDAAqD;AACrD,wBAAgB,WAAW,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAE1D;AAWD,wBAAgB,eAAe,IAAI,YAAY,CAO9C;AAED,oDAAoD;AACpD,wBAAgB,gBAAgB,IAAI,YAAY,GAAG,SAAS,CAE3D;AAED,sDAAsD;AACtD,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAEzD;AAWD,wBAAgB,oBAAoB,IAAI,iBAAiB,CAQxD;AAED,0FAA0F;AAC1F,wBAAgB,qBAAqB,IAAI,iBAAiB,GAAG,SAAS,CAErE;AAED,2DAA2D;AAC3D,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,iBAAiB,GAAG,SAAS,GAAG,IAAI,CAEpF;AAmDD,yFAAyF;AACzF,wBAAgB,YAAY,IAAI,eAAe,CAE9C;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,IAAI,YAAY,CAE/C;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,YAAY,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,cAAc,CAE5F;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED,wBAAgB,iBAAiB,IAAI,IAAI,CAExC;AAED,eAAe,WAAW,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { PaymentsManager } from '../payments_manager.js';
1
2
  /**
2
3
  * Lazy singleton accessor for the payments manager. Set by the provider when it builds
3
4
  * the manager; importing this module never triggers the app boot (media/authkit pattern).
@@ -9,9 +10,138 @@ export function getPayments() {
9
10
  }
10
11
  return payments;
11
12
  }
13
+ /** The manager if there is one, without the throw — for callers that can handle absence. */
14
+ export function findPayments() {
15
+ return payments;
16
+ }
12
17
  /** Set by the provider once the manager is built. */
13
18
  export function setPayments(manager) {
14
19
  payments = manager;
15
20
  }
21
+ /**
22
+ * The billing store the provider resolved from `config.billing.store` — the Lucid
23
+ * store over the published tables unless the app configured another one.
24
+ *
25
+ * `@inject()`-ing `LucidBillingStore` covers the default case; this accessor is what
26
+ * reaches a **custom** store, which has no class to use as a DI token.
27
+ */
28
+ let billingStore;
29
+ export function getBillingStore() {
30
+ if (!billingStore) {
31
+ throw new Error('[payments] The billing store is not ready yet. Make sure the PaymentsProvider is registered, the app has booted, and `billing.enabled` is not false.');
32
+ }
33
+ return billingStore;
34
+ }
35
+ /** The store if there is one, without the throw. */
36
+ export function findBillingStore() {
37
+ return billingStore;
38
+ }
39
+ /** Set by the provider once the store is resolved. */
40
+ export function setBillingStore(store) {
41
+ billingStore = store;
42
+ }
43
+ /**
44
+ * The webhook dispatcher the provider built for this app, when the billing layer is on.
45
+ *
46
+ * Published for the same reason as the store: it is built inside the provider from config
47
+ * nothing else can reconstruct, and tests need to reach it — `flushWebhooks()` in
48
+ * `@adonis-agora/payments/testing` is the whole reason it is here.
49
+ */
50
+ let webhookDispatcher;
51
+ export function getWebhookDispatcher() {
52
+ if (!webhookDispatcher) {
53
+ throw new Error('[payments] The webhook dispatcher is not ready yet. Make sure the PaymentsProvider is ' +
54
+ 'registered, the app has booted, and `billing.enabled` is not false.');
55
+ }
56
+ return webhookDispatcher;
57
+ }
58
+ /** The dispatcher if there is one, without throwing — for callers that can do nothing. */
59
+ export function findWebhookDispatcher() {
60
+ return webhookDispatcher;
61
+ }
62
+ /** Set by the provider once the billing layer is built. */
63
+ export function setWebhookDispatcher(dispatcher) {
64
+ webhookDispatcher = dispatcher;
65
+ }
66
+ /**
67
+ * A stand-in that resolves the real singleton on FIRST PROPERTY ACCESS, not when it is
68
+ * built.
69
+ *
70
+ * `getPayments()` throws until the provider's `booted()` hook runs, and providers that
71
+ * registered earlier boot first — `@adonis-agora/durable` boots workflows whose services are
72
+ * constructed before payments has set anything. So `private payments = getPayments()` in a
73
+ * constructor throws, and every app hits it: the ecosystem app that found this had the SAME
74
+ * four-line comment copy-pasted into four files explaining why it could not resolve in a
75
+ * constructor.
76
+ *
77
+ * These accessors make the field initializer safe. Nothing is resolved until the service
78
+ * actually CALLS something, which by definition happens after boot.
79
+ *
80
+ * ```ts
81
+ * export default class GrantAccess {
82
+ * #payments = lazyPayments() // safe in a field initializer
83
+ * #store = lazyBillingStore()
84
+ * #pix = lazyPaymentsDriver('pix')
85
+ * }
86
+ * ```
87
+ *
88
+ * `getPayments()`/`getBillingStore()` are unchanged for callers that WANT the eager throw —
89
+ * a `start/` file asserting the wiring is right is a legitimate use of it.
90
+ */
91
+ function lazyProxy(resolve, prototype) {
92
+ // `Object.create(prototype)` rather than `{}`: `instanceof PaymentsManager` and
93
+ // `Object.getPrototypeOf` then answer what a caller expects, and a Proxy may not lie about
94
+ // an invariant its target does not have.
95
+ return new Proxy(Object.create(prototype), {
96
+ get(_target, property) {
97
+ const instance = resolve();
98
+ const value = Reflect.get(instance, property, instance);
99
+ // Bound to the instance, never to the proxy: these classes read `#private` fields, and
100
+ // a proxy is not the object those belong to.
101
+ return typeof value === 'function' ? value.bind(instance) : value;
102
+ },
103
+ set(_target, property, value) {
104
+ return Reflect.set(resolve(), property, value);
105
+ },
106
+ has(_target, property) {
107
+ return Reflect.has(resolve(), property);
108
+ },
109
+ getPrototypeOf() {
110
+ return prototype;
111
+ },
112
+ });
113
+ }
114
+ /** A {@link PaymentsManager} that resolves on first use. Safe in a field initializer. */
115
+ export function lazyPayments() {
116
+ return lazyProxy(getPayments, PaymentsManager.prototype);
117
+ }
118
+ /** The billing store, resolved on first use. Safe in a field initializer. */
119
+ export function lazyBillingStore() {
120
+ return lazyProxy(getBillingStore, Object.prototype);
121
+ }
122
+ /**
123
+ * One driver — by method or by provider name — resolved on first use.
124
+ *
125
+ * The shape the ecosystem app actually wanted: `private driver = lazyPaymentsDriver('pix')`.
126
+ * Note the routing is resolved lazily too, so a driver named in `config.methods` does not
127
+ * have to exist yet when the field is initialized.
128
+ */
129
+ export function lazyPaymentsDriver(methodOrName) {
130
+ return lazyProxy(() => getPayments().driver(methodOrName), Object.prototype);
131
+ }
132
+ /**
133
+ * Forget the resolved singletons.
134
+ *
135
+ * Exists for tests: `@adonis-agora/payments/testing`'s `swapPayments()` restores what was
136
+ * there before, and "before" is legitimately *nothing at all* — the app under test may never
137
+ * have booted a provider. Without this, a restore had to call `setPayments` with a value it
138
+ * did not have.
139
+ */
140
+ export function resetPayments() {
141
+ payments = undefined;
142
+ }
143
+ export function resetBillingStore() {
144
+ billingStore = undefined;
145
+ }
16
146
  export default getPayments;
17
147
  //# sourceMappingURL=main.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/services/main.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,IAAI,QAAqC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AAY;IAC1B,OAAO,SAAS,CAAC,WAAW,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;AAC3D,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,gBAAgB;IAC9B,OAAO,SAAS,CAAC,eAAe,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;AACtD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAyC;IAC1E,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;AAC/E,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa;IAC3B,QAAQ,GAAG,SAAS,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,YAAY,GAAG,SAAS,CAAC;AAC3B,CAAC;AAED,eAAe,WAAW,CAAC"}
@@ -1,3 +1,5 @@
1
1
  export { PaymentsWatcher } from './payments_watcher.js';
2
2
  export type { WatcherContext } from './telescope-sdk.js';
3
+ export { registerPaymentsWatcher } from './register.js';
4
+ export type { RegisteredWatcher, TelescopeRecorder } from './register.js';
3
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/telescope/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/telescope/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
@@ -1,2 +1,3 @@
1
1
  export { PaymentsWatcher } from './payments_watcher.js';
2
+ export { registerPaymentsWatcher } from './register.js';
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/telescope/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/telescope/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC"}
@@ -1,10 +1,16 @@
1
1
  import type { WatcherContext } from './telescope-sdk.js';
2
2
  /**
3
3
  * A payments-specific Telescope watcher: records a typed `payments` entry for every
4
- * milestone `agora:payments:*` event the library emits (charges, refunds,
5
- * subscriptions, invoice emission, webhook lifecycle), flattening the event's payload
6
- * into structured content rather than storing the raw envelope like the generic
7
- * diagnostics bridge does.
4
+ * `agora:payments:*` event the library emits — the milestones (charges, refunds,
5
+ * subscriptions, invoice emission, webhook lifecycle) and the debug events
6
+ * (`gateway.request*`, `webhook.verification`) — flattening the event's payload into
7
+ * structured content rather than storing the raw envelope like the generic diagnostics
8
+ * bridge does.
9
+ *
10
+ * Entries are SHAPED, not spread blindly: the fields you scan a timeline by lead
11
+ * ({@link LEAD_KEYS} — who, which object, what happened, how long), then whatever else
12
+ * the payload carried, then `ts`. Same family as the other Agora watchers, which is why
13
+ * the `event` + flattened-payload envelope handling is identical to media's.
8
14
  *
9
15
  * Zero coupling: payments publishes via the structural `@adonis-agora/diagnostics`
10
16
  * emit slot; this subscribes to the same `node:diagnostics_channel` channels — neither
@@ -1 +1 @@
1
- {"version":3,"file":"payments_watcher.d.ts","sourceRoot":"","sources":["../../../src/telescope/payments_watcher.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAkBzD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,eAAe;IAC1B,QAAQ,CAAC,IAAI,cAAc;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IAEnD,0FAA0F;IAC1F,QAAQ,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI;IAuBnC,mFAAmF;IACnF,OAAO,IAAI,IAAI;CAGhB"}
1
+ {"version":3,"file":"payments_watcher.d.ts","sourceRoot":"","sources":["../../../src/telescope/payments_watcher.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAsEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,eAAe;IAC1B,QAAQ,CAAC,IAAI,cAAc;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IAEnD,0FAA0F;IAC1F,QAAQ,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI;IAenC,mFAAmF;IACnF,OAAO,IAAI,IAAI;CAGhB"}
@@ -6,12 +6,72 @@ const RECORDED_EVENTS = [...PAYMENTS_DIAGNOSTIC_EVENTS];
6
6
  function paymentsChannelName(event) {
7
7
  return `agora:payments:${event}`;
8
8
  }
9
+ /**
10
+ * The fields that lead an entry, in this order — the ones a developer scans a timeline
11
+ * by. Everything else in the payload follows, unreordered.
12
+ *
13
+ * `traceId` is handled separately (it can come from either the envelope or the payload)
14
+ * and always sits right after `event`.
15
+ */
16
+ const LEAD_KEYS = [
17
+ 'provider',
18
+ 'gatewayId',
19
+ 'id',
20
+ 'type',
21
+ 'method',
22
+ 'host',
23
+ 'path',
24
+ 'query',
25
+ 'status',
26
+ 'outcome',
27
+ 'scheme',
28
+ 'amount',
29
+ 'currency',
30
+ 'durationMs',
31
+ 'error',
32
+ 'reason',
33
+ ];
34
+ /**
35
+ * Build the entry content: `event`, the correlation id, the {@link LEAD_KEYS} that are
36
+ * present, then the rest of the payload, then `ts`.
37
+ *
38
+ * The trace id can arrive two ways. `@adonis-agora/diagnostics` fills the ENVELOPE's
39
+ * `traceId` from the host's request-context accessor; payments cannot set that field
40
+ * (the structural emit slot is `(lib, event, payload)`) so it puts its own webhook-chain
41
+ * id on the PAYLOAD. The envelope's wins when both are there — it is the host's real
42
+ * request trace, and correlating to it reaches other libraries too.
43
+ */
44
+ function shapeContent(envelope) {
45
+ const payload = envelope.payload ?? {};
46
+ const traceId = envelope.traceId ?? payload.traceId;
47
+ const content = { event: envelope.event };
48
+ if (traceId !== undefined)
49
+ content.traceId = traceId;
50
+ for (const key of LEAD_KEYS) {
51
+ if (payload[key] !== undefined)
52
+ content[key] = payload[key];
53
+ }
54
+ for (const [key, value] of Object.entries(payload)) {
55
+ if (key === 'traceId' || key in content)
56
+ continue;
57
+ content[key] = value;
58
+ }
59
+ if (envelope.ts !== undefined)
60
+ content.ts = envelope.ts;
61
+ return content;
62
+ }
9
63
  /**
10
64
  * A payments-specific Telescope watcher: records a typed `payments` entry for every
11
- * milestone `agora:payments:*` event the library emits (charges, refunds,
12
- * subscriptions, invoice emission, webhook lifecycle), flattening the event's payload
13
- * into structured content rather than storing the raw envelope like the generic
14
- * diagnostics bridge does.
65
+ * `agora:payments:*` event the library emits — the milestones (charges, refunds,
66
+ * subscriptions, invoice emission, webhook lifecycle) and the debug events
67
+ * (`gateway.request*`, `webhook.verification`) — flattening the event's payload into
68
+ * structured content rather than storing the raw envelope like the generic diagnostics
69
+ * bridge does.
70
+ *
71
+ * Entries are SHAPED, not spread blindly: the fields you scan a timeline by lead
72
+ * ({@link LEAD_KEYS} — who, which object, what happened, how long), then whatever else
73
+ * the payload carried, then `ts`. Same family as the other Agora watchers, which is why
74
+ * the `event` + flattened-payload envelope handling is identical to media's.
15
75
  *
16
76
  * Zero coupling: payments publishes via the structural `@adonis-agora/diagnostics`
17
77
  * emit slot; this subscribes to the same `node:diagnostics_channel` channels — neither
@@ -35,15 +95,7 @@ export class PaymentsWatcher {
35
95
  const envelope = message;
36
96
  if (envelope === null || typeof envelope !== 'object')
37
97
  return;
38
- ctx.record({
39
- type: this.type,
40
- content: {
41
- event: envelope.event,
42
- ...(envelope.ts !== undefined ? { ts: envelope.ts } : {}),
43
- ...(envelope.traceId !== undefined ? { traceId: envelope.traceId } : {}),
44
- ...(envelope.payload ?? {}),
45
- },
46
- });
98
+ ctx.record({ type: this.type, content: shapeContent(envelope) });
47
99
  };
48
100
  subscribe(channel, onMessage);
49
101
  this.disposers.push(() => unsubscribe(channel, onMessage));
@@ -1 +1 @@
1
- {"version":3,"file":"payments_watcher.js","sourceRoot":"","sources":["../../../src/telescope/payments_watcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EACL,0BAA0B,EAE1B,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAG3B,4EAA4E;AAC5E,MAAM,eAAe,GAA8B,CAAC,GAAG,0BAA0B,CAAC,CAAC;AAEnF,iLAAiL;AACjL,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,kBAAkB,KAAK,EAAE,CAAC;AACnC,CAAC;AAUD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,eAAe;IACjB,IAAI,GAAG,UAAU,CAAC;IACV,SAAS,GAAsB,EAAE,CAAC;IAEnD,0FAA0F;IAC1F,QAAQ,CAAC,GAAmB;QAC1B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,wBAAwB,CAAC,eAAe,CAAC,CAAC,CAAC;QAE/D,KAAK,MAAM,KAAK,IAAI,eAAe,EAAE,CAAC;YACpC,MAAM,OAAO,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAC;YAC3C,MAAM,SAAS,GAAG,CAAC,OAAgB,EAAE,EAAE;gBACrC,MAAM,QAAQ,GAAG,OAAqC,CAAC;gBACvD,IAAI,QAAQ,KAAK,IAAI,IAAI,OAAO,QAAQ,KAAK,QAAQ;oBAAE,OAAO;g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