Credit card implementation with Node Js and Iyzico package

Photo by CardMapr on Unsplash

Credit card implementation with Node Js and Iyzico package

How to create a Node Js server that communicates with an online payment provider and responds back to our frontend

Hello, welcome to my first blog post :)

Today I will talk about how to implement an online payment server. There are many online payment providers, they work very similar, but I will integrate with Iyzico which is the largest online payment provider here in Turkey. They have good documentation and a sandbox to play with so lets play.

What we will need

On the Iyzico you need to register for a sandbox account, then get your sandbox api key and secret key. On the coding side we will use Node Js and Vs code.

Lets start

Create a folder, I name mine as 'Iyzico Node Js'. Then open the folder in Vs Code. Open terminal and type

npm init

This will start the process for the node project and will ask some questions to create the package.json file

Lets get the packages we need, in your terminal execute the following

npm i nodemon -D
npm i express
npm i dotenv
npm i iyzipay
npm i morgan

Then edit your package.json to have a start and a dev script. At the end package.json should look like this

Screen Shot 2022-04-16 at 17.36.08.png

Ok, now its time to create our folders and files. We will need an app.js file as entry point for our server. We will need a .env file to store our variables. Also we will create payment.js and payment_controller.js files. We also will need a .gitignore file as we will put our keys to the .env file and we want to aviod accidentally pushing these to a public repo. At the end our project will look like this.

Screen Shot 2022-04-20 at 10.32.09.png

Lets code

First create our server. Our app.js should look like this.

const dotenv = require('dotenv').config();
const express = require('express');
const morgan = require('morgan');
const paymentRouter = require('./routes/payment');

const app = express();
app.use(express.json());

// dev logging middleware
if(process.env.NODE_ENV === 'development'){
    app.use(morgan('dev'));
}
// routes
app.get('/api', (req,res) => res.json({message:"IYZICO server"}));
app.use('/api/payment', paymentRouter);
// server
app.listen(process.env.PORT, () => console.log('Server is active'));

Now our server is created. We need to give environment variables to the .env file

PORT = 3000
NODE_ENV = development

IYZIPAY_URI = https://sandbox-api.iyzipay.com
IYZIPAY_API_KEY = 'your api key here'
IYZIPAY_SECRET_KEY = 'your secret key here'

We need to implement our .gitignore file now as we have keys in the .env file. Our .gitignore file should look like this

# dependency directories
node_modules/
# dotenv environment variables file
.env

We need to create our route. This we need to implement it in payment.js file

const router = require('express').Router();
const PaymentController = require('../controllers/payment_controller');

router.post('/iyzico',  PaymentController.iyzico);

module.exports = router;

All looks good, as final we need to implement our payment_controller.js file

var Iyzipay = require('iyzipay');

const iyzico = (req, res, next) => {

    const { conversationId, 
        basketId, 
        paymentGroup, 
        paymentChannel, 
        locale, 
        installment, 
        currency, 
        basketItems, 
        billingAddress, 
        shippingAddress, 
        paymentCard, 
        buyer, 
        price, 
        paidPrice } = req.body;

    console.log(req.body);

    var iyzipay = new Iyzipay();
    var request = {
        locale: locale,
        conversationId: conversationId,
        price: price,
        paidPrice: paidPrice,
        currency: currency,
        installment: installment,
        basketId: basketId,
        paymentChannel: paymentChannel,
        paymentGroup: paymentGroup,
        paymentCard: paymentCard,
        buyer: buyer,
        shippingAddress: shippingAddress,
        billingAddress: billingAddress,
        basketItems: basketItems,
    };

    iyzipay.payment.create(request, function (err, result) {
        if (err) {
            console.log(err.body);
        };        
        console.log(request);
        res.status(200).json(result);
        console.log(result);
    });
}

module.exports = {
    iyzico
}

All set, now type this to console to start server

npm run dev

If we go to localhost:3000/api we should see {"message":"IYZICO server"} message, and on console server started message. For testing I am going to use Thunder Client which works like Postman but better as it operates within Vs Code. You can use any of them. In Vs Code install Thunder Client extension and make a new post request to localhost:3000/api/payment/iyzico with a body of sample json request found in docs This should return a success response like below

Screen Shot 2022-04-19 at 11.01.03.png

Now we need to implement a frontend to test our server. Up next will create a credit card application with Flutter and Iyzico.