Hi @givemeawhisky , thanks for the support, I’m didnt understand very well rsrs
my client side code looks like this
call the function payment onClick button
//call backend function to start payment
criarToken({
number: String($w('#ncartao').value),
verification_value: String($w('#cvc').value),
first_name: String(prinome),
last_name: String(ultnome),
month: String($w('#expiracaoMM').value),
year: String($w('#expiracaoAA').value)},
$w('#text248').text,
Number($w('#parcelamento').value),
[{ description: descricao, quantity: 1, price_cents: price }],
String($w('#cpf').value),
UserName, userEmail,
String($w('#cep').value),
idAtendimento,
itemsAtendimento.title,
itemsAtendimento.valorTotal,
dataAgendamento
)
//function ends, here i want to return the information from backend
.then(attstatus => {
$w('#mensagemfinal').text = ???
$w('#mensagemfinal').show()
})
Here is the backend code
//first function on backend is to create a toke payment and verify his credit card
export function criarToken(dadosCartao, userEmail, parcelas, produtos, cpf, name, email, cep, idAtendimento, os, valorTotal, dataAgendamento) {
console.log('chamou back')
var request = require("request");
var options = {
method: 'POST',
url: 'https://api.iugu.com/v1/payment_token',
body:
{
account_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
method: 'credit_card',
test: 'true',
data: dadosCartao
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
let tokenpgto = body.id
let insertToken = {
'fk_atendimento': idAtendimento,
'token': tokenpgto
}
wixData.insert('cobracas_iugu', insertToken)
.then(res => {
//then I call the function 'cobrar' to charge the user
cobrar(tokenpgto, userEmail, parcelas, produtos, cpf, name, email, cep, idAtendimento, os, valorTotal, dataAgendamento)
})
})
}
function cobrar(token, userEmail, parcelas, produtos, cpf, name, email, cep, idAtendimento, os, valorTotal, dataAgendamento) {
var request = require("request");
var options = {
method: 'POST',
url: 'https://api.iugu.com/v1/charge?api_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
body:
{
token: token,
email: userEmail,
months: parcelas,
items: produtos,
payer:
{
cpf_cnpj: cpf,
name: name,
email: email,
address:
{
zip_code: cep
}
}
},
json: true
}
request(options, function (error, response, body) {
// the results of the payment post
wixData.get('atendimento', idAtendimento)
.then((item) => {
item.pago = body.success
item.invoice_id = body.invoice_id
item.url_iugu = body.url
item.recibo_pdf = body.pdf
item.mensage_iugu = body.message
item.parcelascliente = parcelas
wixData.update('atendimento', item)
console.log(item)
})
if (body.success === true) {
wixData.get('atendimento', idAtendimento)
.then((item) => {
item.dataAgendamento = dataAgendamento
wixData.update('atendimento', item)
})
notifySiteContributors('OS ' + os + ' - R$ ' + valorTotal, 'Fatura paga pelo cliente ' + name, '', 'https://www.otimicar.com')
} else {
if (error) throw new Error(error);
notifySiteContributors('OS ' + os + ' - R$ ' + valorTotal, ' PROBLEMA NO PAGAMENTO CLIENTE ' + name, ' ', 'https://www.otimicar.com')
}
// How I can send the information 'body.success' to frontend? And how do i get it in the frontend?
return body.success
});
}
Thanks a lot!!