medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.26k stars 2.67k forks source link

[Bug]: `MedusaContainer` inside payment provider services does not work as expected #10180

Closed damianr13 closed 1 week ago

damianr13 commented 1 week ago

Package.json file

{
  "name": "medusa-starter-default",
  "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",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --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": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@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"
  },
  "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

v22.11.0

Database and its version

PostgreSQL 16.2

Operating system name and version

MacOS Sonoma 14.6.1

Browser name

No response

What happended?

I am trying to extends the existing stripe payment provider with custom logic to handle transfers through stripe connect for a marketplace.

To achieve this, I need to run a workflow that splits an order to different vendors and creates stripe payments for each of them. This requires the usage of workflows from within the payment provider service.

So the payment provider gets a MedusaContainer injected to the constructor: https://github.com/medusajs/medusa/blob/6f7467f071119871d9c35e399f85cfd939952b5d/packages/modules/providers/payment-stripe/src/core/stripe-base.ts

The problem is that MedusaContainer is itself an Awilix proxy object, but it overwrites the resolve method: https://github.com/medusajs/medusa/blob/9c39cf69fb0e40cd215856189d3621320dba7ad7/packages/core/types/src/common/medusa-container.ts#L14

So when I am trying to use this.container_.resolve(ContainerRegistrationKeys.LOGGER), it crashes saying

 AwilixResolutionError: Could not resolve 'resolve'

But if I do this.container_.logger even though typescript complains that MedusaContainer has no such field, I can execute this code (checked through debugging line by line).

That is because the overriding resolve method looks at the registered keys inside the MedusaContainer but if I try to use the Proxy as is, I would call MedusaContainer methods and they are not registered to this overwritten resolve method.

I could just use .logger in this instance, but I need the container down the line in a couple of workflows, and then they fail because of the same issue.

Expected behavior

this.container_.resolve(ContainerRegistrationKeys.LOGGER) should resolve into a valid instance of the logger module service.

Actual behavior

The execution crashes with the error:

 AwilixResolutionError: Could not resolve 'resolve'

Link to reproduction repo

https://github.com/damianr13/medusa-broken-container-example

adrien2p commented 1 week ago

Hi @damianr13,

Thank you for reporting this issue, I have created a PR that fix the type of the container. on the other hand, we are not allowing executing a workflow from withing a service. You might need to move your logic to a separate place (a workflow for example)