Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions with-astro/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://docs.polar.sh/integrate/oat
POLAR_ACCESS_TOKEN=

# https://docs.polar.sh/integrate/webhooks/endpoints#setup-webhooks
POLAR_WEBHOOK_SECRET=

# URL to redirect to after successful order
POLAR_SUCCESS_URL=

# use the above same approach to get the sandbox credentials.
SANDBOX_POLAR_ACCESS_TOKEN=

SANDBOX_POLAR_WEBHOOK_SECRET=

SANDBOX_POLAR_SUCCESS_URL=

# Polar server mode (production or sandbox)
POLAR_MODE=
29 changes: 29 additions & 0 deletions with-astro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# build output
dist/
# generated types
.astro/

# dependencies
node_modules/

# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*


# environment variables
.env
.env.production

# macOS-specific files
.DS_Store

# jetbrains setting folder
.idea/

#server-build files
.vercel

.vercel
3 changes: 3 additions & 0 deletions with-astro/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
README.md
5 changes: 5 additions & 0 deletions with-astro/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"printWidth": 180,
"singleQuote": true
}
4 changes: 4 additions & 0 deletions with-astro/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"recommendations": ["astro-build.astro-vscode"],
"unwantedRecommendations": []
}
11 changes: 11 additions & 0 deletions with-astro/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "./node_modules/.bin/astro dev",
"name": "Development server",
"request": "launch",
"type": "node-terminal"
}
]
}
110 changes: 110 additions & 0 deletions with-astro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
![](../logo.svg)

# Getting Started with Polar and Astro

This repo is a demonstration of the integration of Polar features such as Webhooks, Customer Portal and Checkout creation organization in Astro.

## Prerequisites

- Node.js installed on your system
- Your POLAR_ACCESS_TOKEN, POLAR_WEBHOOK_SECRET, SUCCESS_URL and SANDBOX_POLAR_ACCESS_TOKEN, SANDBOX_POLAR_WEBHOOK_SECRET, SANDBOX_POLAR_SUCCESS_URL, and POLAR_MODE
> this is an optional configuration, adjust based on your needs



## 1. Clone the repository

```bash
npx degit polarsource/examples/with-astro ./with-astro
```

## 2. Install dependencies:

```bash
npm install
```

## 3. Configure environment variables:

Create a `.env` file in the project root with your Polar credentials:

```bash
cp .env.example .env
```

Add your Polar API credentials to the `.env` file:

```env
POLAR_ACCESS_TOKEN=

POLAR_WEBHOOK_SECRET=

POLAR_SUCCESS_URL=

SANDBOX_POLAR_ACCESS_TOKEN=

SANDBOX_POLAR_WEBHOOK_SECRET=

SANDBOX_POLAR_SUCCESS_URL=

POLAR_MODE=
```

You can find your POLAR_ACCESS_TOKEN and POLAR_WEBHOOK_SECRET variables in your Polar dashboard settings. see `.env.example`

## 4. Start Development Server

```bash
npm run dev
```

Visit `http://localhost:4321` to see the demo interface.

## Configuration

### Polar Dashboard Setup

1. **Create Products**: Set up products in your Polar dashboard
2. **Configure Webhooks**: Add webhook endpoint `https://your-domain.com/api/webhooks/polar`
3. **Get Credentials**: Copy your access token and webhook secret

## Deployment

### Vercel (Recommended)

1. Add the vercel adapter to your projects, [link here](https://docs.astro.build/en/guides/integrations-guide/vercel/#installation)
1. Connect your repository to Vercel
1. Add environment variables in Vercel dashboard
1. Deploy automatically on push

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/polarsource/examples/tree/main/with-astro&env=POLAR_ACCESS_TOKEN,POLAR_WEBHOOK_SECRET,POLAR_SUCCESS_URL,SANDBOX_POLAR_ACCESS_TOKEN,SANDBOX_POLAR_WEBHOOK_SECRET,SANDBOX_POLAR_SUCCESS_URL,POLAR_MODE&envDescription=Configure%20your%20Polar%20API%20credentials%20and%20mode.&envLink=https://docs.polar.sh/integrate/webhooks/endpoints#setup-webhooks)

### Other Platforms

The project works with any platform that supports Astro:

> Ensure to add the respective adapter for those supported to the project, before deploying to that platform. [Astro deployment guide](https://docs.astro.build/en/guides/deploy/)

- Cloudflare
- Netlify
- Node etc.

## 5. Testing

### Local Testing

1. Use Polar's sandbox environment
2. Test with sandbox product IDs
3. Monitor webhook events payloads in your console and
4. Verify your access tokens by running the `validateAccessToken.ts` test in `./scripts`

```bash
npm run validate-token
```

### Webhook Testing

1. Use tools like ngrok for local webhook testing
2. Configure webhook URL in Polar dashboard
3. Configure `vite.server.allowedhosts` in `astro.config.mjs` to allow it.
4. Trigger test events from Polar dashboard
51 changes: 51 additions & 0 deletions with-astro/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { defineConfig, envField } from 'astro/config'
import tailwindcss from '@tailwindcss/vite'
import vercel from '@astrojs/vercel'

export default defineConfig({
output: 'server',
env: {
schema: {
POLAR_ACCESS_TOKEN: envField.string({
context: 'server',
access: 'secret',
}),
POLAR_WEBHOOK_SECRET: envField.string({
context: 'server',
access: 'secret',
}),
POLAR_SUCCESS_URL: envField.string({
context: 'server',
access: 'public',
}),
SANDBOX_POLAR_ACCESS_TOKEN: envField.string({
context: 'server',
access: 'secret',
}),
SANDBOX_POLAR_WEBHOOK_SECRET: envField.string({
context: 'server',
access: 'secret',
}),
SANDBOX_POLAR_SUCCESS_URL: envField.string({
context: 'server',
access: 'public',
}),
POLAR_MODE: envField.string({
context: 'server',
access: 'secret',
}),
},
},

vite: {
plugins: [tailwindcss()],
server: {
allowedHosts: [
'.ngrok-free.app', // e.g when testing on local ensure to set your forwarding URL
],
},
},

integrations: [],
adapter: vercel(),
})
Loading