import as EfiPay from 'sdk-node-apis-efi'
import { Injectable } from '@nestjs/common'
import 'dotenv/config'
@Injectable()
export class PixService {
private efiPay = new EfiPay({
sandbox: false,
client_id: process.env.EFIPAY_CLIENT_ID,
client_secret: process.env.EFIPAY_CLIENT_SECRET,
certificate: './certs/producao-453290-LinkZap-v2.p12',
})
constructor() {
//
}
public async getBalance() {
const { saldo } = await this.efiPay.getAccountBalance()
return Number(saldo)
}
public async payPix(pixCode: string, amount: number) {
const balance = await this.getBalance()
if (balance < amount) {
throw new Error('Saldo insuficiente')
}
try {
const response = await this.efiPay.pixSend(
{ idEnvio: '01' },
{
valor: amount,
pagador: {
chave: 'a4c3dcd3-0005-454e-957f-df416a63c4ed',
},
favorecido: {
chave: pixCode,
},
},
)
return response
} catch (error) {
console.log(error)
}
}
}