Skip to content

Commit

Permalink
Merge pull request #164 from mercadopago/mercadopago/request-config
Browse files Browse the repository at this point in the history
*Adding custom configuration per request (override access token, custom headers)
*Adding Card operations
*Adding Card Token operations
*Adding Refund resource
*Adding DiscountCampaign resource
*Adjust payment refund
*Add payment capture
*Changing Customer address street_number from string to integer
  • Loading branch information
delias-silva authored Sep 20, 2019
2 parents 78495ac + a477e12 commit ce87928
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 25 deletions.
10 changes: 9 additions & 1 deletion lib/mercadopago.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ var moneyRequestsModule = require('./resources/moneyRequests');
var customersModule = require('./resources/customers');
var ipnModule = require('./resources/ipn');
var connectModule = require('./resources/connect');
var cardModule = require('./resources/card');
var cardTokenModule = require('./resources/cardToken');
var refundModule = require('./resources/refund');
var discountCampaignModule = require('./resources/discountCampaign');
var sdk;
var resources;

Expand Down Expand Up @@ -54,7 +58,11 @@ resources = {
customers: customersModule,
ipn: ipnModule,
connect: connectModule,
money_requests: moneyRequestsModule
money_requests: moneyRequestsModule,
card: cardModule,
card_token: cardTokenModule,
refund: refundModule,
discount_campaign: discountCampaignModule
};

