@easypayment/medusa-payment-paypal 1.2.3 → 1.2.5

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 (274) hide show
  1. package/.medusa/server/src/admin/index.js +392 -138
  2. package/.medusa/server/src/admin/index.mjs +392 -138
  3. package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts +1 -0
  4. package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
  5. package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js +5 -1
  6. package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js.map +1 -1
  7. package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts +1 -0
  8. package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts.map +1 -1
  9. package/.medusa/server/src/api/admin/paypal/disconnect/route.js +1 -0
  10. package/.medusa/server/src/api/admin/paypal/disconnect/route.js.map +1 -1
  11. package/.medusa/server/src/api/admin/paypal/environment/route.d.ts +2 -0
  12. package/.medusa/server/src/api/admin/paypal/environment/route.d.ts.map +1 -1
  13. package/.medusa/server/src/api/admin/paypal/environment/route.js +2 -0
  14. package/.medusa/server/src/api/admin/paypal/environment/route.js.map +1 -1
  15. package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts +9 -0
  16. package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts.map +1 -1
  17. package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js +9 -4
  18. package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js.map +1 -1
  19. package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts +7 -1
  20. package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts.map +1 -1
  21. package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js +9 -2
  22. package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js.map +1 -1
  23. package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts +1 -0
  24. package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts.map +1 -1
  25. package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js +1 -0
  26. package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js.map +1 -1
  27. package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts +1 -0
  28. package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts.map +1 -1
  29. package/.medusa/server/src/api/admin/paypal/save-credentials/route.js +10 -3
  30. package/.medusa/server/src/api/admin/paypal/save-credentials/route.js.map +1 -1
  31. package/.medusa/server/src/api/admin/paypal/settings/route.d.ts +2 -0
  32. package/.medusa/server/src/api/admin/paypal/settings/route.d.ts.map +1 -1
  33. package/.medusa/server/src/api/admin/paypal/settings/route.js +9 -2
  34. package/.medusa/server/src/api/admin/paypal/settings/route.js.map +1 -1
  35. package/.medusa/server/src/api/admin/paypal/status/route.d.ts +4 -0
  36. package/.medusa/server/src/api/admin/paypal/status/route.d.ts.map +1 -1
  37. package/.medusa/server/src/api/admin/paypal/status/route.js +9 -1
  38. package/.medusa/server/src/api/admin/paypal/status/route.js.map +1 -1
  39. package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts +4 -0
  40. package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -1
  41. package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js +11 -1
  42. package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js.map +1 -1
  43. package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts +8 -0
  44. package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
  45. package/.medusa/server/src/api/store/paypal/capture-order/route.js +23 -3
  46. package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
  47. package/.medusa/server/src/api/store/paypal/config/route.d.ts +5 -0
  48. package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
  49. package/.medusa/server/src/api/store/paypal/config/route.js +12 -5
  50. package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -1
  51. package/.medusa/server/src/api/store/paypal/create-order/route.d.ts +8 -0
  52. package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
  53. package/.medusa/server/src/api/store/paypal/create-order/route.js +80 -26
  54. package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
  55. package/.medusa/server/src/api/store/paypal/onboard-return/route.d.ts.map +1 -1
  56. package/.medusa/server/src/api/store/paypal/onboard-return/route.js +19 -19
  57. package/.medusa/server/src/api/store/paypal/onboard-return/route.js.map +1 -1
  58. package/.medusa/server/src/api/store/paypal/settings/route.d.ts +1 -0
  59. package/.medusa/server/src/api/store/paypal/settings/route.d.ts.map +1 -1
  60. package/.medusa/server/src/api/store/paypal/settings/route.js +4 -1
  61. package/.medusa/server/src/api/store/paypal/settings/route.js.map +1 -1
  62. package/.medusa/server/src/api/store/paypal/webhook/route.d.ts +7 -0
  63. package/.medusa/server/src/api/store/paypal/webhook/route.d.ts.map +1 -1
  64. package/.medusa/server/src/api/store/paypal/webhook/route.js +34 -3
  65. package/.medusa/server/src/api/store/paypal/webhook/route.js.map +1 -1
  66. package/.medusa/server/src/api/store/paypal-complete/route.d.ts +8 -0
  67. package/.medusa/server/src/api/store/paypal-complete/route.d.ts.map +1 -1
  68. package/.medusa/server/src/api/store/paypal-complete/route.js +55 -8
  69. package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
  70. package/.medusa/server/src/api/utils/rate-limit.d.ts +4 -0
  71. package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
  72. package/.medusa/server/src/api/utils/rate-limit.js +9 -4
  73. package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
  74. package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts +5 -0
  75. package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
  76. package/.medusa/server/src/jobs/paypal-webhook-retry.js +13 -2
  77. package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
  78. package/.medusa/server/src/modules/paypal/index.d.ts +1 -0
  79. package/.medusa/server/src/modules/paypal/index.d.ts.map +1 -1
  80. package/.medusa/server/src/modules/paypal/index.js +1 -0
  81. package/.medusa/server/src/modules/paypal/index.js.map +1 -1
  82. package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js +22 -22
  83. package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js +11 -11
  84. package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js +18 -18
  85. package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js +16 -16
  86. package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js +20 -20
  87. package/.medusa/server/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.js +14 -14
  88. package/.medusa/server/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.js +15 -15
  89. package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.d.ts.map +1 -1
  90. package/.medusa/server/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.js.map +1 -1
  91. package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.d.ts.map +1 -1
  92. package/.medusa/server/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.js.map +1 -1
  93. package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.d.ts.map +1 -1
  94. package/.medusa/server/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.js.map +1 -1
  95. package/.medusa/server/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.js +6 -6
  96. package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts +5 -0
  97. package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts.map +1 -1
  98. package/.medusa/server/src/modules/paypal/models/paypal_connection.js +5 -0
  99. package/.medusa/server/src/modules/paypal/models/paypal_connection.js.map +1 -1
  100. package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts +4 -0
  101. package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts.map +1 -1
  102. package/.medusa/server/src/modules/paypal/models/paypal_metric.js +4 -0
  103. package/.medusa/server/src/modules/paypal/models/paypal_metric.js.map +1 -1
  104. package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts +5 -0
  105. package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts.map +1 -1
  106. package/.medusa/server/src/modules/paypal/models/paypal_settings.js +5 -0
  107. package/.medusa/server/src/modules/paypal/models/paypal_settings.js.map +1 -1
  108. package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts +7 -0
  109. package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts.map +1 -1
  110. package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js +7 -0
  111. package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js.map +1 -1
  112. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts +24 -1
  113. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
  114. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js +296 -0
  115. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
  116. package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts +14 -7
  117. package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
  118. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +40 -198
  119. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
  120. package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts +6 -1
  121. package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts.map +1 -1
  122. package/.medusa/server/src/modules/paypal/payment-provider/index.js +6 -4
  123. package/.medusa/server/src/modules/paypal/payment-provider/index.js.map +1 -1
  124. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +9 -2
  125. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
  126. package/.medusa/server/src/modules/paypal/payment-provider/service.js +29 -181
  127. package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
  128. package/.medusa/server/src/modules/paypal/payment-provider/status-utils.d.ts +21 -0
  129. package/.medusa/server/src/modules/paypal/payment-provider/status-utils.d.ts.map +1 -1
  130. package/.medusa/server/src/modules/paypal/payment-provider/status-utils.js +54 -0
  131. package/.medusa/server/src/modules/paypal/payment-provider/status-utils.js.map +1 -1
  132. package/.medusa/server/src/modules/paypal/service.d.ts +72 -2
  133. package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
  134. package/.medusa/server/src/modules/paypal/service.js +159 -33
  135. package/.medusa/server/src/modules/paypal/service.js.map +1 -1
  136. package/.medusa/server/src/modules/paypal/types/config.d.ts +2 -0
  137. package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
  138. package/.medusa/server/src/modules/paypal/types/config.js +5 -2
  139. package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
  140. package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -0
  141. package/.medusa/server/src/modules/paypal/utils/amounts.d.ts.map +1 -1
  142. package/.medusa/server/src/modules/paypal/utils/amounts.js +15 -5
  143. package/.medusa/server/src/modules/paypal/utils/amounts.js.map +1 -1
  144. package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts +10 -0
  145. package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
  146. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +45 -1
  147. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
  148. package/.medusa/server/src/modules/paypal/utils/currencies.d.ts +9 -0
  149. package/.medusa/server/src/modules/paypal/utils/currencies.d.ts.map +1 -1
  150. package/.medusa/server/src/modules/paypal/utils/currencies.js +13 -0
  151. package/.medusa/server/src/modules/paypal/utils/currencies.js.map +1 -1
  152. package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts +12 -0
  153. package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts.map +1 -1
  154. package/.medusa/server/src/modules/paypal/utils/payment-session.js +4 -3
  155. package/.medusa/server/src/modules/paypal/utils/payment-session.js.map +1 -1
  156. package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts +1 -0
  157. package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts.map +1 -1
  158. package/.medusa/server/src/modules/paypal/utils/paypal-auth.js +1 -0
  159. package/.medusa/server/src/modules/paypal/utils/paypal-auth.js.map +1 -1
  160. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts +11 -0
  161. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
  162. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +17 -1
  163. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
  164. package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts +6 -0
  165. package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts.map +1 -1
  166. package/.medusa/server/src/modules/paypal/utils/provider-ids.js +6 -0
  167. package/.medusa/server/src/modules/paypal/utils/provider-ids.js.map +1 -1
  168. package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts +2 -0
  169. package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts.map +1 -1
  170. package/.medusa/server/src/modules/paypal/utils/secret-crypto.js +6 -1
  171. package/.medusa/server/src/modules/paypal/utils/secret-crypto.js.map +1 -1
  172. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +43 -0
  173. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
  174. package/.medusa/server/src/modules/paypal/webhook-processor.js +57 -5
  175. package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
  176. package/.medusa/server/src/providers/paypal/index.d.ts +1 -0
  177. package/.medusa/server/src/providers/paypal/index.d.ts.map +1 -1
  178. package/.medusa/server/src/providers/paypal/index.js +1 -0
  179. package/.medusa/server/src/providers/paypal/index.js.map +1 -1
  180. package/.medusa/server/src/providers/paypal_card/index.d.ts +1 -0
  181. package/.medusa/server/src/providers/paypal_card/index.d.ts.map +1 -1
  182. package/.medusa/server/src/providers/paypal_card/index.js +1 -0
  183. package/.medusa/server/src/providers/paypal_card/index.js.map +1 -1
  184. package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts +6 -0
  185. package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts.map +1 -1
  186. package/.medusa/server/src/subscribers/paypal-order-invoice.js +9 -1
  187. package/.medusa/server/src/subscribers/paypal-order-invoice.js.map +1 -1
  188. package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts +1 -0
  189. package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
  190. package/.medusa/server/src/subscribers/paypal-webhook-process.js +17 -4
  191. package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
  192. package/CHANGELOG.md +65 -0
  193. package/README.md +184 -184
  194. package/package.json +7 -4
  195. package/src/admin/index.ts +1 -1
  196. package/src/admin/routes/settings/paypal/_components/FieldRow.tsx +38 -29
  197. package/src/admin/routes/settings/paypal/_components/SectionCard.tsx +29 -26
  198. package/src/admin/routes/settings/paypal/_components/Tabs.tsx +49 -48
  199. package/src/admin/routes/settings/paypal/_components/Toast.tsx +63 -52
  200. package/src/admin/routes/settings/paypal/_utils/adminFetch.ts +52 -32
  201. package/src/admin/routes/settings/paypal/additional-settings/page.tsx +246 -139
  202. package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +208 -123
  203. package/src/admin/routes/settings/paypal/apple-pay/page.tsx +9 -5
  204. package/src/admin/routes/settings/paypal/connection/page.tsx +974 -908
  205. package/src/admin/routes/settings/paypal/google-pay/page.tsx +9 -5
  206. package/src/admin/routes/settings/paypal/page.tsx +14 -13
  207. package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +9 -5
  208. package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +304 -273
  209. package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +48 -39
  210. package/src/api/admin/paypal/disconnect/route.ts +15 -14
  211. package/src/api/admin/paypal/environment/route.ts +34 -32
  212. package/src/api/admin/paypal/onboard-complete/route.ts +61 -50
  213. package/src/api/admin/paypal/onboarding-link/route.ts +91 -76
  214. package/src/api/admin/paypal/onboarding-status/route.ts +21 -20
  215. package/src/api/admin/paypal/save-credentials/route.ts +48 -37
  216. package/src/api/admin/paypal/settings/route.ts +63 -45
  217. package/src/api/admin/paypal/status/route.ts +29 -17
  218. package/src/api/hooks/paypal/onboard-return/route.ts +15 -15
  219. package/src/api/hooks/paypal/webhook/route.ts +17 -17
  220. package/src/api/middlewares.ts +61 -61
  221. package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +69 -54
  222. package/src/api/store/paypal/capture-order/route.ts +416 -370
  223. package/src/api/store/paypal/config/route.ts +121 -115
  224. package/src/api/store/paypal/create-order/route.ts +611 -539
  225. package/src/api/store/paypal/onboard-return/route.ts +116 -113
  226. package/src/api/store/paypal/settings/route.ts +29 -19
  227. package/src/api/store/paypal/webhook/route.ts +380 -314
  228. package/src/api/store/paypal-complete/route.ts +184 -128
  229. package/src/api/utils/rate-limit.ts +143 -129
  230. package/src/index.ts +1 -1
  231. package/src/jobs/paypal-webhook-retry.ts +257 -241
  232. package/src/modules/paypal/index.ts +9 -8
  233. package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -33
  234. package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -22
  235. package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -29
  236. package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -27
  237. package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -31
  238. package/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.ts +25 -25
  239. package/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.ts +26 -26
  240. package/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.ts +15 -11
  241. package/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.ts +15 -11
  242. package/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.ts +15 -11
  243. package/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.ts +17 -17
  244. package/src/modules/paypal/migrations/20270202000000_align_nullable_defaults.ts +44 -44
  245. package/src/modules/paypal/migrations/20270301000000_add_webhook_transmission_id_index.ts +15 -15
  246. package/src/modules/paypal/models/paypal_connection.ts +28 -23
  247. package/src/modules/paypal/models/paypal_metric.ts +13 -9
  248. package/src/modules/paypal/models/paypal_settings.ts +13 -8
  249. package/src/modules/paypal/models/paypal_webhook_event.ts +26 -19
  250. package/src/modules/paypal/payment-provider/base-provider.ts +700 -275
  251. package/src/modules/paypal/payment-provider/card-service.ts +693 -855
  252. package/src/modules/paypal/payment-provider/index.ts +14 -15
  253. package/src/modules/paypal/payment-provider/service.ts +673 -836
  254. package/src/modules/paypal/payment-provider/status-utils.ts +116 -59
  255. package/src/modules/paypal/payment-provider/webhook-utils.ts +109 -109
  256. package/src/modules/paypal/service.ts +362 -115
  257. package/src/modules/paypal/types/config.ts +70 -65
  258. package/src/modules/paypal/utils/amounts.ts +123 -107
  259. package/src/modules/paypal/utils/core-workflow.ts +46 -46
  260. package/src/modules/paypal/utils/credential-resolver.ts +315 -271
  261. package/src/modules/paypal/utils/currencies.ts +97 -84
  262. package/src/modules/paypal/utils/partner.ts +8 -8
  263. package/src/modules/paypal/utils/payment-session.ts +144 -138
  264. package/src/modules/paypal/utils/paypal-auth.ts +6 -5
  265. package/src/modules/paypal/utils/paypal-fetch.ts +197 -181
  266. package/src/modules/paypal/utils/provider-ids.ts +21 -15
  267. package/src/modules/paypal/utils/secret-crypto.ts +226 -197
  268. package/src/modules/paypal/utils/webhook-verify.ts +61 -61
  269. package/src/modules/paypal/webhook-processor.ts +641 -573
  270. package/src/providers/paypal/index.ts +9 -8
  271. package/src/providers/paypal_card/index.ts +9 -8
  272. package/src/subscribers/paypal-order-invoice.ts +180 -173
  273. package/src/subscribers/paypal-webhook-process.ts +147 -131
  274. package/tsconfig.json +21 -31
@@ -1 +1 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../../../src/modules/paypal/service.ts"],"names":[],"mappings":"AAYA,KAAK,WAAW,GAAG,SAAS,GAAG,MAAM,CAAA;AAErC,KAAK,MAAM,GACP,cAAc,GACd,SAAS,GACT,qBAAqB,GACrB,WAAW,GACX,SAAS,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBb,cAAM,mBAAoB,SAAQ,wBAKhC;IACA,SAAS,CAAC,GAAG,8CAAoB;IACjC,OAAO,CAAC,mBAAmB,CAA+B;IAG1D,OAAO,CAAC,YAAY,CACd;gBAEM,GAAG,IAAI,EAAE,GAAG,EAAE;IAa1B,OAAO,KAAK,MAAM,GAEjB;YAEa,eAAe;YAKf,sBAAsB;IA0D9B,aAAa;;;;IAQnB,OAAO,CAAC,mBAAmB;YAIb,oBAAoB;YAKpB,aAAa;YAQb,qBAAqB;IAUnC,OAAO,CAAC,WAAW;IAuBnB,OAAO,CAAC,kBAAkB;YA6DZ,+BAA+B;YA4C/B,+BAA+B;YAwC/B,uCAAuC;YAoFvC,oCAAoC;YA4BpC,yBAAyB;IAiBjC,cAAc,CAAC,GAAG,EAAE,WAAW;IAiD/B,oBAAoB,CAAC,KAAK,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;QAAC,GAAG,CAAC,EAAE,WAAW,CAAA;KAAE;;;;IA2HvF,eAAe;IAgBf,mBAAmB,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;;;;;;;;;;;;;;;;;IAsBjE,gCAAgC,CAAC,KAA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AC1B,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAC5B;;;;;;;;;;;;;;;;;;IAYD;;;;;;;;OAQG;IACG,gBAAgB,CACpB,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAiBpC;;;OAGG;IACG,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;;;;;;IAsDnE,6DAA6D;IACvD,gBAAgB,CACpB,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAKpC;;;OAGG;IACG,SAAS,CAAC,KAAK,EAAE;QACrB,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KACnC;CA2CF;AAED,eAAe,mBAAmB,CAAA"}
@@ -29,10 +29,21 @@ function redactSensitive(value, depth = 0) {
29
29
  }
30
30
  const out = {};
31
31
  for (const [k, v] of Object.entries(value)) {
32
- out[k] = SENSITIVE_KEY_RE.test(k) ? "[REDACTED]" : redactSensitive(v, depth + 1);
32
+ out[k] = SENSITIVE_KEY_RE.test(k)
33
+ ? "[REDACTED]"
34
+ : redactSensitive(v, depth + 1);
33
35
  }
34
36
  return out;
35
37
  }
38
+ /**
39
+ * Application-container service for the PayPal module.
40
+ *
41
+ * Owns the merchant's PayPal connection (partner onboarding, seller
42
+ * credentials, cached app access token), the plugin settings singleton,
43
+ * webhook registration/event records, metrics, and audit logging. The payment
44
+ * providers run in the isolated payment-module container and therefore read
45
+ * the same tables through `PayPalCredentialResolver` instead of this service.
46
+ */
36
47
  class PayPalModuleService extends (0, utils_1.MedusaService)({
37
48
  PayPalConnection: paypal_connection_1.default,
38
49
  PayPalMetric: paypal_metric_1.default,
@@ -44,6 +55,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
44
55
  // Raw knex connection for the atomic metric upsert; null when the container
45
56
  // doesn't expose it (recordMetric then falls back to the ORM path).
46
57
  pgForMetrics = null;
58
+ // Only probe PayPal for a stale legacy webhook URL once per process per
59
+ // environment — this runs on admin status reads and must stay cheap.
60
+ webhookUrlMigrationChecked = {};
61
+ webhookHealScheduled = {};
47
62
  constructor(...args) {
48
63
  // MedusaService's generated constructor signature varies across framework
49
64
  // versions — pass everything through untouched.
@@ -117,6 +132,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
117
132
  }
118
133
  return { onboarding, apiDetails };
119
134
  }
135
+ /**
136
+ * Onboarding + API-detail settings with plugin defaults filled in (and
137
+ * persisted) for any key that is still unset.
138
+ */
120
139
  async getApiDetails() {
121
140
  const { onboarding, apiDetails } = await this.ensureSettingsDefaults();
122
141
  return {
@@ -125,11 +144,15 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
125
144
  };
126
145
  }
127
146
  getAlertWebhookUrls() {
128
- return (this.cfg.alertWebhookUrls || []).map((url) => url.trim()).filter(Boolean);
147
+ return (this.cfg.alertWebhookUrls || [])
148
+ .map((url) => url.trim())
149
+ .filter(Boolean);
129
150
  }
130
151
  async getPartnerMerchantId(env) {
131
152
  const { onboarding } = await this.ensureSettingsDefaults();
132
- return env === "live" ? onboarding.partner_merchant_id_live : onboarding.partner_merchant_id_sandbox;
153
+ return env === "live"
154
+ ? onboarding.partner_merchant_id_live
155
+ : onboarding.partner_merchant_id_sandbox;
133
156
  }
134
157
  async getCurrentRow() {
135
158
  const rows = await this.listPayPalConnections({}, { take: 1, order: { created_at: "DESC" } });
@@ -220,7 +243,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
220
243
  throw new Error("Missing PayPal partner merchant id configuration.");
221
244
  }
222
245
  const baseUrl = (0, paypal_auth_1.getPayPalApiBase)(env);
223
- const accessToken = accessTokenOverride ?? await this.getAppAccessToken();
246
+ const accessToken = accessTokenOverride ?? (await this.getAppAccessToken());
224
247
  const resp = await (0, paypal_fetch_1.paypalFetch)(`${baseUrl}/v1/customer/partners/${encodeURIComponent(partnerMerchantId)}/merchant-integrations/${encodeURIComponent(merchantId)}`, {
225
248
  method: "GET",
226
249
  headers: {
@@ -290,7 +313,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
290
313
  accessToken = await this.getAppAccessToken();
291
314
  }
292
315
  let sellerEmail = this.extractSellerEmail(tokenPayload || undefined);
293
- let sellerMerchantId = String(tokenPayload?.merchant_id || tokenPayload?.payer_id || tokenPayload?.account_id || "").trim() || null;
316
+ let sellerMerchantId = String(tokenPayload?.merchant_id ||
317
+ tokenPayload?.payer_id ||
318
+ tokenPayload?.account_id ||
319
+ "").trim() || null;
294
320
  try {
295
321
  const userInfoResp = await (0, paypal_fetch_1.paypalFetch)(`${baseUrl}/v1/identity/oauth2/userinfo?schema=paypalv1`, {
296
322
  method: "GET",
@@ -306,7 +332,11 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
306
332
  sellerEmail = sellerEmail || this.extractSellerEmail(userInfo);
307
333
  sellerMerchantId =
308
334
  sellerMerchantId ||
309
- String(userInfo?.merchant_id || userInfo?.payer_id || userInfo?.user_id || userInfo?.sub || "").trim() ||
335
+ String(userInfo?.merchant_id ||
336
+ userInfo?.payer_id ||
337
+ userInfo?.user_id ||
338
+ userInfo?.sub ||
339
+ "").trim() ||
310
340
  null;
311
341
  }
312
342
  }
@@ -326,7 +356,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
326
356
  if (credsResp.ok) {
327
357
  const credsJson = await credsResp.json().catch(() => ({}));
328
358
  sellerEmail = sellerEmail || this.extractSellerEmail(credsJson);
329
- sellerMerchantId = sellerMerchantId || String(credsJson?.merchant_id || "").trim() || null;
359
+ sellerMerchantId =
360
+ sellerMerchantId ||
361
+ String(credsJson?.merchant_id || "").trim() ||
362
+ null;
330
363
  }
331
364
  }
332
365
  catch (e) {
@@ -376,6 +409,12 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
376
409
  },
377
410
  });
378
411
  }
