Build your own marketplace from scratch using Medusa
up
and down
functions :
index.d.ts
in case you are going to use that property, or it will throw TypeScript errors.medusa-config.js
at the root of your backend directory, we’ll setup the medusa-payment-stripe
so that we can use it later :
medusa-payment-stripe
dependencymedusa-payment-stripe
package available in your project, otherwise you can install it this way:
StripeConnectService
. I invite you to create a new file in the /src/services
folder with the name stripe-connect.ts
.
Once the file has been created, here’s what you’ll need to paste inside it for now :
StripeConnectService.createAccount
function, which will allow us to create a Stripe account for the store in question.
StripeConnectService.createOnboarding
. This link will be saved directly in the store metadata in our case, but you can also add a new property to your Store entity that will store the onboarding url if you want.
StripeConnect.createTransfer
function once an order meets our conditions. In this example, we’ll trigger a transfer when an order has been delivered and paid, of course you can adapt to your needs.
YOUR_COUNTRY_CODE
value in the StripeConnectService.createAccount
functionStoreService.Events.CREATED
event :
server_url
In fact, this property doesn’t exist by default, so you can add it directly to your configuration in this way to point to the url of your Medusa backend so that Stripe can redirect you back to this URL in the case of a success or error following their onboarding process.
store
table in your database, you should see a store with not null metadata column, which contains the link to start Stripe’s onboarding process.
On the other hand, before starting a process, we need to add the two routes that will handle the success/leave and error cases
/src/api/stripe/onboarding/return/route.ts
folder :
storeId
from the query parameters, and then set the store.stripe_account_enabled
value to true
for that store. Once the value has been updated, we can redirect the user to the admin UI
But in case the user has just leaved the onboarding process, we do nothing and just redirect.
/src/api/stripe/onboarding
folder, I invite you to create a refresh
folder containing this route.ts
file :
ProductService
completed
.
By implementing this new subscriber, we can specifically track when an order is truly completed. Once the order is marked as completed, we can then trigger the money transfer process using our StripeConnectService.createTransfer
completed
and create a transfer for the order’s store :
/src/admin/widgets/stripe-onboarding-widget.tsx
: