import * as log from 'loglevel'; import { get, post, patch, postBff } from './request'; import { notNullAndEmpty } from '../utils/helperFunctions'; import { MiniAppErrorType } from '../model/miniAppError'; export interface PaymentQuery { clientId?: string; merchant?: string; agreeSimilarTransaction: boolean; } export interface PaymentIdQuery { clientId?: string; merchant?: string; sourceInfoApp?: string; type?: string; } interface PaymentAmount { amount: number; currency: string; } type ProductType = 'VIRTUAL_BONUS_INVESTMENT' | 'PAY_LATER_REPAYMENT'; interface PaymentBody { merchantPaymentId: string; amount: PaymentAmount; orderReceiptNumber?: string; orderDescription?: string; orderItems?: Array; metaData?: any; storeId?: string; paymentMethodId: string; terminalId: number | string; merchantAlias?: string; requestedAt?: number; productType?: ProductType; sourceInfoApp?: string; } export function getClientID() { const clientId = JSON.parse(localStorage.getItem('clientId') || '""'); return notNullAndEmpty(clientId) ? clientId : ''; } export const makePayment = (queryParams: PaymentQuery, body: PaymentBody) => { let queryStr = ''; queryStr = Object.keys(queryParams).reduce((resStr, key) => { return resStr + `${key}=${queryParams[key as keyof PaymentQuery]}&`; }, queryStr); return post(`/v1/payment?${queryStr}`, body); }; export const getBalance = () => { return get(`/v1/payment/balance`, { clientId: getClientID(), currency: 'JPY', }); }; export const getUserProfile = () => { return get(`/v1/users/profile`, { clientId: getClientID(), }); }; export const getUserPaymentId = (params: any) => { let queryStr = ''; queryStr = Object.keys(params).reduce((resStr, key) => { return `${resStr}${key}=${params[key as keyof PaymentIdQuery]}&`; }, queryStr); return get(`/v1/payment/methods?${queryStr}`, { clientId: getClientID(), type: params.type, }); }; export const getUAID = async () => { const res: any = await get(`v1/users/id`, { clientId: getClientID(), }); try { const userId = res.data.signed.jws; if (userId?.length) { return Promise.resolve({ jws: userId }); } else { log.error(`getUAID / SERVER_ERROR`); return Promise.reject({ errorCode: MiniAppErrorType.serverError }); } } catch (error) { log.error(`getUAID / SERVER_ERROR / ${JSON.stringify(error)}`); return Promise.reject({ errorCode: MiniAppErrorType.serverError }); } }; export const login = (loginData: any) => { return postBff('/signIn', loginData); }; export const loginWithSMS = (loginData: any) => { return postBff('/signInWithSms', loginData); }; export const linkUser = () => { return post(`/v1/users/link?clientId=${getClientID()}`, {}); }; export const getUserGrantedPermission = () => { return get(`/v1/users/permissions/granted`, { clientId: getClientID(), }); }; export const getMiniAppDetail = () => { return get(`/v1/apps/detail`, { clientId: getClientID(), }); }; export const getKycInfo = () => { return get(`/v1/users/kyc`, { clientId: getClientID(), }); }; export const getNotificationStatus = () => { return get(`/v1/apps/user/notification/status`, { clientId: getClientID(), }); }; export const editUserPermission = (patchBody: any) => { return patch(`/v1/users/permissions?clientId=${getClientID()}`, patchBody); }; export const packageProject = async (params?: any) => { return await get( `/publish/package`, { params: params, }, {}, false ); }; export const uploadProject = async (params?: any) => { return await get( `/publish/upload`, {}, { params: { ...params, clientID: getClientID(), }, }, false ); };