Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: add new subscription types

Abdullah Attafbe551eb29e7
diff --git a/packages/core/__e2e__/user-manager.test.js b/packages/core/__e2e__/user-manager.test.js
index 8ff4d4eeb..22c4d77cd 100644
--- a/packages/core/__e2e__/user-manager.test.js
+++ b/packages/core/__e2e__/user-manager.test.js
@@ -52,6 +52,7 @@ test(
const userData = await db.user.getUser();
expect(userData.email).toBe(USER.email);
+ expect(userData.subscription).toBeDefined();
}),
30000
);
diff --git a/packages/core/src/api/subscriptions.ts b/packages/core/src/api/subscriptions.ts
index 601f7afa7..25f794328 100644
--- a/packages/core/src/api/subscriptions.ts
+++ b/packages/core/src/api/subscriptions.ts
@@ -23,36 +23,37 @@ import TokenManager from "./token-manager.js";
export type TransactionStatus =
| "completed"
- | "refunded"
- | "partially_refunded"
- | "disputed";
-
-export type Transaction = {
- order_id: string;
- checkout_id: string;
- amount: string;
- currency: string;
+ | "billed"
+ | "paid"
+ | "past_due"
+ | "canceled";
+
+export interface Transaction {
+ id: string;
status: TransactionStatus;
- created_at: string;
- passthrough: null;
- product_id: number;
- is_subscription: boolean;
- is_one_off: boolean;
- subscription: Subscription;
- user: User;
- receipt_url: string;
-};
-
-type Subscription = {
- subscription_id: number;
- status: string;
-};
-
-type User = {
- user_id: number;
- email: string;
- marketing_consent: boolean;
-};
+ created_at: Date;
+ billed_at: Date;
+ details: Details;
+}
+
+export interface Details {
+ totals: Totals;
+ line_items: LineItem[];
+}
+
+export interface LineItem {
+ id: string;
+}
+
+export interface Totals {
+ subtotal: number;
+ tax: number;
+ discount: number;
+ total: number;
+ grand_total: number;
+ balance: number;
+ currency_code: string;
+}
export default class Subscriptions {
constructor(private readonly tokenManager: TokenManager) {}
@@ -60,14 +61,38 @@ export default class Subscriptions {
async cancel() {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
- await http.delete(`${hosts.SUBSCRIPTIONS_HOST}/subscriptions`, token);
+ await http.post(
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/cancel`,
+ null,
+ token
+ );
+ }
+
+ async pause() {
+ const token = await this.tokenManager.getAccessToken();
+ if (!token) return;
+ await http.post(
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/pause`,
+ null,
+ token
+ );
+ }
+
+ async resume() {
+ const token = await this.tokenManager.getAccessToken();
+ if (!token) return;
+ await http.post(
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/resume`,
+ null,
+ token
+ );
}
async refund() {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
await http.post(
- `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/refund`,
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/refund`,
null,
token
);
@@ -77,16 +102,28 @@ export default class Subscriptions {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
return await http.get(
- `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/transactions`,
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/transactions`,
+ token
+ );
+ }
+
+ async invoice(transactionId: string): Promise<string | undefined> {
+ const token = await this.tokenManager.getAccessToken();
+ if (!token) return;
+ const response = await http.get(
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/invoice?transactionId=${transactionId}`,
token
);
+ return response.url;
}
- async updateUrl(): Promise<string | undefined> {
+ async urls(): Promise<
+ { update_payment_method: string; cancel: string } | undefined
+ > {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
return await http.get(
- `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/update`,
+ `${hosts.SUBSCRIPTIONS_HOST}/subscriptions/v2/urls`,
token
);
}
diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts
index c992528bb..0c388f13d 100644
--- a/packages/core/src/types.ts
+++ b/packages/core/src/types.ts
@@ -550,6 +550,39 @@ export type TrashItem = BaseTrashItem<Note> | BaseTrashItem<Notebook>;
export type AuthenticatorType = "app" | "sms" | "email";
+export enum SubscriptionPlan {
+ FREE = 0,
+ ESSENTIAL = 1,
+ PRO = 2,
+ BELIEVER = 3,
+ EDUCATION = 4
+}
+
+export enum SubscriptionStatus {
+ ACTIVE,
+ TRIAL,
+ CANCELED,
+ PAUSED,
+ EXPIRED
+}
+
+export enum SubscriptionProvider {
+ STREETWRITERS = 0,
+ APPLE = 1,
+ GOOGLE = 2,
+ PADDLE = 3
+}
+
+export enum SubscriptionType {
+ BASIC = 0,
+ TRIAL = 1,
+ BETA = 2,
+ PREMIUM = 5,
+ PREMIUM_EXPIRED = 6,
+ PREMIUM_CANCELED = 7,
+ PREMIUM_PAUSED = 8
+}
+
export type User = {
id: string;
email: string;
@@ -570,9 +603,11 @@ export type User = {
cancelURL: string | null;
expiry: number;
productId: string;
- provider: 0 | 1 | 2 | 3 | 4;
+ provider: SubscriptionProvider;
start: number;
- type: 0 | 1 | 2 | 5 | 6 | 7;
+ type: SubscriptionType;
+ plan: SubscriptionPlan;
+ status: SubscriptionStatus;
updateURL: string | null;
};
};
diff --git a/packages/core/src/utils/constants.ts b/packages/core/src/utils/constants.ts
index 865000eb2..34e053f45 100644
--- a/packages/core/src/utils/constants.ts
+++ b/packages/core/src/utils/constants.ts
@@ -26,6 +26,7 @@ export function isServerCompatible(version: number) {
}
function isProduction() {
+ return false;
return (
process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test"
);