npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bkmexpress

v2.1.2

Published

Nodejs api for bkm express payment system (Turkey)

Downloads

75

Readme

BKM Express NPM version Downloads Build Status Dependency Status Coverage

BKM Express payment system sdk (Turkey) for Node.js

v1.x.x dokümantasyon için Buraya tıklayınız / For v1.x.x documentation, go HERE

Install

npm install bkmexpress

Sample

Express Router

const express = require("express");
const router = express.Router();
const debug = require("debug")("test:app");
const fs = require("fs");

const Bex = require("bkmexpress");

const privateKey = "PRIVATE-KEY";
const merchantId = "YOUR-MERCHANT-ID"; 

// Kendinize özel end-point url'leri
const urlBase = "YOUR-SERVER-ADDRESS";
const urlForInstallments = urlBase + "/bkm/installments";
const urlForNonce = urlBase + "/bkm/nonce";
// --

// Connection Token'ın Alınabilmesi için Gerekli Konfigürasyonun Yapılması
const config = Bex.BexPayment.startBexPayment(Bex.Environment.SANDBOX, merchantId, privateKey);

router.get("/", function (req, res, next) {
    const merchantService = new Bex.MerchantService(config);

    // Connection Token'ın Alınması
    merchantService.login()
        .then(function (loginResponse) {
            // Ticket Token'ı Üretilmesi
            const ticketResponse = merchantService.oneTimeTicket(loginResponse.Token, 1, urlForInstallments, urlForNonce);
            ticketResponse
                .then(function (response2) {

                    // Ticket Token'ın Ön Yüze İletilmesi
                    res.render("index", {
                        // Client SDK url'ini kendiniz manuel girebilirsiniz
                        // ama bu sekilde de yaparsaniz, her ortam degisiklikligi
                        // yaptiginizda(Sandbox, Production) url'i degistirmenize gerek kalmadan ortam icin dogru url'i verir.
                        baseUrl: config.BexApiConfiguration.BaseJs,
                        ticketId: response2.Token.ShortId,
                        ticketPath: response2.Token.Path,
                        ticketToken: response2.Token.Token
                    });
                })
                .catch(function (err) {
                    debug("merchantService/oneTimeTicket", "ticketResponse", err);
                    next(err);
                });
        })
        .catch(function (err) {
            debug("merchantService/login", "MerchantLoginResponse", err);
            next(err);
        });
});
router.post("/bkm/installments", function (req, res) {
    // Endpoint'in Oluşturulması
    if (req.body.bin && req.body.totalAmount && req.body.ticketId && req.body.signature) {

        const request = new Bex.InstallmentRequest(req.body.bin, req.body.totalAmount, req.body.ticketId, req.body.signature);
        const response = new Bex.InstallmentsResponse();

        if (Bex.EncryptionUtil.verifyBexSign(request.TicketId, request.Signature)) {

            let amount = Bex.MoneyUtils.toNumber(request.TotalAmount);
            let allInstallmentsForEveryBin = {};

            for (let binAndBank of request.binAndBanks()) {
                let bankCode = binAndBank.BankCode;
                let installmentsForThisBin = [];

                // baska bankalar icin vpos hazirlama
                switch (bankCode) {
                    case Bex.Banks.ZIRAATBANK:
                        // Eger istenilen banka icin pos'unuz mevcutsa
                        vposConfig.BankIndicator = Bex.Banks.ZIRAATBANK;
                        vposConfig.VposUserId = "bkmtest";
                        vposConfig.VposPassword = "TEST1691";
                        vposConfig.addExtra("ClientId", "190001691");
                        vposConfig.addExtra("storekey", "TRPS1691");
                        vposConfig.addExtra("orderId", "9073194");
                        vposConfig.ServiceUrl = "http://srvirt01:7200/ziraat";
                        break;

                    case Bex.Banks.AKBANK:
                    default:
                        // Yoksa varsayılan bankanız
                        vposConfig.BankIndicator = Bex.Banks.AKBANK;
                        vposConfig.VposUserId = "akapi";
                        vposConfig.VposPassword = "TEST1234";
                        vposConfig.addExtra("ClientId", "100111222");
                        vposConfig.addExtra("storekey", "TEST1234");
                        vposConfig.ServiceUrl = "http://srvirt01:7200/akbank";
                        break;
                }

                let installment = new Bex.Installment("1", Bex.MoneyUtils.toTRY(amount), Bex.MoneyUtils.toTRY(amount), Bex.EncryptionUtil.encryptWithBex(vposConfig));
                installmentsForThisBin.push(installment);
                // - end
                allInstallmentsForEveryBin[binAndBank.Bin] = installmentsForThisBin;
            }

            response.Installments = allInstallmentsForEveryBin;
            response.Status = "ok";
            response.Error = "";
            res.json(response);
        }
        else {
            response.Status = "fail";
            response.Error = "signature verification failed";
            res.json(response);
        }
    }
    else {
        const response = new Bex.InstallmentsResponse();
        response.Status = "fail";
        response.Error = "RequestBody fields cannot be null or signature verification failed";
        res.json(response);
    }

});

