diff --git a/src/api/subscriptions-common.ts b/src/api/subscriptions-common.ts index bc7eb50..2b415a5 100644 --- a/src/api/subscriptions-common.ts +++ b/src/api/subscriptions-common.ts @@ -1,10 +1,11 @@ import { BACKEND_URL } from "@/constants" import { http } from "./common" +import { User } from "./users" export interface SubscriptionOption { type: string; price: number | null; - payout_address: number | null; + payout_address: string | null; } export async function getSubscriptionOptions( @@ -23,6 +24,24 @@ export async function getSubscriptionOptions( } } +export async function registerSubscriptionOption( + authToken: string, + subscriptionOption: SubscriptionOption, +): Promise { + const url = `${BACKEND_URL}/api/v1/subscriptions/options` + const response = await http(url, { + method: "POST", + authToken, + json: subscriptionOption, + }) + const data = await response.json() + if (response.status !== 200) { + throw new Error(data.message) + } else { + return data + } +} + export interface SubscriptionDetails { id: number, expires_at: string, diff --git a/src/api/subscriptions-ethereum.ts b/src/api/subscriptions-ethereum.ts index 7010af4..b8fec51 100644 --- a/src/api/subscriptions-ethereum.ts +++ b/src/api/subscriptions-ethereum.ts @@ -7,6 +7,7 @@ import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum" import { floatToBigNumber, roundBigNumber } from "@/utils/numbers" import { http } from "./common" import { Contracts, getContract } from "./contracts" +import { registerSubscriptionOption } from "./subscriptions-common" import { Profile, User } from "./users" const SECONDS_IN_DAY = 3600 * 24 @@ -92,18 +93,11 @@ export async function configureSubscriptions( export async function onSubscriptionsEnabled( authToken: string, ): Promise { - const url = `${BACKEND_URL}/api/v1/subscriptions/enable` - const response = await http(url, { - method: "POST", - authToken, - json: { type: "ethereum" }, + return await registerSubscriptionOption(authToken, { + type: "ethereum", + price: null, + payout_address: null, }) - const data = await response.json() - if (response.status !== 200) { - throw new Error(data.message) - } else { - return data - } } export class SubscriptionConfig { diff --git a/src/api/subscriptions-monero.ts b/src/api/subscriptions-monero.ts index 10438ea..1c8e5c3 100644 --- a/src/api/subscriptions-monero.ts +++ b/src/api/subscriptions-monero.ts @@ -2,6 +2,7 @@ import { BigNumber } from "@ethersproject/bignumber" import { BACKEND_URL } from "@/constants" import { http } from "./common" +import { registerSubscriptionOption } from "./subscriptions-common" import { formatAmount, getPricePerMonth as _getPricePerMonth, @@ -24,18 +25,11 @@ export async function enableMoneroSubscriptions( price: number, payoutAddress: string, ): Promise { - const url = `${BACKEND_URL}/api/v1/subscriptions/enable` - const response = await http(url, { - method: "POST", - authToken, - json: { type: "monero", price, payout_address: payoutAddress }, + return await registerSubscriptionOption(authToken, { + type: "monero", + price, + payout_address: payoutAddress, }) - const data = await response.json() - if (response.status !== 200) { - throw new Error(data.message) - } else { - return data - } } export interface Invoice {