I wrote version 1 of this article before. Since I know PHP again, I will explain the subject of PHP again. But as I said before, you can run this article with different programming languages. Or you can make NoCode Micro SaaS.
In the previous article, I had done it on a product that I had experienced in the past and it wasn’t easy. I encountered many bugs. It was very difficult to fix the bugs and I discovered some hard-to-recover bugs. But if we were to give points according to the previous article with the method I will explain in this article, I could give the first article, that is, the API usage, 10 difficulties out of 10. But using webhook is 2 out of 10 difficulties. You too will see this fantastic leap!
Contents:
- Open a LemonSqueezy Account
- How to Add Product to LemonSqueezy?
- Working Principle of LemonSqueezy Webhook?
- Create Webhook
- How to Code a Webhook?
- How to Build a Micro SaaS with No Code?
Open a LemonSqueezy Account
You can read this in my previous post.
How to Add Product to LemonSqueezy?
You can read this in my previous post.
Working Principle of LemonSqueezy Webhook?
I would like to briefly tell my story of trying this. I developed a Notion widget tool called NotionPlus. And the payment system I used in this process was the LemonSqueezy webhook. That’s all our story. 🙂
What is Webhook?
Webhook is talking between web pages with the help of callback.
For example, when we set up a LemonSqueezy webhook, each payment sends a POST request to a web page that we specify. In this way, we process that POST request and use it in our operations!
IMPORTANT ALERT! In order to use the webhook options, the user must be a member! Otherwise, the webhook will try to process an email that does not exist in the database and will not be able to. So the user’s subscription will not be active at all! To resolve this, link to the signup page before linking directly to the LemonSqueezy link in the pricing prices. If the user logs in then replace these links with LemonSqueezy links.
I will tell you how to do this in detail below, but if you just want to download the codes: Download LemonSqueezy Webhook Micro SaaS Codes!
Webhooks That Should Be Used for Micro SaaS
LemonSqueezy has all the webhooks you need. There are webhooks even outside your need. Of course, we don’t need to use all of them. In fact, if you are going to make Micro SaaS, it will be enough to use only 4 webhooks.
If you remember, in the previous article, we were checking with cronjob at regular intervals. Webhook doesn’t need this. Webhook only works when there is a transaction. While this is tremendous for the health of your website, it supports a clean code structure.
Order (webhook):
A page request is sent that we specify in all purchases. This is how we will control every purchase in this area.
It will send a request to the order.php file when a purchase is made. Here it is necessary to process every request. Check the existence of the incoming email in the database. Then, if there is no email, a new email should be added. If there is an email added before, update that email’s membership confirmation to 1.
Cancel (webhook):
It only works on cancellations. As you know, Micro SaaS means recurring payments at a certain time. You must create a LemonSqueezy product and subscribe to the payment area. And this section will work if the user unsubscribes.
Search the database for the email information of the incoming user on a subscription cancellation. Change the membership confirmation in the existing data to “0”. In this way, you cancel the user’s membership approval.
You can prevent access to the main panel by removing the message “Increase your membership” for those whose membership approval is “0” in the user panels of your website. In this way, the member has to buy. When purchased, the user will view the page with the membership confirmation “1” in the user panel. The article in this paragraph constitutes the main mechanics of micro SaaS.
Expired (webhook):
In subscription systems, not every cancellation is made by the user. When you enter this sector, you will see how detailed the system is. Some different situations may take place in this subscription system. For example, the user’s bank may run out of money. That’s why he can’t make the payment. For different situations like this, we need to use the “expired” property. In case the user stops paying in different situations, we still have to do the same thing we did in the “cancel” file. You can use the same code used directly on the “cancel” page.
Resume (webhook):
This is for people who subscribe but then cancel. This feature works for people who have subscribed, or canceled but then resubscribed. In basic terms, you can create a micro SaaS with “order“, “cancel“, and “expired“. But I also use “resume” as a backup plan.
In “resume“, you can use the same codes that you use in the order.
To make Micro SaaS, we can measure this by setting the member’s approval to “1” or “0”. If you have “2” or more subscription packages then what to do?
Actually, we are doing a simple operation. We add a little something to the code in both “order” and “resume“. In the webhook request, the package ID information about which package you purchased is also sent. The numbers in the URL of our subscription product on LemonSqueezy become our ID for that product.
https://app.lemonsqueezy.com/products/01234
Create Webhook
Creating a webhook is very easy.
https://app.lemonsqueezy.com/settings/webhooks
- Go to this page and click the “+” button.
- “Callback URL” is important! Here you should write the file to which the request will go. For example, if you want to order, create a file called order.php on your website. Paste the link to that file here.
- In the “Signing secret” section, you should write a password so that if other people reach your callback URL, it cannot run. When you type this password, do not lose it! We will use it later!
- “What updates should we send?” here we have to check the function we will use. If we are using it for order.php, we must select the “order_created” option.
order_created: runs on every purchase.
subscription_cancelled: works only when subscriptions are canceled.
subscription_resumed: works when the canceled subscription is subscribed again.
subscription_expired: Runs in scenarios where the user does not cancel it, if it is still unpaid.
How to Code a Webhook?
https://docs.lemonsqueezy.com/help/webhooks
When you go to this page, it shows you how to do it. But I will compress and distill the information and tell you about micro SaaS.
Paste the following code at the top of my PHP file:
$secret = '[SIGNING_SECRET]';
$payload = file_get_contents('php://input');
$hash = hash_hmac('sha256', $payload, $secret);
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
if (!hash_equals($hash, $signature)) {
throw new Exception('Invalid signature.');
}
Do you know how I told you about a password you had to lose? Paste it into $secret. Now we will write every action we will do under this code block.
If you noticed, webhook outputs are in JSON format! Then the first thing we will do is convert it to array format. So we need to write the following code.
$j = json_decode($webhook,true);
Redundant Info: You can write $json instead of $j. Since I don’t want to sell my NotionPlus product, I coded it myself. If I was thinking of exiting, I would have said $json. Because the buyer should not be confused in the development process. But I have shared the codes for you under this article as downloadable. You can download and use it directly!
There are two things that will work for us. 1. the buyer’s email address and the product ID with which we can identify which product he has purchased. For this, we need to write the following code!
$user_email = $j["data"]["attributes"]["user_email"];
$product_id = $j["data"]["attributes"]["first_order_item"]["product_id"];
Now, what we need to do is update the purchased product according to the product ids.
if ($product_id == "123") { // Starter
// Update code
}elseif ($product_id == "321") { // PRO
// Update code
}elseif ($product_id == "987") { // PRE
// Update code
}elseif ($product_id == "789") { // Ultra
// Update code
}
In the cancellation process, you will update the member confirmation to “0” by using the update code. I don’t want to deal with these, can you give me the direct codes? If you want to download and use:
Download LemonSqueezy Webhook Micro SaaS Codes!
How to Build a Micro SaaS with No Code?
I’m not a great no-coder. I like automation more. Although it also falls within the nocode field. So how do we make Micro SaaS without using code? Although I do not know detailed information about this, you can access a lot of information by doing a little research.
It is possible to make micro SaaS using webhook with Bubble. According to the logic I explained above, if you adapt it to Bubble, I believe you will.
Zapier itself has a zap about it! View the Zap!
Like Zapier, IFTTT and Make.com have webhooks. Here you can process every transaction. For example, you can add data to Airtable in every webhook request. You can delete those lines in every “cancel” and “expired“. In this way, you will have a list of your active subscribers. Then you can create an area that the emails in this list can access. You can do this with Webflow or Softr.
As a result, you can create Micro SaaS using LemonSqueezy webhooks. If you want to see my Micro SaaS product that I created with this method, it’s called Notion Plus. Notion widgets tool.
I understood what you said, but if you wish we had a chance to download all the codes, I compiled the necessary codes for you. You can download Micro SaaS webhook codes by clicking the link below and using them quickly in your own project.