const axios = require("axios");

async function prepararCheckout() {
  const terminalId = "SEU_TERMINAL_ID";
  const transactionId = "gp9253eg4o";
  const now = new Date().toISOString();
  const expiration = new Date(Date.now() + 15 * 60 * 1000).toISOString(); // 15 min

  const payload = {
    merchant: {
      terminalId: terminalId,
      channel: "web",
      merchantTransactionId: transactionId
    },
    transaction: {
      transactionTimestamp: now,
      description: `My transaction -> Order Order Id: ${transactionId}`,
      moto: false,
      paymentType: "PURS",
      amount: {
        value: 5,
        currency: "EUR"
      },
      paymentMethod: ["REFERENCE", "CARD", "MBWAY"],
      paymentReference: {
        initialDatetime: now,
        finalDatetime: expiration,
        maxAmount: {
          value: 5,
          currency: "EUR"
        },
        minAmount: {
          value: 5,
          currency: "EUR"
        },
        entity: "24000"
      }
    },
    customer: {
      customerInfo: {
        customerEmail: "client.name@hostname.pt",
        shippingAddress: {
          street1: "Rua 123",
          street2: "porta 2",
          city: "Lisboa",
          postcode: "1200-999",
          country: "PT"
        },
        billingAddress: {
          street1: "Rua 123",
          street2: "porta 2",
          city: "Lisboa",
          postcode: "1200-999",
          country: "PT"
        }
      }
    }
  };

  try {
    const response = await axios.post(
      "https://spg.qly.site1.sibs.pt/api/v1/payments",
      payload,
      {
        headers: {
          "Content-Type": "application/json"
        }
      }
    );

    const { transactionID, formContext } = response.data;

    // Retorne ao front-end
    return { transactionID, formContext };
  } catch (error) {
    console.error("Erro ao preparar checkout:", error.response?.data || error);
    throw error;
  }
}

				
			

Sinonimo D’honra Construções Copyright ® 2025 – Todos os direitos reservados