mirror of
https://github.com/LukeHagar/polar.git
synced 2025-12-06 04:20:58 +00:00
accept polar config in code
This commit is contained in:
@@ -97,6 +97,10 @@ export const polar = new Polar(components.polar, {
|
||||
email: user.email,
|
||||
};
|
||||
},
|
||||
// Optional: Configure Polar settings directly in code (alternatively use environment variables)
|
||||
// organizationToken: "your_organization_token", // Optional: Falls back to POLAR_ORGANIZATION_TOKEN env var
|
||||
// webhookSecret: "your_webhook_secret", // Optional: Falls back to POLAR_WEBHOOK_SECRET env var
|
||||
// server: "sandbox", // Optional: "sandbox" or "production", falls back to POLAR_SERVER env var
|
||||
});
|
||||
|
||||
// Export the API functions
|
||||
@@ -273,6 +277,9 @@ The example app demonstrates:
|
||||
The `Polar` class accepts a configuration object with:
|
||||
- `products`: Map of product keys to Polar product IDs
|
||||
- `getUserInfo`: Function to get the current user's ID and email
|
||||
- `organizationToken`: (Optional) Your Polar organization token. Falls back to `POLAR_ORGANIZATION_TOKEN` env var
|
||||
- `webhookSecret`: (Optional) Your Polar webhook secret. Falls back to `POLAR_WEBHOOK_SECRET` env var
|
||||
- `server`: (Optional) Polar server environment: "sandbox" or "production". Falls back to `POLAR_SERVER` env var
|
||||
|
||||
### React Components
|
||||
|
||||
@@ -324,4 +331,4 @@ polar.registerRoutes(http, {
|
||||
});
|
||||
```
|
||||
|
||||
The webhook handler requires the `POLAR_WEBHOOK_SECRET` environment variable to be set.
|
||||
The webhook handler uses the `webhookSecret` from the Polar client configuration or the `POLAR_WEBHOOK_SECRET` environment variable.
|
||||
@@ -21,6 +21,11 @@ export const polar = new Polar(components.polar, {
|
||||
email: user.email,
|
||||
};
|
||||
},
|
||||
// These can be configured in code or via environment variables
|
||||
// Uncomment and replace with actual values to configure in code:
|
||||
// organizationToken: "your_organization_token", // Or use POLAR_ORGANIZATION_TOKEN env var
|
||||
// webhookSecret: "your_webhook_secret", // Or use POLAR_WEBHOOK_SECRET env var
|
||||
// server: "sandbox", // "sandbox" or "production", falls back to POLAR_SERVER env var
|
||||
});
|
||||
|
||||
export const MAX_FREE_TODOS = 3;
|
||||
|
||||
@@ -53,6 +53,10 @@ export class Polar<
|
||||
> {
|
||||
public sdk: PolarSdk;
|
||||
public products: Products;
|
||||
private organizationToken: string;
|
||||
private webhookSecret: string;
|
||||
private server: "sandbox" | "production";
|
||||
|
||||
constructor(
|
||||
public component: ComponentApi,
|
||||
private config: {
|
||||
@@ -61,13 +65,24 @@ export class Polar<
|
||||
userId: string;
|
||||
email: string;
|
||||
}>;
|
||||
organizationToken?: string;
|
||||
webhookSecret?: string;
|
||||
server?: "sandbox" | "production";
|
||||
}
|
||||
) {
|
||||
this.products = config.products ?? ({} as Products);
|
||||
this.organizationToken =
|
||||
config.organizationToken ?? process.env["POLAR_ORGANIZATION_TOKEN"] ?? "";
|
||||
this.webhookSecret =
|
||||
config.webhookSecret ?? process.env["POLAR_WEBHOOK_SECRET"] ?? "";
|
||||
this.server =
|
||||
config.server ??
|
||||
(process.env["POLAR_SERVER"] as "sandbox" | "production") ??
|
||||
"sandbox";
|
||||
|
||||
this.sdk = new PolarSdk({
|
||||
accessToken: process.env["POLAR_ORGANIZATION_TOKEN"] ?? "",
|
||||
server:
|
||||
(process.env["POLAR_SERVER"] as "sandbox" | "production") ?? "sandbox",
|
||||
accessToken: this.organizationToken,
|
||||
server: this.server,
|
||||
});
|
||||
}
|
||||
getCustomerByUserId(ctx: RunQueryCtx, userId: string) {
|
||||
@@ -322,11 +337,7 @@ export class Polar<
|
||||
const body = await request.text();
|
||||
const headers = Object.fromEntries(request.headers.entries());
|
||||
try {
|
||||
const event = validateEvent(
|
||||
body,
|
||||
headers,
|
||||
process.env["POLAR_WEBHOOK_SECRET"] ?? ""
|
||||
);
|
||||
const event = validateEvent(body, headers, this.webhookSecret);
|
||||
switch (event.type) {
|
||||
case "subscription.created": {
|
||||
await ctx.runMutation(this.component.lib.createSubscription, {
|
||||
|
||||
Reference in New Issue
Block a user