├── .gitignore ├── api ├── fetch.js ├── payment-controller.js └── payment-init-data-process.js ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /node_modules 3 | 4 | -------------------------------------------------------------------------------- /api/fetch.js: -------------------------------------------------------------------------------- 1 | const fetch = require("node-fetch"); 2 | const httpCall = ({url, method = 'POST', data = {}}) => { 3 | // Default options are marked with * 4 | return fetch(url, { 5 | method: 'POST', // *GET, POST, PUT, DELETE, etc. 6 | mode: 'cors', // no-cors, cors, *same-origin 7 | cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached 8 | credentials: 'same-origin', // include, *same-origin, omit 9 | redirect: 'follow', // manual, *follow, error 10 | referrer: 'no-referrer', // no-referrer, *client 11 | ...["POST", 'PUT', "PATCH", "UPDATE"].includes(method) && {body: data}, // body data type must match "Content-Type" header 12 | }).then(response => response.json()).catch(err => err); 13 | } 14 | module.exports = httpCall; 15 | -------------------------------------------------------------------------------- /api/payment-controller.js: -------------------------------------------------------------------------------- 1 | const httpCall = require("./fetch"); 2 | const paymentInitDataProcess = require("./payment-init-data-process"); 3 | 4 | 5 | class SslCommerzPayment { 6 | 7 | constructor(store_id, store_passwd, live = false) { 8 | this.baseURL = `https://${live ? 'securepay' : 'sandbox'}.sslcommerz.com`; 9 | this.initURL = this.baseURL + '/gwprocess/v4/api.php'; 10 | this.validationURL = this.baseURL + '/validator/api/validationserverAPI.php?'; 11 | this.refundURL = this.baseURL + '/validator/api/merchantTransIDvalidationAPI.php?'; 12 | this.refundQueryURL = this.baseURL + '/validator/api/merchantTransIDvalidationAPI.php?'; 13 | this.transactionQueryBySessionIdURL = this.baseURL + '/validator/api/merchantTransIDvalidationAPI.php?'; 14 | this.transactionQueryByTransactionIdURL = this.baseURL + '/validator/api/merchantTransIDvalidationAPI.php?'; 15 | this.store_id = store_id; 16 | this.store_passwd = store_passwd; 17 | } 18 | 19 | init(data, url = false, method = "POST") { 20 | data.store_id = this.store_id; 21 | data.store_passwd = this.store_passwd; 22 | return httpCall({url: url || this.initURL, method: method || "POST", data: paymentInitDataProcess(data)}); 23 | } 24 | 25 | validate(data, url = false, method = "GET") { 26 | return httpCall({ 27 | url: url || this.validationURL + `val_id=${data.val_id}&store_id=${this.store_id}&store_passwd=${this.store_passwd}&v=1&format=json`, 28 | method: method || "GET" 29 | }); 30 | } 31 | 32 | initiateRefund(data, url = false, method = "GET") { 33 | return httpCall({ 34 | url: url || this.refundURL + `refund_amount=${data.refund_amount}&refund_remarks=${data.refund_remarks}&bank_tran_id=${data.bank_tran_id}&refe_id=${data.refe_id}&store_id=${this.store_id}&store_passwd=${this.store_passwd}&v=1&format=json`, 35 | method: method || "GET" 36 | }); 37 | } 38 | 39 | refundQuery(data, url = false, method = "GET") { 40 | return httpCall({ 41 | url: url || this.refundQueryURL + `refund_ref_id=${data.refund_ref_id}&store_id=${this.store_id}&store_passwd=${this.store_passwd}&v=1&format=json`, 42 | method: method || "GET" 43 | }); 44 | } 45 | 46 | transactionQueryBySessionId(data, url = false, method = "GET") { 47 | return httpCall({ 48 | url: url || this.transactionQueryBySessionIdURL + `sessionkey=${data.sessionkey}&store_id=${this.store_id}&store_passwd=${this.store_passwd}&v=1&format=json`, 49 | method: method || "GET" 50 | }); 51 | } 52 | 53 | transactionQueryByTransactionId(data, url = false, method = "GET") { 54 | return httpCall({ 55 | url: url || this.transactionQueryByTransactionIdURL + `tran_id=${data.tran_id}&store_id=${this.store_id}&store_passwd=${this.store_passwd}&v=1&format=json`, 56 | method: method || "GET" 57 | }); 58 | } 59 | } 60 | 61 | module.exports = SslCommerzPayment; 62 | 63 | 64 | -------------------------------------------------------------------------------- /api/payment-init-data-process.js: -------------------------------------------------------------------------------- 1 | const FormData = require('form-data'); 2 | 3 | const paymentInitDataProcess = (data) => { 4 | let postData = {}; 5 | /* Integration Required Parameters */ 6 | //required// 7 | postData['store_id'] = data.store_id; 8 | postData['store_passwd'] = data.store_passwd; 9 | postData['productcategory'] = data.productcategory; 10 | postData['tran_id'] = data.tran_id; 11 | postData['total_amount'] = data.total_amount; 12 | postData['currency'] = data.currency; 13 | postData['success_url'] = data.success_url; 14 | postData['fail_url'] = data.fail_url; 15 | postData['cancel_url'] = data.cancel_url; 16 | //optional// 17 | postData['ipn_url'] = data.ipn_url; 18 | postData['multi_card_name'] = data.multi_card_name; 19 | postData['allowed_bin'] = data.allowed_bin; 20 | 21 | /* Parameters to Handle EMI Transaction */ 22 | // required// 23 | postData['emi_option'] = data.emi_option; 24 | //optional// 25 | postData['emi_max_inst_option'] = data.emi_max_inst_option; 26 | postData['emi_selected_inst'] = data.emi_selected_inst; 27 | 28 | /* Customer Information */ 29 | //required 30 | postData['cus_name'] = data.cus_name; 31 | postData['cus_email'] = data.cus_email; 32 | postData['cus_add1'] = data.cus_add1; 33 | postData['cus_add2'] = data.cus_add2; 34 | postData['cus_city'] = data.cus_city; 35 | postData['cus_state'] = data.cus_state; 36 | postData['cus_postcode'] = data.cus_postcode; 37 | postData['cus_country'] = data.cus_country; 38 | postData['cus_phone'] = data.cus_phone; 39 | //optional 40 | postData['cus_fax'] = data.cus_fax; 41 | 42 | /* Shipment Information */ 43 | //required 44 | postData['shipping_method'] = data.shipping_method; 45 | postData['num_of_item'] = data.num_of_item; 46 | //optional 47 | postData['ship_name'] = data.ship_name; 48 | postData['shipcity'] = data.shipcity; 49 | postData['ship_add1'] = data.ship_add1; 50 | postData['ship_add2'] = data.ship_add2; 51 | postData['ship_city'] = data.ship_city; 52 | postData['ship_state'] = data.ship_state; 53 | postData['ship_postcode'] = data.ship_postcode; 54 | postData['ship_country'] = data.ship_country; 55 | 56 | /* Product Information */ 57 | //required 58 | postData['product_name'] = data.product_name; 59 | postData['product_category'] = data.product_category; 60 | postData['product_profile'] = data.product_profile; 61 | //optional 62 | postData['hours_till_departure'] = data.hours_till_departure; 63 | postData['flight_type'] = data.flight_type; 64 | postData['pnr'] = data.pnr; 65 | postData['journey_from_to'] = data.journey_from_to; 66 | postData['third_party_booking'] = data.third_party_booking; 67 | postData['hotel_name'] = data.hotel_name; 68 | postData['length_of_stay'] = data.length_of_stay; 69 | postData['check_in_time'] = data.check_in_time; 70 | postData['hotel_city'] = data.hotel_city; 71 | postData['product_type'] = data.product_type; 72 | postData['topup_number'] = data.topup_number; 73 | postData['country_topup'] = data.country_topup; 74 | postData['cart'] = data.cart; 75 | postData['product_amount'] = data.product_amount; 76 | postData['discount_amount'] = data.discount_amount; 77 | postData['convenience_fee'] = data.convenience_fee; 78 | 79 | /* Customized or Additional Parameters */ 80 | //optional 81 | postData['value_a'] = data.value_a; 82 | postData['value_b'] = data.value_b; 83 | postData['value_c'] = data.value_c; 84 | postData['value_d'] = data.value_d; 85 | 86 | const fdata = new FormData(); 87 | for (const a in postData) { 88 | fdata.append(a, postData[a] ? postData[a] : ''); 89 | } 90 | 91 | return fdata; 92 | } 93 | module.exports = paymentInitDataProcess; 94 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const SslCommerzPayment = require("./api/payment-controller"); 2 | 3 | class SSLCommerzPayment extends SslCommerzPayment { 4 | constructor(store_id, store_password, live = false) { 5 | super(store_id, store_password, live); 6 | } 7 | } 8 | 9 | module.exports = SSLCommerzPayment; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sslcommerz-lts", 3 | "description": "LTS package, Officially supported by SSLCommerz Integration Team.", 4 | "version": "1.1.0", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/sslcommerz/SSLCommerz-NodeJS.git" 12 | }, 13 | "keywords": [], 14 | "author": "SSLCommerz Integration Team", 15 | "license": "ISC", 16 | "bugs": { 17 | "email" : "integration@sslcommerz.com" 18 | }, 19 | "homepage": "https://github.com/sslcommerz/SSLCommerz-NodeJS.git#readme", 20 | "description": "", 21 | "dependencies": { 22 | "form-data": "2.5.0", 23 | "node-fetch": "2.6.1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # [SSLCommerz - Node.js Library](https://www.npmjs.com/package/sslcommerz-lts) 2 | 3 | LTS package, Officially supported by SSLCommerz Integration Team. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm i sslcommerz-lts 9 | 10 | ``` 11 | 12 | ## How to use: 13 | 14 | 15 | ### Initialize a Transaction 16 | ```js 17 | const express = require('express') 18 | const app = express() 19 | 20 | const SSLCommerzPayment = require('sslcommerz-lts') 21 | const store_id = '' 22 | const store_passwd = '' 23 | const is_live = false //true for live, false for sandbox 24 | 25 | const port = 3030 26 | 27 | //sslcommerz init 28 | app.get('/init', (req, res) => { 29 | const data = { 30 | total_amount: 100, 31 | currency: 'BDT', 32 | tran_id: 'REF123', // use unique tran_id for each api call 33 | success_url: 'http://localhost:3030/success', 34 | fail_url: 'http://localhost:3030/fail', 35 | cancel_url: 'http://localhost:3030/cancel', 36 | ipn_url: 'http://localhost:3030/ipn', 37 | shipping_method: 'Courier', 38 | product_name: 'Computer.', 39 | product_category: 'Electronic', 40 | product_profile: 'general', 41 | cus_name: 'Customer Name', 42 | cus_email: 'customer@example.com', 43 | cus_add1: 'Dhaka', 44 | cus_add2: 'Dhaka', 45 | cus_city: 'Dhaka', 46 | cus_state: 'Dhaka', 47 | cus_postcode: '1000', 48 | cus_country: 'Bangladesh', 49 | cus_phone: '01711111111', 50 | cus_fax: '01711111111', 51 | ship_name: 'Customer Name', 52 | ship_add1: 'Dhaka', 53 | ship_add2: 'Dhaka', 54 | ship_city: 'Dhaka', 55 | ship_state: 'Dhaka', 56 | ship_postcode: 1000, 57 | ship_country: 'Bangladesh', 58 | }; 59 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 60 | sslcz.init(data).then(apiResponse => { 61 | // Redirect the user to payment gateway 62 | let GatewayPageURL = apiResponse.GatewayPageURL 63 | res.redirect(GatewayPageURL) 64 | console.log('Redirecting to: ', GatewayPageURL) 65 | }); 66 | }) 67 | 68 | app.listen(port, () => { 69 | console.log(`Example app listening at http://localhost:${port}`) 70 | }) 71 | ``` 72 | 73 | ### Validate after successful transaction (inside success and ipn controller methods) 74 | ```js 75 | //sslcommerz validation 76 | 77 | app.get('/validate', (req, res) => { 78 | const data = { 79 | val_id:ADGAHHGDAKJ456454 //that you go from sslcommerz response 80 | }; 81 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 82 | sslcz.validate(data).then(data => { 83 | //process the response that got from sslcommerz 84 | // https://developer.sslcommerz.com/doc/v4/#order-validation-api 85 | }); 86 | }) 87 | ``` 88 | 89 | ### To initiate a refund through API 90 | ```js 91 | //SSLCommerz initiateRefund 92 | 93 | app.get('/initiate-refund', (req, res) => { 94 | const data = { 95 | refund_amount:10, 96 | refund_remarks:'', 97 | bank_tran_id:CB5464321445456456, 98 | refe_id:EASY5645415455, 99 | }; 100 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 101 | sslcz.initiateRefund(data).then(data => { 102 | //process the response that got from sslcommerz 103 | //https://developer.sslcommerz.com/doc/v4/#initiate-the-refund 104 | }); 105 | }) 106 | ``` 107 | 108 | ### Query the status of a refund request 109 | ```js 110 | //SSLCommerz refundQuery 111 | 112 | app.get('/refund-query', (req, res) => { 113 | const data = { 114 | refund_ref_id:SL4561445410, 115 | }; 116 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 117 | sslcz.refundQuery(data).then(data => { 118 | //process the response that got from sslcommerz 119 | //https://developer.sslcommerz.com/doc/v4/#initiate-the-refund 120 | }); 121 | }) 122 | ``` 123 | 124 | ### Query the status of a transaction (by Transaction ID) 125 | ```js 126 | //SSLCommerz transactionQueryByTransactionId 127 | //you also use this as internal method 128 | app.get('/transaction-query-by-transaction-id', (req, res) => { 129 | const data = { 130 | tran_id:AKHLAKJS5456454, 131 | }; 132 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 133 | sslcz.transactionQueryByTransactionId(data).then(data => { 134 | //process the response that got from sslcommerz 135 | //https://developer.sslcommerz.com/doc/v4/#by-session-id 136 | }); 137 | }) 138 | ``` 139 | 140 | ### Query the status of a transaction (by session ID) 141 | ```js 142 | //SSLCommerz transactionQueryBySessionId 143 | //you also use this as internal method 144 | app.get('/transaction-query-by-session-id', (req, res) => { 145 | const data = { 146 | sessionkey:AKHLAKJS5456454, 147 | }; 148 | const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live) 149 | sslcz.transactionQueryBySessionId(data).then(data => { 150 | //process the response that got from sslcommerz 151 | //https://developer.sslcommerz.com/doc/v4/#by-session-id 152 | }); 153 | }) 154 | ``` 155 | 156 | - Find more details in [SSLCommerz Developer's Guide](https://developer.sslcommerz.com/) 157 | - For any technical queries: integration@sslcommerz.com 158 | 159 | © 2019-2021 SSLCOMMERZ ALL RIGHTS RESERVED 160 | 161 | --------------------------------------------------------------------------------