feat: add new api methods for chargebee
This commit is contained in:
parent
5522b2f1f7
commit
0cf8f2898d
@ -11,6 +11,9 @@ import {
|
||||
DailyForecasts,
|
||||
SubscriptionItems,
|
||||
SubscriptionCheckout,
|
||||
SubscriptionStatus,
|
||||
SubscriptionReceipts,
|
||||
PaymentIntents,
|
||||
} from './resources'
|
||||
|
||||
export interface ApiContextValue {
|
||||
@ -25,6 +28,10 @@ export interface ApiContextValue {
|
||||
getAuras: ReturnType<typeof createMethod<Auras.Payload, Auras.Response>>
|
||||
getSubscriptionItems: ReturnType<typeof createMethod<SubscriptionItems.Payload, SubscriptionItems.Response>>
|
||||
getSubscriptionCheckout: ReturnType<typeof createMethod<SubscriptionCheckout.Payload, SubscriptionCheckout.Response>>
|
||||
getSubscriptionStatus: ReturnType<typeof createMethod<SubscriptionStatus.Payload, SubscriptionStatus.Response>>,
|
||||
getSubscriptionReceipt: ReturnType<typeof createMethod<SubscriptionReceipts.GetPayload, SubscriptionReceipts.Response>>,
|
||||
createSubscriptionReceipt: ReturnType<typeof createMethod<SubscriptionReceipts.Payload, SubscriptionReceipts.Response>>,
|
||||
createPaymentIntent: ReturnType<typeof createMethod<PaymentIntents.Payload, PaymentIntents.Response>>
|
||||
}
|
||||
|
||||
export const ApiContext = createContext<ApiContextValue>({} as ApiContextValue)
|
||||
|
||||
@ -11,6 +11,9 @@ import {
|
||||
DailyForecasts,
|
||||
SubscriptionItems,
|
||||
SubscriptionCheckout,
|
||||
SubscriptionReceipts,
|
||||
SubscriptionStatus,
|
||||
PaymentIntents
|
||||
} from './resources'
|
||||
|
||||
const api = {
|
||||
@ -25,6 +28,10 @@ const api = {
|
||||
getAuras: createMethod<Auras.Payload, Auras.Response>(Auras.createRequest),
|
||||
getSubscriptionItems: createMethod<SubscriptionItems.Payload, SubscriptionItems.Response>(SubscriptionItems.createRequest),
|
||||
getSubscriptionCheckout: createMethod<SubscriptionCheckout.Payload, SubscriptionCheckout.Response>(SubscriptionCheckout.createRequest),
|
||||
getSubscriptionStatus: createMethod<SubscriptionStatus.Payload, SubscriptionStatus.Response>(SubscriptionStatus.createRequest),
|
||||
getSubscriptionReceipt: createMethod<SubscriptionReceipts.GetPayload, SubscriptionReceipts.Response>(SubscriptionReceipts.createGetRequest),
|
||||
createSubscriptionReceipt: createMethod<SubscriptionReceipts.Payload, SubscriptionReceipts.Response>(SubscriptionReceipts.createRequest),
|
||||
createPaymentIntent: createMethod<PaymentIntents.Payload, PaymentIntents.Response>(PaymentIntents.createRequest)
|
||||
}
|
||||
|
||||
export function createApi(): ApiContextValue {
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
export interface Payload extends AuthPayload {
|
||||
locale: string
|
||||
token: AuthToken
|
||||
}
|
||||
|
||||
export interface Response {
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { AssetCategory } from "./AssetCategories"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
token: AuthToken
|
||||
export interface Payload extends AuthPayload {
|
||||
category: string
|
||||
page?: number
|
||||
perPage?: number
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
token: AuthToken
|
||||
}
|
||||
export type Payload = AuthPayload
|
||||
|
||||
export interface Response {
|
||||
user_aura: UserAura
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface GetPayload {
|
||||
token: AuthToken
|
||||
}
|
||||
export type GetPayload = AuthPayload
|
||||
|
||||
export interface PatchPayload extends GetPayload {
|
||||
export interface PatchPayload extends AuthPayload {
|
||||
user: Partial<UserPatch>
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
token: AuthToken
|
||||
}
|
||||
export type Payload = AuthPayload
|
||||
|
||||
export interface Response {
|
||||
user_daily_forecast: UserDailyForecast
|
||||
|
||||
59
src/api/resources/UserPaymentIntents.ts
Normal file
59
src/api/resources/UserPaymentIntents.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload extends AuthPayload {
|
||||
paymentMethod: PaymentMethod
|
||||
currencyCode: CurrencyCode
|
||||
}
|
||||
|
||||
export interface Response {
|
||||
payment_intent: PaymentIntent
|
||||
customer: Customer
|
||||
}
|
||||
|
||||
export type PaymentMethod = 'apple_pay' | 'google_pay' | 'card'
|
||||
export type CurrencyCode = 'USD'
|
||||
export interface PaymentIntent {
|
||||
id: string
|
||||
status: 'inited' | 'in_progress' | 'authorized' | 'consumed' | 'expired'
|
||||
amount: number
|
||||
gateway_account_id: string
|
||||
payment_method_type: PaymentMethod
|
||||
expires_at: number
|
||||
created_at: number
|
||||
modified_at: number
|
||||
currency_code: CurrencyCode
|
||||
gateway: string
|
||||
}
|
||||
export interface Customer {
|
||||
id: string
|
||||
email: string
|
||||
auto_collection: 'on' | 'off'
|
||||
net_term_days: number
|
||||
allow_direct_debit: boolean
|
||||
taxability: 'taxable' | 'exempt'
|
||||
created_at: number
|
||||
updated_at: number
|
||||
pii_cleared: 'active' | 'scheduled_for_clear' | 'cleared'
|
||||
channel: 'web' | 'app_store' | 'play_store'
|
||||
resource_version: number
|
||||
deleted: boolean
|
||||
card_status: 'no_card' | 'valid' | 'expiring' | 'expired'
|
||||
promotional_credits: number
|
||||
refundable_credits: number
|
||||
excess_payments: number
|
||||
unbilled_charges: number
|
||||
preferred_currency_code: CurrencyCode
|
||||
}
|
||||
|
||||
export const createRequest = ({ token, paymentMethod, currencyCode }: Payload): Request => {
|
||||
const url = new URL(routes.server.paymentIntents())
|
||||
const body = JSON.stringify({
|
||||
payment_intent: {
|
||||
payment_method_type: paymentMethod,
|
||||
currency_code: currencyCode,
|
||||
}
|
||||
})
|
||||
return new Request(url, { method: 'POST', headers: getAuthHeaders(token), body })
|
||||
}
|
||||
@ -1,9 +1,8 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
token: AuthToken
|
||||
export interface Payload extends AuthPayload {
|
||||
embed?: boolean
|
||||
locale: string
|
||||
itemPriceId: string
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthToken } from "../types"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface Payload {
|
||||
export interface Payload extends AuthPayload {
|
||||
locale: string
|
||||
token: AuthToken
|
||||
}
|
||||
|
||||
export interface Response {
|
||||
|
||||
83
src/api/resources/UserSubscriptionReceipts.ts
Normal file
83
src/api/resources/UserSubscriptionReceipts.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export interface GetPayload extends AuthPayload {
|
||||
id: string
|
||||
}
|
||||
|
||||
export interface ChargebeeReceiptPayload extends AuthPayload {
|
||||
itemPriceId: string
|
||||
gwToken: string
|
||||
referenceId: string
|
||||
}
|
||||
|
||||
export interface AppleReceiptPayload extends AuthPayload {
|
||||
receiptData: string
|
||||
autorenewable: boolean
|
||||
sandbox: boolean
|
||||
}
|
||||
|
||||
export type Payload = ChargebeeReceiptPayload | AppleReceiptPayload
|
||||
|
||||
export interface Response {
|
||||
subscription_receipt: {
|
||||
id: string
|
||||
user_id: number
|
||||
status: number
|
||||
expires_at: null | string
|
||||
requested_at: string
|
||||
created_at: string
|
||||
data: {
|
||||
input: {
|
||||
subscription_items: [
|
||||
{
|
||||
item_price_id: string
|
||||
}
|
||||
],
|
||||
payment_intent: {
|
||||
gw_token: string
|
||||
gateway_account_id: string
|
||||
}
|
||||
},
|
||||
app_bundle_id: string
|
||||
autorenewable: boolean
|
||||
error: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createRequest({ token, itemPriceId, gwToken, referenceId }: ChargebeeReceiptPayload): Request
|
||||
function createRequest({ token, receiptData, autorenewable, sandbox }: AppleReceiptPayload): Request
|
||||
function createRequest(payload: Payload): Request
|
||||
function createRequest(payload: Payload): Request {
|
||||
const url = new URL(routes.server.subscriptionReceipts())
|
||||
const data = isChargebeeReceipt(payload) ? {
|
||||
way: 'chargebee',
|
||||
subscription_receipt: {
|
||||
item_price_id: payload.itemPriceId,
|
||||
gw_token: payload.gwToken,
|
||||
reference_id: payload.referenceId,
|
||||
}
|
||||
} : {
|
||||
way: 'apple',
|
||||
subscription_receipt: {
|
||||
receipt_data: payload.receiptData,
|
||||
autorenewable: payload.autorenewable,
|
||||
sandbox: payload.sandbox,
|
||||
}
|
||||
}
|
||||
const body = JSON.stringify(data)
|
||||
return new Request(url, { method: 'POST', headers: getAuthHeaders(payload.token), body })
|
||||
}
|
||||
|
||||
function isChargebeeReceipt(payload: Payload ): payload is ChargebeeReceiptPayload {
|
||||
return 'itemPriceId' in payload && 'gwToken' in payload && 'referenceId' in payload
|
||||
}
|
||||
|
||||
function createGetRequest({ id, token }: GetPayload): Request {
|
||||
const url = new URL(routes.server.subscriptionReceipt(id))
|
||||
return new Request(url, { method: 'GET', headers: getAuthHeaders(token) })
|
||||
}
|
||||
|
||||
export { createRequest, createGetRequest }
|
||||
16
src/api/resources/UserSubscriptionStatus.ts
Normal file
16
src/api/resources/UserSubscriptionStatus.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import routes from "../../routes"
|
||||
import { AuthPayload } from "../types"
|
||||
import { getAuthHeaders } from "../utils"
|
||||
|
||||
export type Payload = AuthPayload
|
||||
|
||||
export interface Response {
|
||||
user: {
|
||||
has_subscription: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export const createRequest = ({ token }: Payload): Request => {
|
||||
const url = new URL(routes.server.subscriptionStatus())
|
||||
return new Request(url, { method: 'GET', headers: getAuthHeaders(token) })
|
||||
}
|
||||
@ -8,3 +8,6 @@ export * as Elements from './Elements'
|
||||
export * as AuthTokens from './AuthTokens'
|
||||
export * as SubscriptionItems from './UserSubscriptionItemPrices'
|
||||
export * as SubscriptionCheckout from './UserSubscriptionCheckout'
|
||||
export * as SubscriptionStatus from './UserSubscriptionStatus'
|
||||
export * as SubscriptionReceipts from './UserSubscriptionReceipts'
|
||||
export * as PaymentIntents from './UserPaymentIntents'
|
||||
|
||||
@ -16,3 +16,7 @@ export interface ErrorResponse {
|
||||
}
|
||||
|
||||
export type AuthToken = string
|
||||
|
||||
export interface AuthPayload {
|
||||
token: AuthToken
|
||||
}
|
||||
|
||||
@ -24,8 +24,12 @@ const routes = {
|
||||
assetCategories: () => [apiHost, prefix, 'assets', 'categories.json'].join('/'),
|
||||
dailyForecasts: () => [apiHost, prefix, 'user', 'daily_forecast.json'].join('/'),
|
||||
auras: () => [apiHost, prefix, 'user', 'aura.json'].join('/'),
|
||||
paymentIntents: () => [apiHost, prefix, 'user', 'payment_intents.json'].join('/'),
|
||||
subscriptionItems: () => [apiHost, prefix, 'user', 'subscription', 'item_prices.json'].join('/'),
|
||||
subscriptionCheckout: () => [apiHost, prefix, 'user', 'subscription', 'checkout', 'new.json'].join('/'),
|
||||
subscriptionStatus: () => [apiHost, prefix, 'user', 'subscription_receipts', 'status.json'].join('/'),
|
||||
subscriptionReceipts: () => [apiHost, prefix, 'user', 'subscription_receipts.json'].join('/'),
|
||||
subscriptionReceipt: (id: string) => [apiHost, prefix, 'user', 'subscription_receipts', `${id}.json`].join('/'),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user