Implementation
The main logic can be found in theservice-go-user/domain/payment/provider.go
file.
The payments implementation can generate the client Checkout Session to process payments, the Customer Portal to check invoices, and includes a Webhook implementation to listen for changes.
The flow is as follows:
- The user clicks the subscribe button, sending a request to the backend.
- The backend generates a Checkout URL.
- The user completes the order.
- A webhook on the backend listens for changes and updates the client’s status accordingly.
- After the client is created in the payment provider, they can visit their customer portal to check all invoices.
Webhook
Since the webhook needs to listen for changes, the gRPC configuration still includes an HTTP server. The payments webhook has a separate file for HTTP routes:service-go-user/http/route_webhook.go
.
Regardless of the provider, remember to set up the webhook URL in the provider’s dashboard, pointing to the /payments/webhook
route.
To test Stripe webhook locally, you can use the Stripe CLI, authenticate, and forward the events to the local server:
Configuration
This choice can be made using the GoFast CLI or by modifying thedocker-compose.yml
file.
via CLI
Possible options areStripe
, Lemon Squeezy
and Local (mock)
.
via Docker Compose
Possible options arestripe
, lemon
and local
.
Payments Configuration
Depending on the provider, you need to set the following environment variables in thedocker-compose.yml
file:
- for
stripe
, setSTRIPE_API_KEY
andSTRIPE_PRICE_ID
. - for
lemon
, setLEMON_API_KEY
,LEMON_VARIANT_ID
andLEMON_STORE_ID
. - for
local
, no configuration is needed.
Getting Secrets
Stripe
To get theSTRIPE_API_KEY
, STRIPE_PRICE_ID
and STRIPE_WEBHOOK_SECRET
, you need to create an account on the Stripe website and get the keys from the dashboard.
- Go to the Stripe Api Section.
- Copy the
Secret Key
and set it as theSTRIPE_API_KEY
. - Create a new product and price in the Products section.
- Go to the
Prices
tab and create a new price. - Go to the price details.
- Copy the
Price ID
and set it as theSTRIPE_PRICE_ID
. - Go to the Webhooks section.
- Create a new webhook with a
Signing Secret
. - Copy the
Signing Secret
and set it as theSTRIPE_WEBHOOK_SECRET
.
Lemon Squeezy
To get theLEMON_API_KEY
, LEMON_VARIANT_ID
and LEMON_STORE_ID
, you need to create an account on the Lemon Squeezy website and get the keys from the dashboard.
- Go to the Lemon Squeezy Api Section.
- Create a new API key and set it as the
LEMON_API_KEY
. - Create a new product on the Products section.
- On the list, open the dropdown and click on
Copy variant ID
. Set it as theLEMON_VARIANT_ID
. - Go to the Stores section, where you can find the
Store ID
. Set it as theLEMON_STORE_ID
.