pagarme-js
pagarme-js copied to clipboard
Pagarme not available on window
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?
@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
That does not work. the pagarme object isn't available at all.
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:

@LuigiLazzari, your problem was solved?
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?
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 🤔
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 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?
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