@portone/server-sdk
Version:
PortOne JavaScript SDK for server-side usage
153 lines (152 loc) • 4.3 kB
JavaScript
import { BillingKeyError } from "./BillingKeyError.mjs";
import { USER_AGENT } from "../../../client.mjs";
export function BillingKeyClient(init) {
const baseUrl = init.baseUrl ?? "https://api.portone.io";
const secret = init.secret;
return {
getBillingKeyInfos: async (options) => {
const page = options?.page;
const sort = options?.sort;
const filter = options?.filter;
const requestBody = JSON.stringify({
page,
sort,
filter
});
const query = [
["requestBody", requestBody]
].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(
new URL(`/billing-keys?${query}`, baseUrl),
{
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new GetBillingKeyInfosError(await response.json());
}
return response.json();
},
issueBillingKey: async (options) => {
const {
storeId,
method,
channelKey,
channelGroupId,
customer,
customData,
bypass,
noticeUrls
} = options;
const requestBody = JSON.stringify({
storeId: storeId ?? init.storeId,
method,
channelKey,
channelGroupId,
customer,
customData,
bypass,
noticeUrls
});
const response = await fetch(
new URL("/billing-keys", baseUrl),
{
method: "POST",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
},
body: requestBody
}
);
if (!response.ok) {
throw new IssueBillingKeyError(await response.json());
}
return response.json();
},
getBillingKeyInfo: async (options) => {
const {
billingKey,
storeId
} = options;
const query = [
["storeId", storeId]
].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(
new URL(`/billing-keys/${encodeURIComponent(billingKey)}?${query}`, baseUrl),
{
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new GetBillingKeyInfoError(await response.json());
}
return response.json();
},
deleteBillingKey: async (options) => {
const {
billingKey,
storeId,
reason
} = options;
const query = [
["storeId", storeId],
["reason", reason]
].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(
new URL(`/billing-keys/${encodeURIComponent(billingKey)}?${query}`, baseUrl),
{
method: "DELETE",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new DeleteBillingKeyError(await response.json());
}
return response.json();
}
};
}
export class GetBillingKeyInfosError extends BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetBillingKeyInfosError.prototype);
this.name = "GetBillingKeyInfosError";
}
}
export class IssueBillingKeyError extends BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, IssueBillingKeyError.prototype);
this.name = "IssueBillingKeyError";
}
}
export class GetBillingKeyInfoError extends BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetBillingKeyInfoError.prototype);
this.name = "GetBillingKeyInfoError";
}
}
export class DeleteBillingKeyError extends BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, DeleteBillingKeyError.prototype);
this.name = "DeleteBillingKeyError";
}
}