Switch to /api/v1/subscriptions/options method

This commit is contained in:
silverpill 2022-09-08 10:06:24 +00:00
parent 86945aa057
commit e6b0d51dfe
3 changed files with 30 additions and 23 deletions

View file

@ -1,10 +1,11 @@
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { http } from "./common" import { http } from "./common"
import { User } from "./users"
export interface SubscriptionOption { export interface SubscriptionOption {
type: string; type: string;
price: number | null; price: number | null;
payout_address: number | null; payout_address: string | null;
} }
export async function getSubscriptionOptions( export async function getSubscriptionOptions(
@ -23,6 +24,24 @@ export async function getSubscriptionOptions(
} }
} }
export async function registerSubscriptionOption(
authToken: string,
subscriptionOption: SubscriptionOption,
): Promise<User> {
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 { export interface SubscriptionDetails {
id: number, id: number,
expires_at: string, expires_at: string,

View file

@ -7,6 +7,7 @@ import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum"
import { floatToBigNumber, roundBigNumber } from "@/utils/numbers" import { floatToBigNumber, roundBigNumber } from "@/utils/numbers"
import { http } from "./common" import { http } from "./common"
import { Contracts, getContract } from "./contracts" import { Contracts, getContract } from "./contracts"
import { registerSubscriptionOption } from "./subscriptions-common"
import { Profile, User } from "./users" import { Profile, User } from "./users"
const SECONDS_IN_DAY = 3600 * 24 const SECONDS_IN_DAY = 3600 * 24
@ -92,18 +93,11 @@ export async function configureSubscriptions(
export async function onSubscriptionsEnabled( export async function onSubscriptionsEnabled(
authToken: string, authToken: string,
): Promise<User> { ): Promise<User> {
const url = `${BACKEND_URL}/api/v1/subscriptions/enable` return await registerSubscriptionOption(authToken, {
const response = await http(url, { type: "ethereum",
method: "POST", price: null,
authToken, payout_address: null,
json: { type: "ethereum" },
}) })
const data = await response.json()
if (response.status !== 200) {
throw new Error(data.message)
} else {
return data
}
} }
export class SubscriptionConfig { export class SubscriptionConfig {

View file

@ -2,6 +2,7 @@ import { BigNumber } from "@ethersproject/bignumber"
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { http } from "./common" import { http } from "./common"
import { registerSubscriptionOption } from "./subscriptions-common"
import { import {
formatAmount, formatAmount,
getPricePerMonth as _getPricePerMonth, getPricePerMonth as _getPricePerMonth,
@ -24,18 +25,11 @@ export async function enableMoneroSubscriptions(
price: number, price: number,
payoutAddress: string, payoutAddress: string,
): Promise<User> { ): Promise<User> {
const url = `${BACKEND_URL}/api/v1/subscriptions/enable` return await registerSubscriptionOption(authToken, {
const response = await http(url, { type: "monero",
method: "POST", price,
authToken, payout_address: payoutAddress,
json: { 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 { export interface Invoice {