-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (90 loc) · 2.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*!
* @author Renato Aloi <[email protected]>
* date 12/21/2018
* Pagar.me Api integration react-native module
*/
//import mom from 'moment';
//const moment = mom().locale('pt-br');
import moment from 'moment/min/moment-with-locales';
import 'moment/locale/pt-br';
//import { NativeModules } from 'react-native';
//const { RNPagarMe } = NativeModules;
/* export default class RNPagarMe {
getName = () => {
return "Alguma coisa ai";
}
}; */
export default class RNPagarMe {
static getName = (name = 'Bob') => {
return `Olá ${name}`;
}
/**
* Inicia token da api
*
* @param {string} ap_key
*/
static init = (api_key) => {
RNPagarMe.ApiToken = api_key;
console.log('RNPagarMe.ApiToken 1', api_key);
}
/**
* Cria cliente
*
* @param {string} id
* @param {string} nome
* @param {string} email
* @param {string} cpf
* @param {string} telefone (formato DDNNNNNNNN)
* @param {string} data_nascimento (formato DD/MM/YYYY)
*
* @return {object} { status (int), response (string) }
*/
static createCustomer = async (id, nome, email, cpf, telefone, data_nascimento) => {
var status = 200;
var response = '';
console.log('RNPagarMe.ApiToken 2', RNPagarMe.ApiToken);
const customer = {
api_key: RNPagarMe.ApiToken,
external_id: id,
name: nome,
type: 'individual',
country: 'br',
email: email,
documents: [
{
type: 'cpf',
number: cpf
}
],
phone_numbers: [
'+55' + telefone
],
birthday: moment(data_nascimento, 'DD/MM/YYYY').format('YYYY-MM-DD')
};
const config = {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(customer)
};
const url = 'https://api.pagar.me/1/customers';
console.log('config.body', config.body);
var resultId = '';
try {
const res = await fetch(url, config);
const j = await res.json();
console.log('sent ok!', response);
status = res.status;
response = JSON.stringify(j);
} catch (e) {
console.log('sent error!', e.message);
status = 500;
response = e.message;
}
console.log('response object', { status, response });
return { status, response };
}
};
// usage ---
//PagarMe.getName();