A FaciConnect API usa OAuth2 client credentials. O teu backend troca as credenciais do parceiro por um access_token de curta duração e usa-o como Bearer token.

Obter um token

POST {baseUrl}/token
Authorization: Basic base64(clientId:clientSecret)
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&validity_period=3600
Resposta:
{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6...",
  "token_type": "Bearer",
  "expires_in": 3600
}

Exemplo

const basic = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64');

const res = await fetch(`${API_URL}/token`, {
  method: 'POST',
  headers: {
    Authorization: `Basic ${basic}`,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: 'grant_type=client_credentials&validity_period=3600',
});
const { access_token } = await res.json();

Cache do token

Faz cache do token em memória e renova-o ~60s antes de expirar. Evita pedir um token novo a cada chamada.
let cachedToken = null;

async function getAccessToken() {
  if (cachedToken && cachedToken.exp > Date.now()) return cachedToken.value;
  const token = await requestNewToken();
  cachedToken = { value: token.access_token, exp: Date.now() + (token.expires_in - 60) * 1000 };
  return cachedToken.value;
}

Usar o token

POST {baseUrl}/facipaypartner/createPaymentOrder
Authorization: Bearer <access_token>
Accept-Language: pt
Content-Type: application/json
O clientSecret nunca pode aparecer no frontend. Este fluxo corre só no backend.

Próximo passo: Webhooks

Receber e verificar notificações de estado.