// Adding modules to prototype and a static method - This is going to be delete
Expand Down
102 changes: 102 additions & 0 deletions lib/models/cardModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* istanbul ignore next */
module.exports = {
additionalProperties: false, // Fails if an extra param is added to the model
properties: {
id: {
type: 'string'
},
token: {
type: 'string'
},
customer_id: {
type: 'string'
},
expiration_month: {
type: 'integer'
},
expiration_year: {
type: 'integer'
},
first_six_digits: {
type: 'string'
},
last_four_digits: {
type: 'string'
},
payment_method: {
type: 'object',
properties: {
id: {
type: 'string'
},
name: {
type: 'string'
},
payment_type_id: {
type: 'string'
},
thumbnail: {
type: 'string'
},
secure_thumbnail: {
type: 'string'
}
}
},
security_code: {
type: 'object',
properties: {
length: {
type: 'integer'
},
card_location: {
type: 'string'
}
}
},
issuer: {
type: 'object',
properties: {
id: {
type: 'string'
},
name: {
type: 'string'
}
}
},
cardholder: {
type: 'object',
properties: {
name: {
type: 'string'
},
identification: {
type: 'object',
properties: {
number: {
type: 'string'
},
sub_type: {
type: 'string'
},
type: {
type: 'string'
}
}
}
}
},
date_created: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
date_last_updated: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
payment_method_id: {
type: 'string'
}
}
};
42 changes: 42 additions & 0 deletions lib/models/cardTokenModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* istanbul ignore next */
module.exports = {
additionalProperties: false, // Fails if an extra param is added to the model
properties: {
id: {
type: 'string'
},
customer_id: {
type: 'string'
},
card_id: {
type: 'string'
},
security_code: {
type: 'string'
},
status: {
type: 'string'
},
date_created: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
date_last_updated: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
date_due: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
luhn_validation: {
type: 'boolean'
},
live_mode: {
type: 'boolean'
},
require_esc: {
type: 'boolean'
}
}
};
2 changes: 1 addition & 1 deletion lib/models/customersModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
type: 'string'
},
street_number: {
type: 'string'
type: 'integer'
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions lib/models/discountCampaignModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* istanbul ignore next */
module.exports = {
additionalProperties: false, // Fails if an extra param is added to the model
properties: {
id: {
type: 'string'
},
name: {
type: 'string'
},
percent_off: {
type: 'number'
},
amount_off: {
type: 'number'
},
coupon_amount: {
type: 'number'
},
currency_id: {
type: 'string'
}
}
};
36 changes: 36 additions & 0 deletions lib/models/refundModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* istanbul ignore next */
module.exports = {
additionalProperties: false, // Fails if an extra param is added to the model
properties: {
id: {
type: 'string'
},
payment_id: {
type: 'string'
},
amount: {
type: 'number'
},
source: {
type: 'object',
properties: {
id: {
type: 'string'
},
name: {
type: 'string'
},
type: {
type: 'string'
}
}
},
date_created: {
type: 'string',
pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}-[0-9]{2}:[0-9]{2}$'
},
unique_sequence_number: {
type: 'string'
}
}
};
21 changes: 17 additions & 4 deletions lib/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,31 @@ requestManager.getUserCredentials = function (clientSecret, authorizationCode, r
requestManager.buildRequest = function (options) {
var req = {};
var schemaErrors = [];
var headersNames = [];
var headerName;
var i;

req.uri = (options.base_url) ? options.base_url + options.path : configurations.getBaseUrl() + options.path;
req.method = options.method;
req.headers = {
'user-agent': configurations.getUserAgent(),
accept: (options.headers && options.headers.accept) ? options.headers.accept : requestManager.JSON_MIME_TYPE,
'content-type': (options.headers && options.headers['content-type'])
? options.headers['content-type'] : requestManager.JSON_MIME_TYPE
accept: requestManager.JSON_MIME_TYPE,
'content-type': requestManager.JSON_MIME_TYPE
};
req.qs = (options.config && options.config.qs) ? options.config.qs : {}; // Always set the querystring object
req.json = true; // Autoparse the response to JSON

if (options.config && options.config.headers && typeof options.config.headers === 'object') {
headersNames = Object.keys(options.config.headers);
for (i = 0; i < headersNames.length; i += 1) {
headerName = headersNames[i];
if (headerName !== 'user-agent' && headerName !== 'x-idempotency-key'
&& Object.prototype.hasOwnProperty.call(options.config.headers, headerName)) {
req.headers[headerName] = options.config.headers[headerName];
}
}
}

if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
// Set idempotency header if the resource needs idempotency of the config specified one
if (options.idempotency || (options.config && options.config.idempotency)) {
Expand All @@ -280,7 +293,7 @@ requestManager.buildRequest = function (options) {
}

// Always add the access_token to the querystring
req.qs.access_token = options.access_token;
req.qs.access_token = (options.config && options.config.access_token) ? options.config.access_token : options.access_token;
// Requires SSL certificates be valid
req.strictSSL = true;

Expand Down
35 changes: 35 additions & 0 deletions lib/resources/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var requestManager = require('../request-manager');
var cardModel = require('../models/cardModel');

var card = module.exports = {
schema: cardModel
};

card.all = requestManager.describe({
path: '/v1/customers/:customer_id/cards',
method: 'GET'
});

card.get = requestManager.describe({
path: '/v1/customers/:customer_id/cards/:id',
method: 'GET'
});

card.findById = card.get;

card.create = requestManager.describe({
path: '/v1/customers/:customer_id/cards',
method: 'POST'
});

card.save = card.create;

card.update = requestManager.describe({
path: '/v1/customers/:customer_id/cards',
method: 'PUT'
});

card.delete = requestManager.describe({
path: '/v1/customers/:customer_id/cards/:id',
method: 'DELETE'
});
20 changes: 20 additions & 0 deletions lib/resources/cardToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var requestManager = require('../request-manager');
var cardTokenModel = require('../models/cardTokenModel');

var cardToken = module.exports = {
schema: cardTokenModel
};

cardToken.get = requestManager.describe({
path: '/v1/card_tokens/:id',
method: 'GET'
});

cardToken.findById = cardToken.get;

cardToken.create = requestManager.describe({
path: '/v1/card_tokens',
method: 'POST'
});

cardToken.save = cardToken.create;
11 changes: 11 additions & 0 deletions lib/resources/discountCampaign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var requestManager = require('../request-manager');
var discountCampaignModel = require('../models/discountCampaignModel');

var discountCampaign = module.exports = {
schema: discountCampaignModel
};

discountCampaign.get = requestManager.describe({
path: '/v1/discount_campaigns',
method: 'GET'
});
Loading

0 comments on commit ce87928

Please sign in to comment.