@validapay/tokenize 1.0.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +35 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
|
-
const
|
|
1
|
+
const OAUTH_URL_PRD = "https://oauth2.validapay.com.br/auth/token";
|
|
2
|
+
const OAUTH_URL_DEV = "https://oauth2-dev.validapay.com.br/auth/token";
|
|
3
|
+
const API_URL_PRD = "https://api.validapay.com.br";
|
|
4
|
+
const API_URL_DEV = "https://dev.validapay.com.br";
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
6
|
+
async function fetchToken({ clientId, clientSecret, dev }) {
|
|
7
|
+
const oauthUrl = dev ? OAUTH_URL_DEV : OAUTH_URL_PRD;
|
|
8
|
+
|
|
9
|
+
const res = await fetch(oauthUrl, {
|
|
10
|
+
method: "POST",
|
|
11
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
12
|
+
body: new URLSearchParams({
|
|
13
|
+
grant_type: "client_credentials",
|
|
14
|
+
client_id: clientId,
|
|
15
|
+
client_secret: clientSecret,
|
|
16
|
+
scope: "payment.methods/write",
|
|
17
|
+
}).toString(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
if (!res.ok) {
|
|
21
|
+
const error = await res.json().catch(() => ({ message: res.statusText }));
|
|
22
|
+
throw new Error(error?.message || "Falha ao obter token");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const data = await res.json();
|
|
26
|
+
return data.access_token;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function tokenize({ clientId, clientSecret, dev = false, card, customer }) {
|
|
30
|
+
if (!clientId) throw new Error("clientId é obrigatório");
|
|
31
|
+
if (!clientSecret) throw new Error("clientSecret é obrigatório");
|
|
5
32
|
if (!card?.number) throw new Error("card.number é obrigatório");
|
|
6
|
-
if (!card?.
|
|
33
|
+
if (!card?.cardHolderName) throw new Error("card.cardHolderName é obrigatório");
|
|
7
34
|
if (!card?.cvv) throw new Error("card.cvv é obrigatório");
|
|
8
35
|
if (!card?.expiration) throw new Error("card.expiration é obrigatório");
|
|
9
36
|
if (!customer?.document) throw new Error("customer.document é obrigatório");
|
|
10
37
|
if (!customer?.name) throw new Error("customer.name é obrigatório");
|
|
11
38
|
if (!customer?.email) throw new Error("customer.email é obrigatório");
|
|
12
39
|
|
|
40
|
+
const token = await fetchToken({ clientId, clientSecret, dev });
|
|
41
|
+
const apiUrl = dev ? API_URL_DEV : API_URL_PRD;
|
|
42
|
+
|
|
13
43
|
const res = await fetch(`${apiUrl}/v1/payment-methods/tokenize`, {
|
|
14
44
|
method: "POST",
|
|
15
45
|
headers: {
|
|
@@ -17,7 +47,7 @@ export async function tokenize({ apiUrl = DEFAULT_API_URL, token, card, customer
|
|
|
17
47
|
"Authorization": `Bearer ${token}`,
|
|
18
48
|
},
|
|
19
49
|
body: JSON.stringify({
|
|
20
|
-
|
|
50
|
+
cardHolderName: card.cardHolderName,
|
|
21
51
|
number: card.number,
|
|
22
52
|
cvv: card.cvv,
|
|
23
53
|
expiration: card.expiration,
|