Problem API pagseguro in JAVASCRIPT

Asked

Viewed 199 times

1

I am doing a project for my Lean Startup and to better understand how the Pagseguro API works, I decided to test. Currently the entire project API is in NODEJS. I did a lot of research and only found tutorials in PHP to use pagseguro.

I tried several times and in different ways make the request of checkout, to see how was in my application the pagseguro LIGHTBOX.

Unfortunately, I’ve only had mistakes and frustrations. Below is what I’m using to request in the API

const request = require("request");
const parseString = require("xml2js").parseString;

here follows the code in the function that calls in the NODEJS API to "Pick up the checkout from pagseguro"

async getProduct(req, res) {
console.log(req.body);
var plano = req.body;
let url_endpoint = "application/x-www-form-urlencoded;charset=ISO-8859-1"
data= {
  email : 'email.....',
  token: 'token sandbox....',
  currency: 'BRL',
  itemId1: '0001',
  itemDescription1: 'PRODUTO 1',
  itemAmount1: '2250.00', //Sempre com decimais
  itemQuantity1: '1',
  itemWeight1: '100',
  itemId2: '0002',
  itemDescription2: 'PRODUTO 2',
  itemAmount2: '35.00', //Sempre com decimais
  itemQuantity2: '2',
  itemWeight2: '750',
  reference: 'REF1234',
  senderName: 'Jose Comprador',
  senderAreaCode: '11',
  senderPhone: '56713293',
  senderEmail: '[email protected]',
  shippingType: '1', //Formas de envio, consulte a documentacao
  shippingAddressStreet: 'Av. Brig. Faria Lima',
  shippingAddressNumber: '1384',
  shippingAddressComplement: '2o andar',
  shippingAddressDistrict: 'Jardim Paulistano',
  shippingAddressPostalCode: '01452002',
  shippingAddressCity: 'Sao Paulo',
  shippingAddressState: 'SP',
  shippingAddressCountry: 'BRA'}
try {
  const options = {
    method: "POST",
    url: `https://ws.sandbox.pagseguro.uol.com.br/v2/checkout/email=${config.email}&token=${config.token}`,
    headers: {"Content-Type": url_endpoint },
    body: data,
    json: true
};

let result = await new Promise(function (resolve, reject) {
    request(options, function(error, response, body) {
        if (error) {
            console.log(error);
            reject(error);
        }
        resolve(body);
        console.log(response)
    });
});
console.log(result)
return result;
} catch (e) {
  console.log(e.message);
}

},

config.email and config.token work fine, so disregard.

Error is returned by BODY.

<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - Could not find resource for relative : /v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D of full path: http://ws.sandbox.pagseguro.uol.com.br/pagseguro-api/v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Could not find resource for relative : /v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D of full path: http://ws.sandbox.pagseguro.uol.com.br/pagseguro-api/v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D</u></p><p><b>description</b> <u>The requested resource (Could not find resource for relative : /v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D of full path: http://ws.sandbox.pagseguro.uol.com.br/pagseguro-api/v2/checkout/[email protected]&amp;token=7C742AD5AA394C47B39CDCC3CB40120D) is not available.</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>'

I even tried to use php but found that php with nodejs does not work.

1 answer

0

const got = require('got')
const qs = require('qs')
const { parseStringPromise } = require('xml2js')

const config = {
  email: 'email do vendedor',
  token: 'seu token de acesso'
}

async function getProduct (req, res) {
  const data = {
    currency: 'BRL',
    itemId1: '0001',
    itemDescription1: 'PRODUTO 1',
    itemAmount1: '2250.00', // Sempre com decimais
    itemQuantity1: '1',
    itemWeight1: '100',
    itemId2: '0002',
    itemDescription2: 'PRODUTO 2',
    itemAmount2: '35.00', // Sempre com decimais
    itemQuantity2: '2',
    itemWeight2: '750',
    reference: 'REF1234',
    senderName: 'Jose Comprador',
    senderAreaCode: '11',
    senderPhone: '56713293',
    senderEmail: 'email do comprador',
    shippingType: '1', // Formas de envio, consulte a documentacao
    shippingAddressStreet: 'Av. Brig. Faria Lima',
    shippingAddressNumber: '1384',
    shippingAddressComplement: '2o andar',
    shippingAddressDistrict: 'Jardim Paulistano',
    shippingAddressPostalCode: '01452002',
    shippingAddressCity: 'Sao Paulo',
    shippingAddressState: 'SP',
    shippingAddressCountry: 'BRA'
  }
  try {
    const params = qs.stringify({
      email: config.email,
      token: config.token
    })
    const { body } = await got.post('https://ws.sandbox.pagseguro.uol.com.br/v2/checkout?' + params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: qs.stringify(data)
    })
    const response = await parseStringPromise(body)
    if (response == null) throw new Error()

    const { checkout } = response
    if (checkout == null) throw new Error()

    if (!Array.isArray(checkout.code) && checkout.code.length <= 0) {
      throw new Error()
    }
    return checkout.code[0]
  } catch (e) {
    console.log(e.message)
    return null
  }
}

getProduct().then((code) => console.log(code))

npm install Qs
npm install got

Browser other questions tagged

You are not signed in. Login or sign up in order to post.