@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,128 +1,184 @@
1
- import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
- import type { IPaymentModuleService, Logger } from "@medusajs/framework/types"
3
- import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
4
- import { randomUUID } from "crypto"
5
- import { isPayPalProviderId } from "../../../modules/paypal/utils/provider-ids"
6
- import { runCoreWorkflow } from "../../../modules/paypal/utils/core-workflow"
7
-
8
- export async function POST(req: MedusaRequest, res: MedusaResponse) {
9
- const logger = req.scope.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
10
- const requestId = randomUUID()
11
- const { cart_id } = (req.body || {}) as { cart_id: string }
12
-
13
- if (!cart_id || typeof cart_id !== "string") {
14
- return res.status(400).json({ message: "cart_id is required" })
15
- }
16
-
17
- if (!cart_id.startsWith("cart_")) {
18
- return res.status(400).json({ message: "Invalid cart_id format" })
19
- }
20
-
21
- try {
22
- const query = req.scope.resolve("query")
23
- const { data: carts } = await query.graph({
24
- entity: "cart",
25
- fields: [
26
- "id",
27
- "completed_at",
28
- "payment_collection.payment_sessions.id",
29
- "payment_collection.payment_sessions.data",
30
- "payment_collection.payment_sessions.status",
31
- "payment_collection.payment_sessions.provider_id",
32
- "payment_collection.payment_sessions.created_at",
33
- "payment_collection.payment_sessions.amount",
34
- "payment_collection.payment_sessions.currency_code",
35
- ],
36
- filters: { id: cart_id },
37
- })
38
-
39
- const cart = carts?.[0]
40
- if (cart?.completed_at) {
41
- return res.json({ success: true, cart_id, already_completed: true })
42
- }
43
-
44
- const sessions = cart?.payment_collection?.payment_sessions || []
45
- const session = sessions
46
- .filter((s: any) => isPayPalProviderId(s.provider_id))
47
- .sort(
48
- (a: any, b: any) =>
49
- new Date(b.created_at || 0).getTime() - new Date(a.created_at || 0).getTime()
50
- )[0]
51
-
52
- if (!session) {
53
- return res.status(400).json({ message: "No PayPal payment session found for cart" })
54
- }
55
-
56
- const paymentModule = req.scope.resolve(Modules.PAYMENT) as IPaymentModuleService
57
-
58
- // Do NOT fabricate captured_at/authorized_at here. The authoritative
59
- // timestamp and status are set by the provider's authorizePayment (invoked
60
- // via authorizePaymentSession below) only after it verifies the capture /
61
- // authorization against PayPal. Stamping it ourselves would mark an unpaid
62
- // session as paid.
63
-
64
- const currentStatus = String(session.status || "")
65
- if (currentStatus !== "authorized") {
66
- try {
67
- await (paymentModule as any).authorizePaymentSession(session.id, {})
68
- logger.info(
69
- `[paypal] paypal-complete authorizePaymentSession succeeded (request_id=${requestId}, session_id=${session.id})`
70
- )
71
- } catch (e: any) {
72
- // Authorization is the gate for completion; if it fails, the
73
- // completeCartWorkflow below fails too and we report that honestly.
74
- logger.warn(
75
- `[paypal] paypal-complete authorizePaymentSession failed (request_id=${requestId}, session_id=${session.id}): ${
76
- e?.message ?? String(e)
77
- }`
78
- )
79
- }
80
- }
81
-
82
- try {
83
- const { result } = await runCoreWorkflow<{ id: string }, { id?: string }>(
84
- req.scope,
85
- "complete-cart",
86
- { id: cart_id }
87
- )
88
- logger.info(
89
- `[paypal] paypal-complete cart completed (request_id=${requestId}, cart_id=${cart_id}, order_id=${result?.id})`
90
- )
91
- return res.json({ success: true, cart_id, order_id: result?.id, request_id: requestId })
92
- } catch (e: any) {
93
- // The cart may have been completed by a concurrent request between our
94
- // initial check and now — treat that as success, not an error.
95
- const recheck = await query
96
- .graph({ entity: "cart", fields: ["id", "completed_at"], filters: { id: cart_id } })
97
- .catch(() => ({ data: [] as any[] }))
98
- if (recheck?.data?.[0]?.completed_at) {
99
- return res.json({ success: true, cart_id, already_completed: true, request_id: requestId })
100
- }
101
-
102
- logger.error(
103
- `[paypal] paypal-complete completeCartWorkflow failed (request_id=${requestId}, cart_id=${cart_id}): ${
104
- e?.message ?? String(e)
105
- }`,
106
- e instanceof Error ? e : undefined
107
- )
108
- // The payment may have been captured at PayPal but the order was not
109
- // finalized. Report failure (not a false success) so the storefront can
110
- // surface a "payment taken, contact support" state.
111
- return res.status(500).json({
112
- success: false,
113
- cart_id,
114
- session_id: session.id,
115
- message: "Payment was processed but the order could not be finalized.",
116
- request_id: requestId,
117
- })
118
- }
119
- } catch (e: any) {
120
- logger.error(
121
- `[paypal] paypal-complete failed (request_id=${requestId}, cart_id=${cart_id}): ${
122
- e?.message ?? String(e)
123
- }`,
124
- e instanceof Error ? e : undefined
125
- )
126
- return res.status(500).json({ message: "Internal error", request_id: requestId })
127
- }
128
- }
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+ import type { IPaymentModuleService, Logger } from "@medusajs/framework/types"
3
+ import {
4
+ ContainerRegistrationKeys,
5
+ MedusaError,
6
+ Modules,
7
+ } from "@medusajs/framework/utils"
8
+ import { randomUUID } from "crypto"
9
+ import { isPayPalProviderId } from "../../../modules/paypal/utils/provider-ids"
10
+ import { runCoreWorkflow } from "../../../modules/paypal/utils/core-workflow"
11
+
12
+ /**
13
+ * POST /store/paypal-complete — finalize a cart after the buyer's PayPal
14
+ * payment was captured/authorized.
15
+ *
16
+ * Authorizes the PayPal payment session (which re-verifies the payment against
17
+ * PayPal) and then runs the core complete-cart workflow. A cart completed by a
18
+ * concurrent request (e.g. the webhook safety net) is reported as success.
19
+ */
20
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
21
+ const logger = req.scope.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
22
+ const requestId = randomUUID()
23
+ const { cart_id } = (req.body || {}) as { cart_id: string }
24
+
25
+ if (!cart_id || typeof cart_id !== "string") {
26
+ return res.status(400).json({ message: "cart_id is required" })
27
+ }
28
+
29
+ if (!cart_id.startsWith("cart_")) {
30
+ return res.status(400).json({ message: "Invalid cart_id format" })
31
+ }
32
+
33
+ try {
34
+ const query = req.scope.resolve("query")
35
+ const { data: carts } = await query.graph({
36
+ entity: "cart",
37
+ fields: [
38
+ "id",
39
+ "completed_at",
40
+ "payment_collection.payment_sessions.id",
41
+ "payment_collection.payment_sessions.data",
42
+ "payment_collection.payment_sessions.status",
43
+ "payment_collection.payment_sessions.provider_id",
44
+ "payment_collection.payment_sessions.created_at",
45
+ "payment_collection.payment_sessions.amount",
46
+ "payment_collection.payment_sessions.currency_code",
47
+ ],
48
+ filters: { id: cart_id },
49
+ })
50
+
51
+ const cart = carts?.[0]
52
+ if (cart?.completed_at) {
53
+ return res.json({ success: true, cart_id, already_completed: true })
54
+ }
55
+
56
+ const sessions = cart?.payment_collection?.payment_sessions || []
57
+ const session = sessions
58
+ .filter((s: any) => isPayPalProviderId(s.provider_id))
59
+ .sort(
60
+ (a: any, b: any) =>
61
+ new Date(b.created_at || 0).getTime() -
62
+ new Date(a.created_at || 0).getTime()
63
+ )[0]
64
+
65
+ if (!session) {
66
+ return res
67
+ .status(400)
68
+ .json({ message: "No PayPal payment session found for cart" })
69
+ }
70
+
71
+ const paymentModule = req.scope.resolve(
72
+ Modules.PAYMENT
73
+ ) as IPaymentModuleService
74
+
75
+ // Do NOT fabricate captured_at/authorized_at here. The authoritative
76
+ // timestamp and status are set by the provider's authorizePayment (invoked
77
+ // via authorizePaymentSession below) only after it verifies the capture /
78
+ // authorization against PayPal. Stamping it ourselves would mark an unpaid
79
+ // session as paid.
80
+
81
+ const currentStatus = String(session.status || "")
82
+ if (currentStatus !== "authorized") {
83
+ try {
84
+ await (paymentModule as any).authorizePaymentSession(session.id, {})
85
+ logger.info(
86
+ `[paypal] paypal-complete authorizePaymentSession succeeded (request_id=${requestId}, session_id=${session.id})`
87
+ )
88
+ } catch (e: any) {
89
+ logger.warn(
90
+ `[paypal] paypal-complete authorizePaymentSession failed (request_id=${requestId}, session_id=${session.id}): ${
91
+ e?.message ?? String(e)
92
+ }`
93
+ )
94
+ // The providers throw NOT_ALLOWED when PayPal has no approved /
95
+ // captured payment for the session (buyer never approved, capture
96
+ // declined, …). Nothing was charged, so say so — the generic
97
+ // "payment was processed but the order could not be finalized" below
98
+ // would tell a storefront to show "do not pay again" for a payment
99
+ // that never happened. Anything else (transient DB/PayPal errors) is
100
+ // still allowed to fall through to the completion attempt and its
101
+ // retryable 5xx. Checked via the `__isMedusaError` flag, not
102
+ // `instanceof`: the provider runs inside the payment module's own
103
+ // loader and its MedusaError is a different class instance than ours.
104
+ if (
105
+ MedusaError.isMedusaError(e) &&
106
+ (e as MedusaError).type === MedusaError.Types.NOT_ALLOWED
107
+ ) {
108
+ return res.status(402).json({
109
+ success: false,
110
+ cart_id,
111
+ session_id: session.id,
112
+ payment_authorized: false,
113
+ message:
114
+ e.message ||
115
+ "The PayPal payment for this cart has not been approved.",
116
+ request_id: requestId,
117
+ })
118
+ }
119
+ }
120
+ }
121
+
122
+ try {
123
+ const { result } = await runCoreWorkflow<{ id: string }, { id?: string }>(
124
+ req.scope,
125
+ "complete-cart",
126
+ { id: cart_id }
127
+ )
128
+ logger.info(
129
+ `[paypal] paypal-complete cart completed (request_id=${requestId}, cart_id=${cart_id}, order_id=${result?.id})`
130
+ )
131
+ return res.json({
132
+ success: true,
133
+ cart_id,
134
+ order_id: result?.id,
135
+ request_id: requestId,
136
+ })
137
+ } catch (e: any) {
138
+ // The cart may have been completed by a concurrent request between our
139
+ // initial check and now — treat that as success, not an error.
140
+ const recheck = await query
141
+ .graph({
142
+ entity: "cart",
143
+ fields: ["id", "completed_at"],
144
+ filters: { id: cart_id },
145
+ })
146
+ .catch(() => ({ data: [] as any[] }))
147
+ if (recheck?.data?.[0]?.completed_at) {
148
+ return res.json({
149
+ success: true,
150
+ cart_id,
151
+ already_completed: true,
152
+ request_id: requestId,
153
+ })
154
+ }
155
+
156
+ logger.error(
157
+ `[paypal] paypal-complete completeCartWorkflow failed (request_id=${requestId}, cart_id=${cart_id}): ${
158
+ e?.message ?? String(e)
159
+ }`,
160
+ e instanceof Error ? e : undefined
161
+ )
162
+ // The payment may have been captured at PayPal but the order was not
163
+ // finalized. Report failure (not a false success) so the storefront can
164
+ // surface a "payment taken, contact support" state.
165
+ return res.status(500).json({
166
+ success: false,
167
+ cart_id,
168
+ session_id: session.id,
169
+ message: "Payment was processed but the order could not be finalized.",
170
+ request_id: requestId,
171
+ })
172
+ }
173
+ } catch (e: any) {
174
+ logger.error(
175
+ `[paypal] paypal-complete failed (request_id=${requestId}, cart_id=${cart_id}): ${
176
+ e?.message ?? String(e)
177
+ }`,
178
+ e instanceof Error ? e : undefined
179
+ )
180
+ return res
181
+ .status(500)
182
+ .json({ message: "Internal error", request_id: requestId })
183
+ }
184
+ }
@@ -1,129 +1,143 @@
1
- import type { MedusaNextFunction, MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
-
3
- /**
4
- * Lightweight in-memory rate limiter for the public store payment routes.
5
- *
6
- * The store PayPal routes (create-order, capture-order, paypal-complete) are
7
- * unauthenticated by necessity, so without a limit a caller can hammer them —
8
- * e.g. enumerate cart ids and create unbounded PayPal orders against the
9
- * merchant account. This applies a fixed-window per-client cap.
10
- *
11
- * OPT-IN: disabled unless `PAYPAL_RATE_LIMIT_MAX` is set to a positive number.
12
- * This is deliberate — behind a load balancer / CDN that doesn't forward the
13
- * real client IP, every request would share one bucket and a low limit could
14
- * block legitimate checkouts. Merchants opt in once they've confirmed the real
15
- * client IP reaches the app (e.g. `x-forwarded-for` is set correctly).
16
- *
17
- * NOTE: the counter lives in process memory, so the limit is enforced per
18
- * server instance, not globally across a horizontally-scaled deployment. It is
19
- * a pragmatic first line of defense and a backstop; a shared store (Redis) or
20
- * an edge/CDN limiter is the right tool for a hard global guarantee. Tunable via
21
- * PAYPAL_RATE_LIMIT_MAX (requests) and PAYPAL_RATE_LIMIT_WINDOW_MS (window).
22
- */
23
-
24
- const DEFAULT_WINDOW_MS = 60_000
25
-
26
- // Cap the number of distinct client buckets we retain so a flood of unique
27
- // client keys can't grow the map unbounded (a memory-exhaustion vector of its
28
- // own). Oldest buckets are evicted first once the cap is hit.
29
- const MAX_BUCKETS = 10_000
30
-
31
- type Bucket = { count: number; resetAt: number }
32
-
33
- function readEnvInt(name: string, fallback: number): number {
34
- const v = Number(process.env[name])
35
- return Number.isFinite(v) && v > 0 ? v : fallback
36
- }
37
-
38
- /** The configured max, or null when rate limiting is disabled (opt-in). */
39
- function readConfiguredMax(envVar: string): number | null {
40
- const raw = process.env[envVar]
41
- if (raw === undefined || raw === "") return null
42
- const v = Number(raw)
43
- return Number.isFinite(v) && v > 0 ? v : null
44
- }
45
-
46
- function clientKey(req: MedusaRequest): string {
47
- // Use the RIGHT-most x-forwarded-for entry: proxies append the address they
48
- // received the request from, so the right-most value was written by the
49
- // merchant's own edge proxy and cannot be forged by the client. The
50
- // left-most entry is fully client-controlled — keying on it would let an
51
- // attacker rotate a header value to get a fresh bucket per request,
52
- // bypassing the limiter entirely.
53
- const fwd = req.headers["x-forwarded-for"]
54
- const fwdRaw = Array.isArray(fwd) ? fwd[fwd.length - 1] : fwd
55
- const parts = fwdRaw
56
- ? fwdRaw.split(",").map((s) => s.trim()).filter(Boolean)
57
- : []
58
- const ip =
59
- parts[parts.length - 1] ||
60
- req.socket?.remoteAddress ||
61
- "unknown"
62
- return ip
63
- }
64
-
65
- export type RateLimiterOptions = {
66
- /**
67
- * Env var holding the request cap for this limiter. Defaults to the shared
68
- * PAYPAL_RATE_LIMIT_MAX. The webhook routes pass their own dedicated var:
69
- * PayPal delivers from a handful of egress IPs, so a cap sized for
70
- * individual buyers would throttle legitimate webhook bursts (a sale spike)
71
- * into 429-driven redelivery loops.
72
- */
73
- maxEnvVar?: string
74
- /** Env var for the window; defaults to PAYPAL_RATE_LIMIT_WINDOW_MS. */
75
- windowEnvVar?: string
76
- }
77
-
78
- export function createRateLimiter(scope: string, options: RateLimiterOptions = {}) {
79
- const buckets = new Map<string, Bucket>()
80
- const maxEnvVar = options.maxEnvVar || "PAYPAL_RATE_LIMIT_MAX"
81
- const windowEnvVar = options.windowEnvVar || "PAYPAL_RATE_LIMIT_WINDOW_MS"
82
-
83
- return function rateLimit(
84
- req: MedusaRequest,
85
- res: MedusaResponse,
86
- next: MedusaNextFunction
87
- ) {
88
- // Opt-in: with no configured max, this middleware is a pass-through so
89
- // existing deployments (especially behind a shared-IP proxy) are unaffected.
90
- const max = readConfiguredMax(maxEnvVar)
91
- if (max === null) {
92
- return next()
93
- }
94
- const windowMs = readEnvInt(windowEnvVar, DEFAULT_WINDOW_MS)
95
- const now = Date.now()
96
- const key = `${scope}:${clientKey(req)}`
97
-
98
- let bucket = buckets.get(key)
99
- if (!bucket || bucket.resetAt <= now) {
100
- bucket = { count: 0, resetAt: now + windowMs }
101
- buckets.set(key, bucket)
102
- }
103
-
104
- bucket.count += 1
105
-
106
- if (buckets.size > MAX_BUCKETS) {
107
- // Evict expired buckets first; if still over, drop the oldest inserted.
108
- for (const [k, b] of buckets) {
109
- if (b.resetAt <= now) buckets.delete(k)
110
- if (buckets.size <= MAX_BUCKETS) break
111
- }
112
- while (buckets.size > MAX_BUCKETS) {
113
- const oldest = buckets.keys().next().value
114
- if (oldest === undefined) break
115
- buckets.delete(oldest)
116
- }
117
- }
118
-
119
- if (bucket.count > max) {
120
- const retryAfterSec = Math.max(1, Math.ceil((bucket.resetAt - now) / 1000))
121
- res.setHeader("Retry-After", String(retryAfterSec))
122
- return res
123
- .status(429)
124
- .json({ message: "Too many requests. Please slow down and try again." })
125
- }
126
-
127
- return next()
128
- }
129
- }
1
+ import type {
2
+ MedusaNextFunction,
3
+ MedusaRequest,
4
+ MedusaResponse,
5
+ } from "@medusajs/framework/http"
6
+
7
+ /**
8
+ * Lightweight in-memory rate limiter for the public store payment routes.
9
+ *
10
+ * The store PayPal routes (create-order, capture-order, paypal-complete) are
11
+ * unauthenticated by necessity, so without a limit a caller can hammer them —
12
+ * e.g. enumerate cart ids and create unbounded PayPal orders against the
13
+ * merchant account. This applies a fixed-window per-client cap.
14
+ *
15
+ * OPT-IN: disabled unless `PAYPAL_RATE_LIMIT_MAX` is set to a positive number.
16
+ * This is deliberate — behind a load balancer / CDN that doesn't forward the
17
+ * real client IP, every request would share one bucket and a low limit could
18
+ * block legitimate checkouts. Merchants opt in once they've confirmed the real
19
+ * client IP reaches the app (e.g. `x-forwarded-for` is set correctly).
20
+ *
21
+ * NOTE: the counter lives in process memory, so the limit is enforced per
22
+ * server instance, not globally across a horizontally-scaled deployment. It is
23
+ * a pragmatic first line of defense and a backstop; a shared store (Redis) or
24
+ * an edge/CDN limiter is the right tool for a hard global guarantee. Tunable via
25
+ * PAYPAL_RATE_LIMIT_MAX (requests) and PAYPAL_RATE_LIMIT_WINDOW_MS (window).
26
+ */
27
+
28
+ const DEFAULT_WINDOW_MS = 60_000
29
+
30
+ // Cap the number of distinct client buckets we retain so a flood of unique
31
+ // client keys can't grow the map unbounded (a memory-exhaustion vector of its
32
+ // own). Oldest buckets are evicted first once the cap is hit.
33
+ const MAX_BUCKETS = 10_000
34
+
35
+ type Bucket = { count: number; resetAt: number }
36
+
37
+ function readEnvInt(name: string, fallback: number): number {
38
+ const v = Number(process.env[name])
39
+ return Number.isFinite(v) && v > 0 ? v : fallback
40
+ }
41
+
42
+ /** The configured max, or null when rate limiting is disabled (opt-in). */
43
+ function readConfiguredMax(envVar: string): number | null {
44
+ const raw = process.env[envVar]
45
+ if (raw === undefined || raw === "") return null
46
+ const v = Number(raw)
47
+ return Number.isFinite(v) && v > 0 ? v : null
48
+ }
49
+
50
+ function clientKey(req: MedusaRequest): string {
51
+ // Use the RIGHT-most x-forwarded-for entry: proxies append the address they
52
+ // received the request from, so the right-most value was written by the
53
+ // merchant's own edge proxy and cannot be forged by the client. The
54
+ // left-most entry is fully client-controlled — keying on it would let an
55
+ // attacker rotate a header value to get a fresh bucket per request,
56
+ // bypassing the limiter entirely.
57
+ const fwd = req.headers["x-forwarded-for"]
58
+ const fwdRaw = Array.isArray(fwd) ? fwd[fwd.length - 1] : fwd
59
+ const parts = fwdRaw
60
+ ? fwdRaw
61
+ .split(",")
62
+ .map((s) => s.trim())
63
+ .filter(Boolean)
64
+ : []
65
+ const ip = parts[parts.length - 1] || req.socket?.remoteAddress || "unknown"
66
+ return ip
67
+ }
68
+
69
+ export type RateLimiterOptions = {
70
+ /**
71
+ * Env var holding the request cap for this limiter. Defaults to the shared
72
+ * PAYPAL_RATE_LIMIT_MAX. The webhook routes pass their own dedicated var:
73
+ * PayPal delivers from a handful of egress IPs, so a cap sized for
74
+ * individual buyers would throttle legitimate webhook bursts (a sale spike)
75
+ * into 429-driven redelivery loops.
76
+ */
77
+ maxEnvVar?: string
78
+ /** Env var for the window; defaults to PAYPAL_RATE_LIMIT_WINDOW_MS. */
79
+ windowEnvVar?: string
80
+ }
81
+
82
+ /**
83
+ * Build a fixed-window rate-limit middleware with its own bucket map. `scope`
84
+ * namespaces the buckets so limiters on different routes never share counts.
85
+ */
86
+ export function createRateLimiter(
87
+ scope: string,
88
+ options: RateLimiterOptions = {}
89
+ ) {
90
+ const buckets = new Map<string, Bucket>()
91
+ const maxEnvVar = options.maxEnvVar || "PAYPAL_RATE_LIMIT_MAX"
92
+ const windowEnvVar = options.windowEnvVar || "PAYPAL_RATE_LIMIT_WINDOW_MS"
93
+
94
+ return function rateLimit(
95
+ req: MedusaRequest,
96
+ res: MedusaResponse,
97
+ next: MedusaNextFunction
98
+ ) {
99
+ // Opt-in: with no configured max, this middleware is a pass-through so
100
+ // existing deployments (especially behind a shared-IP proxy) are unaffected.
101
+ const max = readConfiguredMax(maxEnvVar)
102
+ if (max === null) {
103
+ return next()
104
+ }
105
+ const windowMs = readEnvInt(windowEnvVar, DEFAULT_WINDOW_MS)
106
+ const now = Date.now()
107
+ const key = `${scope}:${clientKey(req)}`
108
+
109
+ let bucket = buckets.get(key)
110
+ if (!bucket || bucket.resetAt <= now) {
111
+ bucket = { count: 0, resetAt: now + windowMs }
112
+ buckets.set(key, bucket)
113
+ }
114
+
115
+ bucket.count += 1
116
+
117
+ if (buckets.size > MAX_BUCKETS) {
118
+ // Evict expired buckets first; if still over, drop the oldest inserted.
119
+ for (const [k, b] of buckets) {
120
+ if (b.resetAt <= now) buckets.delete(k)
121
+ if (buckets.size <= MAX_BUCKETS) break
122
+ }
123
+ while (buckets.size > MAX_BUCKETS) {
124
+ const oldest = buckets.keys().next().value
125
+ if (oldest === undefined) break
126
+ buckets.delete(oldest)
127
+ }
128
+ }
129
+
130
+ if (bucket.count > max) {
131
+ const retryAfterSec = Math.max(
132
+ 1,
133
+ Math.ceil((bucket.resetAt - now) / 1000)
134
+ )
135
+ res.setHeader("Retry-After", String(retryAfterSec))
136
+ return res
137
+ .status(429)
138
+ .json({ message: "Too many requests. Please slow down and try again." })
139
+ }
140
+
141
+ return next()
142
+ }
143
+ }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { default } from "./modules/paypal"
1
+ export { default } from "./modules/paypal"