-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment, payment-stripe): Add Stripe module provider (#6311)
- Loading branch information
1 parent
ac829fc
commit ce39b9b
Showing
49 changed files
with
1,253 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@medusajs/medusa": patch | ||
"@medusajs/types": patch | ||
"@medusajs/utils": patch | ||
--- | ||
|
||
feat(payment-stripe): new Stripe payment provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { MiddlewareRoute } from "../../types/middlewares" | ||
|
||
export const hooksRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
method: ["POST"], | ||
bodyParser: { preserveRawBody: true }, | ||
matcher: "/hooks/payment/:provider", | ||
}, | ||
] |
32 changes: 32 additions & 0 deletions
32
packages/medusa/src/api-v2/hooks/payment/[provider]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { PaymentWebhookEvents } from "@medusajs/utils" | ||
import { PaymentModuleOptions } from "@medusajs/types" | ||
|
||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" | ||
|
||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { | ||
try { | ||
const { provider } = req.params | ||
|
||
const options: PaymentModuleOptions = | ||
req.scope.resolve(ModuleRegistrationName.PAYMENT).options || {} | ||
|
||
const event = { | ||
provider, | ||
payload: { data: req.body, rawData: req.rawBody, headers: req.headers }, | ||
} | ||
|
||
const eventBus = req.scope.resolve("eventBusService") | ||
|
||
// we delay the processing of the event to avoid a conflict caused by a race condition | ||
await eventBus.emit(PaymentWebhookEvents.WebhookReceived, event, { | ||
delay: options.webhook_delay || 5000, | ||
attempts: options.webhook_retries || 3, | ||
}) | ||
} catch (err) { | ||
res.status(400).send(`Webhook Error: ${err.message}`) | ||
return | ||
} | ||
|
||
res.sendStatus(200) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { PaymentWebhookEvents } from "@medusajs/utils" | ||
|
||
import { | ||
IEventBusService, | ||
IPaymentModuleService, | ||
ProviderWebhookPayload, | ||
Subscriber, | ||
} from "@medusajs/types" | ||
import { EventBusService } from "../services" | ||
|
||
type SerializedBuffer = { | ||
data: ArrayBuffer | ||
type: "Buffer" | ||
} | ||
|
||
type InjectedDependencies = { | ||
paymentModuleService: IPaymentModuleService | ||
eventBusService: EventBusService | ||
} | ||
|
||
class PaymentWebhookSubscriber { | ||
private readonly eventBusService_: IEventBusService | ||
private readonly paymentModuleService_: IPaymentModuleService | ||
|
||
constructor({ eventBusService, paymentModuleService }: InjectedDependencies) { | ||
this.eventBusService_ = eventBusService | ||
this.paymentModuleService_ = paymentModuleService | ||
|
||
this.eventBusService_.subscribe( | ||
PaymentWebhookEvents.WebhookReceived, | ||
this.processEvent as Subscriber | ||
) | ||
} | ||
|
||
/** | ||
* TODO: consider moving this to a workflow | ||
*/ | ||
processEvent = async (data: ProviderWebhookPayload): Promise<void> => { | ||
if ( | ||
(data.payload.rawData as unknown as SerializedBuffer).type === "Buffer" | ||
) { | ||
data.payload.rawData = Buffer.from( | ||
(data.payload.rawData as unknown as SerializedBuffer).data | ||
) | ||
} | ||
await this.paymentModuleService_.processEvent(data) | ||
} | ||
} | ||
|
||
export default PaymentWebhookSubscriber |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
.DS_store | ||
yarn.lock |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
globals: { | ||
"ts-jest": { | ||
tsconfig: "tsconfig.spec.json", | ||
isolatedModules: false, | ||
}, | ||
}, | ||
transform: { | ||
"^.+\\.[jt]s?$": "ts-jest", | ||
}, | ||
testEnvironment: `node`, | ||
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@medusajs/payment-stripe", | ||
"version": "0.0.1", | ||
"description": "Stripe payment provider for Medusa", | ||
"main": "dist/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/medusajs/medusa", | ||
"directory": "packages/payment-stripe" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"author": "Medusa", | ||
"license": "MIT", | ||
"scripts": { | ||
"prepublishOnly": "cross-env NODE_ENV=production tsc --build", | ||
"test": "jest --passWithNoTests src", | ||
"build": "rimraf dist && tsc -p ./tsconfig.json", | ||
"watch": "tsc --watch" | ||
}, | ||
"devDependencies": { | ||
"@medusajs/medusa": "^1.19.1", | ||
"@types/stripe": "^8.0.417", | ||
"awilix": "^8.0.1", | ||
"cross-env": "^5.2.1", | ||
"jest": "^25.5.4", | ||
"rimraf": "^5.0.1", | ||
"typescript": "^4.9.5" | ||
}, | ||
"peerDependencies": { | ||
"@medusajs/medusa": "^1.12.0" | ||
}, | ||
"dependencies": { | ||
"@medusajs/utils": "^1.11.3", | ||
"body-parser": "^1.19.0", | ||
"express": "^4.17.1", | ||
"stripe": "latest" | ||
}, | ||
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808", | ||
"keywords": [ | ||
"medusa-plugin", | ||
"medusa-plugin-payment" | ||
] | ||
} |
Oops, something went wrong.