PayZu PaymentsTutoriais

Cobrar em moeda estrangeira

  1. Por padrão, toda chamada leva o certificado de cliente emitido pela PayZu. No fetch do Node, ele entra por um Agent do pacote undici. Se a PayZu desligou a exigência na sua credencial, tire o Agent e o dispatcher da chamada.

  2. POST /token com client_id e client_secret em Basic, sobre o mesmo mTLS. Depois, o token vai em Bearer.

  3. amount vai em centavos de real e a resposta vem em centavos da moeda de destino. type escolhe a taxa: bid é a de compra e ask, a de venda.

  4. Em moeda estrangeira, customer só exige name. phone vai com 55, DDD e 9 dígitos, sem +.

  5. Com currency USD, amount vale em centavos de dólar e installments é sempre 1. fraudAnalysis é obrigatório, com os MDD 1, 4, 9 e 83, e só cartões estrangeiros são aceitos.

foreign-currency-charge.js
import { readFile } from 'node:fs/promises'import { Agent } from 'undici' const API = 'https://api.sandbox.payzu.dev/v1'const dispatcher = new Agent({  connect: { cert: await readFile('cliente.crt'), key: await readFile('cliente.key'), ca: await readFile('ca.pem') }})const basic = Buffer.from(`${process.env.PAYZU_CLIENT_ID}:${process.env.PAYZU_CLIENT_SECRET}`).toString('base64')const headers = { authorization: `Basic ${basic}`, 'content-type': 'application/json' }const call = (path, body) => fetch(`${API}${path}`, {  dispatcher, headers, method: body ? 'POST' : 'GET', body: JSON.stringify(body)}).then((r) => r.json()) const { access_token } = await call('/token', { grant_type: 'client_credentials' })headers.authorization = `Bearer ${access_token}` const quote = await call('/currencies/USD/convert?amount=10000&type=ask')const amount = Math.round(quote.amount)console.log(`R$ 100,00 = US$ ${(amount / 100).toFixed(2)} (ask ${quote.currency.rate.ask})`) const charge = await call('/charges', {  externalId: 'pedido-2025-001',  amount,  paymentType: 'creditcard',  customer: {    name: 'Maria Souza', identity: '11144477735', identityType: 'CPF',    email: 'maria.souza@example.com', phone: '5511999998888',    address: {      street: 'Avenida Paulista', number: '1000', zipCode: '01310100', district: 'Bela Vista',      city: 'Sao Paulo', state: 'SP', country: 'BR'    }  },  cart: [{ name: 'Plano Pro', quantity: 1, sku: 'PRO', unitPrice: amount }],  creditCardPayment: {    currency: 'USD', installments: 1, authenticate: false,    card: { number: '4111111111111111', holder: 'MARIA SOUZA', expiration: '12/2030', cvv: '123' },    fraudAnalysis: {      fingerPrintId: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6',      browser: { cookiesAccepted: true, ipAddress: '200.201.202.203', type: 'Chrome' },      definedFields: [{ id: 1, value: 'Guest' }, { id: 4, value: 'Web' }, { id: 9, value: 'NAO' }, { id: 83, value: 'Varejo' }]    }  }})console.log(charge.id, charge.creditCardPayment?.status, charge.creditCardPayment?.returnMessage)
Resposta · 200 Valor convertido
{  "amount": 1841.62,  "currency": {    "code": "USD",    "rate": {      "ask": 5.43    }  }}

Referência: Converter um valor para outra moeda