Created
August 5, 2022 15:21
-
-
Save ceelsoin/aaf55051d6bac1c70bda9e0cf069b7fb to your computer and use it in GitHub Desktop.
Iugu service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { inject, injectable } from "inversify"; | |
import { ICustomerModel } from "../../Infra/Models/Customer.model"; | |
import log, { LogLevelEnum } from "../Utils/logger.util"; | |
require("dotenv").config(); | |
const axios = require("axios").default; | |
export interface IIuguService { | |
getCustomers(): Promise<any> | |
getCustomerById(id: any): Promise<any> | |
createCustomer(payload: ICustomerModel): Promise<any> | |
updateCustomer(customer_id: any,payload: any): Promise<any> | |
createInvoice(payload: any): Promise<any> | |
refundInvoice(id: any): Promise<any> | |
directCharge(payload: any): Promise<any> | |
savePaymentTokenIntoCustomer(customer: any, card_token: string, saveDefault): Promise<any> | |
getPaymentMethodById(customer_id, id: any): Promise<any> | |
getPaymentMethods(customer_id): Promise<any> | |
getInvoiceById(id: any): Promise<any> | |
createPlan(payload: any): Promise<any> | |
updatePlan(payload: any, id: any): Promise<any> | |
createSubscription(payload: any): Promise<any> | |
suspendSubscription(id: any): Promise<any> | |
getSubscriptionById(id: any): Promise<any> | |
createSubAccount(name: any): Promise<any> | |
getAccountDataById(id: any, subaccount_token: any): Promise<any> | |
requestAccountVerification(id: any, payload, subaccount_token: any): Promise<any> | |
} | |
@injectable() | |
export class IuguService implements IIuguService { | |
BASE_URL = "https://api.iugu.com/v1"; | |
constructor() { | |
} | |
private getEncodedToken(token?): string { | |
//https://dev.iugu.com/reference/autentica%C3%A7%C3%A3o | |
return Buffer.from(`${token || process.env.IUGU_TOKEN}:`).toString('base64') | |
} | |
private async get(url: string, token?) { | |
try { | |
const config = { | |
headers: { | |
"accept": "application/json", | |
"Authorization": `Basic ${token ? this.getEncodedToken(token) : this.getEncodedToken()}` | |
}, | |
}; | |
const response = await axios.get(`${this.BASE_URL}/${url}`, config) | |
return response.data | |
} catch (ex) { | |
// log(LogLevelEnum.ERROR, `Error while fetch ${this.BASE_URL}/${url} data: ` + ex) | |
throw ex | |
} | |
} | |
private async post(url: string, payload: any, token?) { | |
try { | |
const config = { | |
headers: { | |
"accept": "application/json", | |
"Authorization": `Basic ${token ? this.getEncodedToken(token) : this.getEncodedToken()}` | |
}, | |
}; | |
const response = await axios.post(`${this.BASE_URL}/${url}`, payload, config) | |
return response.data | |
} catch (ex) { | |
// log(LogLevelEnum.ERROR, `Error while fetch ${this.BASE_URL}/${url} data: ` + ex) | |
// console.log(JSON.stringify(ex)) | |
throw ex | |
} | |
} | |
private async put(url: string, payload: any) { | |
try { | |
const config = { | |
headers: { | |
"accept": "application/json", | |
"Authorization": `Basic ${this.getEncodedToken()}` | |
}, | |
}; | |
const response = await axios.put(`${this.BASE_URL}/${url}`, payload, config) | |
return response.data | |
} catch (ex) { | |
// log(LogLevelEnum.ERROR, `Error while fetch ${this.BASE_URL}/${url} data: ` + ex) | |
// console.log(ex.response.data) | |
throw ex | |
} | |
} | |
public async getCustomers(): Promise<any> { | |
return await this.get("customers") | |
} | |
public async getCustomerById(id: any): Promise<any> { | |
return await this.get(`customers/${id}`) | |
} | |
public async createCustomer(payload: ICustomerModel): Promise<any> { | |
return await this.post("customers", payload) | |
} | |
public async updateCustomer(customer_id: any, payload: any): Promise<any> { | |
return await this.put(`customers/${customer_id}`, payload) | |
} | |
public async savePaymentTokenIntoCustomer(customer: any, card_token: string, saveDefault = true): Promise<any> { | |
console.log(customer, card_token) | |
return await this.post(`customers/${customer.iuguCustomerId}/payment_methods`, { | |
token: card_token, | |
description: "Cartão de crédito", | |
set_as_default: saveDefault ? "true" : "false" | |
}) | |
} | |
public async getPaymentMethodById(customer_id, id: any): Promise<any> { | |
return await this.get(`customers/${customer_id}/payment_methods/${id}`) | |
} | |
public async getPaymentMethods(customer_id): Promise<any> { | |
return await this.get(`customers/${customer_id}/payment_methods`) | |
} | |
public async createInvoice(payload: any): Promise<any> { | |
return await this.post("invoices", payload) | |
} | |
public async refundInvoice(id: any): Promise<any> { | |
//https://dev.iugu.com/docs/realizar-o-reembolso-de-faturas-estorno-por-api | |
return await this.post(`invoices/${id}/refund`, {}) | |
} | |
public async getInvoiceById(id: any): Promise<any> { | |
return await this.get(`invoices/${id}`) | |
} | |
public async directCharge(payload: any): Promise<any> { | |
return await this.post("charge", payload) | |
} | |
public async createPlan(payload: any): Promise<any> { | |
//https://dev.iugu.com/reference/criar-plano | |
return await this.post("plans", payload) | |
} | |
public async updatePlan(payload: any, id: any): Promise<any> { | |
//https://dev.iugu.com/reference/alterar-plano | |
return await this.put(`plans/${id}`, payload) | |
} | |
public async createSubscription(payload: any): Promise<any> { | |
//https://dev.iugu.com/reference/criar-assinatura | |
return await this.post("subscriptions", payload) | |
} | |
public async suspendSubscription(id: any): Promise<any> { | |
//https://dev.iugu.com/docs/suspender_ativar_e_remover_assinaturas | |
return await this.post(`subscriptions/${id}/suspend`, {}) | |
} | |
public async getSubscriptionById(id: any): Promise<any> { | |
//https://dev.iugu.com/reference/buscar-assinatura | |
return await this.get(`subscriptions/${id}`) | |
} | |
public async createSubAccount(name: any): Promise<any> { | |
//https://dev.iugu.com/reference/criar-subconta | |
try { | |
const response = await this.post(`${this.BASE_URL}/marketplace/create_account`, {name}) | |
return response.data | |
} catch (ex) { | |
log(LogLevelEnum.ERROR, `Error while fetch marketplace/create_account data: ` + ex) | |
throw ex | |
} | |
} | |
public async getAccountDataById(id: any, subaccount_token: any): Promise<any> { | |
//https://dev.iugu.com/reference/informacoes-da-conta | |
return await this.get(`accounts/${id}`, subaccount_token) | |
} | |
public async requestAccountVerification(id: any, payload, subaccount_token: any): Promise<any> { | |
//https://dev.iugu.com/reference/enviar-verificacao-de-conta | |
return await this.post(`accounts/${id}/request_verification`, payload, subaccount_token) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment