Coder Social home page Coder Social logo

qiwi-shop's Introduction

Install

npm install qiwi-shop

About

Qiwi shop rest api module (qiwi.com)

Example

Dependencies

const QiwiShop = require("qiwi-shop");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();

app.use(bodyParser.json());

Creation of bills

const projectId = "0000001"; // shop id
const apiId = "000000001"; // api id
const apiPassword = "api"; // api password
const notificationPassword = "notification"; // notification password

const inFiveDays = new Date().getTime() + 1000 * 60 * 60 * 24 * 5;
const qiwi = new QiwiShop(projectId, apiId, apiPassword, notificationPassword);

// this action is optional
qiwi.beforeCreateBill = function(billId, data) {
    // you can save an information to db before request e.t.c.
    // you may return promise
}

app.post('/payments/bill/create/', (req, res, next) => {    
    qiwi.createBill({
        user: 'tel:+7910100100',
        amount: '10',
        ccy: 'RUB', 
        comment: 'recharge',
        lifetime: new Date(inFiveDays).toISOString(),
        pay_source: 'qw',
        prv_name: 'example.com' 
    }).then((result) => {
        if(!result || !result.response || result.response.result_code != 0) {
            throw new Error('Qiwi bill creation fail');
        }
        
        // redirect to payment (optional)
        res.redirect(qiwi.getPaymentUrl({ shop: qiwi.projectId, transaction: result.response.bill.bill_id }));
    });
});

Handling a notification

// notification handler 
const successHandler = (data, callback) => {
    // data === req.body    
    // save payment info or something else in a db e.t.c    
    // callback() or return promise
};

const errorHandler = (err, meta) => {
    // you can save something to a file, db e.t.c.
    // the operation must be synchronous or in the background 
};

let authenticationBySignature = false; // false == basic authentication

app.post('payments/notification/', qiwi.notify(successHandler, errorHandler, authenticationBySignature));

Description

You can write custom notification handler, but library version includes data/authentication validation and automatically send all headers in the necessary format

API

.constructor(projectId, apiId, apiPassword, notificationPassword)

you can find all arguments in your qiwi shop account

.getPaymentUrl([query])

returns qiwi bill creation url

.beforeCreateBill(billId, data)

will be called before a bill creation request, but after a hash creation

.createBill(data)

returns promise, create a bill, data options must comply with documentation
.user and .amount is required

.getBillStatus(billId)

returns promise, get a bill status

.cancelBill(billId)

returns promise, cancel a bill

.refundBill(billId, data)

returns promise, refunds a bill, data options must comply with documentation

.getRefundStatus(billId, refundId)

returns promise, gets a bill refund status

.checkNotificationAuthBasic(req)

checks a notification by basic authentication

.checkNotificationAuthSignature(req)

checks a notification by signature

.createXml(code)

creates xml string for response

.notify(fn, onError, [checkSignature])

qiwi notification handler, it is "connect" middleware

qiwi-shop's People

Contributors

ortexx avatar heeby avatar

Watchers

James Cloos 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.