@easypayment/medusa-payment-paypal 1.2.3 → 1.2.4

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 (267) hide show
  1. package/.medusa/server/src/admin/index.js +387 -133
  2. package/.medusa/server/src/admin/index.mjs +387 -133
  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 +5 -0
  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 +54 -20
  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 +33 -6
  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.map +1 -1
  113. package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
  114. package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts +14 -6
  115. package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
  116. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +55 -32
  117. package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
  118. package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts +6 -1
  119. package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts.map +1 -1
  120. package/.medusa/server/src/modules/paypal/payment-provider/index.js +6 -4
  121. package/.medusa/server/src/modules/paypal/payment-provider/index.js.map +1 -1
  122. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +8 -0
  123. package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
  124. package/.medusa/server/src/modules/paypal/payment-provider/service.js +39 -12
  125. package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
  126. package/.medusa/server/src/modules/paypal/service.d.ts +72 -2
  127. package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
  128. package/.medusa/server/src/modules/paypal/service.js +159 -33
  129. package/.medusa/server/src/modules/paypal/service.js.map +1 -1
  130. package/.medusa/server/src/modules/paypal/types/config.d.ts +2 -0
  131. package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
  132. package/.medusa/server/src/modules/paypal/types/config.js +5 -2
  133. package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
  134. package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -0
  135. package/.medusa/server/src/modules/paypal/utils/amounts.d.ts.map +1 -1
  136. package/.medusa/server/src/modules/paypal/utils/amounts.js +15 -5
  137. package/.medusa/server/src/modules/paypal/utils/amounts.js.map +1 -1
  138. package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
  139. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +3 -1
  140. package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
  141. package/.medusa/server/src/modules/paypal/utils/currencies.d.ts +9 -0
  142. package/.medusa/server/src/modules/paypal/utils/currencies.d.ts.map +1 -1
  143. package/.medusa/server/src/modules/paypal/utils/currencies.js +13 -0
  144. package/.medusa/server/src/modules/paypal/utils/currencies.js.map +1 -1
  145. package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts +12 -0
  146. package/.medusa/server/src/modules/paypal/utils/payment-session.d.ts.map +1 -1
  147. package/.medusa/server/src/modules/paypal/utils/payment-session.js +4 -3
  148. package/.medusa/server/src/modules/paypal/utils/payment-session.js.map +1 -1
  149. package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts +1 -0
  150. package/.medusa/server/src/modules/paypal/utils/paypal-auth.d.ts.map +1 -1
  151. package/.medusa/server/src/modules/paypal/utils/paypal-auth.js +1 -0
  152. package/.medusa/server/src/modules/paypal/utils/paypal-auth.js.map +1 -1
  153. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts +11 -0
  154. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
  155. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +17 -1
  156. package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
  157. package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts +6 -0
  158. package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts.map +1 -1
  159. package/.medusa/server/src/modules/paypal/utils/provider-ids.js +6 -0
  160. package/.medusa/server/src/modules/paypal/utils/provider-ids.js.map +1 -1
  161. package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts +2 -0
  162. package/.medusa/server/src/modules/paypal/utils/secret-crypto.d.ts.map +1 -1
  163. package/.medusa/server/src/modules/paypal/utils/secret-crypto.js +6 -1
  164. package/.medusa/server/src/modules/paypal/utils/secret-crypto.js.map +1 -1
  165. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +43 -0
  166. package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
  167. package/.medusa/server/src/modules/paypal/webhook-processor.js +57 -5
  168. package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
  169. package/.medusa/server/src/providers/paypal/index.d.ts +1 -0
  170. package/.medusa/server/src/providers/paypal/index.d.ts.map +1 -1
  171. package/.medusa/server/src/providers/paypal/index.js +1 -0
  172. package/.medusa/server/src/providers/paypal/index.js.map +1 -1
  173. package/.medusa/server/src/providers/paypal_card/index.d.ts +1 -0
  174. package/.medusa/server/src/providers/paypal_card/index.d.ts.map +1 -1
  175. package/.medusa/server/src/providers/paypal_card/index.js +1 -0
  176. package/.medusa/server/src/providers/paypal_card/index.js.map +1 -1
  177. package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts +6 -0
  178. package/.medusa/server/src/subscribers/paypal-order-invoice.d.ts.map +1 -1
  179. package/.medusa/server/src/subscribers/paypal-order-invoice.js +9 -1
  180. package/.medusa/server/src/subscribers/paypal-order-invoice.js.map +1 -1
  181. package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts +1 -0
  182. package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
  183. package/.medusa/server/src/subscribers/paypal-webhook-process.js +17 -4
  184. package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
  185. package/CHANGELOG.md +11 -0
  186. package/README.md +184 -184
  187. package/package.json +7 -4
  188. package/src/admin/index.ts +1 -1
  189. package/src/admin/routes/settings/paypal/_components/FieldRow.tsx +38 -29
  190. package/src/admin/routes/settings/paypal/_components/SectionCard.tsx +29 -26
  191. package/src/admin/routes/settings/paypal/_components/Tabs.tsx +49 -48
  192. package/src/admin/routes/settings/paypal/_components/Toast.tsx +63 -52
  193. package/src/admin/routes/settings/paypal/_utils/adminFetch.ts +52 -32
  194. package/src/admin/routes/settings/paypal/additional-settings/page.tsx +246 -139
  195. package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +208 -123
  196. package/src/admin/routes/settings/paypal/apple-pay/page.tsx +9 -5
  197. package/src/admin/routes/settings/paypal/connection/page.tsx +974 -908
  198. package/src/admin/routes/settings/paypal/google-pay/page.tsx +9 -5
  199. package/src/admin/routes/settings/paypal/page.tsx +14 -13
  200. package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +9 -5
  201. package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +304 -273
  202. package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +48 -39
  203. package/src/api/admin/paypal/disconnect/route.ts +15 -14
  204. package/src/api/admin/paypal/environment/route.ts +34 -32
  205. package/src/api/admin/paypal/onboard-complete/route.ts +61 -50
  206. package/src/api/admin/paypal/onboarding-link/route.ts +91 -76
  207. package/src/api/admin/paypal/onboarding-status/route.ts +21 -20
  208. package/src/api/admin/paypal/save-credentials/route.ts +48 -37
  209. package/src/api/admin/paypal/settings/route.ts +63 -45
  210. package/src/api/admin/paypal/status/route.ts +29 -17
  211. package/src/api/hooks/paypal/onboard-return/route.ts +15 -15
  212. package/src/api/hooks/paypal/webhook/route.ts +17 -17
  213. package/src/api/middlewares.ts +61 -61
  214. package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +63 -54
  215. package/src/api/store/paypal/capture-order/route.ts +416 -370
  216. package/src/api/store/paypal/config/route.ts +121 -115
  217. package/src/api/store/paypal/create-order/route.ts +586 -539
  218. package/src/api/store/paypal/onboard-return/route.ts +116 -113
  219. package/src/api/store/paypal/settings/route.ts +29 -19
  220. package/src/api/store/paypal/webhook/route.ts +380 -314
  221. package/src/api/store/paypal-complete/route.ts +157 -128
  222. package/src/api/utils/rate-limit.ts +143 -129
  223. package/src/index.ts +1 -1
  224. package/src/jobs/paypal-webhook-retry.ts +257 -241
  225. package/src/modules/paypal/index.ts +9 -8
  226. package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -33
  227. package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -22
  228. package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -29
  229. package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -27
  230. package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -31
  231. package/src/modules/paypal/migrations/20261101090000_remove_paypal_reconciliation_status.ts +25 -25
  232. package/src/modules/paypal/migrations/20261201090000_remove_paypal_audit_log.ts +26 -26
  233. package/src/modules/paypal/migrations/20270101090000_set_paypal_environment_default_live.ts +15 -11
  234. package/src/modules/paypal/migrations/20270102090000_add_paypal_seller_email.ts +15 -11
  235. package/src/modules/paypal/migrations/20270103090000_add_paypal_seller_merchant_id.ts +15 -11
  236. package/src/modules/paypal/migrations/20270201000000_add_webhook_dead_letter.ts +17 -17
  237. package/src/modules/paypal/migrations/20270202000000_align_nullable_defaults.ts +44 -44
  238. package/src/modules/paypal/migrations/20270301000000_add_webhook_transmission_id_index.ts +15 -15
  239. package/src/modules/paypal/models/paypal_connection.ts +28 -23
  240. package/src/modules/paypal/models/paypal_metric.ts +13 -9
  241. package/src/modules/paypal/models/paypal_settings.ts +13 -8
  242. package/src/modules/paypal/models/paypal_webhook_event.ts +26 -19
  243. package/src/modules/paypal/payment-provider/base-provider.ts +287 -275
  244. package/src/modules/paypal/payment-provider/card-service.ts +927 -855
  245. package/src/modules/paypal/payment-provider/index.ts +14 -15
  246. package/src/modules/paypal/payment-provider/service.ts +906 -836
  247. package/src/modules/paypal/payment-provider/status-utils.ts +59 -59
  248. package/src/modules/paypal/payment-provider/webhook-utils.ts +109 -109
  249. package/src/modules/paypal/service.ts +362 -115
  250. package/src/modules/paypal/types/config.ts +70 -65
  251. package/src/modules/paypal/utils/amounts.ts +123 -107
  252. package/src/modules/paypal/utils/core-workflow.ts +46 -46
  253. package/src/modules/paypal/utils/credential-resolver.ts +275 -271
  254. package/src/modules/paypal/utils/currencies.ts +97 -84
  255. package/src/modules/paypal/utils/partner.ts +8 -8
  256. package/src/modules/paypal/utils/payment-session.ts +144 -138
  257. package/src/modules/paypal/utils/paypal-auth.ts +6 -5
  258. package/src/modules/paypal/utils/paypal-fetch.ts +197 -181
  259. package/src/modules/paypal/utils/provider-ids.ts +21 -15
  260. package/src/modules/paypal/utils/secret-crypto.ts +226 -197
  261. package/src/modules/paypal/utils/webhook-verify.ts +61 -61
  262. package/src/modules/paypal/webhook-processor.ts +641 -573
  263. package/src/providers/paypal/index.ts +9 -8
  264. package/src/providers/paypal_card/index.ts +9 -8
  265. package/src/subscribers/paypal-order-invoice.ts +180 -173
  266. package/src/subscribers/paypal-webhook-process.ts +147 -131
  267. package/tsconfig.json +21 -31
@@ -1,128 +1,157 @@
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 { 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
+ /**
9
+ * POST /store/paypal-complete — finalize a cart after the buyer's PayPal
10
+ * payment was captured/authorized.
11
+ *
12
+ * Authorizes the PayPal payment session (which re-verifies the payment against
13
+ * PayPal) and then runs the core complete-cart workflow. A cart completed by a
14
+ * concurrent request (e.g. the webhook safety net) is reported as success.
15
+ */
16
+ export async function POST(req: MedusaRequest, res: MedusaResponse) {
17
+ const logger = req.scope.resolve<Logger>(ContainerRegistrationKeys.LOGGER)
18
+ const requestId = randomUUID()
19
+ const { cart_id } = (req.body || {}) as { cart_id: string }
20
+
21
+ if (!cart_id || typeof cart_id !== "string") {
22
+ return res.status(400).json({ message: "cart_id is required" })
23
+ }
24
+
25
+ if (!cart_id.startsWith("cart_")) {
26
+ return res.status(400).json({ message: "Invalid cart_id format" })
27
+ }
28
+
29
+ try {
30
+ const query = req.scope.resolve("query")
31
+ const { data: carts } = await query.graph({
32
+ entity: "cart",
33
+ fields: [
34
+ "id",
35
+ "completed_at",
36
+ "payment_collection.payment_sessions.id",
37
+ "payment_collection.payment_sessions.data",
38
+ "payment_collection.payment_sessions.status",
39
+ "payment_collection.payment_sessions.provider_id",
40
+ "payment_collection.payment_sessions.created_at",
41
+ "payment_collection.payment_sessions.amount",
42
+ "payment_collection.payment_sessions.currency_code",
43
+ ],
44
+ filters: { id: cart_id },
45
+ })
46
+
47
+ const cart = carts?.[0]
48
+ if (cart?.completed_at) {
49
+ return res.json({ success: true, cart_id, already_completed: true })
50
+ }
51
+
52
+ const sessions = cart?.payment_collection?.payment_sessions || []
53
+ const session = sessions
54
+ .filter((s: any) => isPayPalProviderId(s.provider_id))
55
+ .sort(
56
+ (a: any, b: any) =>
57
+ new Date(b.created_at || 0).getTime() -
58
+ new Date(a.created_at || 0).getTime()
59
+ )[0]
60
+
61
+ if (!session) {
62
+ return res
63
+ .status(400)
64
+ .json({ message: "No PayPal payment session found for cart" })
65
+ }
66
+
67
+ const paymentModule = req.scope.resolve(
68
+ Modules.PAYMENT
69
+ ) as IPaymentModuleService
70
+
71
+ // Do NOT fabricate captured_at/authorized_at here. The authoritative
72
+ // timestamp and status are set by the provider's authorizePayment (invoked
73
+ // via authorizePaymentSession below) only after it verifies the capture /
74
+ // authorization against PayPal. Stamping it ourselves would mark an unpaid
75
+ // session as paid.
76
+
77
+ const currentStatus = String(session.status || "")
78
+ if (currentStatus !== "authorized") {
79
+ try {
80
+ await (paymentModule as any).authorizePaymentSession(session.id, {})
81
+ logger.info(
82
+ `[paypal] paypal-complete authorizePaymentSession succeeded (request_id=${requestId}, session_id=${session.id})`
83
+ )
84
+ } catch (e: any) {
85
+ // Authorization is the gate for completion; if it fails, the
86
+ // completeCartWorkflow below fails too and we report that honestly.
87
+ logger.warn(
88
+ `[paypal] paypal-complete authorizePaymentSession failed (request_id=${requestId}, session_id=${session.id}): ${
89
+ e?.message ?? String(e)
90
+ }`
91
+ )
92
+ }
93
+ }
94
+
95
+ try {
96
+ const { result } = await runCoreWorkflow<{ id: string }, { id?: string }>(
97
+ req.scope,
98
+ "complete-cart",
99
+ { id: cart_id }
100
+ )
101
+ logger.info(
102
+ `[paypal] paypal-complete cart completed (request_id=${requestId}, cart_id=${cart_id}, order_id=${result?.id})`
103
+ )
104
+ return res.json({
105
+ success: true,
106
+ cart_id,
107
+ order_id: result?.id,
108
+ request_id: requestId,
109
+ })
110
+ } catch (e: any) {
111
+ // The cart may have been completed by a concurrent request between our
112
+ // initial check and now — treat that as success, not an error.
113
+ const recheck = await query
114
+ .graph({
115
+ entity: "cart",
116
+ fields: ["id", "completed_at"],
117
+ filters: { id: cart_id },
118
+ })
119
+ .catch(() => ({ data: [] as any[] }))
120
+ if (recheck?.data?.[0]?.completed_at) {
121
+ return res.json({
122
+ success: true,
123
+ cart_id,
124
+ already_completed: true,
125
+ request_id: requestId,
126
+ })
127
+ }
128
+
129
+ logger.error(
130
+ `[paypal] paypal-complete completeCartWorkflow failed (request_id=${requestId}, cart_id=${cart_id}): ${
131
+ e?.message ?? String(e)
132
+ }`,
133
+ e instanceof Error ? e : undefined
134
+ )
135
+ // The payment may have been captured at PayPal but the order was not
136
+ // finalized. Report failure (not a false success) so the storefront can
137
+ // surface a "payment taken, contact support" state.
138
+ return res.status(500).json({
139
+ success: false,
140
+ cart_id,
141
+ session_id: session.id,
142
+ message: "Payment was processed but the order could not be finalized.",
143
+ request_id: requestId,
144
+ })
145
+ }
146
+ } catch (e: any) {
147
+ logger.error(
148
+ `[paypal] paypal-complete failed (request_id=${requestId}, cart_id=${cart_id}): ${
149
+ e?.message ?? String(e)
150
+ }`,
151
+ e instanceof Error ? e : undefined
152
+ )
153
+ return res
154
+ .status(500)
155
+ .json({ message: "Internal error", request_id: requestId })
156
+ }
157
+ }
@@ -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"