412
+ /**
413
+ * Switch the active environment (`sandbox` | `live`). Credentials are stored
414
+ * per environment, so switching never discards the other environment's
415
+ * credentials — it only changes which set is active and clears the cached
416
+ * app token.
417
+ */
379
418
  async setEnvironment(env) {
380
419
  const nextEnv = env === "sandbox" ? "sandbox" : "live";
381
420
  const row = await this.getCurrentRow();
@@ -420,6 +459,11 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
420
459
  });
421
460
  return await this.getCurrentRow();
422
461
  }
462
+ /**
463
+ * Request a PayPal partner-referral onboarding URL from the partner service.
464
+ * Returns the URL to open in PayPal's mini-browser plus the `return_url` the
465
+ * popup lands on afterwards.
466
+ */
423
467
  async createOnboardingLink(input) {
424
468
  const { onboarding } = await this.ensureSettingsDefaults();
425
469
  // Honor an explicit environment from the caller so the generated link can't
@@ -488,7 +532,9 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
488
532
  throw new Error(`Onboarding wrapper JSON 'body' is not valid JSON / URL: ${typeof inner === "string" ? inner.slice(0, 200) : "[object]"}`);
489
533
  }
490
534
  }
491
- if (json?.name && json?.message && (json?.debug_id || json?.details || json?.links)) {
535
+ if (json?.name &&
536
+ json?.message &&
537
+ (json?.debug_id || json?.details || json?.links)) {
492
538
  const debug = json.debug_id ? ` debug_id=${json.debug_id}` : "";
493
539
  const details = Array.isArray(json.details)
494
540
  ? json.details
@@ -504,12 +550,15 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
504
550
  : "";
505
551
  throw new Error(`PayPal onboarding error: ${json.name}: ${json.message}.${debug}${details ? ` Details: ${details}` : ""}`);
506
552
  }
507
- if (json?.onboarding_url && String(json.onboarding_url).startsWith("http")) {
553
+ if (json?.onboarding_url &&
554
+ String(json.onboarding_url).startsWith("http")) {
508
555
  return { onboarding_url: String(json.onboarding_url), return_url };
509
556
  }
510
557
  const links = Array.isArray(json?.links) ? json.links : null;
511
558
  if (links) {
512
- const action = links.find((l) => l?.rel === "action_url" || l?.rel === "actionUrl" || l?.rel === "action-url");
559
+ const action = links.find((l) => l?.rel === "action_url" ||
560
+ l?.rel === "actionUrl" ||
561
+ l?.rel === "action-url");
513
562
  const href = action?.href ? String(action.href) : null;
514
563
  if (href && href.startsWith("http")) {
515
564
  return { onboarding_url: href, return_url };
@@ -517,6 +566,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
517
566
  }
518
567
  throw new Error(`Onboarding JSON missing action_url link. Keys: ${Object.keys(json || {}).join(", ")}`);
519
568
  }
569
+ /** Mark the connection as onboarding-in-progress (creates the row if needed). */
520
570
  async startOnboarding() {
521
571
  const row = await this.getCurrentRow();
522
572
  const env = await this.getCurrentEnvironment();
@@ -530,6 +580,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
530
580
  metadata: {},
531
581
  });
532
582
  }
