YouTube2Text

MkSaaS模板的支付模块的方案和流程 — Transcript

by MkSaaS · 3,681 words · 370 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello everyone, the MkSaaS template recently underwent a round of payment module reconstruction.
  2. 0:06This video mainly introduces the technical implementation of the new payment module
  3. 0:11and why this reconstruction was needed
  4. 0:15what problems the old version had and how the new version solves these problems.
  5. 0:21First, let's look at Stripe's best practices reference
  6. 0:28which is this GitHub repository, the author is the author of T3 Chat
  7. 0:32a very famous international developer who wrote a document
  8. 0:38this document introduces how to integrate Stripe payment
  9. 0:43he recommended a process for connecting to Stripe.
  10. 0:49Everyone can take a look at the content of this document when you have time.
  11. 0:53The core idea is that Stripe's Webhook events are numerous
  12. 0:59and the order is quite messy, making them troublesome to handle.
  13. 1:04Processing them can be quite troublesome.
  14. 1:07So it provides an implementation solution, which is a method called sync after success.
  15. 1:15That is, when you receive any Stripe event
  16. 1:20it will call this method to actively request the Stripe interface
  17. 1:27then synchronize some of the user's data to the local system
  18. 1:32to ensure that the user's subscription status remains consistent with the status on Stripe.
  19. 1:40The complete process is quite troublesome
  20. 1:44and it doesn't provide complete source code for accessing a real project.
  21. 1:51So I based it on this article
  22. 1:57and did a practice on the MkSaaS template.
  23. 2:02First, let's take a look. According to this article's introduction
  24. 2:08it's no exaggeration to say that basically all open source SaaS templates you can see on the market have problems.
  25. 2:14What is this problem?
  26. 2:15It's that users may not have actually completed the payment
  27. 2:22but this SaaS website thinks it has completed the payment
  28. 2:26and then changes its subscription status or issues credits to it.
  29. 2:31Recently, customers using the MkSaaS template
  30. 2:35occasionally have users reporting similar issues
  31. 2:39which is actually the same reason.
  32. 2:43Every SaaS website that wants to connect to Stripe
  33. 2:47I summarized that the main thing is to solve these three problems.
  34. 2:50The first is that there are many Webhook events, we must choose the correct events to monitor and process
  35. 2:57if you listen to the wrong events
  36. 3:01and process them, it's actually useless.
  37. 3:03The second is that the order of Stripe events may be messy
  38. 3:07and the timing is also uncertain.
  39. 3:09So the code of the SaaS website must be compatible.
  40. 3:13Moreover, if necessary, it may be necessary to wait for a certain event to arrive.
  41. 3:20The third is that events may be triggered repeatedly, that is, events sent to you by Stripe
  42. 3:27may be triggered repeatedly, possibly due to Stripe issues
  43. 3:31or possibly because your website failed to handle this event last time
  44. 3:34then Stripe waits for a while and then retriggers.
  45. 3:38So the website's processing logic should consider idempotency
  46. 3:41to avoid repeated processing of the same event
  47. 3:43for example, credits being issued repeatedly.
  48. 3:47Next, we'll look at the old version's payment solution.
  49. 3:49As for the payment process demonstration of the old version, you can see the previous
  50. 3:55video tutorial and payment documentation.
  51. 3:59The previous video demonstrates how to use the MkSaaS template to configure and deploy from scratch
  52. 4:09including how to configure the payment module
  53. 4:11how to create products in Stripe and then configure these environment variables in the MkSaaS template.
  54. 4:21I won't introduce this process
  55. 4:25as for the original flowchart, the one introduced before is this one
  56. 4:28it's quite simple, users on your webpage
  57. 4:32click a payment button, then we call the Stripe interface
  58. 4:35get a Checkout URL and then open this payment interface.
  59. 4:42After the user completes payment on this payment interface
  60. 4:45some Webhook event notifications will be sent to this website
  61. 4:48after this website receives this event, it determines whether the user payment succeeded or failed
  62. 4:52then performs some operations.
  63. 4:54The previous process was that simple.
  64. 4:56This old version of the payment solution listened to four events
  65. 4:59the first is Checkout session completed, where users complete payment in the Checkout session and three subscription-related events
  66. 5:08including Customer subscription created, updated and deleted.
  67. 5:13What was the problem before?
  68. 5:15If we received the Checkout session completed event
  69. 5:21we would create a Payment record
  70. 5:25and mark the user's payment as completed.
  71. 5:27But in fact, the arrival of this event doesn't always mean that payment is actually completed
  72. 5:33because why?
  73. 5:33Some payment methods may need some time to process and confirm
  74. 5:39it just means that the user completed the payment process on the Checkout page
  75. 5:44and then returned to your website.
  76. 5:47But at this time, the payment may still be processing and confirming
  77. 5:53there's a possibility that the payment will eventually fail or be canceled
  78. 5:56but we had already marked the user payment as successful in this case.
  79. 6:02So the user's subscription status has been modified
  80. 6:07or credits have been issued to the user.
  81. 6:09So this solution is problematic.
  82. 6:12This is how most open source SaaS templates on the market handle it
  83. 6:17so basically all of them have this problem.
  84. 6:20Let's see how the new version's payment solution solves these problems.
  85. 6:25This is the complete flowchart of the new version's payment, which is this diagram
  86. 6:30we'll explain the process in this diagram in detail later
  87. 6:34and perform a functional demonstration at the demo MkSaaS demo site.
  88. 6:40Let me first briefly introduce what changes were made to solve the previous problems.
  89. 6:49First, the first change is that the monitored events added one called Invoice Paid.
  90. 6:56Why should we monitor this event?
  91. 6:57Because when this event is received, it means that payment is actually completed
  92. 7:01in this case we can update the user's subscription status
  93. 7:06or issue credits.
  94. 7:09Previously, we would create the Payment record when Checkout Session completed or when receiving
  95. 7:16Subscription created event
  96. 7:21marking that the user created, marking that the user completed payment
  97. 7:25and creating a successful payment record for them.
  98. 7:30But this is problematic, right?
  99. 7:31So this time we changed it to when we receive the Invoice paid event
  100. 7:36only then create this Payment record and save it to the database
  101. 7:40and at the same time update the user's subscription status or issue credits to them.
  102. 7:45There's a question here about why we chose to add this event
  103. 7:52instead of the Invoice payment succeeded event which looks similar
  104. 7:57one is payment successful, the other is paid.
  105. 7:59Actually there is a difference.
  106. 8:01The paid event includes a special case
  107. 8:04such as zero-dollar purchases, if your product
  108. 8:10gives someone a coupon, and they can purchase your product for zero dollars
  109. 8:17then it won't send the Invoice payment succeeded event
  110. 8:21but in this case it will still send the Invoice paid event
  111. 8:26that is, the scope of the Invoice paid event is broader than this event.
  112. 8:31So we choose to listen to this event, and in the case of this event
  113. 8:34we can issue credits to users. This is the first point.
  114. 8:38The second point. The first point actually solves the first problem
  115. 8:44there are many Webhook events, which one should we choose
  116. 8:47which correct event to listen to and process.
  117. 8:49The second problem is that the timing of events is uncertain.
  118. 8:56As mentioned earlier, we need to monitor the Invoice pay event
  119. 9:00but this pay event is later than the Checkout Session completed.
  120. 9:05That is, when the user completes payment on the payment interface and jumps back to the website
  121. 9:11this event may not have arrived yet or has arrived but not been processed yet
  122. 9:15all of this is possible.
  123. 9:17The solution is to introduce a payment confirmation interface
  124. 9:20that is, after the user completes payment, don't immediately redirect them to the website's billing interface or credits interface
  125. 9:32but jump to an intermediate page, this intermediate page is called the payment confirmation page.
  126. 9:36We will conduct polling every two seconds on this page
  127. 9:41to check whether the Payment record is generated.
  128. 9:45As mentioned earlier, the Payment record is only created when an Invoice pay event is received.
  129. 9:52If we check that the Payment record has been generated
  130. 9:55that means this event has been received
  131. 9:58the user payment is confirmed successful, then in this case
  132. 10:02we can jump back to the website's billing page
  133. 10:05or the credits page.
  134. 10:08But if this check finds that the Payment record has not been generated yet
  135. 10:14then it may be that the event has not been processed yet, has not arrived yet, or has not been processed yet
  136. 10:19then wait a moment and check again after two seconds.
  137. 10:22At the same time, we limit the maximum check duration to one minute
  138. 10:25that is, the payment confirmation interface in the middle waits for up to one minute
  139. 10:30waiting for this Stripe event to arrive and be processed.
  140. 10:34If it hasn't been waited for after one minute, it times out.
  141. 10:38There's a small detail here
  142. 10:40how do I confirm whether the Payment record has been generated?
  143. 10:44What do I use to check this Payment record?
  144. 10:48Here we use the Session ID, if the user manually triggers payment
  145. 10:56they either click on the Price page
  146. 11:02to subscribe payment or one-time payment
  147. 11:08that is, purchasing a lifetime membership.
  148. 11:10Or they purchase credit packages on the credits interface
  149. 11:15and then jump to the payment page.
  150. 11:17In both cases, a Checkout Session will be created
  151. 11:21so there will be a Session ID.
  152. 11:24When we create a Checkout Session, we will set a
  153. 11:29payment completion.
  154. 11:30A page to jump back to after payment completion.
  155. 11:33For this page, we previously set it to either the billing page
  156. 11:38or the credits page.
  157. 11:41Now we will return it to this payment confirmation interface
  158. 11:44that is Payment, with two parameters.
  159. 11:48The first parameter is the Session ID, which is the Checkout Session ID mentioned above
  160. 11:52this is unique.
  161. 11:53Every time you enter the Checkout payment page, there will be a new
  162. 11:57Session ID.
  163. 11:58There's also a callback address
  164. 12:01that is, if the payment confirmation interface confirms payment completion
  165. 12:05then where to return?
  166. 12:07Automatically jump to which page.
  167. 12:10OK, now that we have this Session ID
  168. 12:16we can use the Session ID to find the payment table in the database
  169. 12:21to see if there's a corresponding payment record.
  170. 12:25OK.
  171. 12:29The third change, the third major change, is adding a new database field.
  172. 12:34Let's talk about why we need to add this new database field.
  173. 12:37According to the third problem, events may be triggered repeatedly
  174. 12:41so we need to avoid repeated processing of this event.
  175. 12:45For example, we want to avoid repeatedly issuing credits to this user
  176. 12:51because if they have been issued, this purchase is successful
  177. 12:54and we have already issued them, we can't issue credits to them repeatedly
  178. 12:59so how do we avoid this repeated issuance of credits?
  179. 13:01First consideration here is whether Session ID can be used, but it's not possible
  180. 13:08because looking at the entire website based on MkSaaS template functionality
  181. 13:13in most cases using Session ID is OK.
  182. 13:15But there's one situation, if the user has already subscribed
  183. 13:19in the case of automatic renewal, users don't need to go to the payment page to make payment
  184. 13:26then they won't enter the Checkout session, and there will be no Session ID.
  185. 13:30So in this case, if automatic renewal is successful
  186. 13:33and the Invoice paid event is received, then there is no Session ID.
  187. 13:38In this case, there's no Session ID in the payment record
  188. 13:43so there's no way to guarantee it's unique.
  189. 13:48But although there's no Session ID, there must be an Invoice ID
  190. 13:52that is, an identifier for the invoice number that was successfully paid this time
  191. 13:57this invoice number is unique
  192. 14:00at the database level, we ensure that every payment record in the database
  193. 14:07has a unique Invoice ID field, so it's impossible to insert two payment records with the same Invoice ID.
  194. 14:17Because if you insert two identical ones, when trying to insert the second payment record with the same Invoice ID
  195. 14:25the database will report an error.
  196. 14:26In this case we can return this error to the upper layer.
  197. 14:33OK, this is the new version's technical
  198. 14:38payment solution changes, mainly three points
  199. 14:42adding a new event, adding a new page
  200. 14:46adding a new database field.
  201. 14:49Below we combine the new version's complete payment process diagram
  202. 14:54and practice a process on the MkSaaS Demo site.
  203. 15:04This is the complete flowchart of the new version's payment module
  204. 15:09let's go through this step by step.
  205. 15:11First, the user needs to enter the payment page
  206. 15:17here we start by registering a user.
  207. 15:27Let's create a new account.
  208. 15:36Receive the email and confirm.
  209. 15:45By default, after registering and logging in, users will enter their Dashboard interface
  210. 15:51then on the billing interface you can see it's currently a free plan
  211. 15:57then look at their credits.
  212. 15:59Currently, they have 100 credits, mainly because registration gave 50
  213. 16:04and another is that every month free users will be given 50 credits
  214. 16:09like this.
  215. 16:10OK, now let's have this user upgrade their membership
  216. 16:17upgrade to Pro, which is the monthly subscription membership.
  217. 16:23First make a payment, this is Stripe's Checkout interface.
  218. 16:28Here, use the Stripe test card 4242 to make a payment.
  219. 16:34It's currently in test mode, so no actual money will be deducted.
  220. 16:38MkSaaS's official website Demo site is in test mode
  221. 16:43so feel free to try it.
  222. 16:44This is the payment confirmation page, after successful confirmation
  223. 16:48it will return to this billing page.
  224. 16:50OK, it shows this user is already on the Pro plan
  225. 16:54and the validity period is from September 13th to October 13th.
  226. 17:00Next we'll have the user purchase a credit package.
  227. 17:05Just now the user had 1,100 credits
  228. 17:08suppose in the next 30 days, these 1,100 credits are not enough
  229. 17:12they will go and buy more credits.
  230. 17:17Let's test the credit purchase process, it's the same here
  231. 17:22enter the Stripe Checkout interface and complete payment.
  232. 17:26After payment completion, it will jump to the payment confirmation interface
  233. 17:31the payment confirmation interface URL has a Session ID
  234. 17:34and a Callback, after successful confirmation, returning you can see their current credits are 1,300.
  235. 17:42We can look at the credit transaction records.
  236. 17:46We just purchased 200 credits
  237. 17:51and they were added.
  238. 17:54OK, now let's see what the process is in this flowchart.
  239. 18:02First, if the user clicks payment, they will enter the Stripe payment interface
  240. 18:07after payment completion, they will be redirected.
  241. 18:12Zoom out a bit.
  242. 18:16It will jump to the payment confirmation interface, but in between
  243. 18:20what is this here?
  244. 18:22These are three events, the three types of events that Stripe sends to the website that we monitor
  245. 18:32the first is related to checkout, a checkout session completed
  246. 18:36indicating that the payment session just completed
  247. 18:40then after completion, it will return to the payment confirmation interface.
  248. 18:45Then the second event is the subscription event subscription create that we monitor
  249. 18:51update and delete three types of events.
  250. 18:54What to do when receiving this event
  251. 18:57and this time we added a new invoice paid
  252. 19:00event.
  253. 19:02How do we handle when receiving this event?
  254. 19:06Let's look at the previous ones. When the old version received the checkout session complete event
  255. 19:11we would create the payment, the payment record indicating that the user paid successfully
  256. 19:17but this time, we won't handle this event
  257. 19:21but moved it to the invoice pay event
  258. 19:24and previously the old version would create the payment record in the subscription
  259. 19:27create event
  260. 19:32now it doesn't do that either, just outputs a log record.
  261. 19:35So in the new version, you actually don't need to monitor these two events.
  262. 19:40Our subscription events mainly monitor two
  263. 19:43one is update and another is delete, because the validity period of the subscription status may change
  264. 19:49or if the user cancels the subscription
  265. 19:52the validity period will also change.
  266. 19:54The subscription status here may also change.
  267. 19:58Then the key point is that this time we added monitoring
  268. 20:01invoice paid event, and let's see how it handles it.
  269. 20:05There are two types here. After receiving the event, we will parse this event
  270. 20:10to see what kind of purchase happened inside. Is it a one-time payment
  271. 20:16or a subscription payment?
  272. 20:18If it's a one-time payment, there are two situations
  273. 20:21it's possible that the user purchased credits, or it's possible that the user purchased the lifetime plan.
  274. 20:27Then call different methods respectively, for example, if it's a credit purchase
  275. 20:31then call this method to update the user's credit balance.
  276. 20:35If a lifetime plan is purchased, then update the user's subscription status.
  277. 20:42Then if it's a subscription payment, then update the user's
  278. 20:47subscription status and validity period.
  279. 20:51Earlier we talked about the user who completed payment on the checkout interface
  280. 20:56will return to the payment confirmation interface, where the user waits for the result.
  281. 21:00Why wait for the result?
  282. 21:02Because this interface will do polling
  283. 21:07that is, it will check every two seconds to see if the payment value record for this payment has been generated
  284. 21:19so it checks the database, this column is the database
  285. 21:23check whether the payment table in the database has generated the payment record corresponding to this session
  286. 21:30if generated, it means this payment is confirmed completed
  287. 21:34then it shows the payment success status and then jumps back.
  288. 21:39But if after waiting for a minute it still doesn't find that a new payment record has been created
  289. 21:47then the polling has timed out, we will display a timeout status.
  290. 21:52If the payment is successful, I didn't see it just now
  291. 21:57if the payment is successful, the first time I demonstrated was subscription payment success
  292. 22:03this will automatically redirect to the billing interface
  293. 22:07then the user displayed is Pro plan, and the validity period is such and such
  294. 22:11here it will enter the billing interface
  295. 22:15then call the use current plan hook to query
  296. 22:19it will call a server action to query the current price plan and subscription status
  297. 22:24and return this result to be displayed on the interface.
  298. 22:27But if the user pays successfully and purchases credits
  299. 22:33then we will redirect the user to the credits interface.
  300. 22:37Similarly for the credits interface, there will be a credits page
  301. 22:40then there's a credits card component
  302. 22:44then that component will call the use credit balance hook
  303. 22:49it will trigger calling a server action to query the user's credit balance
  304. 22:54after querying, it returns the user's credit balance to be displayed on the interface.
  305. 22:58OK, the complete process is like this, roughly divided into three major steps
  306. 23:03the first step is for the user to perform an operation on the interface
  307. 23:08click to pay, and then enter Stripe's payment interface
  308. 23:13after the user completes payment, they will be redirected to the payment confirmation interface we configured
  309. 23:21so what does this interface do?
  310. 23:22It's polling, constantly querying the record of this payment
  311. 23:28confirming whether it was successful.
  312. 23:30Here we're actually waiting for Stripe to send us the event checkout session completed
  313. 23:36or subscription events, or invoice paid events
  314. 23:40what we're mainly waiting for is the invoice paid event
  315. 23:42after receiving the invoice paid event, we will parse the information in this event
  316. 23:49then create a payment record
  317. 23:53a payment record.
  318. 23:54At the same time, update the user's credit balance or their subscription status.
  319. 24:01With this payment record, the query here will find the result
  320. 24:06then payment confirmation is completed.
  321. 24:08Then we come to the third major step, which is to redirect the user to the corresponding interface
  322. 24:12if it's subscription status, then it shows subscription
  323. 24:15then redirect to the billing interface, let the user check their subscription status.
  324. 24:17If purchasing credits, redirect to the credits interface
  325. 24:21let the user check their credit balance.
  326. 24:25The complete process is like this.
  327. 24:28The various modules of the entire process are actually quite decoupled
  328. 24:35and I also ensured naming consistency in the code.
  329. 24:41For example, first on the pricing page
  330. 24:45the pricing page has a pricing page, and below the page there's a pricing card
  331. 24:48then the user clicks the checkout button on this pricing card to create a checkout session
  332. 24:53then redirects the user to the payment page.
  333. 25:00Then if payment is completed, the user will be redirected to this payment page.
  334. 25:06Then the payment page has a payment card
  335. 25:15this card will do some polling and checking
  336. 25:18regularly checking whether the payment record has been generated.
  337. 25:22If the payment record has been generated, then based on the callback in the URL parameters
  338. 25:26determine whether this is a subscription and return to the billing interface
  339. 25:32or purchase credits and return to the credits interface.
  340. 25:38If it's a subscription, it will enter the billing page
  341. 25:42then there's a component called billing card, which will show the user's current
  342. 25:45price plan and subscription status, validity period, etc.
  343. 25:50Then if returning to the credits interface, the credits interface
  344. 25:54will enter the credits page, which contains a credits card
  345. 25:58it will display the user's current credit balance and credit status.
  346. 26:02That is, how many credits will expire within a month recently.
  347. 26:09That's roughly it, this code is relatively clear and easy to understand.
  348. 26:15The fifth point is template limitations. Although the MkSaaS template has considered many usage scenarios
  349. 26:18different products have different pricing plans
  350. 26:23so it's impossible for the template to consider all scenarios.
  351. 26:33If your product's pricing plan is relatively complex
  352. 26:38please be sure to read the source code, and based on fully understanding this source code
  353. 26:44then make your own customizations.
  354. 26:47The second point is about changes in product pricing plans
  355. 26:54please be careful when setting product prices, and also be cautious about product price changes
  356. 26:59don't change prices or pricing plans arbitrarily.
  357. 27:04Why?
  358. 27:14Because this will have an impact on the payment module.
  359. 27:19If you really want to change, make some price adjustments
  360. 27:19it's recommended to set the original pricing plan to disabled
  361. 27:25and then add a new plan.
  362. 27:33If you set the original plan to disabled
  363. 27:39its original data remains consistent, but
  364. 27:41it won't be displayed on the interface, so new users won't be able to purchase it
  365. 27:45this is no problem.
  366. 27:51If you add a new pricing plan, users will actually see this new plan
  367. 27:55they can only purchase the new product at this price and won't purchase the old product.
  368. 27:58So here you must read the source code
  369. 28:02then see which method best suits your actual situation
  370. 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.