Configuration

This choice can be made using the GoFast CLI or by modifying the docker-compose.yml file.

via CLI

Possible options are Stripe, Lemon Squeezy and Local (mock).

via Docker Compose

Possible options are stripe, lemon and local.

Payments Configuration

Depending on the provider, you need to set the following environment variables in the docker-compose.yml file:

  • for stripe, set STRIPE_API_KEY and STRIPE_PRICE_ID.
  • for lemon, set LEMON_API_KEY, LEMON_VARIANT_ID and LEMON_STORE_ID.
  • for local, no configuration is needed.
services:
  server:
    environment:
      PAYMENT_PROVIDER: stripe
      STRIPE_API_KEY: ${STRIPE_API_KEY}
      STRIPE_PRICE_ID: ${STRIPE_PRICE_ID}
      # PAYMENT_PROVIDER: lemon
      # LEMON_API_KEY: ${LEMON_API_KEY}
      # LEMON_VARIANT_ID: ${LEMON_VARIANT_ID}
      # LEMON_STORE_ID: ${LEMON_STORE_ID}
      # PAYMENT_PROVIDER: local

Implementation Details

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:

  1. The user clicks the subscribe button, sending a request to the backend.
  2. The backend generates a Checkout URL.
  3. The user completes the order.
  4. A webhook on the backend listens for changes and updates the client’s status accordingly.
  5. After the client is created in the payment provider, they can visit their customer portal to check all invoices.

The local provider is a mock implementation that doesn’t require any configuration. It generates a random customer ID and subscription end date.

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: /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:

stripe listen --forward-to localhost:4002/payments/webhook

Lemon Squeezy doesn’t have a CLI, so you need to set up a tunnel to forward the events to the local server. One option is Cloudflare Tunnel.

Adding a New Provider

To add a new provider, follow these steps in the /payment/provider.go file:

  1. Add the new provider to the Provider constant.
const (
    stripe Provider = "stripe"
    lemon Provider = "lemon"
    local Provider = "local"
    paddle Provider = "paddle"
)
  1. Create a new provider struct.
type paddleProvider struct{}
  1. Return the new provider in the NewProvider function:
case Poodle:
    return &paddleProvider{}
  1. Implement the required methods:
func (p *paddleProvider) createCustomer(ctx context.Context, userEmail string) (customerId string, err error) {
    // Implement the logic to create a customer
}

func (p *paddleProvider) createPaymentCheckout(ctx context.Context, subscriptionId string) (url string, err error) {
    // Implement the logic to create a payment checkout
}

func (p *paddleProvider) createPaymentPortal(ctx context.Context, subscriptionId string) (url string, err error) {
    // Implement the logic to create a payment portal
}

func (p *paddleProvider) handleWebhook(ctx context.Context, payload []byte) (customerId string, subEndDate *time.Time, err error) {
    // Implement the logic to handle a webhook
}
  1. Add any new secrets in the env.go file.

  2. Fill in the docker-compose.yml file with the new provider configuration.

services:
  server:
    environment:
      PAYMENT_PROVIDER: paddle
      PADDLE_API_KEY: ${PADDLE_API_KEY}
      PADDLE_PRICE_ID: ${PADDLE_PRICE_ID}

Getting Secrets

Stripe

To get the STRIPE_API_KEY and STRIPE_PRICE_ID, you need to create an account on the Stripe website and get the keys from the dashboard.

  1. Go to the Stripe Api Section.
  2. Copy the Secret Key and set it as the STRIPE_API_KEY.
  3. Create a new product and price in the Products section.
  4. Go to the Prices tab and create a new price.
  5. Go to the price details.
  6. Copy the Price ID and set it as the STRIPE_PRICE_ID.

Lemon Squeezy

To get the LEMON_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.

  1. Go to the Lemon Squeezy Api Section.
  2. Create a new API key and set it as the LEMON_API_KEY.
  3. Create a new product on the Products section.
  4. On the list, open the dropdown and click on Copy variant ID. Set it as the LEMON_VARIANT_ID.
  5. Go to the Stores section, where you can find the Store ID. Set it as the LEMON_STORE_ID.

Need help?

Visit our discord server to ask any questions, make suggestions and give feedback :).

https://discord.gg/EdSZbQbRyJ