MkSaaS模板的支付模块的方案和流程 — Transcript
Full transcript
- 0:00Hello everyone, the MkSaaS template recently underwent a round of payment module reconstruction.
- 0:06This video mainly introduces the technical implementation of the new payment module
- 0:11and why this reconstruction was needed
- 0:15what problems the old version had and how the new version solves these problems.
- 0:21First, let's look at Stripe's best practices reference
- 0:28which is this GitHub repository, the author is the author of T3 Chat
- 0:32a very famous international developer who wrote a document
- 0:38this document introduces how to integrate Stripe payment
- 0:43he recommended a process for connecting to Stripe.
- 0:49Everyone can take a look at the content of this document when you have time.
- 0:53The core idea is that Stripe's Webhook events are numerous
- 0:59and the order is quite messy, making them troublesome to handle.
- 1:04Processing them can be quite troublesome.
- 1:07So it provides an implementation solution, which is a method called sync after success.
- 1:15That is, when you receive any Stripe event
- 1:20it will call this method to actively request the Stripe interface
- 1:27then synchronize some of the user's data to the local system
- 1:32to ensure that the user's subscription status remains consistent with the status on Stripe.
- 1:40The complete process is quite troublesome
- 1:44and it doesn't provide complete source code for accessing a real project.
- 1:51So I based it on this article
- 1:57and did a practice on the MkSaaS template.
- 2:02First, let's take a look. According to this article's introduction
- 2:08it's no exaggeration to say that basically all open source SaaS templates you can see on the market have problems.
- 2:14What is this problem?
- 2:15It's that users may not have actually completed the payment
- 2:22but this SaaS website thinks it has completed the payment
- 2:26and then changes its subscription status or issues credits to it.
- 2:31Recently, customers using the MkSaaS template
- 2:35occasionally have users reporting similar issues
- 2:39which is actually the same reason.
- 2:43Every SaaS website that wants to connect to Stripe
- 2:47I summarized that the main thing is to solve these three problems.
- 2:50The first is that there are many Webhook events, we must choose the correct events to monitor and process
- 2:57if you listen to the wrong events
- 3:01and process them, it's actually useless.
- 3:03The second is that the order of Stripe events may be messy
- 3:07and the timing is also uncertain.
- 3:09So the code of the SaaS website must be compatible.
- 3:13Moreover, if necessary, it may be necessary to wait for a certain event to arrive.
- 3:20The third is that events may be triggered repeatedly, that is, events sent to you by Stripe
- 3:27may be triggered repeatedly, possibly due to Stripe issues
- 3:31or possibly because your website failed to handle this event last time
- 3:34then Stripe waits for a while and then retriggers.
- 3:38So the website's processing logic should consider idempotency
- 3:41to avoid repeated processing of the same event
- 3:43for example, credits being issued repeatedly.
- 3:47Next, we'll look at the old version's payment solution.
- 3:49As for the payment process demonstration of the old version, you can see the previous
- 3:55video tutorial and payment documentation.
- 3:59The previous video demonstrates how to use the MkSaaS template to configure and deploy from scratch
- 4:09including how to configure the payment module
- 4:11how to create products in Stripe and then configure these environment variables in the MkSaaS template.
- 4:21I won't introduce this process
- 4:25as for the original flowchart, the one introduced before is this one
- 4:28it's quite simple, users on your webpage
- 4:32click a payment button, then we call the Stripe interface
- 4:35get a Checkout URL and then open this payment interface.
- 4:42After the user completes payment on this payment interface
- 4:45some Webhook event notifications will be sent to this website
- 4:48after this website receives this event, it determines whether the user payment succeeded or failed
- 4:52then performs some operations.
- 4:54The previous process was that simple.
- 4:56This old version of the payment solution listened to four events
- 4:59the first is Checkout session completed, where users complete payment in the Checkout session and three subscription-related events
- 5:08including Customer subscription created, updated and deleted.
- 5:13What was the problem before?
- 5:15If we received the Checkout session completed event
- 5:21we would create a Payment record
- 5:25and mark the user's payment as completed.
- 5:27But in fact, the arrival of this event doesn't always mean that payment is actually completed
- 5:33because why?
- 5:33Some payment methods may need some time to process and confirm
- 5:39it just means that the user completed the payment process on the Checkout page
- 5:44and then returned to your website.
- 5:47But at this time, the payment may still be processing and confirming
- 5:53there's a possibility that the payment will eventually fail or be canceled
- 5:56but we had already marked the user payment as successful in this case.
- 6:02So the user's subscription status has been modified
- 6:07or credits have been issued to the user.
- 6:09So this solution is problematic.
- 6:12This is how most open source SaaS templates on the market handle it
- 6:17so basically all of them have this problem.
- 6:20Let's see how the new version's payment solution solves these problems.
- 6:25This is the complete flowchart of the new version's payment, which is this diagram
- 6:30we'll explain the process in this diagram in detail later
- 6:34and perform a functional demonstration at the demo MkSaaS demo site.
- 6:40Let me first briefly introduce what changes were made to solve the previous problems.
- 6:49First, the first change is that the monitored events added one called Invoice Paid.
- 6:56Why should we monitor this event?
- 6:57Because when this event is received, it means that payment is actually completed
- 7:01in this case we can update the user's subscription status
- 7:06or issue credits.
- 7:09Previously, we would create the Payment record when Checkout Session completed or when receiving
- 7:16Subscription created event
- 7:21marking that the user created, marking that the user completed payment
- 7:25and creating a successful payment record for them.
- 7:30But this is problematic, right?
- 7:31So this time we changed it to when we receive the Invoice paid event
- 7:36only then create this Payment record and save it to the database
- 7:40and at the same time update the user's subscription status or issue credits to them.
- 7:45There's a question here about why we chose to add this event
- 7:52instead of the Invoice payment succeeded event which looks similar
- 7:57one is payment successful, the other is paid.
- 7:59Actually there is a difference.
- 8:01The paid event includes a special case
- 8:04such as zero-dollar purchases, if your product
- 8:10gives someone a coupon, and they can purchase your product for zero dollars
- 8:17then it won't send the Invoice payment succeeded event
- 8:21but in this case it will still send the Invoice paid event
- 8:26that is, the scope of the Invoice paid event is broader than this event.
- 8:31So we choose to listen to this event, and in the case of this event
- 8:34we can issue credits to users. This is the first point.
- 8:38The second point. The first point actually solves the first problem
- 8:44there are many Webhook events, which one should we choose
- 8:47which correct event to listen to and process.
- 8:49The second problem is that the timing of events is uncertain.
- 8:56As mentioned earlier, we need to monitor the Invoice pay event
- 9:00but this pay event is later than the Checkout Session completed.
- 9:05That is, when the user completes payment on the payment interface and jumps back to the website
- 9:11this event may not have arrived yet or has arrived but not been processed yet
- 9:15all of this is possible.
- 9:17The solution is to introduce a payment confirmation interface
- 9:20that is, after the user completes payment, don't immediately redirect them to the website's billing interface or credits interface
- 9:32but jump to an intermediate page, this intermediate page is called the payment confirmation page.
- 9:36We will conduct polling every two seconds on this page
- 9:41to check whether the Payment record is generated.
- 9:45As mentioned earlier, the Payment record is only created when an Invoice pay event is received.
- 9:52If we check that the Payment record has been generated
- 9:55that means this event has been received
- 9:58the user payment is confirmed successful, then in this case
- 10:02we can jump back to the website's billing page
- 10:05or the credits page.
- 10:08But if this check finds that the Payment record has not been generated yet
- 10:14then it may be that the event has not been processed yet, has not arrived yet, or has not been processed yet
- 10:19then wait a moment and check again after two seconds.
- 10:22At the same time, we limit the maximum check duration to one minute
- 10:25that is, the payment confirmation interface in the middle waits for up to one minute
- 10:30waiting for this Stripe event to arrive and be processed.
- 10:34If it hasn't been waited for after one minute, it times out.
- 10:38There's a small detail here
- 10:40how do I confirm whether the Payment record has been generated?
- 10:44What do I use to check this Payment record?
- 10:48Here we use the Session ID, if the user manually triggers payment
- 10:56they either click on the Price page
- 11:02to subscribe payment or one-time payment
- 11:08that is, purchasing a lifetime membership.
- 11:10Or they purchase credit packages on the credits interface
- 11:15and then jump to the payment page.
- 11:17In both cases, a Checkout Session will be created
- 11:21so there will be a Session ID.
- 11:24When we create a Checkout Session, we will set a
- 11:29payment completion.
- 11:30A page to jump back to after payment completion.
- 11:33For this page, we previously set it to either the billing page
- 11:38or the credits page.
- 11:41Now we will return it to this payment confirmation interface
- 11:44that is Payment, with two parameters.
- 11:48The first parameter is the Session ID, which is the Checkout Session ID mentioned above
- 11:52this is unique.
- 11:53Every time you enter the Checkout payment page, there will be a new
- 11:57Session ID.
- 11:58There's also a callback address
- 12:01that is, if the payment confirmation interface confirms payment completion
- 12:05then where to return?
- 12:07Automatically jump to which page.
- 12:10OK, now that we have this Session ID
- 12:16we can use the Session ID to find the payment table in the database
- 12:21to see if there's a corresponding payment record.
- 12:25OK.
- 12:29The third change, the third major change, is adding a new database field.
- 12:34Let's talk about why we need to add this new database field.
- 12:37According to the third problem, events may be triggered repeatedly
- 12:41so we need to avoid repeated processing of this event.
- 12:45For example, we want to avoid repeatedly issuing credits to this user
- 12:51because if they have been issued, this purchase is successful
- 12:54and we have already issued them, we can't issue credits to them repeatedly
- 12:59so how do we avoid this repeated issuance of credits?
- 13:01First consideration here is whether Session ID can be used, but it's not possible
- 13:08because looking at the entire website based on MkSaaS template functionality
- 13:13in most cases using Session ID is OK.
- 13:15But there's one situation, if the user has already subscribed
- 13:19in the case of automatic renewal, users don't need to go to the payment page to make payment
- 13:26then they won't enter the Checkout session, and there will be no Session ID.
- 13:30So in this case, if automatic renewal is successful
- 13:33and the Invoice paid event is received, then there is no Session ID.
- 13:38In this case, there's no Session ID in the payment record
- 13:43so there's no way to guarantee it's unique.
- 13:48But although there's no Session ID, there must be an Invoice ID
- 13:52that is, an identifier for the invoice number that was successfully paid this time
- 13:57this invoice number is unique
- 14:00at the database level, we ensure that every payment record in the database
- 14:07has a unique Invoice ID field, so it's impossible to insert two payment records with the same Invoice ID.
- 14:17Because if you insert two identical ones, when trying to insert the second payment record with the same Invoice ID
- 14:25the database will report an error.
- 14:26In this case we can return this error to the upper layer.
- 14:33OK, this is the new version's technical
- 14:38payment solution changes, mainly three points
- 14:42adding a new event, adding a new page
- 14:46adding a new database field.
- 14:49Below we combine the new version's complete payment process diagram
- 14:54and practice a process on the MkSaaS Demo site.
- 15:04This is the complete flowchart of the new version's payment module
- 15:09let's go through this step by step.
- 15:11First, the user needs to enter the payment page
- 15:17here we start by registering a user.
- 15:27Let's create a new account.
- 15:36Receive the email and confirm.
- 15:45By default, after registering and logging in, users will enter their Dashboard interface
- 15:51then on the billing interface you can see it's currently a free plan
- 15:57then look at their credits.
- 15:59Currently, they have 100 credits, mainly because registration gave 50
- 16:04and another is that every month free users will be given 50 credits
- 16:09like this.
- 16:10OK, now let's have this user upgrade their membership
- 16:17upgrade to Pro, which is the monthly subscription membership.
- 16:23First make a payment, this is Stripe's Checkout interface.
- 16:28Here, use the Stripe test card 4242 to make a payment.
- 16:34It's currently in test mode, so no actual money will be deducted.
- 16:38MkSaaS's official website Demo site is in test mode
- 16:43so feel free to try it.
- 16:44This is the payment confirmation page, after successful confirmation
- 16:48it will return to this billing page.
- 16:50OK, it shows this user is already on the Pro plan
- 16:54and the validity period is from September 13th to October 13th.
- 17:00Next we'll have the user purchase a credit package.
- 17:05Just now the user had 1,100 credits
- 17:08suppose in the next 30 days, these 1,100 credits are not enough
- 17:12they will go and buy more credits.
- 17:17Let's test the credit purchase process, it's the same here
- 17:22enter the Stripe Checkout interface and complete payment.
- 17:26After payment completion, it will jump to the payment confirmation interface
- 17:31the payment confirmation interface URL has a Session ID
- 17:34and a Callback, after successful confirmation, returning you can see their current credits are 1,300.
- 17:42We can look at the credit transaction records.
- 17:46We just purchased 200 credits
- 17:51and they were added.
- 17:54OK, now let's see what the process is in this flowchart.
- 18:02First, if the user clicks payment, they will enter the Stripe payment interface
- 18:07after payment completion, they will be redirected.
- 18:12Zoom out a bit.
- 18:16It will jump to the payment confirmation interface, but in between
- 18:20what is this here?
- 18:22These are three events, the three types of events that Stripe sends to the website that we monitor
- 18:32the first is related to checkout, a checkout session completed
- 18:36indicating that the payment session just completed
- 18:40then after completion, it will return to the payment confirmation interface.
- 18:45Then the second event is the subscription event subscription create that we monitor
- 18:51update and delete three types of events.
- 18:54What to do when receiving this event
- 18:57and this time we added a new invoice paid
- 19:00event.
- 19:02How do we handle when receiving this event?
- 19:06Let's look at the previous ones. When the old version received the checkout session complete event
- 19:11we would create the payment, the payment record indicating that the user paid successfully
- 19:17but this time, we won't handle this event
- 19:21but moved it to the invoice pay event
- 19:24and previously the old version would create the payment record in the subscription
- 19:27create event
- 19:32now it doesn't do that either, just outputs a log record.
- 19:35So in the new version, you actually don't need to monitor these two events.
- 19:40Our subscription events mainly monitor two
- 19:43one is update and another is delete, because the validity period of the subscription status may change
- 19:49or if the user cancels the subscription
- 19:52the validity period will also change.
- 19:54The subscription status here may also change.
- 19:58Then the key point is that this time we added monitoring
- 20:01invoice paid event, and let's see how it handles it.
- 20:05There are two types here. After receiving the event, we will parse this event
- 20:10to see what kind of purchase happened inside. Is it a one-time payment
- 20:16or a subscription payment?
- 20:18If it's a one-time payment, there are two situations
- 20:21it's possible that the user purchased credits, or it's possible that the user purchased the lifetime plan.
- 20:27Then call different methods respectively, for example, if it's a credit purchase
- 20:31then call this method to update the user's credit balance.
- 20:35If a lifetime plan is purchased, then update the user's subscription status.
- 20:42Then if it's a subscription payment, then update the user's
- 20:47subscription status and validity period.
- 20:51Earlier we talked about the user who completed payment on the checkout interface
- 20:56will return to the payment confirmation interface, where the user waits for the result.
- 21:00Why wait for the result?
- 21:02Because this interface will do polling
- 21:07that is, it will check every two seconds to see if the payment value record for this payment has been generated
- 21:19so it checks the database, this column is the database
- 21:23check whether the payment table in the database has generated the payment record corresponding to this session
- 21:30if generated, it means this payment is confirmed completed
- 21:34then it shows the payment success status and then jumps back.
- 21:39But if after waiting for a minute it still doesn't find that a new payment record has been created
- 21:47then the polling has timed out, we will display a timeout status.
- 21:52If the payment is successful, I didn't see it just now
- 21:57if the payment is successful, the first time I demonstrated was subscription payment success
- 22:03this will automatically redirect to the billing interface
- 22:07then the user displayed is Pro plan, and the validity period is such and such
- 22:11here it will enter the billing interface
- 22:15then call the use current plan hook to query
- 22:19it will call a server action to query the current price plan and subscription status
- 22:24and return this result to be displayed on the interface.
- 22:27But if the user pays successfully and purchases credits
- 22:33then we will redirect the user to the credits interface.
- 22:37Similarly for the credits interface, there will be a credits page
- 22:40then there's a credits card component
- 22:44then that component will call the use credit balance hook
- 22:49it will trigger calling a server action to query the user's credit balance
- 22:54after querying, it returns the user's credit balance to be displayed on the interface.
- 22:58OK, the complete process is like this, roughly divided into three major steps
- 23:03the first step is for the user to perform an operation on the interface
- 23:08click to pay, and then enter Stripe's payment interface
- 23:13after the user completes payment, they will be redirected to the payment confirmation interface we configured
- 23:21so what does this interface do?
- 23:22It's polling, constantly querying the record of this payment
- 23:28confirming whether it was successful.
- 23:30Here we're actually waiting for Stripe to send us the event checkout session completed
- 23:36or subscription events, or invoice paid events
- 23:40what we're mainly waiting for is the invoice paid event
- 23:42after receiving the invoice paid event, we will parse the information in this event
- 23:49then create a payment record
- 23:53a payment record.
- 23:54At the same time, update the user's credit balance or their subscription status.
- 24:01With this payment record, the query here will find the result
- 24:06then payment confirmation is completed.
- 24:08Then we come to the third major step, which is to redirect the user to the corresponding interface
- 24:12if it's subscription status, then it shows subscription
- 24:15then redirect to the billing interface, let the user check their subscription status.
- 24:17If purchasing credits, redirect to the credits interface
- 24:21let the user check their credit balance.
- 24:25The complete process is like this.
- 24:28The various modules of the entire process are actually quite decoupled
- 24:35and I also ensured naming consistency in the code.
- 24:41For example, first on the pricing page
- 24:45the pricing page has a pricing page, and below the page there's a pricing card
- 24:48then the user clicks the checkout button on this pricing card to create a checkout session
- 24:53then redirects the user to the payment page.
- 25:00Then if payment is completed, the user will be redirected to this payment page.
- 25:06Then the payment page has a payment card
- 25:15this card will do some polling and checking
- 25:18regularly checking whether the payment record has been generated.
- 25:22If the payment record has been generated, then based on the callback in the URL parameters
- 25:26determine whether this is a subscription and return to the billing interface
- 25:32or purchase credits and return to the credits interface.
- 25:38If it's a subscription, it will enter the billing page
- 25:42then there's a component called billing card, which will show the user's current
- 25:45price plan and subscription status, validity period, etc.
- 25:50Then if returning to the credits interface, the credits interface
- 25:54will enter the credits page, which contains a credits card
- 25:58it will display the user's current credit balance and credit status.
- 26:02That is, how many credits will expire within a month recently.
- 26:09That's roughly it, this code is relatively clear and easy to understand.
- 26:15The fifth point is template limitations. Although the MkSaaS template has considered many usage scenarios
- 26:18different products have different pricing plans
- 26:23so it's impossible for the template to consider all scenarios.
- 26:33If your product's pricing plan is relatively complex
- 26:38please be sure to read the source code, and based on fully understanding this source code
- 26:44then make your own customizations.
- 26:47The second point is about changes in product pricing plans
- 26:54please be careful when setting product prices, and also be cautious about product price changes
- 26:59don't change prices or pricing plans arbitrarily.
- 27:04Why?
- 27:14Because this will have an impact on the payment module.
- 27:19If you really want to change, make some price adjustments
- 27:19it's recommended to set the original pricing plan to disabled
- 27:25and then add a new plan.
- 27:33If you set the original plan to disabled
- 27:39its original data remains consistent, but
- 27:41it won't be displayed on the interface, so new users won't be able to purchase it
- 27:45this is no problem.
- 27:51If you add a new pricing plan, users will actually see this new plan
- 27:55they can only purchase the new product at this price and won't purchase the old product.
- 27:58So here you must read the source code
- 28:02then see which method best suits your actual situation
- 28:09then make your decision.
About this transcript
This page contains the full transcript of MkSaaS模板的支付模块的方案和流程 by MkSaaS, generated from the public captions YouTube serves with the video. The transcript has 3,681 words across 370 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.