@easypayment/medusa-paypal 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +2127 -0
- package/.medusa/server/src/admin/index.mjs +2128 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js +25 -0
- package/.medusa/server/src/api/admin/payment-collections/[id]/payment-sessions/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/audit-logs/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/audit-logs/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/audit-logs/route.js +12 -0
- package/.medusa/server/src/api/admin/paypal/audit-logs/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js +9 -0
- package/.medusa/server/src/api/admin/paypal/disconnect/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/[id]/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/disputes/[id]/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/[id]/route.js +17 -0
- package/.medusa/server/src/api/admin/paypal/disputes/[id]/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/disputes/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/route.js +27 -0
- package/.medusa/server/src/api/admin/paypal/disputes/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/summary/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/disputes/summary/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/disputes/summary/route.js +17 -0
- package/.medusa/server/src/api/admin/paypal/disputes/summary/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.js +23 -0
- package/.medusa/server/src/api/admin/paypal/environment/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts +8 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js +41 -0
- package/.medusa/server/src/api/admin/paypal/onboard-complete/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js +35 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-link/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js +20 -0
- package/.medusa/server/src/api/admin/paypal/onboarding-status/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/reconciliation-status/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/reconciliation-status/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/reconciliation-status/route.js +8 -0
- package/.medusa/server/src/api/admin/paypal/reconciliation-status/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/rotate-credentials/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/rotate-credentials/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/rotate-credentials/route.js +9 -0
- package/.medusa/server/src/api/admin/paypal/rotate-credentials/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js +13 -0
- package/.medusa/server/src/api/admin/paypal/save-credentials/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.js +14 -0
- package/.medusa/server/src/api/admin/paypal/settings/route.js.map +1 -0
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/paypal/status/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/status/route.js +11 -0
- package/.medusa/server/src/api/admin/paypal/status/route.js.map +1 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts +3 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js +43 -0
- package/.medusa/server/src/api/store/payment-collections/[id]/payment-sessions/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.js +215 -0
- package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/config/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/config/route.js +45 -0
- package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.js +305 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/disputes/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/disputes/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/disputes/route.js +46 -0
- package/.medusa/server/src/api/store/paypal/disputes/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/settings/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/settings/route.js +14 -0
- package/.medusa/server/src/api/store/paypal/settings/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts +3 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.d.ts.map +1 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.js +203 -0
- package/.medusa/server/src/api/store/paypal/webhook/route.js.map +1 -0
- package/.medusa/server/src/jobs/paypal-reconcile.d.ts +7 -0
- package/.medusa/server/src/jobs/paypal-reconcile.d.ts.map +1 -0
- package/.medusa/server/src/jobs/paypal-reconcile.js +131 -0
- package/.medusa/server/src/jobs/paypal-reconcile.js.map +1 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts +7 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.js +78 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -0
- package/.medusa/server/src/modules/paypal/clients/paypal-seller.client.d.ts +14 -0
- package/.medusa/server/src/modules/paypal/clients/paypal-seller.client.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/clients/paypal-seller.client.js +65 -0
- package/.medusa/server/src/modules/paypal/clients/paypal-seller.client.js.map +1 -0
- package/.medusa/server/src/modules/paypal/index.d.ts +92 -0
- package/.medusa/server/src/modules/paypal/index.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/index.js +13 -0
- package/.medusa/server/src/modules/paypal/index.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js +36 -0
- package/.medusa/server/src/modules/paypal/migrations/20260115120000_create_paypal_connection.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js +25 -0
- package/.medusa/server/src/modules/paypal/migrations/20260123090000_create_paypal_settings.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js +32 -0
- package/.medusa/server/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260301090000_create_paypal_audit_log.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260301090000_create_paypal_audit_log.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260301090000_create_paypal_audit_log.js +29 -0
- package/.medusa/server/src/modules/paypal/migrations/20260301090000_create_paypal_audit_log.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js +30 -0
- package/.medusa/server/src/modules/paypal/migrations/20260401090000_create_paypal_metric.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260501090000_create_paypal_dispute.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260501090000_create_paypal_dispute.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260501090000_create_paypal_dispute.js +43 -0
- package/.medusa/server/src/modules/paypal/migrations/20260501090000_create_paypal_dispute.js.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js +34 -0
- package/.medusa/server/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_audit_log.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_audit_log.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_audit_log.js +10 -0
- package/.medusa/server/src/modules/paypal/models/paypal_audit_log.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts +14 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js +17 -0
- package/.medusa/server/src/modules/paypal/models/paypal_connection.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_dispute.d.ts +16 -0
- package/.medusa/server/src/modules/paypal/models/paypal_dispute.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_dispute.js +19 -0
- package/.medusa/server/src/modules/paypal/models/paypal_dispute.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js +10 -0
- package/.medusa/server/src/modules/paypal/models/paypal_metric.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts +6 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js +9 -0
- package/.medusa/server/src/modules/paypal/models/paypal_settings.js.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts +17 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js +20 -0
- package/.medusa/server/src/modules/paypal/models/paypal_webhook_event.js.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts +35 -0
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +569 -0
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts +10 -0
- package/.medusa/server/src/modules/paypal/payment-provider/index.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/index.js +22 -0
- package/.medusa/server/src/modules/paypal/payment-provider/index.js.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +44 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +825 -0
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/webhook-utils.d.ts +3 -0
- package/.medusa/server/src/modules/paypal/payment-provider/webhook-utils.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/payment-provider/webhook-utils.js +74 -0
- package/.medusa/server/src/modules/paypal/payment-provider/webhook-utils.js.map +1 -0
- package/.medusa/server/src/modules/paypal/service.d.ts +362 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/service.js +1180 -0
- package/.medusa/server/src/modules/paypal/service.js.map +1 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts +14 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/types/config.js +33 -0
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +3 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.js +40 -0
- package/.medusa/server/src/modules/paypal/utils/amounts.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/crypto.d.ts +4 -0
- package/.medusa/server/src/modules/paypal/utils/crypto.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/crypto.js +47 -0
- package/.medusa/server/src/modules/paypal/utils/crypto.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts +19 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.js +69 -0
- package/.medusa/server/src/modules/paypal/utils/currencies.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts +9 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js +50 -0
- package/.medusa/server/src/modules/paypal/utils/provider-ids.js.map +1 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +38 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.js +265 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -0
- package/LICENSE +21 -0
- package/README.md +67 -0
- package/package.json +61 -0
- package/postcss.config.cjs +3 -0
- package/src/admin/index.ts +7 -0
- package/src/admin/routes/settings/paypal/_components/Tabs.tsx +55 -0
- package/src/admin/routes/settings/paypal/_components/Toast.tsx +51 -0
- package/src/admin/routes/settings/paypal/additional-settings/page.tsx +346 -0
- package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +381 -0
- package/src/admin/routes/settings/paypal/apple-pay/page.tsx +5 -0
- package/src/admin/routes/settings/paypal/audit-logs/page.tsx +131 -0
- package/src/admin/routes/settings/paypal/connection/page.tsx +750 -0
- package/src/admin/routes/settings/paypal/disputes/page.tsx +259 -0
- package/src/admin/routes/settings/paypal/google-pay/page.tsx +5 -0
- package/src/admin/routes/settings/paypal/page.tsx +16 -0
- package/src/admin/routes/settings/paypal/pay-later-messaging/page.tsx +5 -0
- package/src/admin/routes/settings/paypal/paypal-settings/page.tsx +557 -0
- package/src/admin/routes/settings/paypal/reconciliation-status/page.tsx +165 -0
- package/src/api/admin/payment-collections/[id]/payment-sessions/route.ts +32 -0
- package/src/api/admin/paypal/audit-logs/route.ts +13 -0
- package/src/api/admin/paypal/disconnect/route.ts +8 -0
- package/src/api/admin/paypal/disputes/[id]/route.ts +19 -0
- package/src/api/admin/paypal/disputes/route.ts +30 -0
- package/src/api/admin/paypal/disputes/summary/route.ts +18 -0
- package/src/api/admin/paypal/environment/route.ts +25 -0
- package/src/api/admin/paypal/onboard-complete/route.ts +44 -0
- package/src/api/admin/paypal/onboarding-link/route.ts +45 -0
- package/src/api/admin/paypal/onboarding-status/route.ts +18 -0
- package/src/api/admin/paypal/reconciliation-status/route.ts +7 -0
- package/src/api/admin/paypal/rotate-credentials/route.ts +8 -0
- package/src/api/admin/paypal/save-credentials/route.ts +14 -0
- package/src/api/admin/paypal/settings/route.ts +14 -0
- package/src/api/admin/paypal/status/route.ts +12 -0
- package/src/api/store/payment-collections/[id]/payment-sessions/route.ts +51 -0
- package/src/api/store/paypal/capture-order/route.ts +270 -0
- package/src/api/store/paypal/config/route.ts +59 -0
- package/src/api/store/paypal/create-order/route.ts +374 -0
- package/src/api/store/paypal/disputes/route.ts +67 -0
- package/src/api/store/paypal/settings/route.ts +12 -0
- package/src/api/store/paypal/webhook/route.ts +247 -0
- package/src/jobs/paypal-reconcile.ts +135 -0
- package/src/jobs/paypal-webhook-retry.ts +86 -0
- package/src/modules/paypal/clients/paypal-seller.client.ts +59 -0
- package/src/modules/paypal/index.ts +8 -0
- package/src/modules/paypal/migrations/20260115120000_create_paypal_connection.ts +33 -0
- package/src/modules/paypal/migrations/20260123090000_create_paypal_settings.ts +22 -0
- package/src/modules/paypal/migrations/20260201090000_create_paypal_webhook_event.ts +29 -0
- package/src/modules/paypal/migrations/20260301090000_create_paypal_audit_log.ts +26 -0
- package/src/modules/paypal/migrations/20260401090000_create_paypal_metric.ts +27 -0
- package/src/modules/paypal/migrations/20260501090000_create_paypal_dispute.ts +40 -0
- package/src/modules/paypal/migrations/20260701090000_add_paypal_webhook_event_processing.ts +31 -0
- package/src/modules/paypal/models/paypal_audit_log.ts +9 -0
- package/src/modules/paypal/models/paypal_connection.ts +21 -0
- package/src/modules/paypal/models/paypal_dispute.ts +18 -0
- package/src/modules/paypal/models/paypal_metric.ts +9 -0
- package/src/modules/paypal/models/paypal_settings.ts +8 -0
- package/src/modules/paypal/models/paypal_webhook_event.ts +19 -0
- package/src/modules/paypal/payment-provider/README.md +22 -0
- package/src/modules/paypal/payment-provider/card-service.ts +710 -0
- package/src/modules/paypal/payment-provider/index.ts +19 -0
- package/src/modules/paypal/payment-provider/service.ts +1035 -0
- package/src/modules/paypal/payment-provider/webhook-utils.ts +88 -0
- package/src/modules/paypal/service.ts +1422 -0
- package/src/modules/paypal/types/config.ts +47 -0
- package/src/modules/paypal/utils/amounts.ts +41 -0
- package/src/modules/paypal/utils/crypto.ts +51 -0
- package/src/modules/paypal/utils/currencies.ts +84 -0
- package/src/modules/paypal/utils/provider-ids.ts +53 -0
- package/src/modules/paypal/webhook-processor.ts +313 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
import { AbstractPaymentProvider } from "@medusajs/framework/utils"
|
|
2
|
+
import { randomUUID } from "crypto"
|
|
3
|
+
import type {
|
|
4
|
+
AuthorizePaymentInput,
|
|
5
|
+
AuthorizePaymentOutput,
|
|
6
|
+
CapturePaymentInput,
|
|
7
|
+
CapturePaymentOutput,
|
|
8
|
+
CancelPaymentInput,
|
|
9
|
+
CancelPaymentOutput,
|
|
10
|
+
CreateAccountHolderInput,
|
|
11
|
+
CreateAccountHolderOutput,
|
|
12
|
+
DeletePaymentInput,
|
|
13
|
+
DeletePaymentOutput,
|
|
14
|
+
GetPaymentStatusInput,
|
|
15
|
+
GetPaymentStatusOutput,
|
|
16
|
+
InitiatePaymentInput,
|
|
17
|
+
InitiatePaymentOutput,
|
|
18
|
+
RefundPaymentInput,
|
|
19
|
+
RefundPaymentOutput,
|
|
20
|
+
RetrievePaymentInput,
|
|
21
|
+
RetrievePaymentOutput,
|
|
22
|
+
UpdatePaymentInput,
|
|
23
|
+
UpdatePaymentOutput,
|
|
24
|
+
ProviderWebhookPayload,
|
|
25
|
+
WebhookActionResult,
|
|
26
|
+
} from "@medusajs/framework/types"
|
|
27
|
+
import { getPayPalWebhookActionAndData } from "./webhook-utils"
|
|
28
|
+
import { formatAmountForPayPal } from "../utils/amounts"
|
|
29
|
+
import {
|
|
30
|
+
assertPayPalCurrencySupported,
|
|
31
|
+
normalizeCurrencyCode,
|
|
32
|
+
} from "../utils/currencies"
|
|
33
|
+
import type PayPalModuleService from "../service"
|
|
34
|
+
|
|
35
|
+
type Options = {}
|
|
36
|
+
|
|
37
|
+
function generateSessionId() {
|
|
38
|
+
try {
|
|
39
|
+
return randomUUID()
|
|
40
|
+
} catch {
|
|
41
|
+
return `pp_card_${Date.now()}_${Math.random().toString(16).slice(2)}`
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class PayPalAdvancedCardProvider extends AbstractPaymentProvider<Options> {
|
|
46
|
+
static identifier = "paypal_card"
|
|
47
|
+
|
|
48
|
+
protected readonly options_: Options
|
|
49
|
+
|
|
50
|
+
constructor(cradle: Record<string, any>, options: Options) {
|
|
51
|
+
super(cradle, options)
|
|
52
|
+
this.options_ = options
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private resolvePayPalService() {
|
|
56
|
+
const container = this.container as {
|
|
57
|
+
resolve<T>(key: string): T
|
|
58
|
+
}
|
|
59
|
+
return container.resolve<PayPalModuleService>("paypal_onboarding")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private async resolveSettings() {
|
|
63
|
+
const paypal = this.resolvePayPalService()
|
|
64
|
+
const settings = await paypal.getSettings().catch(() => ({}))
|
|
65
|
+
const data =
|
|
66
|
+
settings && typeof settings === "object" && "data" in settings
|
|
67
|
+
? ((settings as { data?: Record<string, any> }).data ?? {})
|
|
68
|
+
: {}
|
|
69
|
+
return {
|
|
70
|
+
additionalSettings: (data.additional_settings || {}) as Record<string, any>,
|
|
71
|
+
advancedCardSettings: (data.advanced_card_payments || {}) as Record<string, any>,
|
|
72
|
+
apiDetails: (data.api_details || {}) as Record<string, any>,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async resolveCurrencyOverride() {
|
|
77
|
+
const { apiDetails } = await this.resolveSettings()
|
|
78
|
+
if (typeof apiDetails.currency_code === "string" && apiDetails.currency_code.trim()) {
|
|
79
|
+
return normalizeCurrencyCode(apiDetails.currency_code)
|
|
80
|
+
}
|
|
81
|
+
return normalizeCurrencyCode(process.env.PAYPAL_CURRENCY || "USD")
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private async getPayPalAccessToken() {
|
|
85
|
+
const paypal = this.resolvePayPalService()
|
|
86
|
+
const creds = await paypal.getActiveCredentials()
|
|
87
|
+
const base =
|
|
88
|
+
creds.environment === "live"
|
|
89
|
+
? "https://api-m.paypal.com"
|
|
90
|
+
: "https://api-m.sandbox.paypal.com"
|
|
91
|
+
const auth = Buffer.from(`${creds.client_id}:${creds.client_secret}`).toString("base64")
|
|
92
|
+
|
|
93
|
+
const resp = await fetch(`${base}/v1/oauth2/token`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: {
|
|
96
|
+
Authorization: `Basic ${auth}`,
|
|
97
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
98
|
+
},
|
|
99
|
+
body: "grant_type=client_credentials",
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const text = await resp.text()
|
|
103
|
+
if (!resp.ok) {
|
|
104
|
+
throw new Error(`PayPal token error (${resp.status}): ${text}`)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const json = JSON.parse(text)
|
|
108
|
+
return { accessToken: String(json.access_token), base }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async getOrderDetails(orderId: string) {
|
|
112
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
113
|
+
const resp = await fetch(`${base}/v2/checkout/orders/${orderId}`, {
|
|
114
|
+
method: "GET",
|
|
115
|
+
headers: {
|
|
116
|
+
Authorization: `Bearer ${accessToken}`,
|
|
117
|
+
"Content-Type": "application/json",
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const text = await resp.text()
|
|
122
|
+
if (!resp.ok) {
|
|
123
|
+
throw new Error(`PayPal get order error (${resp.status}): ${text}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return JSON.parse(text)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private getIdempotencyKey(
|
|
130
|
+
input: { context?: { idempotency_key?: string } },
|
|
131
|
+
suffix: string
|
|
132
|
+
) {
|
|
133
|
+
const key = input?.context?.idempotency_key?.trim()
|
|
134
|
+
if (key) {
|
|
135
|
+
return `${key}-${suffix}`
|
|
136
|
+
}
|
|
137
|
+
return `pp-card-${suffix}-${generateSessionId()}`
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private async normalizePaymentData(input: { data?: Record<string, unknown> }) {
|
|
141
|
+
const data = (input.data || {}) as Record<string, any>
|
|
142
|
+
const amount = Number(data.amount ?? 0)
|
|
143
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
144
|
+
const currencyCode = normalizeCurrencyCode(
|
|
145
|
+
data.currency_code || currencyOverride || "USD"
|
|
146
|
+
)
|
|
147
|
+
assertPayPalCurrencySupported({
|
|
148
|
+
currencyCode,
|
|
149
|
+
paypalCurrencyOverride: currencyOverride,
|
|
150
|
+
})
|
|
151
|
+
return { data, amount, currencyCode }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private mapCaptureStatus(status?: string) {
|
|
155
|
+
const normalized = String(status || "").toUpperCase()
|
|
156
|
+
if (!normalized) {
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
if (normalized === "COMPLETED") {
|
|
160
|
+
return "captured"
|
|
161
|
+
}
|
|
162
|
+
if (normalized === "PENDING") {
|
|
163
|
+
return "pending"
|
|
164
|
+
}
|
|
165
|
+
if (["DENIED", "DECLINED", "FAILED"].includes(normalized)) {
|
|
166
|
+
return "error"
|
|
167
|
+
}
|
|
168
|
+
if (["REFUNDED", "PARTIALLY_REFUNDED", "REVERSED"].includes(normalized)) {
|
|
169
|
+
return "canceled"
|
|
170
|
+
}
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private mapAuthorizationStatus(status?: string) {
|
|
175
|
+
const normalized = String(status || "").toUpperCase()
|
|
176
|
+
if (!normalized) {
|
|
177
|
+
return null
|
|
178
|
+
}
|
|
179
|
+
if (["CREATED", "APPROVED", "PENDING"].includes(normalized)) {
|
|
180
|
+
return "authorized"
|
|
181
|
+
}
|
|
182
|
+
if (["VOIDED", "EXPIRED"].includes(normalized)) {
|
|
183
|
+
return "canceled"
|
|
184
|
+
}
|
|
185
|
+
if (["DENIED", "DECLINED", "FAILED"].includes(normalized)) {
|
|
186
|
+
return "error"
|
|
187
|
+
}
|
|
188
|
+
return null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private mapOrderStatus(status?: string) {
|
|
192
|
+
const normalized = String(status || "").toUpperCase()
|
|
193
|
+
if (!normalized) {
|
|
194
|
+
return "pending"
|
|
195
|
+
}
|
|
196
|
+
if (normalized === "COMPLETED") {
|
|
197
|
+
return "captured"
|
|
198
|
+
}
|
|
199
|
+
if (normalized === "APPROVED") {
|
|
200
|
+
return "authorized"
|
|
201
|
+
}
|
|
202
|
+
if (["VOIDED", "CANCELLED"].includes(normalized)) {
|
|
203
|
+
return "canceled"
|
|
204
|
+
}
|
|
205
|
+
if (["CREATED", "SAVED", "PAYER_ACTION_REQUIRED"].includes(normalized)) {
|
|
206
|
+
return "pending"
|
|
207
|
+
}
|
|
208
|
+
if (["FAILED", "EXPIRED"].includes(normalized)) {
|
|
209
|
+
return "error"
|
|
210
|
+
}
|
|
211
|
+
return "pending"
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async createAccountHolder(
|
|
215
|
+
input: CreateAccountHolderInput
|
|
216
|
+
): Promise<CreateAccountHolderOutput> {
|
|
217
|
+
const customerId = input.context?.customer?.id
|
|
218
|
+
const externalId = customerId ? `paypal_${customerId}` : `paypal_${generateSessionId()}`
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
id: externalId,
|
|
222
|
+
data: {
|
|
223
|
+
email: input.context?.customer?.email || null,
|
|
224
|
+
customer_id: customerId || null,
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async initiatePayment(input: InitiatePaymentInput): Promise<InitiatePaymentOutput> {
|
|
230
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
231
|
+
const currencyCode = normalizeCurrencyCode(
|
|
232
|
+
input.currency_code || currencyOverride || "USD"
|
|
233
|
+
)
|
|
234
|
+
assertPayPalCurrencySupported({
|
|
235
|
+
currencyCode,
|
|
236
|
+
paypalCurrencyOverride: currencyOverride,
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
id: generateSessionId(),
|
|
241
|
+
data: {
|
|
242
|
+
...(input.data || {}),
|
|
243
|
+
// store amount/currency so Medusa has something consistent
|
|
244
|
+
amount: input.amount,
|
|
245
|
+
currency_code: currencyCode,
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
|
251
|
+
const currencyOverride = await this.resolveCurrencyOverride()
|
|
252
|
+
const currencyCode = normalizeCurrencyCode(
|
|
253
|
+
input.currency_code || currencyOverride || "USD"
|
|
254
|
+
)
|
|
255
|
+
assertPayPalCurrencySupported({
|
|
256
|
+
currencyCode,
|
|
257
|
+
paypalCurrencyOverride: currencyOverride,
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
data: {
|
|
262
|
+
...(input.data || {}),
|
|
263
|
+
amount: input.amount,
|
|
264
|
+
currency_code: currencyCode,
|
|
265
|
+
},
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async authorizePayment(_input: AuthorizePaymentInput): Promise<AuthorizePaymentOutput> {
|
|
270
|
+
const { data, amount, currencyCode } = await this.normalizePaymentData(_input)
|
|
271
|
+
const requestId = this.getIdempotencyKey(_input, "authorize")
|
|
272
|
+
let debugId: string | null = null
|
|
273
|
+
const { additionalSettings, advancedCardSettings } = await this.resolveSettings()
|
|
274
|
+
const paymentActionRaw =
|
|
275
|
+
typeof additionalSettings.paymentAction === "string"
|
|
276
|
+
? additionalSettings.paymentAction
|
|
277
|
+
: "capture"
|
|
278
|
+
const orderIntent = paymentActionRaw === "authorize" ? "AUTHORIZE" : "CAPTURE"
|
|
279
|
+
const threeDsRaw =
|
|
280
|
+
typeof advancedCardSettings.threeDS === "string"
|
|
281
|
+
? advancedCardSettings.threeDS
|
|
282
|
+
: "when_required"
|
|
283
|
+
const threeDsMethod =
|
|
284
|
+
threeDsRaw === "always"
|
|
285
|
+
? "SCA_ALWAYS"
|
|
286
|
+
: threeDsRaw === "when_required" || threeDsRaw === "sli"
|
|
287
|
+
? "SCA_WHEN_REQUIRED"
|
|
288
|
+
: null
|
|
289
|
+
const disabledCards = Array.isArray(advancedCardSettings.disabledCards)
|
|
290
|
+
? advancedCardSettings.disabledCards.map((card: string) => String(card).toLowerCase())
|
|
291
|
+
: []
|
|
292
|
+
const cardBrand = String(
|
|
293
|
+
data.card_brand || data.cardBrand || data?.paypal?.card_brand || ""
|
|
294
|
+
).toLowerCase()
|
|
295
|
+
if (cardBrand && disabledCards.includes(cardBrand)) {
|
|
296
|
+
throw new Error(`Card brand ${cardBrand} is disabled by admin settings.`)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
300
|
+
const existingPayPal = (data.paypal || {}) as Record<string, any>
|
|
301
|
+
let orderId = String(existingPayPal.order_id || data.order_id || "")
|
|
302
|
+
let order: Record<string, any> | null = null
|
|
303
|
+
let authorization: Record<string, any> | null = null
|
|
304
|
+
|
|
305
|
+
if (!orderId) {
|
|
306
|
+
const value = formatAmountForPayPal(amount, currencyCode || "USD")
|
|
307
|
+
const orderPayload = {
|
|
308
|
+
intent: orderIntent,
|
|
309
|
+
purchase_units: [
|
|
310
|
+
{
|
|
311
|
+
reference_id: data.cart_id || data.payment_collection_id || undefined,
|
|
312
|
+
custom_id: data.session_id || data.cart_id || data.payment_collection_id || undefined,
|
|
313
|
+
amount: {
|
|
314
|
+
currency_code: currencyCode || "USD",
|
|
315
|
+
value,
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
],
|
|
319
|
+
custom_id: data.session_id || data.cart_id || data.payment_collection_id || undefined,
|
|
320
|
+
...(threeDsMethod
|
|
321
|
+
? {
|
|
322
|
+
payment_source: {
|
|
323
|
+
card: {
|
|
324
|
+
attributes: {
|
|
325
|
+
verification: {
|
|
326
|
+
method: threeDsMethod,
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
}
|
|
332
|
+
: {}),
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const ppResp = await fetch(`${base}/v2/checkout/orders`, {
|
|
336
|
+
method: "POST",
|
|
337
|
+
headers: {
|
|
338
|
+
Authorization: `Bearer ${accessToken}`,
|
|
339
|
+
"Content-Type": "application/json",
|
|
340
|
+
"PayPal-Request-Id": requestId,
|
|
341
|
+
},
|
|
342
|
+
body: JSON.stringify(orderPayload),
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
const ppText = await ppResp.text()
|
|
346
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
347
|
+
if (!ppResp.ok) {
|
|
348
|
+
throw new Error(
|
|
349
|
+
`PayPal create order error (${ppResp.status}): ${ppText}${
|
|
350
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
351
|
+
}`
|
|
352
|
+
)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
order = JSON.parse(ppText) as Record<string, any>
|
|
356
|
+
orderId = String(order.id || "")
|
|
357
|
+
} else {
|
|
358
|
+
order = (await this.getOrderDetails(orderId)) as Record<string, any> | null
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (!order || !orderId) {
|
|
362
|
+
throw new Error("Unable to resolve PayPal order details for authorization.")
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const existingAuthorization =
|
|
366
|
+
order?.purchase_units?.[0]?.payments?.authorizations?.[0] || null
|
|
367
|
+
|
|
368
|
+
if (existingAuthorization) {
|
|
369
|
+
authorization = order
|
|
370
|
+
} else {
|
|
371
|
+
const authorizeResp = await fetch(`${base}/v2/checkout/orders/${orderId}/authorize`, {
|
|
372
|
+
method: "POST",
|
|
373
|
+
headers: {
|
|
374
|
+
Authorization: `Bearer ${accessToken}`,
|
|
375
|
+
"Content-Type": "application/json",
|
|
376
|
+
"PayPal-Request-Id": `${requestId}-auth`,
|
|
377
|
+
},
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
const authorizeText = await authorizeResp.text()
|
|
381
|
+
const authorizeDebugId = authorizeResp.headers.get("paypal-debug-id")
|
|
382
|
+
if (!authorizeResp.ok) {
|
|
383
|
+
throw new Error(
|
|
384
|
+
`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${
|
|
385
|
+
authorizeDebugId ? ` debug_id=${authorizeDebugId}` : ""
|
|
386
|
+
}`
|
|
387
|
+
)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
authorization = JSON.parse(authorizeText)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const authorizationId =
|
|
394
|
+
authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id ||
|
|
395
|
+
existingAuthorization?.id
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
status: "authorized",
|
|
399
|
+
data: {
|
|
400
|
+
...(data || {}),
|
|
401
|
+
paypal: {
|
|
402
|
+
...existingPayPal,
|
|
403
|
+
order_id: orderId,
|
|
404
|
+
order: order || authorization,
|
|
405
|
+
authorization_id: authorizationId,
|
|
406
|
+
authorizations: authorization?.purchase_units?.[0]?.payments?.authorizations || [],
|
|
407
|
+
},
|
|
408
|
+
authorized_at: new Date().toISOString(),
|
|
409
|
+
},
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async capturePayment(_input: CapturePaymentInput): Promise<CapturePaymentOutput> {
|
|
414
|
+
const data = (_input.data || {}) as Record<string, any>
|
|
415
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
416
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
417
|
+
let authorizationId = String(paypalData.authorization_id || data.authorization_id || "")
|
|
418
|
+
if (!orderId) {
|
|
419
|
+
throw new Error("PayPal order_id is required to capture payment")
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (paypalData.capture_id || paypalData.capture) {
|
|
423
|
+
return {
|
|
424
|
+
data: {
|
|
425
|
+
...(data || {}),
|
|
426
|
+
paypal: {
|
|
427
|
+
...paypalData,
|
|
428
|
+
capture_id: paypalData.capture_id,
|
|
429
|
+
capture: paypalData.capture,
|
|
430
|
+
},
|
|
431
|
+
captured_at: new Date().toISOString(),
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const requestId = this.getIdempotencyKey(_input, `capture-${orderId}`)
|
|
437
|
+
const { amount, currencyCode } = await this.normalizePaymentData(_input)
|
|
438
|
+
let debugId: string | null = null
|
|
439
|
+
|
|
440
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
441
|
+
const order = await this.getOrderDetails(orderId).catch(() => null)
|
|
442
|
+
const existingCapture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
443
|
+
if (existingCapture?.id) {
|
|
444
|
+
return {
|
|
445
|
+
data: {
|
|
446
|
+
...(data || {}),
|
|
447
|
+
paypal: {
|
|
448
|
+
...paypalData,
|
|
449
|
+
capture_id: existingCapture.id,
|
|
450
|
+
capture: existingCapture,
|
|
451
|
+
},
|
|
452
|
+
captured_at: new Date().toISOString(),
|
|
453
|
+
},
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const resolvedIntent = String(
|
|
458
|
+
order?.intent || paypalData.order?.intent || data.intent || ""
|
|
459
|
+
).toUpperCase()
|
|
460
|
+
if (!authorizationId && resolvedIntent === "AUTHORIZE") {
|
|
461
|
+
const authorizeResp = await fetch(`${base}/v2/checkout/orders/${orderId}/authorize`, {
|
|
462
|
+
method: "POST",
|
|
463
|
+
headers: {
|
|
464
|
+
Authorization: `Bearer ${accessToken}`,
|
|
465
|
+
"Content-Type": "application/json",
|
|
466
|
+
"PayPal-Request-Id": `${requestId}-auth`,
|
|
467
|
+
},
|
|
468
|
+
})
|
|
469
|
+
const authorizeText = await authorizeResp.text()
|
|
470
|
+
debugId = authorizeResp.headers.get("paypal-debug-id")
|
|
471
|
+
if (!authorizeResp.ok) {
|
|
472
|
+
throw new Error(
|
|
473
|
+
`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${
|
|
474
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
475
|
+
}`
|
|
476
|
+
)
|
|
477
|
+
}
|
|
478
|
+
const authorization = JSON.parse(authorizeText)
|
|
479
|
+
authorizationId =
|
|
480
|
+
authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const isFinalCapture =
|
|
484
|
+
paypalData.is_final_capture ??
|
|
485
|
+
data.is_final_capture ??
|
|
486
|
+
data.final_capture ??
|
|
487
|
+
undefined
|
|
488
|
+
const capturePayload =
|
|
489
|
+
amount > 0
|
|
490
|
+
? {
|
|
491
|
+
amount: {
|
|
492
|
+
currency_code: currencyCode || "USD",
|
|
493
|
+
value: formatAmountForPayPal(amount, currencyCode || "USD"),
|
|
494
|
+
},
|
|
495
|
+
...(typeof isFinalCapture === "boolean"
|
|
496
|
+
? { is_final_capture: isFinalCapture }
|
|
497
|
+
: {}),
|
|
498
|
+
}
|
|
499
|
+
: {
|
|
500
|
+
...(typeof isFinalCapture === "boolean"
|
|
501
|
+
? { is_final_capture: isFinalCapture }
|
|
502
|
+
: {}),
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const captureUrl = authorizationId
|
|
506
|
+
? `${base}/v2/payments/authorizations/${authorizationId}/capture`
|
|
507
|
+
: `${base}/v2/checkout/orders/${orderId}/capture`
|
|
508
|
+
|
|
509
|
+
const ppResp = await fetch(captureUrl, {
|
|
510
|
+
method: "POST",
|
|
511
|
+
headers: {
|
|
512
|
+
Authorization: `Bearer ${accessToken}`,
|
|
513
|
+
"Content-Type": "application/json",
|
|
514
|
+
"PayPal-Request-Id": requestId,
|
|
515
|
+
},
|
|
516
|
+
body: JSON.stringify(capturePayload),
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
const ppText = await ppResp.text()
|
|
520
|
+
debugId = ppResp.headers.get("paypal-debug-id")
|
|
521
|
+
if (!ppResp.ok) {
|
|
522
|
+
throw new Error(
|
|
523
|
+
`PayPal capture error (${ppResp.status}): ${ppText}${
|
|
524
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
525
|
+
}`
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const capture = JSON.parse(ppText)
|
|
530
|
+
const captureId =
|
|
531
|
+
capture?.id || capture?.purchase_units?.[0]?.payments?.captures?.[0]?.id
|
|
532
|
+
const existingCaptures = Array.isArray(paypalData.captures) ? paypalData.captures : []
|
|
533
|
+
const captureEntry = {
|
|
534
|
+
id: captureId,
|
|
535
|
+
status: capture?.status,
|
|
536
|
+
amount: capture?.amount,
|
|
537
|
+
raw: capture,
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return {
|
|
541
|
+
data: {
|
|
542
|
+
...(data || {}),
|
|
543
|
+
paypal: {
|
|
544
|
+
...paypalData,
|
|
545
|
+
order_id: orderId,
|
|
546
|
+
capture_id: captureId,
|
|
547
|
+
capture,
|
|
548
|
+
authorization_id: authorizationId || paypalData.authorization_id,
|
|
549
|
+
captures: [...existingCaptures, captureEntry],
|
|
550
|
+
},
|
|
551
|
+
captured_at: new Date().toISOString(),
|
|
552
|
+
},
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
async cancelPayment(_input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
|
557
|
+
const data = (_input.data || {}) as Record<string, any>
|
|
558
|
+
return {
|
|
559
|
+
data: {
|
|
560
|
+
...(data || {}),
|
|
561
|
+
canceled_at: new Date().toISOString(),
|
|
562
|
+
},
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
async refundPayment(_input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
|
567
|
+
const data = (_input.data || {}) as Record<string, any>
|
|
568
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
569
|
+
const captureId = String(paypalData.capture_id || data.capture_id || "")
|
|
570
|
+
if (!captureId) {
|
|
571
|
+
return {
|
|
572
|
+
data: {
|
|
573
|
+
...(data || {}),
|
|
574
|
+
refunded_at: new Date().toISOString(),
|
|
575
|
+
},
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const requestId = this.getIdempotencyKey(_input, `refund-${captureId}`)
|
|
580
|
+
const amount = Number(data.amount ?? 0)
|
|
581
|
+
const currencyCode = normalizeCurrencyCode(
|
|
582
|
+
data.currency_code || process.env.PAYPAL_CURRENCY || "USD"
|
|
583
|
+
)
|
|
584
|
+
const { accessToken, base } = await this.getPayPalAccessToken()
|
|
585
|
+
const refundPayload: Record<string, any> =
|
|
586
|
+
amount > 0
|
|
587
|
+
? {
|
|
588
|
+
amount: {
|
|
589
|
+
currency_code: currencyCode,
|
|
590
|
+
value: formatAmountForPayPal(amount, currencyCode),
|
|
591
|
+
},
|
|
592
|
+
}
|
|
593
|
+
: {}
|
|
594
|
+
|
|
595
|
+
const resp = await fetch(`${base}/v2/payments/captures/${captureId}/refund`, {
|
|
596
|
+
method: "POST",
|
|
597
|
+
headers: {
|
|
598
|
+
Authorization: `Bearer ${accessToken}`,
|
|
599
|
+
"Content-Type": "application/json",
|
|
600
|
+
"PayPal-Request-Id": requestId,
|
|
601
|
+
},
|
|
602
|
+
body: JSON.stringify(refundPayload),
|
|
603
|
+
})
|
|
604
|
+
|
|
605
|
+
const text = await resp.text()
|
|
606
|
+
if (!resp.ok) {
|
|
607
|
+
const debugId = resp.headers.get("paypal-debug-id")
|
|
608
|
+
throw new Error(
|
|
609
|
+
`PayPal refund error (${resp.status}): ${text}${
|
|
610
|
+
debugId ? ` debug_id=${debugId}` : ""
|
|
611
|
+
}`
|
|
612
|
+
)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
const refund = JSON.parse(text)
|
|
616
|
+
const existingRefunds = Array.isArray(paypalData.refunds) ? paypalData.refunds : []
|
|
617
|
+
const refundEntry = {
|
|
618
|
+
id: refund?.id,
|
|
619
|
+
status: refund?.status,
|
|
620
|
+
amount: refund?.amount,
|
|
621
|
+
raw: refund,
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return {
|
|
625
|
+
data: {
|
|
626
|
+
...(data || {}),
|
|
627
|
+
paypal: {
|
|
628
|
+
...paypalData,
|
|
629
|
+
refund_id: refund?.id,
|
|
630
|
+
refund_status: refund?.status,
|
|
631
|
+
refunds: [...existingRefunds, refundEntry],
|
|
632
|
+
refund,
|
|
633
|
+
},
|
|
634
|
+
refunded_at: new Date().toISOString(),
|
|
635
|
+
},
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
async retrievePayment(_input: RetrievePaymentInput): Promise<RetrievePaymentOutput> {
|
|
640
|
+
const data = (_input.data || {}) as Record<string, any>
|
|
641
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
642
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
643
|
+
if (!orderId) {
|
|
644
|
+
return { data: { ...(data || {}) } }
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const order = await this.getOrderDetails(orderId)
|
|
648
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
649
|
+
const authorization = order?.purchase_units?.[0]?.payments?.authorizations?.[0]
|
|
650
|
+
|
|
651
|
+
return {
|
|
652
|
+
data: {
|
|
653
|
+
...(data || {}),
|
|
654
|
+
paypal: {
|
|
655
|
+
...paypalData,
|
|
656
|
+
order,
|
|
657
|
+
authorization_id: authorization?.id || paypalData.authorization_id,
|
|
658
|
+
capture_id: capture?.id || paypalData.capture_id,
|
|
659
|
+
},
|
|
660
|
+
},
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
async getPaymentStatus(_input: GetPaymentStatusInput): Promise<GetPaymentStatusOutput> {
|
|
665
|
+
const data = (_input.data || {}) as Record<string, any>
|
|
666
|
+
const paypalData = (data.paypal || {}) as Record<string, any>
|
|
667
|
+
const orderId = String(paypalData.order_id || data.order_id || "")
|
|
668
|
+
if (!orderId) {
|
|
669
|
+
return { status: "pending", data: { ...(data || {}) } }
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const order = await this.getOrderDetails(orderId)
|
|
673
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0]
|
|
674
|
+
const authorization = order?.purchase_units?.[0]?.payments?.authorizations?.[0]
|
|
675
|
+
const mappedStatus =
|
|
676
|
+
this.mapCaptureStatus(capture?.status) ||
|
|
677
|
+
this.mapAuthorizationStatus(authorization?.status) ||
|
|
678
|
+
this.mapOrderStatus(order?.status) ||
|
|
679
|
+
"pending"
|
|
680
|
+
|
|
681
|
+
return {
|
|
682
|
+
status: mappedStatus,
|
|
683
|
+
data: {
|
|
684
|
+
...(data || {}),
|
|
685
|
+
paypal: {
|
|
686
|
+
...paypalData,
|
|
687
|
+
order,
|
|
688
|
+
authorization_id: authorization?.id || paypalData.authorization_id,
|
|
689
|
+
capture_id: capture?.id || paypalData.capture_id,
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
async deletePayment(_input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
|
696
|
+
return { data: {} }
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Medusa requires this method even if you don't support webhooks yet.
|
|
701
|
+
*/
|
|
702
|
+
async getWebhookActionAndData(
|
|
703
|
+
payload: ProviderWebhookPayload["payload"]
|
|
704
|
+
): Promise<WebhookActionResult> {
|
|
705
|
+
return getPayPalWebhookActionAndData(payload)
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
export default PayPalAdvancedCardProvider
|
|
710
|
+
export { PayPalAdvancedCardProvider }
|