ramonhenrique / meteor-mercadopago

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MercadoPago SDK for Meteor JS

This library is a wrap of nodejs mercadopago sdk for meteor.

Install

$ meteor add ha:mercadopago

Basic Usage

Config

Get your clientID/ClientSecret or accessToken whether you use basic or custom checkout (See more in basic checkout and customized checkout). Add your credentials to your meteor config.

{
  "mercadopago": {
    "clientID": "yourClientID",
    "clientSecret": "yourClientSecret"
  }
}

Start meteor with the config file.

meteor --settings [settings.json]

If everything went fine you should have (server side) access to the variable MercadPago.

Sync, Promise and Callback

All methods of MercadoPago can be called in a synchronous or asynchronous way. All methods have a regular version (sync) and one starting with a lowdash (async). For example you have three ways of using the method createPreference.

Sync

MercadoPago.createPreference(p)

Callback (Asyc)

MercadoPago._createPreference(p).then(
  function (preference) {
    console.log(preference);
  },
  function (error) {
    console.log (error);
  });

Promise (Async)

MercadoPago._createPreference(p, function (err, preference){
  if (err) {
    console.log (err);
  } else {
    console.log (preference);
  }
});

Primitive access

You have access to the underlying node module and the current mercadopago instance in the following vars

MercadoPago._MP // Node module
Mercadopago.mp // Mercadopago node instance

You can checkout how to use the module in the official repo.

Basic checkout

Configure your credentials

{
  "mercadopago": {
    "clientID": "yourClientID",
    "clientSecret": "yourClientSecret"
  }
}

Preferences

Get an existent Checkout preference

MercadoPago.getPreference(preferenceID);

Create a Checkout preference

var preference = {
  "items": [
    {
      "title": "Test",
      "quantity": 1,
      "currency_id": "USD",
      "unit_price": 10.5
    }
  ]
};

MercadoPago.createPreference(preference);

Update an existent Checkout preference:

var preference = {
  "items": [
    {
      "title": "Test Modified",
      "quantity": 1,
      "currency_id": "USD",
      "unit_price": 20.4
    }
  ]
};

MercadoPago.updatePreference(preferenceID, preference);

Payments/Collections

Search for payments

var filters = {
  "id": null,
  "site_id": null,
  "external_reference": null
};

data = MercadoPago.searchPayment(filters);
console.log (JSON.stringify (data, null, 4);

Get payment data

data = MercadoPago.getPayment(paymentId);
console.log(JSON.stringify(data, null, 4));

Cancel (only for pending payments)

MercadoPago.cancelPayment(paymentID);

Refund (only for accredited payments)

MercadoPago.refundPayment(paymentID);

Customized checkout

Configure your credentials

{
  "mercadopago": {
    "accessToken": "yourAccessToken"
  }
}

Create payment

MercadoPago.post("/v1/payments", payment_data);

Create customer

MercadoPago.post("/v1/customers", {"email": "email@test.com"});

Get customer

MercadoPago.get("/v1/customers/CUSTOMER_ID");

Generic methods

You can access any other resource from the MercadoPago API using the generic methods:

// Get a resource, with optional URL params. Also you can disable authentication for public APIs
MercadoPago.get("/resource/uri", [params], [authenticate=true]);

// Create a resource with "data" and optional URL params.
MercadoPago.post("/resource/uri", data, [params]);

// Update a resource with "data" and optional URL params.
MercadoPago.put("/resource/uri", data, [params]);

// Delete a resource with optional URL params.
MercadoPago.delete("/resource/uri", [params]);

For example, if you want to get the Sites list (no params and no authentication):

sites = MercadoPago.get("/sites", null, false);
console.log(sites);

About


Languages

Language:JavaScript 100.0%