@portone/server-sdk
Version:
PortOne JavaScript SDK for server-side usage
144 lines (143 loc) • 4.37 kB
JavaScript
import { PaymentScheduleError } from "./PaymentScheduleError.mjs";
import { USER_AGENT } from "../../../client.mjs";
export function PaymentScheduleClient(init) {
const baseUrl = init.baseUrl ?? "https://api.portone.io";
const secret = init.secret;
return {
getPaymentSchedule: async (options) => {
const {
paymentScheduleId,
storeId
} = options;
const query = [
["storeId", storeId]
].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(
new URL(`/payment-schedules/${encodeURIComponent(paymentScheduleId)}?${query}`, baseUrl),
{
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new GetPaymentScheduleError(await response.json());
}
return response.json();
},
getPaymentSchedules: 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(`/payment-schedules?${query}`, baseUrl),
{
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new GetPaymentSchedulesError(await response.json());
}
return response.json();
},
revokePaymentSchedules: async (options) => {
const storeId = options?.storeId;
const billingKey = options?.billingKey;
const scheduleIds = options?.scheduleIds;
const requestBody = JSON.stringify({
storeId: storeId ?? init.storeId,
billingKey,
scheduleIds
});
const query = [
["requestBody", requestBody]
].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(
new URL(`/payment-schedules?${query}`, baseUrl),
{
method: "DELETE",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
}
}
);
if (!response.ok) {
throw new RevokePaymentSchedulesError(await response.json());
}
return response.json();
},
createPaymentSchedule: async (options) => {
const {
paymentId,
payment,
timeToPay
} = options;
const requestBody = JSON.stringify({
payment,
timeToPay
});
const response = await fetch(
new URL(`/payments/${encodeURIComponent(paymentId)}/schedule`, baseUrl),
{
method: "POST",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": USER_AGENT
},
body: requestBody
}
);
if (!response.ok) {
throw new CreatePaymentScheduleError(await response.json());
}
return response.json();
}
};
}
export class GetPaymentScheduleError extends PaymentScheduleError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetPaymentScheduleError.prototype);
this.name = "GetPaymentScheduleError";
}
}
export class GetPaymentSchedulesError extends PaymentScheduleError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetPaymentSchedulesError.prototype);
this.name = "GetPaymentSchedulesError";
}
}
export class RevokePaymentSchedulesError extends PaymentScheduleError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, RevokePaymentSchedulesError.prototype);
this.name = "RevokePaymentSchedulesError";
}
}
export class CreatePaymentScheduleError extends PaymentScheduleError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, CreatePaymentScheduleError.prototype);
this.name = "CreatePaymentScheduleError";
}
}