Skip to content

Instantly share code, notes, and snippets.

@wesleyalmd
Created October 10, 2024 22:02
Show Gist options
  • Save wesleyalmd/2d3d6788068b88776d87204d5e157740 to your computer and use it in GitHub Desktop.
Save wesleyalmd/2d3d6788068b88776d87204d5e157740 to your computer and use it in GitHub Desktop.
drderma-pagarme-split
// @ts-nocheck
import fetch from "cross-fetch";
const pagarme_api_key = process.env.PAGARME_API_KEY;
const pagarme_endpoint = "https://api.pagar.me/core/v5";
const pagarme_headers = {
Authorization:
"Basic " + Buffer.from(`${pagarme_api_key}:`).toString("base64"),
"Content-Type": "application/json",
};
export const createPagarmeTransation = async (input: any) => {
const { appointment, payment } = input;
const {
id: appointment_id,
pacient: { user },
} = appointment;
const { code: payment_code, amount } = payment;
const phone = user.telephone.replace(/\D/g, "");
const customer = {
type: "individual",
name: `${user.firstname} ${user.lastname}`,
email: user.email,
document: user.document.replace(/\D/g, ""),
document_type: "CPF",
gender: user.gender === 0 ? "male" : "female",
phones: {
mobile_phone: {
country_code: "55",
area_code: phone.slice(0, 2),
number: phone.slice(2),
type: "mobile",
},
},
};
const shipping = {
description: "Entrega virtual",
address: {
line_1: `${user.address}, ${user.number}`,
line_2: user.complement,
zip_code: user.zipcode,
city: user.city,
state: user.region,
country: user.country,
},
};
const items = [
{
code: `${appointment_id}`,
quantity: 1,
amount: Math.floor(Number(amount)),
description: `Consulta médica com Dr. ${user.firstname} ${user.lastname}`,
},
];
const split = [
{
recipient_id: "re_clv2o4u0z0glo019tsrse6xdn",
amount: Math.floor(Number(amount)) * 0.6,
type: "percentage",
options: {
charge_processing_fee: true,
liable: true,
},
},
{
recipient_id: "re_clrquoldt28ti019t7xwr74zq",
amount: Math.floor(Number(amount)) * 0.4,
type: "percentage",
options: {
charge_processing_fee: true,
liable: true,
},
},
];
if (payment_code === "credit_card") {
return await createCreditCardTransaction({
customer,
shipping,
items,
split,
payment,
});
}
if (payment_code === "pix") {
return await createPixTransaction({
customer,
shipping,
items,
split,
payment,
});
}
};
export const createCreditCardTransaction = async (input: any) => {
const { customer, shipping, items, split, payment } = input;
const { card_number, card_holder_name, card_expiration_date, card_cvv } =
payment;
const billing_address = {
street: customer.address.street,
street_number: customer.address.street_number,
neighborhood: customer.address.neighborhood,
zip_code: customer.address.zip_code,
city: customer.address.city,
state: customer.address.state,
country: customer.address.country,
};
const body = {
customer,
shipping,
items,
payments: [
{
split,
payment_method: "credit_card",
credit_card: {
operation_type: "auth_and_capture",
installments: 1,
card: {
number: card_number,
holder_name: card_holder_name,
holder_document: customer.document,
exp_month: card_expiration_date.slice(0, 2),
exp_year: card_expiration_date.slice(3),
cvv: card_cvv,
billing_address,
},
},
},
],
};
console.log(
"Corpo da requisição de cartão de crédito:",
JSON.stringify(body)
);
return await fetch(`${pagarme_endpoint}/orders`, {
method: "POST",
headers: pagarme_headers,
body: JSON.stringify(body),
})
.then((res) => {
console.log("Resposta da API:", res);
return res.json();
})
.catch((error) => console.log({ error }));
};
export const createPixTransaction = async (input: any) => {
const { customer, shipping, items, split, payment } = input;
const body = {
customer,
shipping,
items,
payments: [
{
split,
payment_method: "pix",
pix: {
expires_at: new Date(Date.now() + 30 * 60 * 1000).toISOString(),
},
},
],
};
console.log("Corpo da requisição de PIX:", JSON.stringify(body));
return await fetch(`${pagarme_endpoint}/orders`, {
method: "POST",
headers: pagarme_headers,
body: JSON.stringify(body),
})
.then((res) => {
console.log("Resposta da API:", res);
return res.json();
})
.catch((error) => console.log({ error }));
};
export const getPagarmeTransaction = async (transaction_id: any) => {
try {
const res = await fetch(`${pagarme_endpoint}/orders/${transaction_id}`, {
method: "GET",
headers: pagarme_headers,
});
const data = await res.json();
return data;
} catch (error) {
console.log({ error });
return error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment