Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: processPaymentWorkflow fails capturing payment when executed via stripe payment webhook #9998

Closed
MiguelFranken opened this issue Nov 8, 2024 · 5 comments

Comments

@MiguelFranken
Copy link

Package.json file

{
  "name": "...",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "build:admin": "DISABLE_MEDUSA_ADMIN=true medusa build --admin-only",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "db:migrate": "medusa db:migrate",
    "db:sync-links": "medusa db:sync-links",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "^2.0.1",
    "@medusajs/cli": "^2.0.1",
    "@medusajs/framework": "^2.0.1",
    "@medusajs/medusa": "^2.0.1",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0",
    "zod": "3.22.4"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

20

Database and its version

16.2

Operating system name and version

Mac OS (Latest)

Browser name

No response

What happended?

I’m setting up Stripe as a payment provider in Medusa, aiming to enable automatic payment method selection and automatic capture.

Configuration:

{
    resolve: '@medusajs/medusa/payment-stripe',
    id: 'stripe',
    options: {
      apiKey: process.env.STRIPE_API_KEY,
      webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
      automaticPaymentMethods: true, // should allow PayPal when configured in Stripe dashobard
      capture: true, // automatically capture (so that order is created?) and cart is completed
    },
}

The intent with this setup is to allow payment methods to be managed directly within the Stripe dashboard. I’m specifically testing PayPal over Stripe but am encountering errors on the payment_intent.succeeded event from Stripe.

I expect this configuration to create (at the end) an order automatically once the payment intent is successfully captured (in Medusa), allowing me to avoid a separate frontend-triggered createOrder call (like in the Medusa NextJS Starter).
Creating an order from the frontend is also not an option for PayPal via Stripe as we get redirected.
The goal is to handle the entire payment and order flow through Stripe webhooks and not calling the createOrder SDK method in the frontend to enable stripe payment methods that need a redirect to another site.

Development:
I use the following command to listen to Stripe webhooks locally:

stripe listen --skip-verify --forward-to localhost:9000/hooks/payment/stripe_stripe

So I am using the default Stripe provider (not one of the others for iDEAL, etc.)
The webhook secret returned from this command is configured in my Medusa setup.

Issue:
All Stripe events seem to process correctly, except the payment_intent.succeeded event, which throws the following error:

{
  "message": "payment - id must be defined",
  "name": "Error",
  "stack": "...",
  "__isMedusaError": true,
  "type": "not_found",
  "date": "2024-11-08T19:03:56.110Z"
}

Stack (Pretty Printed):

Error: payment - id must be defined
    at AbstractService_.retrieve (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/modules-sdk/medusa-internal-service.ts:122:15)
    at AbstractService_.descriptor.value (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/modules-sdk/decorators/inject-manager.ts:54:29)
    at PaymentModuleService.capturePayment_ (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/payment/src/services/payment-module.ts:625:48)
    at transaction.transaction (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/modules-sdk/decorators/inject-transaction-manager.ts:63:39)
    at fork.getConnection.transactional (<MY_ABSOLUTE_PATH>/node_modules/@mikro-orm/core/EntityManager.js:774:35)
    at PostgreSqlConnection.transactional (<MY_ABSOLUTE_PATH>/node_modules/@mikro-orm/postgresql/node_modules/@mikro-orm/knex/AbstractSqlConnection.js:36:31)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async transactionWrapper (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/dal/utils.ts:32:10)
    at async MikroOrmBaseRepository.transaction (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/dal/mikro-orm/mikro-orm-repository.ts:72:12)
    at async PaymentModuleService.descriptor.value (<MY_ABSOLUTE_PATH>/node_modules/@medusajs/utils/src/modules-sdk/decorators/inject-transaction-manager.ts:32:14)
⮑ sat <MY_ABSOLUTE_PATH>/node_modules/@medusajs/core-flows/dist/payment/workflows/capture-payment.js: [capture-payment-workflow -> capture-payment-step (invoke)]
⮑ sat <MY_ABSOLUTE_PATH>/node_modules/@medusajs/core-flows/dist/payment/workflows/process-payment.js: [process-payment-workflow -> capture-payment-workflow-as-step (invoke)]

Note:
I was only able to get this error by changing the log mechanism in the event bus subscribe method.
See issue: #9997

Upon inspecting the webhook payload, I noticed a session_id within data.object.metadata.session_id, confirming this field is present in the event data. However, I do not find a corresponding sessionId entry in the payment table in my database.

Let me know if there’s any further detail you’d like added!

Expected behavior

processPaymentWorkflow does not fail capturing payments when executed via stripe payment webhook

Actual behavior

processPaymentWorkflow fails capturing payment when executed via stripe payment webhook

Link to reproduction repo

/

@MiguelFranken
Copy link
Author

@carlos-r-l-rodrigues
Does your PR (#9980) address this issue?

@carlos-r-l-rodrigues
Copy link
Contributor

Not yet, but we'll investigate this issue tomorrow and the fix will very likely be part of that same PR.

kodiakhq bot pushed a commit that referenced this issue Nov 12, 2024
What:
 When `autocapture` is enabled, the webhook is processed before the order was created.
 The payment processing workflows were merged into a single one
 
FIXES: SUP-118, SUP-9

#9998
@carlos-r-l-rodrigues
Copy link
Contributor

@MiguelFranken I will close this issue because according to our tests the PR is solving all the issues related to payment.
After the new release, in case your issue isn't solved, feel free to reopen here.

Thanks

@MiguelFranken
Copy link
Author

Still get errors after updating to 2.0.6:

error:   payment - id must be defined
{
  message: 'payment - id must be defined',
  name: 'Error',
  stack: 'Error: payment - id must be defined\n' +

@carlos-r-l-rodrigues

@MiguelFranken
Copy link
Author

Stack trace (when using local event bus) points to an error from the "PaymentModuleService.capturePayment_" method.

fyi:
Still not possible to get a proper error message when using redis. See #9997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants