pagarme-js icon indicating copy to clipboard operation
pagarme-js copied to clipboard

Pagarme not available on window

Open LuigiLazzari opened this issue 6 years ago • 9 comments

When trying to import the script from the cdn, nothing is added to the window object. I am using chrome's latest version. With this boilerplate file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
  </head>
  <body>
     <script src="https://assets.pagar.me/pagarme-js/4.4/pagarme.min.js"/>
  </body>
</html>

There is no way whatsoever to access any method described in the docs as a simple:

window.pagarme.client returns Uncaught TypeError: Cannot read property 'client' of undefined.

What am I doing wrong?

LuigiLazzari avatar Jun 16 '19 21:06 LuigiLazzari

@LuigiLazzarii think you don't need to use window object.

Just pagarme.client should solve your problem, like this example: https://docs.pagar.me/docs/obtendo-os-dados-do-cartao#section-web

murilohns avatar Jun 17 '19 13:06 murilohns

That does not work. the pagarme object isn't available at all.

LuigiLazzari avatar Jun 17 '19 13:06 LuigiLazzari

Hello Luigi, it worked for me:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
  </head>
  <body>
     <script src="https://assets.pagar.me/pagarme-js/4.4/pagarme.min.js"/></script>
     <script type="text/javascript">
        pagarme.client.connect({ api_key: 'YOUR_API_KEY' })
          .then(client => {
            return client.transactions.all()
          })
          .then(console.log)
     </script>
  </body>
</html>

Result:

image

murilohns avatar Jun 17 '19 18:06 murilohns

@LuigiLazzari, your problem was solved?

murilohns avatar Jun 27 '19 17:06 murilohns

Estou trabalhando numa aplicação em que estamos tentando integrar a API e estou tendo algumas dificuldades no qual, quando executo o seguinte código:

const pagarme = require('pagarme');
    pagarme.client.connect({ api_key: 'minha key' })
  .then(client => client.transactions.all())
  .then(transactions => console.log(transactions))

não é me retornado nenhuma resposta; Fiz a instalação normal da API usando o npm install pagarme. O que tenho que fazer para me comunicar com sucesso com a API do pagarme?

wesleyfeitosa avatar Jul 31 '19 13:07 wesleyfeitosa

Há alguma mensagem de erro sendo retornada @wesleyfeitosa ?

Tanto no console geral quanto se colocar um catch(error => console.log(error)) após o último then 🤔

lucianopf avatar Jul 31 '19 13:07 lucianopf

Quando chamo a api assim: const pagarme = require('pagarme'); aparece o seguinte erro:

ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'constants' in 'C:\angular\projetos\nabag-app\node_modules\pagarme' ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'crypto' in 'C:\angular\projetos\nabag-app\node_modules\pagarme' ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'http' in 'C:\angular\projetos\nabag-app\node_modules\pagarme' ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'https' in 'C:\angular\projetos\nabag-app\node_modules\pagarme' ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'stream' in 'C:\angular\projetos\nabag-app\node_modules\pagarme' ERROR in ./node_modules/pagarme/pagarme.js Module not found: Error: Can't resolve 'zlib' in 'C:\angular\projetos\nabag-app\node_modules\pagarme'

Aí quando troquei para const pagarme = require('pagarme/browser'); deu certo. Sabe porque isso aconteceu? @lucianopf

wesleyfeitosa avatar Jul 31 '19 13:07 wesleyfeitosa

@wesleyfeitosa o problema que está tendo parece ser na forma de build do projeto que está mexendo, e não uma questão do módulo da pagarme em sí. Conseguiu resolver o problema?

evaporei avatar Aug 06 '19 16:08 evaporei

I'm having the same issue as @wesleyfeitosa, is this thing solved so far? I'm building next.js app and using pagarme library version "^4.15.3". The error I'm getting is:

error - ./node_modules/pagarme/pagarme.js:26857:0
Module not found: Can't resolve 'constants'
null

Here's the function call:

import pagarme from 'pagarme';

export const getCardHash = (
  cardNumber,
  cardHolderName,
  cardExpirationDate,
  cardCvv,
  encryptionKey
) => {
  const card = {
    card_number: cardNumber,
    card_holder_name: cardHolderName,
    card_expiration_date: cardExpirationDate,
    card_cvv: cardCvv,
  };
  return pagarme.client
    .connect({ encryption_key: encryptionKey })
    .then((client) => client.security.encrypt(card))
    .then((cardHash) => cardHash);
};

Any suggestions? @otaviopace @lucianopf

igorani avatar Apr 06 '21 15:04 igorani