mirror of
https://github.com/LukeHagar/polar.git
synced 2025-12-09 12:47:46 +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,
|
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
|
// Export the API functions
|
||||||
@@ -273,6 +277,9 @@ The example app demonstrates:
|
|||||||
The `Polar` class accepts a configuration object with:
|
The `Polar` class accepts a configuration object with:
|
||||||
- `products`: Map of product keys to Polar product IDs
|
- `products`: Map of product keys to Polar product IDs
|
||||||
- `getUserInfo`: Function to get the current user's ID and email
|
- `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
|
### 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,
|
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;
|
export const MAX_FREE_TODOS = 3;
|
||||||
|
|||||||
@@ -53,6 +53,10 @@ export class Polar<
|
|||||||
> {
|
> {
|
||||||
public sdk: PolarSdk;
|
public sdk: PolarSdk;
|
||||||
public products: Products;
|
public products: Products;
|
||||||
|
private organizationToken: string;
|
||||||
|
private webhookSecret: string;
|
||||||
|
private server: "sandbox" | "production";
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public component: ComponentApi,
|
public component: ComponentApi,
|
||||||
private config: {
|
private config: {
|
||||||
@@ -61,13 +65,24 @@ export class Polar<
|
|||||||
userId: string;
|
userId: string;
|
||||||
email: string;
|
email: string;
|
||||||
}>;
|
}>;
|
||||||
|
organizationToken?: string;
|
||||||
|
webhookSecret?: string;
|
||||||
|
server?: "sandbox" | "production";
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
this.products = config.products ?? ({} as Products);
|
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({
|
this.sdk = new PolarSdk({
|
||||||
accessToken: process.env["POLAR_ORGANIZATION_TOKEN"] ?? "",
|
accessToken: this.organizationToken,
|
||||||
server:
|
server: this.server,
|
||||||
(process.env["POLAR_SERVER"] as "sandbox" | "production") ?? "sandbox",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getCustomerByUserId(ctx: RunQueryCtx, userId: string) {
|
getCustomerByUserId(ctx: RunQueryCtx, userId: string) {
|
||||||
@@ -322,11 +337,7 @@ export class Polar<
|
|||||||
const body = await request.text();
|
const body = await request.text();
|
||||||
const headers = Object.fromEntries(request.headers.entries());
|
const headers = Object.fromEntries(request.headers.entries());
|
||||||
try {
|
try {
|
||||||
const event = validateEvent(
|
const event = validateEvent(body, headers, this.webhookSecret);
|
||||||
body,
|
|
||||||
headers,
|
|
||||||
process.env["POLAR_WEBHOOK_SECRET"] ?? ""
|
|
||||||
);
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "subscription.created": {
|
case "subscription.created": {
|
||||||
await ctx.runMutation(this.component.lib.createSubscription, {
|
await ctx.runMutation(this.component.lib.createSubscription, {
|
||||||
|
|||||||
Reference in New Issue
Block a user