function checkPayment(nonceRequest) {
    const merchantNonceResponse = new Bex.MerchantNonceResponse();
    const merchantService = new Bex.MerchantService(config);
    merchantService.login()
        .then(function (loginResponse) {
            if (Bex.EncryptionUtil.verifyBexSign(nonceRequest.TicketId, nonceRequest.Signature)) {
                // İşlemin hala geçerliliğinin korunup, korumadığı kontrol edilir.
                // ....
                // basarili nonce cevabı

                merchantNonceResponse.Result = true;
                merchantNonceResponse.Nonce = nonceRequest.Token;
                merchantNonceResponse.Id = nonceRequest.Path;
                merchantNonceResponse.Message = "YOUR-MESSAGE-WILL-BE-STORED-IN-BKM";

                merchantService.sendNonceResponse(loginResponse.Token, merchantNonceResponse)
                    .then(function (response) {
                        debug("merchantService/sendNonceResponse", "NonceResultResponse", response);
                    })
                    .catch(function (err) {
                        debug("merchantService/sendNonceResponse", "NonceResultResponse", err);
                    });
            }
            else {
                merchantNonceResponse.Result = false;
                merchantNonceResponse.Nonce = nonceRequest.Token;
                merchantNonceResponse.Id = nonceRequest.Path;
                merchantNonceResponse.Message = "Signature verification failed";
                merchantService.sendNonceResponse(loginResponse.Token, merchantNonceResponse)
                    .then(function (response) {
                        debug("merchantService/sendNonceResponse", "NonceResultResponse", response);
                    })
                    .catch(function (err) {
                        debug("merchantService/sendNonceResponse", "NonceResultResponse", err);
                    });
            }
        })
        .catch(function (err) {
            debug("merchantService/login", "MerchantLoginResponse", err);
        });
}

router.post("/bkm/nonce", function (req, res) {
    const response = new Bex.NonceReceivedResponse();
    if (req.body.id && req.body.path && req.body.issuer && req.body.approver && req.body.token && req.body.signature && req.body.reply) {
        // asenkron işlemden önce Nonce isteğinin başarılı olarak alındığı iletilmelidir.
        response.Result = "ok";
        response.Data = "ok";
        res.json(response);

        const request = new Bex.NonceRequest(req.body.id, req.body.path, req.body.issuer, req.body.approver, req.body.token, req.body.signature, req.body.reply);
        // asenkron bir işlem ile işlem kontrolleri yapılmalıdır.
        checkPayment(request);
    }
    else {
        response.Result = "fail";
        response.Data = "fail";
        res.json(response);
    }
});
router.get("/result/:ticketPath", function (req, res) {
    // Ödeme İşlemi Sorgulama Endpoint'i Örneği
    const merchantService = new Bex.MerchantService(config);
    merchantService.login()
        .then(function (loginResponse) {
            merchantService.result(loginResponse.Token, req.params.ticketPath)
                .then(function (result) {
                    res.send(result);
                })
                .catch(function (err) {
                    res.send(err);
                });
        })
        .catch(function (err) {
            res.send(err);
        });
});

module.exports = router;

Template

doctype html
html
    head
        script(src="#{baseUrl}", type="text/javascript")
    body
        #payment-dropin
        script(type="text/javascript").
            // Client SDK'daki BKM Express ile Öde Butonunun Kullanımı
            var ticketIdForInit = "#{ticketId}";
            var ticketPathForInit = "#{ticketPath}";
            var ticketTokenForInit = "#{ticketToken}";

            Bex.init({"id": ticketIdForInit, "path": ticketPathForInit, "token": ticketTokenForInit}, "modal", {
                container: "payment-dropin",
                buttonSize: [135, 70],
                onCancel: function () {
                    console.log("ödeme işlemi iptal edildiğinde yapılacak işlemler için kullanılacak callback. Ticket'ı yenilemek gibi işlemleri bu callback içinde yapabilirsiniz.");
                    location.reload();
                },
                onComplete: function (status) {
                    console.log("ödeme tamamlandığında yapılacak işlemler için kullanılacak callback", status);
                    document.location.href = "/result/" + ticketPathForInit;
                }
            });

Debug

DEBUG=bkmexpress.test:*,bkmexpress:* npm start

Run Tests

npm test

Contributors

We welcome contributions of all kinds from anyone.