Next

import { BlindPay } from 'blindpay';

export default async function handler(req, res) {
  const { method, body } = req;

  if (method !== 'POST') {
    return res.status(405).json({ message: 'Method Not Allowed' });
  }

  try {
    const blindpay = new BlindPay('bp_123456789');

    const receiver = await blindpay.receivers.create({
      type: 'individual',
      address: 'Rua Doutor Jairo de Matos Pereira',
      address_2: 'Apto. 1001',
      city: 'Vila Velha',
      state_province_region: 'ES',
      country: 'BR',
      postal_code: '29101310',
      individual: {
        first_name: 'Bernardo',
        last_name: 'Moura',
        tax_id: '14947677786',
      },
    });

    const account = await blindpay.accounts.create({
      receiver_id: receiver.id,
      currency: 'BRL',
      bank_country: 'BR',
      bank_details: {
        pix_key: '14947677786',
      },
    });

    const quote = await blindpay.quotes.create({
      account_id: account.id,
      amount_currency: 'receiver',
      cover_fees: true,
      amount: 979.80
    });

    /*
    Now you have 5 minutes to make a transaction to
    the address `account.addresses.solana_usdc` with
    the amount of `quote.sender_amount`.
    */

    /*
    After the transaction is done on solana blockchain
    we can use the transaction hash to get payout details.
    */

    const payout = await blindpay.payouts.get({
      tx_hash: '#123456'
    });

    res.status(200).json({ payout });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
}