New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gn-api-sdk-node

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gn-api-sdk-node - npm Package Compare versions

Comparing version 0.0.10 to 0.0.11

docs/carnet-detailing.md

8

CHANGELOG.md

@@ -0,1 +1,9 @@

# 0.0.11
- Refactored: gn-auth - client credentials are now sent within the header
- Refactored: gn-constants/gn-endpoints/gn-sdk - each function now has two arguments: *params* and *body*.
The *body* is meant to be sent in the request body as usual, whereas the *params* will be mapped to url params as defined in gn-constants. If the param is not present in the url, it will be sent as a query string
- Refactored: gn-endpoints - now Gerencianet endpoints are restfull, which means that the sdk must consider sending also put and delete
- Updated: docs
# 0.0.10

@@ -2,0 +10,0 @@

105

docs/all-in-one.md

@@ -1,4 +0,4 @@

## Create charge, customer, payment
## Create charge and payment
The most common case scenarios will consist of the three steps mentioned in the title. The other examples show each part separately. Here goes the most used endpoints together in one example.
The most common case scenarios will consist of the two steps mentioned in the title. The other examples show each part separately. Here goes the most used endpoints together in one example.

@@ -10,3 +10,3 @@ At this point you should've noticed that this sdk works with promises, so that you can concat calls one after another using the `then` method.