583
+ /** Persist the `authCode`/`sharedId` PayPal hands back after onboarding. */
533
584
  async saveOnboardCallback(input) {
534
585
  const row = await this.getCurrentRow();
535
586
  const env = await this.getCurrentEnvironment();
@@ -549,8 +600,15 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
549
600
  shared_id: input.sharedId,
550
601
  });
551
602
  }
603
+ /**
604
+ * Complete partner onboarding: exchange the single-use `authCode` for a
605
+ * seller access token, fetch the seller's REST credentials, and store them.
606
+ * Idempotent — a repeated exchange of an already-consumed code is a no-op
607
+ * when credentials for that environment are already saved.
608
+ */
552
609
  async exchangeAndSaveSellerCredentials(input) {
553
- const env = (input.env || (await this.getCurrentEnvironment()));
610
+ const env = (input.env ||
611
+ (await this.getCurrentEnvironment()));
554
612
  // Onboarding completion can now arrive via several redundant paths
555
613
  // (partner.js's onboardingCallback in the opener, the return_url bridge that
556
614
  // runs inside the popup, and admin status polling). PayPal's authorization
@@ -564,7 +622,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
564
622
  return;
565
623
  }
566
624
  }
567
- await this.saveOnboardCallback({ authCode: input.authCode, sharedId: input.sharedId });
625
+ await this.saveOnboardCallback({
626
+ authCode: input.authCode,
627
+ sharedId: input.sharedId,
628
+ });
568
629
  const baseUrl = (0, paypal_auth_1.getPayPalApiBase)(env);
569
630
  const { onboarding } = await this.ensureSettingsDefaults();
570
631
  const sellerNonce = (onboarding.seller_nonce || "").trim();
@@ -628,7 +689,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
628
689
  credJson = credText ? JSON.parse(credText) : {};
629
690
  }
630
691
  catch (e) {
631
- console.warn("[PayPal] Failed to parse token response JSON:", e?.message);
692
+ console.warn("[PayPal] Failed to parse credentials response JSON:", e?.message);
632
693
  }
633
694
  if (!credRes.ok) {
634
695
  throw new Error(`PayPal credentials fetch failed (${credRes.status}): ${credText || JSON.stringify(credJson)}`);
@@ -639,15 +700,20 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
639
700
  throw new Error(`PayPal credentials response missing client_id/client_secret. Keys: ${Object.keys(credJson || {}).join(", ")}`);
640
701
  }
641
702
  let sellerEmail = this.extractSellerEmail(credJson, tokenJson);
642
- let sellerMerchantId = String(credJson.payer_id || credJson.merchant_id || tokenJson.payer_id || tokenJson.merchant_id || "").trim() ||
643
- null;
703
+ let sellerMerchantId = String(credJson.payer_id ||
704
+ credJson.merchant_id ||
705
+ tokenJson.payer_id ||
706
+ tokenJson.merchant_id ||
707
+ "").trim() || null;
644
708
  if (!sellerEmail) {
645
709
  const merchantCandidates = [
646
710
  String(credJson.payer_id || "").trim(),
647
711
  String(credJson.merchant_id || "").trim(),
648
712
  String(tokenJson.payer_id || "").trim(),
649
713
  String(tokenJson.merchant_id || "").trim(),
650
- ].filter(Boolean).filter((v, i, arr) => arr.indexOf(v) === i);
714
+ ]
715
+ .filter(Boolean)
716
+ .filter((v, i, arr) => arr.indexOf(v) === i);
651
717
  for (const merchantId of merchantCandidates) {
652
718
  try {
653
719
  const merchantDetails = await this.fetchMerchantIntegrationDetails(env, merchantId, sellerAccessToken);
@@ -674,7 +740,12 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
674
740
  const details = await this.fetchMerchantIntegrationDetails(env, sellerMerchantId, appAccessToken);
675
741
  sellerEmail = this.extractSellerEmail(details);
676
742
  if (sellerEmail) {
677
- await this.saveSellerCredentials({ clientId, clientSecret, sellerMerchantId, sellerEmail });
743
+ await this.saveSellerCredentials({
744
+ clientId,
745
+ clientSecret,
746
+ sellerMerchantId,
747
+ sellerEmail,
748
+ });
678
749
  }
679
750
  }
680
751
  catch (e) {
@@ -682,13 +753,23 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
682
753
  }
683
754
  }
684
755
  }
756
+ /**
757
+ * Store seller REST credentials for an environment (encrypted at rest when
758
+ * `PAYPAL_ENCRYPTION_KEY` is set) and make sure a webhook is registered.
759
+ */
685
760
  async saveSellerCredentials(input) {
686
761
  const row = await this.getCurrentRow();
687
762
  const currentEnv = await this.getCurrentEnvironment();
688
763
  const env = (input.environment || currentEnv);
689
764
  const existingCreds = row ? this.getEnvCreds(row, env) : {};
690
- const nextSellerMerchantId = (input.sellerMerchantId || "").trim() || existingCreds.sellerMerchantId || row?.seller_merchant_id || null;
691
- const nextSellerEmail = (input.sellerEmail || "").trim() || existingCreds.sellerEmail || row?.seller_email || null;
765
+ const nextSellerMerchantId = (input.sellerMerchantId || "").trim() ||
766
+ existingCreds.sellerMerchantId ||
767
+ row?.seller_merchant_id ||
768
+ null;
769
+ const nextSellerEmail = (input.sellerEmail || "").trim() ||
770
+ existingCreds.sellerEmail ||
771
+ row?.seller_email ||
772
+ null;
692
773
  // Encrypt once and store the same ciphertext in both the metadata copy and
693
774
  // the denormalized column (no-op when encryption is disabled).
694
775
  const storedClientSecret = (0, secret_crypto_1.encryptSecret)(input.clientSecret);
@@ -751,8 +832,13 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
751
832
  await this.ensureWebhookRegistration();
752
833
  return updated;
753
834
  }
835
+ /**
836
+ * Save manually-entered credentials, then best-effort look up the seller's
837
+ * merchant id / email from PayPal so the admin UI can display them.
838
+ */
754
839
  async saveAndHydrateSellerCredentials(input) {
755
- const env = (input.environment || (await this.getCurrentEnvironment()));
840
+ const env = (input.environment ||
841
+ (await this.getCurrentEnvironment()));
756
842
  await this.saveSellerCredentials({
757
843
  clientId: input.clientId,
758
844
  clientSecret: input.clientSecret,
@@ -812,10 +898,6 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
812
898
  return false;
813
899
  }
814
900
  }
815
- // Only probe PayPal for a stale legacy webhook URL once per process per
816
- // environment — this runs on admin status reads and must stay cheap.
817
- webhookUrlMigrationChecked = {};
818
- webhookHealScheduled = {};
819
901
  async migrateLegacyWebhookUrl(env, webhookId) {
820
902
  if (this.webhookUrlMigrationChecked[env])
821
903
  return;
@@ -846,9 +928,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
846
928
  "Content-Type": "application/json",
847
929
  "PayPal-Partner-Attribution-Id": this.bnCode,
848
930
  },
849
- body: JSON.stringify([
850
- { op: "replace", path: "/url", value: newUrl },
851
- ]),
931
+ body: JSON.stringify([{ op: "replace", path: "/url", value: newUrl }]),
852
932
  });
853
933
  if (patchResp.ok) {
854
934
  await this.recordAuditEvent("webhook_url_migrated", {
@@ -969,7 +1049,11 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
969
1049
  const row = await this.getCurrentRow();
970
1050
  const env = envOverride ?? (await this.getCurrentEnvironment());
971
1051
  if (!row) {
972
- return { environment: env, status: "disconnected", seller_client_id_present: false };
1052
+ return {
1053
+ environment: env,
1054
+ status: "disconnected",
1055
+ seller_client_id_present: false,
1056
+ };
973
1057
  }
974
1058
  const c = this.getEnvCreds(row, env);
975
1059
  const hasCreds = !!(c.clientId && c.clientSecret);
@@ -1001,9 +1085,14 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1001
1085
  const refreshedRow = await this.getCurrentRow();
1002
1086
  if (refreshedRow) {
1003
1087
  const refreshedCreds = this.getEnvCreds(refreshedRow, env);
1004
- sellerEmail = refreshedCreds.sellerEmail || refreshedRow.seller_email || sellerEmail;
1088
+ sellerEmail =
1089
+ refreshedCreds.sellerEmail ||
1090
+ refreshedRow.seller_email ||
1091
+ sellerEmail;
1005
1092
  sellerMerchantId =
1006
- refreshedCreds.sellerMerchantId || refreshedRow.seller_merchant_id || sellerMerchantId;
1093
+ refreshedCreds.sellerMerchantId ||
1094
+ refreshedRow.seller_merchant_id ||
1095
+ sellerMerchantId;
1007
1096
  }
1008
1097
  }
1009
1098
  }
@@ -1024,6 +1113,11 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1024
1113
  updated_at: row.updated_at?.toISOString?.() ?? null,
1025
1114
  };
1026
1115
  }
1116
+ /**
1117
+ * Remove the active environment's credentials. The other environment (if
1118
+ * connected) is untouched, so the row stays "connected" when it still holds
1119
+ * a usable credential set.
1120
+ */
1027
1121
  async disconnect() {
1028
1122
  const row = await this.getCurrentRow();
1029
1123
  if (!row)
@@ -1033,7 +1127,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1033
1127
  const creds = { ...(meta.credentials || {}) };
1034
1128
  delete creds[env];
1035
1129
  const hasAnyCreds = Object.values(creds).some((v) => {
1036
- return v && typeof v === "object" && v.client_id && v.client_secret;
1130
+ return (v &&
1131
+ typeof v === "object" &&
1132
+ v.client_id &&
1133
+ v.client_secret);
1037
1134
  });
1038
1135
  await this.updatePayPalConnections({
1039
1136
  id: row.id,
@@ -1058,12 +1155,19 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1058
1155
  }
1059
1156
  await this.recordAuditEvent("disconnected", { environment: env });
1060
1157
  }
1158
+ /**
1159
+ * Cached client-credentials access token for the active environment.
1160
+ * Refreshes single-flight when the stored token is within two minutes of
1161
+ * expiry so concurrent callers never race to mint duplicate tokens.
1162
+ */
1061
1163
  async getAppAccessToken() {
1062
1164
  const row = await this.getCurrentRow();
1063
1165
  if (!row) {
1064
1166
  throw new Error("PayPal connection row not found. Please complete onboarding.");
1065
1167
  }
1066
- const expiresAt = row.app_access_token_expires_at ? new Date(row.app_access_token_expires_at) : null;
1168
+ const expiresAt = row.app_access_token_expires_at
1169
+ ? new Date(row.app_access_token_expires_at)
1170
+ : null;
1067
1171
  if (row.app_access_token && expiresAt) {
1068
1172
  const msLeft = expiresAt.getTime() - Date.now();
1069
1173
  // Token is encrypted at rest when a key is configured; decrypt the cached
@@ -1112,6 +1216,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1112
1216
  });
1113
1217
  return accessToken;
1114
1218
  }
1219
+ /** Mint a browser `client_token` (required by the hosted card fields SDK). */
1115
1220
  async generateClientToken(opts) {
1116
1221
  const env = await this.getCurrentEnvironment();
1117
1222
  const baseUrl = (0, paypal_auth_1.getPayPalApiBase)(env);
@@ -1136,6 +1241,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1136
1241
  }
1137
1242
  return token;
1138
1243
  }
1244
+ /** The plugin settings singleton (`{ data }`), never null. */
1139
1245
  async getSettings() {
1140
1246
  // Deterministic singleton read: always the OLDEST row. If a first-boot race
1141
1247
  // ever created a duplicate settings row, every reader (here and the
@@ -1164,6 +1270,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1164
1270
  }
1165
1271
  return result;
1166
1272
  }
1273
+ /** Deep-merge `patch` into the settings singleton and return the result. */
1167
1274
  async saveSettings(patch) {
1168
1275
  // Same deterministic ordering as getSettings so reads and writes always
1169
1276
  // target the same singleton row.
@@ -1188,6 +1295,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1188
1295
  await this.updatePayPalSettings({ id: row.id, data: next });
1189
1296
  return { data: next };
1190
1297
  }
1298
+ /** Decrypted REST credentials for the active environment; throws if unset. */
1191
1299
  async getActiveCredentials() {
1192
1300
  const row = await this.getCurrentRow();
1193
1301
  const env = await this.getCurrentEnvironment();
@@ -1205,6 +1313,7 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1205
1313
  client_secret: clientSecret,
1206
1314
  };
1207
1315
  }
1316
+ /** Fetch a PayPal order (`GET /v2/checkout/orders/{id}`). */
1208
1317
  async getOrderDetails(orderId) {
1209
1318
  if (!orderId) {
1210
1319
  throw new Error("PayPal orderId is required");
@@ -1227,6 +1336,11 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1227
1336
  }
1228
1337
  return JSON.parse(text);
1229
1338
  }
1339
+ /**
1340
+ * Insert a webhook event row. Returns `{ created: false, event }` instead of
1341
+ * throwing when `event_id` already exists, so the webhook route can treat a
1342
+ * redelivery as a duplicate.
1343
+ */
1230
1344
  async createWebhookEventRecord(input) {
1231
1345
  try {
1232
1346
  const created = await this.createPayPalWebhookEvents({
@@ -1251,12 +1365,15 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1251
1365
  if (message.includes("paypal_webhook_event_event_id_unique") ||
1252
1366
  code === "23505" ||
1253
1367
  (message.includes("unique") && message.includes("event_id"))) {
1254
- const existing = await this.listPayPalWebhookEvents({ event_id: input.event_id });
1368
+ const existing = await this.listPayPalWebhookEvents({
1369
+ event_id: input.event_id,
1370
+ });
1255
1371
  return { created: false, event: existing?.[0] ?? null };
1256
1372
  }
1257
1373
  throw error;
1258
1374
  }
1259
1375
  }
1376
+ /** Update processing state on a webhook event row. */
1260
1377
  async updateWebhookEventRecord(input) {
1261
1378
  return await this.updatePayPalWebhookEvents({
1262
1379
  id: input.id,
@@ -1291,6 +1408,10 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1291
1408
  }
1292
1409
  return null;
1293
1410
  }
1411
+ /**
1412
+ * Increment a named counter in `paypal_metric`, merging any metadata.
1413
+ * Best-effort — never throws.
1414
+ */
1294
1415
  async recordMetric(name, metadata) {
1295
1416
  // Preferred path: single atomic INSERT ... ON CONFLICT (same statement the
1296
1417
  // credential resolver uses), so concurrent routes/webhooks never lose
@@ -1341,9 +1462,14 @@ class PayPalModuleService extends (0, utils_1.MedusaService)({
1341
1462
  return null;
1342
1463
  }
1343
1464
  }
1465
+ /** Audit-log a payment event under the `payment_` prefix. */
1344
1466
  async recordPaymentLog(eventType, metadata) {
1345
1467
  return await this.recordAuditEvent(`payment_${eventType}`, metadata);
1346
1468
  }
1469
+ /**
1470
+ * POST an operational alert to every configured `PAYPAL_ALERT_WEBHOOK_URLS`
1471
+ * endpoint. Delivery results are audit-logged; failures never throw.
1472
+ */
1347
1473
  async sendAlert(input) {
1348
1474
  const urls = this.getAlertWebhookUrls();
1349
1475
  if (urls.length === 0) {