Giter VIP home page Giter VIP logo

yandex-money-sdk-ext's Introduction

Build Status Coverage Status

NodeJS Yandex.Money API SDK

Yandex.Money API pages: Ru, En

Requirements

  • node >= 6.5

Getting started

Installation

npm install yandex-money-sdk-ext

Payments from the Yandex.Money wallet

Using Yandex.Money API requires following steps

  1. Obtain token URL and redirect user's browser to Yandex.Money service. Note: client_id, redirect_uri, client_secret are constants that you get, when register app in Yandex.Money API.
const YandexMoneySdk = require("yandex-money-sdk-ext");

const clientId = '/* yopur application ID */';
const scope = [
    'account-info',
    'operation-history',
    'operation-details',
    'payment-p2p',
    'incoming-transfers',
    'payment-shop'
];
const redirectURI = 'https://localhost';
const url = YandexMoneySdk.buildObtainTokenUrl(clientId, redirectURI, scope);
  1. After that, user fills Yandex.Money HTML form and user is redirected back to REDIRECT_URI?code=CODE.

  2. You should immediately exchange CODE with ACCESS_TOKEN.

const YandexMoneySdk = require("yandex-money-sdk-ext");
const api = new YandexMoneySdk();

const code = '/* code from query params */';
api.getAccessToken(clientId, code, redirectURI, clientSecret, (err, data) => {
  if (err) {
    // process error
  }
  const access_token = data.access_token;
  // save it to DB, config, etc..
});
  1. Now you can use Yandex.Money API.
const api = new YandexMoneySdk(access_token);

// Get account info (https://tech.yandex.ru/money/doc/dg/reference/account-info-docpage/)
api.accountInfo((err, data) => {
  if (err) {
    // process error
  }
  console.log(data);
  /* Data like:
  { account: '410012345678901',
    balance: 50.25,
    currency: '643',
    account_type: 'personal',
    identified: false,
    account_status: 'named',
    balance_details: { total: 50.25, available: 50.25, hold: 0.20 }
  }
   */
});

// Fetch last 3 records of operation history (https://tech.yandex.ru/money/doc/dg/reference/operation-history-docpage/)
api.operationHistory({}, (err, data) => {
  if (err) {
    // process error
  }
  console.log(data);
  /* Data like:
  { next_record: '3',
    operations:
    [
      {pattern_id: '337',
       operation_id: '546520000000011223',
       title: 'МегаФон (Россия): +7 926 *****66',
       amount: 20,
       direction: 'out',
       datetime: '2017-02-24T12:32:42Z',
       status: 'success',
       type: 'payment-shop',
       group_id: 'pattern_337',
       categories: [Object]
      },
      // ...
    ]}  
   */
});

// Make request payment and process it
const options = {
    "pattern_id": "p2p",
    "to": "410011161616877",
    "amount_due": "0.02",
    "comment": "test payment comment from yandex-money-nodejs",
    "message": "test payment message from yandex-money-nodejs",
    "label": "testPayment",
    "test_payment": true,
    "test_result": "success"
};
api.requestPayment(options, (err, data) => {
  if (err) {
    // process error
  }
  if (data.status !== "success") {
    // process failure
  }
  const request_id = data.request_id;

  api.processPayment({
    "request_id": request_id
  }, (err, data) => {
    if (err) {
      // process error
    }
    // process status
  });
});

Payments from bank cards without authorization

  1. Fetch instantce-id (ussually only once for every client. You can store result in DB).
// Create api without access token
const api = new YandexMoneySdk();
api.getInstanceId(clientId, (err, data) => {
  if (err) {
    // process error
  }
  const instanceId = data.instance_id;
  // save it to DB
});
  1. Make request payment
const options = {
    // pattern_id, etc..
};

YandexMoneySdk.requestExternalPayment(instanceId, options, (err, data) => {
  if (err) {
    // process error
  }
  const requestId = data.request_id;
});
  1. Process the request with process-payment.
externalPayment.process(instanceId, {request_id: requestId}, (err, data) => {
  if (err) {
    // process error
  }
  // process data
});

Request options

Request options are optional, the defaults is:

const api = new YandexMoneySdk("access_token", {
  url: 'https://money.yandex.ru',
  userAgent: 'Yandex.Money.SDK/NodeJS',
  proxy: '' // Optional proxy server like "http://username:password@localhost:3128"
});

Side notes

  1. Each API function recieves a callback in args err, data and response. Where err is equal to null when status of response is 2**, data is JSONed response and response is a full server response(you can check response.statusCode for example).

Running tests

  1. Clone this repo, install deps and devDeps.
  2. Create test/constants.js using test/constants.js.sample as a template.
  3. Run npm run test and check the output.

yandex-money-sdk-ext's People

Contributors

magdel avatar raymank26 avatar romkavt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.