```js
var chargeInput = {
var chargeBody = {
items: [{

@@ -26,19 +26,7 @@ name: 'Product 1',

var customerInput = {
charge_id: 223,
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
}
}
var paymentInput = {
charge_id: 223,
var paymentBody = {
payment: {
credit_card: {
installments: 1,
payment_token: 'fec500b1f3eb16615ca61f7c4781f51dcde49131',
payment_token: '8c888fe1e7d96112020cf9fcf5e4db5b9dba5cf6',
billing_address: {

@@ -51,2 +39,9 @@ street: 'Street 3',

state: 'MG'
},
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
}

@@ -58,15 +53,11 @@ }

Create the callback functions for charge and customer:
Create the callback function:
```js
var createCustomer = function (response) {
console.log(response);
customerInput.charge_id = response.data.charge_id;
paymentInput.charge_id = response.data.charge_id;
return gerencianet.createCustomer(customerInput);
}
var payCharge = function (response) {
var params = {
id: response.data.charge_id
}
var createPayment = function (response) {
console.log(response);
return gerencianet.createPayment(paymentInput)
return gerencianet.payCharge(params, paymentBody);
}

@@ -81,7 +72,7 @@ ```

gerencianet
.createCharge(chargeInput)
.then(createCustomer)
.then(createPayment)
.createCharge({}, chargeBody)
.then(payCharge)
.then(console.log)
.catch(console.log);
.catch(console.log)
.done();
```

@@ -102,4 +93,2 @@

{ "code": 200 } //customer created
{ "code": 200,

@@ -115,51 +104,1 @@ "data": {

```
If we embbed the `customerInput` inside `chargeInput`, we can skip the second step and go straight to payment:
```js
var chargeInput = {
items: [{
name: 'Product 1',
value: 1000,
amount: 2
}],
shippings: [{
name: 'Default Shipping Cost',
value: 100
}, {
name: 'Adicional Shipping Cost',
value: 150
}],
customer: customerInput
}
gerencianet
.createCharge(chargeInput)
.then(createPayment)
.then(console.log)
.catch(console.log);
```
Response:
```js
{ "code": 200,
"data": {
"charge_id": 260,
"total": 2250,
"status": 'new',
"custom_id": null,
"created_at": "2015-05-18"
}
} //charge created
{ "code": 200,
"data": {
"charge_id": 260,
"total": 2400,
"payment": "credit_card",
"installments": 1,
"installment_value": 2400
}
} //payment created
```

@@ -1,3 +0,5 @@

## Associating customers to existing charges
## Associating a customer to a charge
If you create a charge without customers because you didn't know them or was planning to get their info afterwards, no need to panic. You can associate these customers like the following:
Instantiate the module:

@@ -10,4 +12,2 @@

If you created charges without customers because you didn't know them or was planning to get their info afterwards, no need to panic. You can associate these customers to their respective charges like the following:
```js

@@ -19,3 +19,3 @@ var customerInput = {

email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
cpf: '04267484171',
birth: '1977-01-15',

@@ -26,5 +26,4 @@ phone_number: '5144916523'

gerencianet
.createCustomer(customerInput)
.associateChargeCustomer(customerInput)
.then(console.log)

@@ -34,2 +33,25 @@ .catch(console.log);

If the customer is a juridical person, it's necessary send the corporate name and CNPJ (brazilian document for juridical person):
```js
var chargeInput = {
items: [{
name: 'Product A',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
juridical_person: {
corporate_name: 'Fictional Company',
cnpj: '52841284000142'
}
}
}
```
If everything went well, the return is just a response with code 200:

@@ -36,0 +58,0 @@

## Creating carnet billets
Carnet is a payment method that generates a bundle of charges with the same payment information and customer.
In order to generate a carnet, you'll need the items, the customer and the number of repeats (or parcels).
The carnets can also be generated with the `metadata` attribute, just like in the banking billet, containing the `notification_url` and/or `custom_id`
There are other optional params:
- `expiration date` of the first charge
- `post_office_service`, which tells if the carnet must be sent via post office service (to you or to your clients)
- `split_items`, identifying if the total value must be splitted among the charges (defaults to `false`)
- The carnet `instructions`
Instantiate the module:

@@ -10,8 +23,27 @@

A very common payment method, especially in Brazil, are the carnet billets. It is basically a set of a predefined amount of billets that are monthly due.
### Required properties:
Create the carnet input object:
```js
var body = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 12,
split_items: false
}
```
### Required properties plus metadata **(optional)**:
```js
var carnetInput = {
var body = {
items: [{

@@ -25,3 +57,3 @@ name: 'Carnet Item 1',

email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
cpf: '04267484171',
birth: '1977-01-15',

@@ -31,17 +63,118 @@ phone_number: '5144916523'

repeats: 4,
split_items: false
metadata: {
custom_id: 'my_id',
notification_url: 'http://yourdomain.com'
}
}
```
Create the carnet:
The `notification_url` property will be used for sending notifications once things happen with charges statuses, as when it's payment was approved, for example. More about notifications [here](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/notifications.md). The `custom_id` property can be used to set your own reference to the carnet.
### Required properties plus expiration date of the first charge **(optional)**:
If you don't provide the expiration date of the first charge, the defaut value will be the current day + 8.
```js
var body = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 4,
expire_at: '2020-12-12'
}
```
### Required properties plus post office service information **(optional)**:
If you want the carnet to arrive at your house or at your client's house, you can count on Gerencianet's post office service. Just send an extra attribute:
```js
var body = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 4,
post_office_service: {
send_to: 'customer'
}
}
```
If `send_to` is set to *customer*, the carnet arrives at you customer's. If it is set to *seller*, just wait for it to arrive at your place!
### split_items attribute **(optional)**
By default, each parcel has the total value of the carnet. If you want to divide the total value among the parcels, change `split_items` property to *true*.
```js
var body = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 4,
split_items: true
}
```
### Setting instructions **(optional)**
If you want the carnet billet to have extra instructions, it's possible to send a maximum of 4 different instructions with a maximum of 90 caracters, just as follows:
```js
var body = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 4,
instructions: [
"Pay only with money",
"Do not pay with gold"
]
}
```
### Finally, create the carnet:
```js
gerencianet
.createCarnet(carnetInput)
.then(function (carnet) {
console.log('Response:', carnet);
})
.catch(function (err) {
console.log('Error:', err);
})
.createCarnet({}, body)
.then(console.log)
.catch(console.log)
.done();

@@ -63,3 +196,3 @@ ```

"value": 2000,
"expire_at": '2015-06-01',
"expire_at": "2015-06-01",
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61428-LEENA9",

@@ -72,3 +205,3 @@ "barcode": "00190.00009 01523.894002 00061.428181 1 64780000002000"

"value": 2000,
"expire_at": '2015-07-01',
"expire_at": "2015-07-01",
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61429-CORZE4",

@@ -81,3 +214,3 @@ "barcode": "00190.00009 01523.894002 00061.428181 8 65090000002000"

"value": 2000,
"expire_at": '2015-08-01',
"expire_at": "2015-08-01",
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61430-HIRRA4",

@@ -90,3 +223,3 @@ "barcode": "00190.00009 01523.894002 00061.428181 7 65400000002000"

"value": 2000,
"expire_at": '2015-09-01',
"expire_at": "2015-09-01",
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61431-HIRRA4",

@@ -100,97 +233,2 @@ "barcode": "00190.00009 01523.894002 00061.428181 5 65400000002000"

Notice that, as the `repeats` were set to 3, the output contains 3 charges with `waiting` status. Also notice that `expire_at` increases monthly.
What would happen if we toggled `split_items`?
```js
{
"code": 200,
"data": {
"carnet_id": 6,
"cover": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CC-28333-61428-LEENA9/28333-61428-LEENA9",
"charges": [{
"charge_id": 357,
"parcel": "1",
"status": "waiting",
"value": 500,
"expire_at": '2015-06-01',
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61428-LEENA9",
"barcode": "00190.00009 01523.894002 00061.428181 1 64780000002000"
}, {
"charge_id": 358,
"parcel": "2",
"status": "waiting",
"value": 500,
"expire_at": '2015-07-01',
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61429-CORZE4",
"barcode": "00190.00009 01523.894002 00061.428181 8 65090000002000"
}, {
"charge_id": 359,
"parcel": "3",
"status": "waiting",
"value": 500,
"expire_at": '2015-08-01',
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61430-HIRRA4",
"barcode": "00190.00009 01523.894002 00061.428181 7 65400000002000"
}, {
"charge_id": 360,
"parcel": "4",
"status": "waiting",
"value": 500,
"expire_at": '2015-09-01',
"url": "https://visualizacao.gerencianet.com.br/emissao/28333_2385_ZEMAL5/A5CL-28333-61428-LEENA9/28333-61431-HIRRA4",
"barcode": "00190.00009 01523.894002 00061.428181 5 65400000002000"
}
]
}
}
```
Observe that, in the above case, the value of each parcel changed from **2000** to **500**. We can infer that, when `split_items` is **true**, the value of each parcel will be the original total value divided by the number of parcels *(2000 / 4 = 500)*.
## Post office service
If you want the carnet to arrive at your house or at your client's house, you can count on Gerencianet's post office service. Just send an extra attribute:
```js
var carnetInput = {
items: [{
name: 'Carnet Item 1',
value: 1000,
amount: 2
}],
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
},
repeats: 3,
post_office_service: {
send_to: 'customer'
}
split_items: false
}
```
If `send_to` is set to *customer*, the carnet arrives at you customer's. If it is set to *seller*, just wait for it to arrive at your place!
## Detailing carnets
In order to retrieve carnets, just provide the carnet_id:
```js
gerencianet
.detailCarnet({
carnet_id: 6
})
.then(function (carnet) {
console.log('Response:',
util.inspect(carnet, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.done();
```
Notice that, as the `repeats` were set to 4, the output contains 4 charges with `waiting` status. The value of each charge is the sum of the items values, because the `split_items` property was set to *false*. Also notice that `expire_at` increases monthly.

@@ -17,3 +17,3 @@ ## Creating charges with marketplace

```js
var chargeInput = {
var body = {
items: [{

@@ -34,3 +34,3 @@ name: 'Product A',

metadata: {
notification_url: 'http://google.com'
notification_url: 'http://yourdomain.com'
}

@@ -44,3 +44,3 @@ }

gerencianet
.createCharge(chargeInput)
.createCharge({}, body)
.then(console.log)

@@ -50,3 +50,3 @@ .catch(console.log);

The attribute `payee_code` identifies a Gerencianet account, just like in [creating charges with shippings](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-shippings.md). In order to get someone else's `payee_code` you need to ask the account owner. There is no other way. To visualize yours, log in your Gerencianet account and search for *Identificador de Conta* under *Dados Cadastrais*.
The attribute `payee_code` identifies a Gerencianet account, just like in [creating charges with shippings](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-with-shippings.md). In order to get someone else's `payee_code` you need to ask the account owner. There is no other way. To visualize yours, log in your Gerencianet account and search for *Identificador de Conta* under *Dados Cadastrais*.

@@ -53,0 +53,0 @@ In the example above, there are two repasses, both of 25%, but each one for a different account, whereas the `payee_code` differs. The integrator account will receive, at the end, 50% of the total value. Disregarding the rates, the integrator account would receive R$5,00. The other two accounts would receive R$ 2,50 each.

@@ -12,3 +12,3 @@ 'use strict';

var chargeInput = {
var chargeBody = {
items: [{

@@ -28,19 +28,7 @@ name: 'Product 1',

var customerInput = {
charge_id: 223,
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
}
}
var paymentInput = {
charge_id: 223,
var paymentBody = {
payment: {
credit_card: {
installments: 1,
payment_token: '6f4c6e7007531a4e2e4d8dabfe49104ac3912c46',
payment_token: '8c888fe1e7d96112020cf9fcf5e4db5b9dba5cf6',
billing_address: {

@@ -53,2 +41,9 @@ street: 'Street 3',

state: 'MG'
},
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
}

@@ -61,19 +56,15 @@ }

var createCustomer = function (response) {
console.log(response);
customerInput.charge_id = response.data.charge_id;
paymentInput.charge_id = response.data.charge_id;
return gerencianet.createCustomer(customerInput);
}
var payCharge = function (response) {
var params = {
id: response.data.charge_id
}
var createPayment = function (response) {
console.log(response);
return gerencianet.createPayment(paymentInput)
return gerencianet.payCharge(params, paymentBody);
}
gerencianet
.createCharge(chargeInput)
.then(createCustomer)
.then(createPayment)
.createCharge({}, chargeBody)
.then(payCharge)
.then(console.log)
.catch(console.log);
.catch(console.log)
.done();

@@ -12,15 +12,12 @@ 'use strict';

var params = {
id: 18
}
var gerencianet = new Gerencianet(options);
gerencianet
.cancelSubscription({
subscription_id: 18,
customer: true
})
.then(function (subscription) {
console.log(subscription);
})
.catch(function (err) {
console.log('Error:', err);
})
.cancelSubscription(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var moment = require('moment');
var Gerencianet = require('gn-api-sdk-node');
var moment = require('moment');
var credentials = require('./credentials');

@@ -15,9 +15,19 @@

.add(1, 'days')
.format('YYYY-MM-DD 00:00:00');
.format('YYYY-MM-DD');
var paymentInput = {
charge_id: 242,
var params = {
id: 1000
}
var body = {
payment: {
banking_billet: {
expire_at: tomorrow
expire_at: tomorrow,
customer: {
name: 'Gorbadoc Oldbuck',
email: 'oldbuck@gerencianet.com.br',
cpf: '04267484171',
birth: '1977-01-15',
phone_number: '5144916523'
}
}

@@ -30,8 +40,5 @@ }

gerencianet
.createPayment(paymentInput)
.then(function (payment) {
console.log('Payment:', payment);
})
.catch(function (err) {
console.log('Error:', err);
});
.payCharge(params, body)
.then(console.log)
.catch(console.log)
.done();

@@ -12,3 +12,3 @@ 'use strict';

var carnetInput = {
var body = {
items: [{

@@ -22,3 +22,3 @@ name: 'Carnet Item 1',

email: 'oldbuck@gerencianet.com.br',
document: '04267484171',
cpf: '04267484171',
birth: '1977-01-15',

@@ -34,9 +34,5 @@ phone_number: '5144916523'

gerencianet
.createCarnet(carnetInput)
.then(function (carnet) {
console.log('Response:', carnet);
})
.catch(function (err) {
console.log('Error:', err);
})
.createCarnet({}, body)
.then(console.log)
.catch(console.log)
.done();

@@ -12,3 +12,3 @@ 'use strict';

var chargeInput = {
var body = {
items: [{

@@ -28,9 +28,5 @@ name: 'Product 1',

gerencianet
.createCharge(chargeInput)
.then(function (charge) {
console.log('Response:', charge);
})
.catch(function (err) {
console.log('Error:', err);
})
.createCharge({}, body)
.then(console.log)
.catch(console.log)
.done();

@@ -12,6 +12,8 @@ 'use strict';

var planInput = {
name: 'My first plan',
repeats: 24,
interval: 2
var params = {}
var body = {
name: 'My first plan',
repeats: 24,
interval: 2
}

@@ -22,9 +24,5 @@

gerencianet
.createPlan(planInput)
.then(function (plan) {
console.log('Response:', plan);
})
.catch(function (err) {
console.log('Error:', err);
})
.createPlan(params, body)
.then(console.log)
.catch(console.log)
.done();

@@ -12,3 +12,3 @@ 'use strict';

var planInput = {
var planBody = {
name: 'My first plan',

@@ -19,3 +19,3 @@ repeats: 24,

var subscriptionInput = {
var subscriptionBody = {
items: [{

@@ -30,21 +30,15 @@ name: 'Product 1',

var createPlanCallback = function (response) {
console.log(response);
if (response.code === 200) {
subscriptionInput.plan_id = response.data.plan_id;
return subscriptionInput;
} else {
throw new Error();
var createSubscription = function (response) {
var params = {
id: response.data.plan_id
}
}
var createChargeCallback = function (response) {
return gerencianet.createSubscription(subscriptionInput)
return gerencianet.createSubscription(params, subscriptionBody);
}
gerencianet
.createPlan(planInput)
.then(createPlanCallback)
.then(createChargeCallback)
.createPlan({}, planBody)
.then(createSubscription)
.then(console.log)
.catch(console.log);
.catch(console.log)
.done();
'use strict'
module.exports = {
client_id: '',
client_secret: ''
client_id: 'yoda_id',
client_secret: 'yoda_secret',
urls: {
production: 'http://localhost:4400',
sandbox: 'http://localhost:4400'
}
}
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,15 +12,12 @@ var credentials = require('./credentials');

var params = {
id: 1000
}
var gerencianet = new Gerencianet(options);
gerencianet
.deletePlan({
plan_id: 1
})
.then(function (plan) {
console.log('Response:',
util.inspect(plan, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.deletePlan(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,15 +12,12 @@ var credentials = require('./credentials');

var params = {
id: 1002
}
var gerencianet = new Gerencianet(options);
gerencianet
.detailCarnet({
carnet_id: 6
})
.then(function (carnet) {
console.log('Response:',
util.inspect(carnet, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.detailCarnet(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,15 +12,12 @@ var credentials = require('./credentials');

var params = {
id: 1001
}
var gerencianet = new Gerencianet(options);
gerencianet
.detailCharge({
charge_id: 233
})
.then(function (charge) {
console.log('Response:',
util.inspect(charge, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.detailCharge(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,15 +12,12 @@ var credentials = require('./credentials');

var params = {
id: 1000
}
var gerencianet = new Gerencianet(options);
gerencianet
.detailSubscription({
subscription_id: 11
})
.then(function (subscription) {
console.log('Response:',
util.inspect(subscription, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.detailSubscription(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,15 +12,12 @@ var credentials = require('./credentials');

var params = {
token: '252948279264ee47e117cb099ef81'
}
var gerencianet = new Gerencianet(options);
gerencianet
.getNotification({
notification: '25294827-5926-4ee4-b7e1-d17cb099ef81'
})
.then(function (notification) {
console.log('Response:',
util.inspect(notification, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.getNotification(params)
.then(console.log)
.catch(console.log)
.done();
'use strict';
var util = require('util');
var Gerencianet = require('gn-api-sdk-node');

@@ -13,3 +12,3 @@ var credentials = require('./credentials');

var paymentDataInput = {
var params = {
type: 'visa',

@@ -22,9 +21,5 @@ total: 5000

gerencianet
.getPaymentData(paymentDataInput)
.then(function (installments) {
console.log('Response:',
util.inspect(installments, false, null));
})
.catch(function (err) {
console.log('Error:', err);
});
.getInstallments(params)
.then(console.log)
.catch(console.log)
.done();
{
"dependencies": {
"gn-api-sdk-node": "git+ssh://git@github.com:franciscotfmc/gn-api-sdk-node.git",
"gn-api-sdk-node": "git+ssh://git@github.com:gerencianet/gn-api-sdk-node.git",
"moment": "~2.8.4"
}
}

@@ -10,27 +10,6 @@ ## Notifications

Any changes that happen in the charges will trigger an event that notifies the `notification_url` provided at creation time (see [creating charges](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-customer.md)).
Any changes that happen in the charges will trigger an event that notifies the `notification_url` provided at creation time (see [creating charges](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charges.md)).
It's also possible to set or change the `notification_url` for existing charges:
It's also possible to set or change the `notification_url` for existing charges, see [updating informations](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-update.md).
```js
gerencianet
.updateNotification({
charge_id: 233,
notification_url: 'http://google.com'
})
.then(function (notification) {
console.log(util.inspect(notification, false, null));
})
.catch(console.log)
.done();
```
Response:
```js
{
"code": 200
}
```
Given that a charge has a valid `notification_url`, when the notification time comes you'll receive a post with a `token`. This token must be used to get the notification payload data.

@@ -42,11 +21,9 @@

app.post('/notifications', function(req, res) {
var notificationToken = req.body.notification,
params = {
token: req.body.notification
}
gerencianet
.getNotification({
notification: notificationToken
})
.then(function (notification) {
console.log(util.inspect(notification, false, null));
})
.getNotification(params)
.then(console.log)
.catch(console.log)

@@ -62,16 +39,82 @@ .done();

"code": 200,
"data": {
"charge_id": 233,
"subscription_id": 12,
"total": 2000,
"status": "new",
"data": [{
"id": 1,
"type": "charge",
"custom_id": null,
"created_at": "2015-05-14",
"history": [
{
"status": "new",
"timestamp": "2015-05-14 15:39:14"
}
]
}
"status": {
"current": "new",
"previous": null
},
"identifiers": {
"charge_id": 1002
}
}, {
"id": 2,
"type": "charge",
"custom_id": null,
"status": {
"current": "waiting",
"previous": "new"
},
"identifiers": {
"charge_id": 1002
}
}, {
"id": 3,
"type": "charge",
"custom_id": null,
"status": {
"current": "paid",
"previous": "waiting"
},
"identifiers": {
"charge_id": 1002
},
"value": 2000
}, {
"id": 4,
"type": "charge",
"custom_id": null,
"status": {
"current": "refunded",
"previous": "paid"
},
"identifiers": {
"charge_id": 1002
},
"value": 1780
}]
}
```
Response will be an array with all changes of a token that happened within 6 months, and it contains the following parameters:
* id: Each notification has its own sequence, starting from `1` and the `id` parameters is used to mark this sequence. This is useful if you need to keep track which change you have already processed.
* type: The type of this change. The available values are:
* `charge` - a charge have changed.
* `subscription` - a subscription have changed.
* `carnet` - a carnet have changed.
* `subscription_charge` - one subscription's parcel have changed.
* `carnet_charge` - one carnet's parcel have changed.
* custom_id: Your custom_id.
* status: Status of the transaction. It contains the `current` status and `previous` status (before the change) of this transaction.
p.s.: if there is no `previous` status (i.e.: for new charges), the `previous` value will be null.
* identifiers: Identifiers related to this change. It may have one or more identifier depending on the type:
* for `charge` type: identifiers will contain only `charge_id`.
* for `subscription` type: identifiers will contain only `subscription_id`.
* for `carnet` type: identifiers will contain only `carnet_id`.
* for `subscription_charge` type: identifiers will contain both `charge_id` and `subscription_id`.
* for `carnet_charge` type: identifiers will contain both `charge_id` and `carnet_id`.
* value: this parameter will only be shown when the change have a value related.
i.e.: paid charges, refunded charges, etc.
For more information about notifications, please, refer to [Gerencianet](https://docs.gerencianet.com.br/#!/charges/notifications).
## Creating subscriptions
Instantiate the module:
```js
var Gerencianet = require('gn-api-sdk-node');
var gerencianet = new Gerencianet(options);
```
If you ever have to recurrently charge your clients, you can create a different kind of charge, one that belongs to a subscription. This way, subsequent charges will be automatically created and charged in your customers credit card, based on the interval and repetitions supplied in a plan configuration.
The `repeats` parameter defines how many times the transaction will be repeated. If you don't pass it, the subscription will create charges indefinitely.
The `repeats` parameter defines how many times the transaction will be repeated. If you don't provide it, the subscription will create charges indefinitely.
The `interval` parameter defines the interval, in months, that a charge has to be generated. The minimum value is 1, and the maximum is 24. So, define "1" if you want monthly creations for example.
The `interval` parameter defines the interval, in months, that a charge has to be generated. The minimum value is 1, and the maximum is 24.
It's worth to mention that this mechanics is triggered only if the customer commits the subscription. In other words, it takes effect when the customer pays the first charge.
At first, you need to to create a plan. Then, you create a charge passing a plan_id to generate a subscription. You can use the same plan_id whenever you want.
At last, it boils down to creating a plan and then the subscription. The plan can be reused for generating other subscriptions:
```js
var planInput = {
name: 'My first plan',
repeats: 24,
interval: 2
var planBody = {
name: 'My first plan',
repeats: 24,
interval: 2
}
var subscriptionBody = {
items: [{
name: 'Product 1',
value: 1000,
amount: 2
}]
}
var createSubscription = function (response) {
var params = {
id: response.data.plan_id
}
return gerencianet
.createSubscription(params, subscriptionBody);
}
gerencianet
.createPlan(planInput)
.then(function (plan) {
console.log('Response:', plan);
})
.catch(function (err) {
console.log('Error:', err);
})
.createPlan({}, planBody)
.then(createSubscription)
.then(console.log)
.catch(console.log)
.done();
```
Creating the charge:
### Deleting a plan:
*(works just for plans that doesn't have a subscription associated):*
```js
var chargeInput = {
items: [{
name: 'Product 1',
value: 1000,
amount: 2
}],
plan_id: 1
var params = {
id: 1001
}
gerencianet
.createCharge(chargeInput)
.then(function (charge) {
console.log('Response:', charge);
})
.catch(function (err) {
console.log('Error:', err);
})
.deletePlan(params)
.then(console.log)
.catch(console.log)
.done();
}
```
## Canceling subscriptions
### Canceling subscriptions
You can cancel subscriptions at any time:
You can cancel active subscriptions at any time:
```js
var params = {
id: 18,
customer: true
}
gerencianet
.cancelSubscription({
subscription_id: 14,
customer: true
})
.cancelSubscription(params)
.then(console.log)

@@ -83,20 +83,2 @@ .catch(console.log)

The `customer` attribute above indicates who is triggering the cancellation, the customer or the one providing the service. In this case, the customer decided not to continue with the subscription.
## Deleting plans
*(works just for plans that hasn't a subscription associated):*
```js
gerencianet
.deletePlan({
plan_id: 1
})
.then(function (plan) {
console.log('Response:',
util.inspect(plan, false, null));
})
.catch(function (err) {
console.log('Error:', err);
})
.done();
```
The `customer` attribute above indicates who is triggering the cancellation, the customer or the one providing the service. In this case, the customer decided not to continue with the subscription.

@@ -7,2 +7,2 @@ 'use strict';

return new GnSdk(options);
};
};
'use strict';
var request = require('request');
var constants = require('./gn-constants');
var q = require('q');
function GnAuth(options) {
function GnAuth(options, constants) {
this.constants = constants;
this.clientId = options.client_id;

@@ -17,8 +17,10 @@ this.clientSecret = options.client_secret;

var postParams = {
url: this.baseUrl + constants.ENDPOINTS.authorize,
url: this.baseUrl + this.constants.ENDPOINTS.authorize.route,
json: true,
form: {
grant_type: 'client_credentials',
client_id: this.clientId,
client_secret: this.clientSecret
body: {
grant_type: 'client_credentials'
},
auth: {
username: this.clientId,
password: this.clientSecret
}

@@ -28,8 +30,9 @@ };

var callback = function (err, httpResponse, body) {
if (err) {
defer.reject(err);
defer.reject(err, httpResponse);
} else if (httpResponse.statusCode !== 200) {
defer.reject(body);
defer.reject(body, httpResponse);
} else {
defer.resolve(body);
defer.resolve(body, httpResponse);
}

@@ -39,2 +42,3 @@ };

request.post(postParams, callback);
return defer.promise;

@@ -41,0 +45,0 @@ };

@@ -9,20 +9,87 @@ 'use strict';

ENDPOINTS: {
authorize: '/authorize',
createCharge: '/charge',
detailCharge: '/charge/detail',
updateChargeMetadata: '/charge/metadata/update',
updateBillet: '/charge/billet/update',
createCustomer: '/charge/customer/associate',
createPayment: '/pay',
getPaymentData: '/payment/data',
getNotification: '/notification',
updateNotification: '/notification/update',
createSubscription: '/subscription',
detailSubscription: '/subscription/detail',
cancelSubscription: '/subscription/cancel',
createPlan: '/plan',
deletePlan: '/plan/delete',
createCarnet: '/carnet',
detailCarnet: '/carnet/detail'
authorize: {
route: '/authorize',
method: 'post'
},
createCharge: {
route: '/charge',
method: 'post'
},
detailCharge: {
route: '/charge/:id',
method: 'get'
},
updateChargeMetadata: {
route: '/charge/:id/metadata',
method: 'put'
},
updateBillet: {
route: '/charge/:id/billet',
method: 'put'
},
payCharge: {
route: '/charge/:id/pay',
method: 'post'
},
cancelCharge: {
route: '/charge/:id/cancel',
method: 'put'
},
createCarnet: {
route: '/carnet',
method: 'post'
},
detailCarnet: {
route: '/carnet/:id',
method: 'get'
},
updateParcel: {
route: '/carnet/:id/parcel/:parcel',
method: 'put'
},
updateCarnetMetadata: {
route: '/carnet/:id/metadata',
method: 'put'
},
getNotification: {
route: '/notification/:token',
method: 'get'
},
getPlans: {
route: '/plans',
method: 'get'
},
createPlan: {
route: '/plan',
method: 'post'
},
deletePlan: {
route: '/plan/:id',
method: 'del'
},
createSubscription: {
route: '/plan/:id/subscription',
method: 'post'
},
detailSubscription: {
route: '/subscription/:id',
method: 'get'
},
paySubscription: {
route: '/subscription/:id/pay',
method: 'post'
},
cancelSubscription: {
route: '/subscription/:id/cancel',
method: 'put'
},
updateSubscriptionMetadata: {
route: '/subscription/:id/metadata',
method: 'put'
},
getInstallments: {
route: '/installments',
method: 'get'
}
}
};
};

@@ -5,87 +5,142 @@ 'use strict';

var q = require('q');
var constants = require('./gn-constants');
var GnAuth = require('./gn-auth');
function GnEndpoints(options) {
function GnEndpoints(options, constants) {
options.baseUrl = options.sandbox ? constants.URL.sandbox : constants.URL.production;
this.options = options;
this.gnAuth = new GnAuth(options);
this.gnAuth = new GnAuth(options, constants);
this.accessToken = null;
this.constants = constants;
}
var postParams = function (input, route) {
var params = {
url: this.options.baseUrl + route,
json: true,
form: {
data: JSON.stringify(input)
}
};
GnEndpoints.prototype.run = function (name, params, body) {
var self = this;
self.defer = q.defer();
self.endpoint = self.constants.ENDPOINTS[name];
self.body = body;
self.params = params;
if (this.accessToken) {
params.form.access_token = this.accessToken;
if (!self.accessToken) {
self.getAccessToken()
.then(self.directReq.bind(self));
} else {
self.withTokenReq.call(self);
}
return params;
return self.defer.promise;
};
GnEndpoints.prototype.post = function (object, route) {
GnEndpoints.prototype.getAccessToken = function () {
var self = this;
this.defer = q.defer();
return this.gnAuth.getAccessToken()
.then(function (response) {
self.accessToken = response.access_token;
return self.accessToken;
}).catch(function (err) {
return err;
});
};
var directReqCallback = function (err, httpResponse, body) {
if (err) {
self.defer.reject(err);
} else if (httpResponse.statusCode !== 200) {
self.defer.reject(body);
} else {
self.defer.resolve(body);
}
GnEndpoints.prototype.getResponse = function (response, body) {
return this.options
.raw_response ? response : body;
};
GnEndpoints.prototype.req = function (callback) {
request[this.endpoint.method](this.getParams.call(this,
this.endpoint.route), callback);
}
GnEndpoints.prototype.directReq = function () {
this.req(this.directReqCallback.bind(this));
};
GnEndpoints.prototype.withTokenReq = function () {
this.req(this.withTokenReqCallback.bind(this));
};
GnEndpoints.prototype.getParams = function (route) {
var self = this;
var regex = /\:(\w+)/g;
var query = '';
var placeholders = route.match(regex) || [];
var params = {};
for (var prop in self.params) {
params[prop] = self.params[prop];
}
var getVariables = function () {
return placeholders.map(function (item) {
return item.replace(':', '');
});
};
var withTokenReqCallback = function (err, httpResponse, body) {
if (err) {
self.defer.reject(err);
} else if (httpResponse.statusCode === 401) {
self.getAccessToken().then(directReq);
} else if (httpResponse.statusCode !== 200) {
self.defer.reject(body);
} else {
self.defer.resolve(body);
}
var updateRoute = function () {
var variables = getVariables();
variables.forEach(function (value, index) {
if (params[value]) {
route = route.replace(placeholders[index], params[value]);
delete params[value];
}
});
};
var directReq = function () {
request.post(postParams.call(self, object,
constants.ENDPOINTS[route]), directReqCallback);
var getQueryString = function () {
var keys = Object.keys(params);
var initial = keys.length >= 1 ? '?' : '';
return keys.reduce(function (previous, current, index, array) {
var next = (index === array.length - 1) ? '' : '&';
return [previous, current, '=',
params[current], next
].join('');
}, initial);
};
var withTokenReq = function () {
request.post(postParams.call(self, object,
constants.ENDPOINTS[route]), withTokenReqCallback);
updateRoute();
query = getQueryString();
var req = {
url: [this.options.baseUrl, route, query].join(''),
json: true,
body: this.body
};
if (!this.accessToken) {
this.getAccessToken().then(directReq);
} else {
withTokenReq();
if (this.accessToken) {
req.auth = {
bearer: this.accessToken
};
}
return this.defer.promise;
return req;
};
GnEndpoints.prototype.getAccessToken = function () {
GnEndpoints.prototype.withTokenReqCallback = function (err, httpResponse, httpResponseBody) {
var self = this;
return this.gnAuth.getAccessToken().then(function (response) {
self.accessToken = response.access_token;
return self.accessToken;
}).catch(function (err) {
return err;
});
var response = self.getResponse(httpResponse, httpResponseBody);
if (err) {
self.defer.reject(err);
} else if (httpResponse.statusCode === 401) {
self.getAccessToken().then(self.directReq.bind(self));
} else if (httpResponse.statusCode !== 200) {
self.defer.reject(response);
} else {
self.defer.resolve(response);
}
};
GnEndpoints.prototype.run = function (endpoint, options) {
return this.post(options, endpoint);
GnEndpoints.prototype.directReqCallback = function (err, httpResponse, bodyResponse) {
var response = this.getResponse(httpResponse, bodyResponse);
if (err) {
this.defer.reject(err);
} else if (httpResponse.statusCode !== 200) {
this.defer.reject(response);
} else {
this.defer.resolve(response);
}
};
module.exports = GnEndpoints;

@@ -8,3 +8,7 @@ 'use strict';

function GnSdk(options) {
this.endpoints = new GnEndpoints(options);
if (options.urls) {
constants.URL = options.urls;
}
this.options = options;
}

@@ -15,4 +19,5 @@

.forEach(function (key) {
GnSdk.prototype[key] = function (obj) {
return this.endpoints.run(key, obj);
GnSdk.prototype[key] = function (params, body) {
var endpoints = new GnEndpoints(this.options, constants);
return endpoints.run(key, params, body);
};

@@ -19,0 +24,0 @@ });

{
"name": "gn-api-sdk-node",
"description": "Module for integration with Gerencianet API",
"version": "0.0.10",
"version": "0.0.11",
"author": "Francisco Carvalho <f.thiene@gmail.com>",

@@ -44,2 +44,2 @@ "license": "MIT",

}
}
}

@@ -70,3 +70,3 @@ # gn-api-sdk-node

```bash
$ git clone git@github.com:franciscotfmc/gn-api-sdk-node.git
$ git clone git@github.com:gerencianet/gn-api-sdk-node.git
$ cd gn-api-sdk-node/docs/examples

@@ -101,20 +101,43 @@ $ npm install

## Additional docs
## Additional documentation
- [Creating charges with shippings](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-shippings.md)
- [Creating charges associated to customers](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-customer.md)
- [Creating charges with marketplace](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-marketplace.md)
- [Sending charges via post office service](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/charge-with-post-office.md)
- [Associating customers to charges subsequently](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/associate-customer.md)
- [Subscriptions](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/subscriptions.md)
- [Detailing charges and subscriptions](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/detailing.md)
- [Creating and retrieving carnets](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/carnets.md)
- [Listing installments in advance](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/payment-data.md)
- [Paying a charge](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/payments.md)
- [Notifications](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/notifications.md)
- [All in one](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/docs/all-in-one.md)
### Charges
- [Creating charges](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charges.md)
- [Paying a charge](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-payment.md)
- [Detailing charges](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-detailing.md)
- [Updating informations](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-update.md)
### Carnets
- [Creating carnets](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/carnets.md)
- [Detailing carnets](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/carnet-detailing.md)
- [Updating informations](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/carnet-update.md)
### Subscriptions
- [Creating subscriptions](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/subscriptions.md)
- [Paying a subscription](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/subscription-payment.md)
- [Detailing subscriptions](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/subscription-detailing.md)
- [Updating informations](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/subscription-update.md)
### Marketplace
- [Creating a marketplace](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/charge-with-marketplace.md)
### Notifications
- [Getting notifications](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/notifications.md)
### Payments
- [Getting installments](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/installments.md)
### All in one
- [Usage](https://github.com/gerencianet/gn-api-sdk-node/tree/master/docs/all-in-one.md)
## Changelog
[CHANGELOG](https://github.com/franciscotfmc/gn-api-sdk-node/tree/master/CHANGELOG.md)
[CHANGELOG](https://github.com/gerencianet/gn-api-sdk-node/tree/master/CHANGELOG.md)

@@ -121,0 +144,0 @@ ## License

@@ -32,21 +32,21 @@ 'use strict'

describe('GN', function() {
before(function() {
describe('GN', function () {
before(function () {
options = {
clientId: 'clientId',
clientSecret: 'clientSecret',
client_id: 'clientId',
client_secret: 'clientSecret',
baseUrl: constants.URL.sandbox
}
gn = new GnAuth(options);
gn = new GnAuth(options, constants);
});
describe('oauth authentication', function() {
it('should get an access token', function(done) {
describe('oauth authentication', function () {
it('should get an access token', function (done) {
var expected = nock(constants.URL.sandbox)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk);
gn.getAccessToken()
.then(function(response) {
.then(function (response) {
should(response)

@@ -56,11 +56,14 @@ .eql(accessTokenResponseOk)

})
.catch(function (err) {
console.log(err);
})
.done();
});
it('should reject invalid credentials', function(done) {
it('should reject invalid credentials', function (done) {
var expected = nock(constants.URL.sandbox)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(400, accessTokenResponseErr);
gn.getAccessToken().then(null, function(response) {
gn.getAccessToken().then(null, function (response) {
should(response)

@@ -73,10 +76,10 @@ .eql(accessTokenResponseErr)

it('should reject when an error occurs', function(done) {
requestStub.post = function(params, callback) {
it('should reject when an error occurs', function (done) {
requestStub.post = function (params, callback) {
callback(new Error('ops'));
}
_gn = new _GnAuth(options);
_gn = new _GnAuth(options, constants);
_gn.getAccessToken().then(null, function(response) {
_gn.getAccessToken().then(null, function (response) {
should(response)

@@ -83,0 +86,0 @@ .eql(new Error('ops'))

@@ -21,4 +21,3 @@ 'use strict'

var _GnEndpoints = proxyquire('../lib/gn-endpoints', {
'request': requestStub,
'constants': constants
'request': requestStub
});

@@ -62,4 +61,4 @@

options = {
clientId: 'clientId',
clientSecret: 'clientSecret',
client_id: 'clientId',
client_secret: 'clientSecret',
sandbox: false

@@ -72,5 +71,5 @@ }

gn = new GnEndpoints(options);
gn = new GnEndpoints(options, constants);
options.sandbox = true;
_gn = new _GnEndpoints(options);
_gn = new _GnEndpoints(options, constants);

@@ -84,3 +83,3 @@ nock.cleanAll();

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk);

@@ -99,3 +98,3 @@

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(401, accessTokenResponseErr);

@@ -117,8 +116,8 @@

expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(200, createChargeResponseOk);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(function (response) {

@@ -134,8 +133,8 @@ response.should.be.eql(createChargeResponseOk)

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(400, createChargeResponseError);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(null, function (response) {

@@ -150,3 +149,3 @@ response.should.be.eql(createChargeResponseError)

it('should reject promise when an error occurs', function (done) {
_gn.run({})
_gn.run('createCharge')
.then(null, function (response) {

@@ -170,10 +169,10 @@ response.should.be.eql(new Error('ops'));

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(401, createChargeResponseError)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(200, createChargeResponseOk);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(function (response) {

@@ -188,12 +187,33 @@ response.should.be.eql(createChargeResponseOk);

it('should resolve promise with raw response', function (done) {
var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge.route)
.reply(401, createChargeResponseError)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk)
.post(constants.ENDPOINTS.createCharge.route)
.reply(200, createChargeResponseOk);
gn.options.raw_response = true;
gn.run('createCharge', {}, {})
.then(function (response) {
response.headers.should.not.be.undefined;
should(gn.getAccessToken.callCount).equal(1);
expected.done();
done();
})
.done();
});
it('should reject promise', function (done) {
var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(401, createChargeResponseError)
.post(constants.ENDPOINTS.authorize)
.post(constants.ENDPOINTS.authorize.route)
.reply(200, accessTokenResponseOk)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(500, createChargeResponseError);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(null, function (response) {

@@ -218,6 +238,6 @@ response.should.be.eql(createChargeResponseError);

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.replyWithError('server is off');
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(null, function (response) {

@@ -240,6 +260,6 @@ should(gn.getAccessToken.callCount).equal(0);

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(200, createChargeResponseOk);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(function (response) {

@@ -256,6 +276,6 @@ response.should.be.eql(createChargeResponseOk);

var expected = nock(constants.URL.production)
.post(constants.ENDPOINTS.createCharge)
.post(constants.ENDPOINTS.createCharge.route)
.reply(400, createChargeResponseOk);
gn.post({}, 'createCharge')
gn.run('createCharge', {}, {})
.then(null, function (response) {

@@ -272,3 +292,42 @@ response.should.be.eql(createChargeResponseOk);

});
});
});
describe('url params', function () {
it('should map params object to url specific placeholders', function (done) {
gn.params = {
id: 1,
other: 'other'
}
var params = gn.getParams('/charge/:id/:other');
should(params.url)
.equal('http://localhost/charge/1/other');
done();
});
it('should map spare params attributes to a query string', function (done) {
gn.params = {
id: 1,
name: 'name',
lastName: 'lastName'
}
var params = gn.getParams('/charge/:id');
should(params.url)
.equal('http://localhost/charge/1?name=name&lastName=lastName');
done();
});
it('should not map empy params', function (done) {
gn.params = {
id: 1
}
var params = gn.getParams('/charges/:missing');
should(params.url)
.equal('http://localhost/charges/:missing?id=1');
done();
});
});
}); //endpoints
}); //GN

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc