@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,181 +1,197 @@
1
- /**
2
- * Outbound HTTP for PayPal API calls with timeout, circuit breaker, and
3
- * optional retry for critical payment operations.
4
- */
5
-
6
- export const PAYPAL_HTTP_TIMEOUT_MS = (() => {
7
- const v = Number(process.env.PAYPAL_HTTP_TIMEOUT_MS)
8
- return Number.isFinite(v) && v > 0 ? v : 30_000
9
- })()
10
-
11
- // ---------------------------------------------------------------------------
12
- // Circuit breaker
13
- // ---------------------------------------------------------------------------
14
-
15
- const CB_FAILURE_THRESHOLD = 5
16
- const CB_WINDOW_MS = 30_000
17
- const CB_HALF_OPEN_MS = 60_000
18
-
19
- type CBState = "closed" | "open" | "half-open"
20
-
21
- // One breaker per target host: `paypalFetch` is also used for non-PayPal hosts
22
- // (onboarding service, alert webhooks). A shared breaker would let five
23
- // failures of a side-channel host block live captures/refunds against a
24
- // perfectly healthy PayPal API.
25
- type Breaker = {
26
- state: CBState
27
- failures: number[]
28
- openedAt: number
29
- halfOpenProbeInFlight: boolean
30
- }
31
-
32
- const breakers = new Map<string, Breaker>()
33
-
34
- function cbHostKey(input: string | URL): string {
35
- try {
36
- return new URL(String(input)).host || "default"
37
- } catch {
38
- return "default"
39
- }
40
- }
41
-
42
- function getBreaker(host: string): Breaker {
43
- let b = breakers.get(host)
44
- if (!b) {
45
- b = { state: "closed", failures: [], openedAt: 0, halfOpenProbeInFlight: false }
46
- breakers.set(host, b)
47
- }
48
- return b
49
- }
50
-
51
- function cbRecordSuccess(host: string) {
52
- const b = getBreaker(host)
53
- b.state = "closed"
54
- b.failures = []
55
- b.halfOpenProbeInFlight = false
56
- }
57
-
58
- function cbRecordFailure(host: string) {
59
- const b = getBreaker(host)
60
- b.halfOpenProbeInFlight = false
61
- const now = Date.now()
62
- b.failures = b.failures.filter((t) => now - t < CB_WINDOW_MS)
63
- b.failures.push(now)
64
- if (b.failures.length >= CB_FAILURE_THRESHOLD) {
65
- b.state = "open"
66
- b.openedAt = now
67
- console.warn(
68
- `[PayPal] circuit breaker OPEN for ${host} after ${CB_FAILURE_THRESHOLD} failures in ${CB_WINDOW_MS / 1000}s`
69
- )
70
- }
71
- }
72
-
73
- function cbAllowRequest(host: string): boolean {
74
- const b = getBreaker(host)
75
- if (b.state === "closed") return true
76
- if (b.state === "open") {
77
- if (Date.now() - b.openedAt >= CB_HALF_OPEN_MS) {
78
- b.state = "half-open"
79
- b.halfOpenProbeInFlight = true
80
- return true
81
- }
82
- return false
83
- }
84
- // half-open: allow only one probe at a time
85
- if (b.halfOpenProbeInFlight) return false
86
- b.halfOpenProbeInFlight = true
87
- return true
88
- }
89
-
90
- class CircuitOpenError extends Error {
91
- constructor() {
92
- super(
93
- "PayPal circuit breaker is open — API calls are temporarily blocked after repeated failures. Will retry automatically."
94
- )
95
- this.name = "CircuitOpenError"
96
- }
97
- }
98
-
99
- // ---------------------------------------------------------------------------
100
- // Core fetch with timeout + circuit breaker
101
- // ---------------------------------------------------------------------------
102
-
103
- export function paypalFetch(
104
- input: string | URL,
105
- init: RequestInit = {}
106
- ): Promise<Response> {
107
- const host = cbHostKey(input)
108
- if (!cbAllowRequest(host)) {
109
- return Promise.reject(new CircuitOpenError())
110
- }
111
-
112
- const fetchPromise = init.signal
113
- ? fetch(input, init)
114
- : fetch(input, {
115
- ...init,
116
- signal: AbortSignal.timeout(PAYPAL_HTTP_TIMEOUT_MS),
117
- })
118
-
119
- return fetchPromise.then(
120
- (res) => {
121
- if (res.status >= 500) {
122
- cbRecordFailure(host)
123
- } else {
124
- cbRecordSuccess(host)
125
- }
126
- return res
127
- },
128
- (err) => {
129
- cbRecordFailure(host)
130
- throw err
131
- }
132
- )
133
- }
134
-
135
- // ---------------------------------------------------------------------------
136
- // Retry wrapper for critical payment operations (capture, refund)
137
- // ---------------------------------------------------------------------------
138
-
139
- const MAX_RETRIES = 2
140
- const BASE_DELAY_MS = 500
141
-
142
- function jitteredDelay(attempt: number): number {
143
- const base = BASE_DELAY_MS * Math.pow(2, attempt)
144
- return base + Math.random() * base
145
- }
146
-
147
- function isRetryable(err: unknown): boolean {
148
- if (err instanceof CircuitOpenError) return true
149
- if (err instanceof Error) {
150
- if (err.name === "TimeoutError" || err.name === "AbortError") return true
151
- }
152
- return false
153
- }
154
-
155
- function isRetryableStatus(status: number): boolean {
156
- return status >= 500 || status === 429
157
- }
158
-
159
- export async function paypalFetchWithRetry(
160
- input: string | URL,
161
- init: RequestInit = {}
162
- ): Promise<Response> {
163
- let lastError: unknown
164
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
165
- try {
166
- const res = await paypalFetch(input, init)
167
- if (isRetryableStatus(res.status) && attempt < MAX_RETRIES) {
168
- const delay = jitteredDelay(attempt)
169
- await new Promise((r) => setTimeout(r, delay))
170
- continue
171
- }
172
- return res
173
- } catch (err) {
174
- lastError = err
175
- if (!isRetryable(err) || attempt >= MAX_RETRIES) throw err
176
- const delay = jitteredDelay(attempt)
177
- await new Promise((r) => setTimeout(r, delay))
178
- }
179
- }
180
- throw lastError
181
- }
1
+ /**
2
+ * Outbound HTTP for PayPal API calls with timeout, circuit breaker, and
3
+ * optional retry for critical payment operations.
4
+ */
5
+
6
+ export const PAYPAL_HTTP_TIMEOUT_MS = (() => {
7
+ const v = Number(process.env.PAYPAL_HTTP_TIMEOUT_MS)
8
+ return Number.isFinite(v) && v > 0 ? v : 30_000
9
+ })()
10
+
11
+ // ---------------------------------------------------------------------------
12
+ // Circuit breaker
13
+ // ---------------------------------------------------------------------------
14
+
15
+ const CB_FAILURE_THRESHOLD = 5
16
+ const CB_WINDOW_MS = 30_000
17
+ const CB_HALF_OPEN_MS = 60_000
18
+
19
+ type CBState = "closed" | "open" | "half-open"
20
+
21
+ // One breaker per target host: `paypalFetch` is also used for non-PayPal hosts
22
+ // (onboarding service, alert webhooks). A shared breaker would let five
23
+ // failures of a side-channel host block live captures/refunds against a
24
+ // perfectly healthy PayPal API.
25
+ type Breaker = {
26
+ state: CBState
27
+ failures: number[]
28
+ openedAt: number
29
+ halfOpenProbeInFlight: boolean
30
+ }
31
+
32
+ const breakers = new Map<string, Breaker>()
33
+
34
+ function cbHostKey(input: string | URL): string {
35
+ try {
36
+ return new URL(String(input)).host || "default"
37
+ } catch {
38
+ return "default"
39
+ }
40
+ }
41
+
42
+ function getBreaker(host: string): Breaker {
43
+ let b = breakers.get(host)
44
+ if (!b) {
45
+ b = {
46
+ state: "closed",
47
+ failures: [],
48
+ openedAt: 0,
49
+ halfOpenProbeInFlight: false,
50
+ }
51
+ breakers.set(host, b)
52
+ }
53
+ return b
54
+ }
55
+
56
+ function cbRecordSuccess(host: string) {
57
+ const b = getBreaker(host)
58
+ b.state = "closed"
59
+ b.failures = []
60
+ b.halfOpenProbeInFlight = false
61
+ }
62
+
63
+ function cbRecordFailure(host: string) {
64
+ const b = getBreaker(host)
65
+ b.halfOpenProbeInFlight = false
66
+ const now = Date.now()
67
+ b.failures = b.failures.filter((t) => now - t < CB_WINDOW_MS)
68
+ b.failures.push(now)
69
+ if (b.failures.length >= CB_FAILURE_THRESHOLD) {
70
+ b.state = "open"
71
+ b.openedAt = now
72
+ console.warn(
73
+ `[PayPal] circuit breaker OPEN for ${host} after ${CB_FAILURE_THRESHOLD} failures in ${CB_WINDOW_MS / 1000}s`
74
+ )
75
+ }
76
+ }
77
+
78
+ function cbAllowRequest(host: string): boolean {
79
+ const b = getBreaker(host)
80
+ if (b.state === "closed") return true
81
+ if (b.state === "open") {
82
+ if (Date.now() - b.openedAt >= CB_HALF_OPEN_MS) {
83
+ b.state = "half-open"
84
+ b.halfOpenProbeInFlight = true
85
+ return true
86
+ }
87
+ return false
88
+ }
89
+ // half-open: allow only one probe at a time
90
+ if (b.halfOpenProbeInFlight) return false
91
+ b.halfOpenProbeInFlight = true
92
+ return true
93
+ }
94
+
95
+ class CircuitOpenError extends Error {
96
+ constructor() {
97
+ super(
98
+ "PayPal circuit breaker is open — API calls are temporarily blocked after repeated failures. Will retry automatically."
99
+ )
100
+ this.name = "CircuitOpenError"
101
+ }
102
+ }
103
+
104
+ // ---------------------------------------------------------------------------
105
+ // Core fetch with timeout + circuit breaker
106
+ // ---------------------------------------------------------------------------
107
+
108
+ /**
109
+ * `fetch` with a default timeout (`PAYPAL_HTTP_TIMEOUT_MS`) and a per-host
110
+ * circuit breaker. 5xx responses and network/timeout errors count as failures;
111
+ * once the breaker opens, calls to that host reject immediately with
112
+ * `CircuitOpenError` until the half-open probe succeeds.
113
+ */
114
+ export function paypalFetch(
115
+ input: string | URL,
116
+ init: RequestInit = {}
117
+ ): Promise<Response> {
118
+ const host = cbHostKey(input)
119
+ if (!cbAllowRequest(host)) {
120
+ return Promise.reject(new CircuitOpenError())
121
+ }
122
+
123
+ const fetchPromise = init.signal
124
+ ? fetch(input, init)
125
+ : fetch(input, {
126
+ ...init,
127
+ signal: AbortSignal.timeout(PAYPAL_HTTP_TIMEOUT_MS),
128
+ })
129
+
130
+ return fetchPromise.then(
131
+ (res) => {
132
+ if (res.status >= 500) {
133
+ cbRecordFailure(host)
134
+ } else {
135
+ cbRecordSuccess(host)
136
+ }
137
+ return res
138
+ },
139
+ (err) => {
140
+ cbRecordFailure(host)
141
+ throw err
142
+ }
143
+ )
144
+ }
145
+
146
+ // ---------------------------------------------------------------------------
147
+ // Retry wrapper for critical payment operations (capture, refund)
148
+ // ---------------------------------------------------------------------------
149
+
150
+ const MAX_RETRIES = 2
151
+ const BASE_DELAY_MS = 500
152
+
153
+ function jitteredDelay(attempt: number): number {
154
+ const base = BASE_DELAY_MS * Math.pow(2, attempt)
155
+ return base + Math.random() * base
156
+ }
157
+
158
+ function isRetryable(err: unknown): boolean {
159
+ if (err instanceof CircuitOpenError) return true
160
+ if (err instanceof Error) {
161
+ if (err.name === "TimeoutError" || err.name === "AbortError") return true
162
+ }
163
+ return false
164
+ }
165
+
166
+ function isRetryableStatus(status: number): boolean {
167
+ return status >= 500 || status === 429
168
+ }
169
+
170
+ /**
171
+ * `paypalFetch` with bounded exponential-backoff retries on 5xx/429, timeouts,
172
+ * and an open circuit. Only use for calls that are idempotent on PayPal's side
173
+ * (i.e. carry a `PayPal-Request-Id`) — otherwise a retry can double-charge.
174
+ */
175
+ export async function paypalFetchWithRetry(
176
+ input: string | URL,
177
+ init: RequestInit = {}
178
+ ): Promise<Response> {
179
+ let lastError: unknown
180
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
181
+ try {
182
+ const res = await paypalFetch(input, init)
183
+ if (isRetryableStatus(res.status) && attempt < MAX_RETRIES) {
184
+ const delay = jitteredDelay(attempt)
185
+ await new Promise((r) => setTimeout(r, delay))
186
+ continue
187
+ }
188
+ return res
189
+ } catch (err) {
190
+ lastError = err
191
+ if (!isRetryable(err) || attempt >= MAX_RETRIES) throw err
192
+ const delay = jitteredDelay(attempt)
193
+ await new Promise((r) => setTimeout(r, delay))
194
+ }
195
+ }
196
+ throw lastError
197
+ }
@@ -1,15 +1,21 @@
1
- export const PAYPAL_WALLET_PROVIDER_ID = "pp_paypal_paypal" as const
2
- export const PAYPAL_CARD_PROVIDER_ID = "pp_paypal_card_paypal_card" as const
3
-
4
- export const PAYPAL_PROVIDER_IDS = [
5
- PAYPAL_WALLET_PROVIDER_ID,
6
- PAYPAL_CARD_PROVIDER_ID,
7
- ] as const
8
-
9
- export const isPayPalProviderId = (providerId?: string | null) => {
10
- if (!providerId) return false
11
-
12
- return PAYPAL_PROVIDER_IDS.includes(
13
- providerId as (typeof PAYPAL_PROVIDER_IDS)[number]
14
- )
15
- }
1
+ /**
2
+ * Fully-qualified Medusa payment-provider ids for the two PayPal providers
3
+ * (`pp_<module-key>_<provider identifier>`), as they appear on payment
4
+ * sessions and in region configuration.
5
+ */
6
+ export const PAYPAL_WALLET_PROVIDER_ID = "pp_paypal_paypal" as const
7
+ export const PAYPAL_CARD_PROVIDER_ID = "pp_paypal_card_paypal_card" as const
8
+
9
+ export const PAYPAL_PROVIDER_IDS = [
10
+ PAYPAL_WALLET_PROVIDER_ID,
11
+ PAYPAL_CARD_PROVIDER_ID,
12
+ ] as const
13
+
14
+ /** Whether a payment session's `provider_id` belongs to this plugin. */
15
+ export const isPayPalProviderId = (providerId?: string | null) => {
16
+ if (!providerId) return false
17
+
18
+ return PAYPAL_PROVIDER_IDS.includes(
19
+ providerId as (typeof PAYPAL_PROVIDER_IDS)[number]
20
+ )
21
+ }