├── .gitignore ├── LICENSE ├── README.md ├── README_react.md ├── README_vuejs.md ├── _env_local ├── package.json ├── shurjopay.js └── test ├── crud_test.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | logs 2 | *.log 3 | npm-debug.log* 4 | yarn-debug.log* 5 | yarn-error.log* 6 | lerna-debug.log* 7 | 8 | pids 9 | *.pid 10 | 11 | # Build tool 12 | bin/ 13 | dist/ 14 | 15 | # Eclipse 16 | .classpath 17 | .project 18 | .factorypath 19 | .settings/ 20 | .springBeans 21 | # IntelliJ IDEA [http://gary-rowe.com/agilestack/2012/10/12/a-gitignore-file-for-intellij-and-eclipse-with-maven] 22 | /.idea/ 23 | *.iml 24 | *.iws 25 | 26 | .DS_Store 27 | Thumbs.db 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 ShurjoMukhi Limited. (shurjomukhi.com.bd) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![shurjoPay](https://shurjopay.com.bd/dev/images/shurjoPay.png) JavaScript plugin package 2 | 3 | ![Made With](https://badgen.net/badge/Made%20with/javascript) 4 | [![Test Status](https://github.com/rust-random/rand/workflows/Tests/badge.svg?event=push)]() 5 | ![NPM](https://img.shields.io/npm/l/sp-plugin) 6 | ![version](https://badgen.net/npm/v/shurjopay-js) 7 | 8 | Official shurjoPay JavaScript package (plugin) for merchants or service providers to connect with [**_shurjoPay_**](https://shurjopay.com.bd) Payment Gateway v2.1 developed and maintained by [_**ShurjoMukhi Limited**_](https://shurjomukhi.com.bd). 9 | 10 | This plugin package can be used with any javascript application or framework (e.g. React, vue.js, Angular etc). 11 | Also it makes it easy for developers to integrate with shurjoPay v2.1 with just three API calls: 12 | 13 | 1. **makePayment**: create and send payment processing request 14 | 1. **verifyPayment**: verify payment status at shurjoPay 15 | 16 | Also reduces many of the things that you had to do manually 17 | 18 | - Handles http request and errors 19 | - JSON serialization and deserialization 20 | - Authentication during checkout and verification of payments 21 | 22 | ## Audience 23 | 24 | This document is intended for the developers and technical personnel of merchants and service providers who want to integrate the shurjoPay online payment gateway using javascript. 25 | 26 | # How to use this shurjoPay Plugin 27 | 28 | To integrate the shurjoPay Payment Gateway in your JavaScript project do the following tasks sequentially. 29 | 30 | #### Step 1: Install the plugin inside your project environment 31 | 32 | ``` 33 | npm install shurjopay-js 34 | ``` 35 | 36 | #### Step 2: Import the functions from package in your code as necessary and initiate payment method. 37 | 38 | ```javaScript 39 | import { makePayment, verifyPayment } from "shurjopay-js"; 40 | ``` 41 | 42 | ```javaScript 43 | const makePayment_details= await makePayment(your_order_id, form_data); 44 | const { checkout_url } = makePayment_details; 45 | ``` 46 | 47 | Checkout this [react project](https://github.com/shurjopay-plugins/sp-plugin-usage-examples/tree/dev/react-app-js-plugin) to see this plugin in action. 48 | 49 | #### Step 3: Setup configuration parameters for shurjopay plugin correctly in your application. 50 | 51 | e.g. API endpoint URL, username, password, prefix for the order id and return URL. 52 | Create a shurjopay_config.js in the project directory with text similar like below: 53 | 54 | ```javaScript 55 | const shurjopay_config = { 56 | SP_ENDPOINT: "https://sandbox.shurjopayment.com", 57 | SP_USERNAME: "sp_sandbox", 58 | SP_PASSWORD: "pyyk97hu&6u6", 59 | SP_PREFIX: "sp", 60 | SP_RETURN_URL: "https:///shurjopay-response", 61 | }; 62 | 63 | export { shurjopay_config }; 64 | ``` 65 | 66 | #### Payment verification can be done after each transaction with shurjopay order id 67 | 68 | ```javascript 69 | const verifyPayment_details = await verifyPayment(sp_order_id); 70 | ``` 71 | 72 | **NOTE**: Please checkout this readme docs for [react](README_react.md) or [vue.js](README_vuejs.md) projects. 73 | 74 | ## References 75 | 76 | 1. [React sample project](https://github.com/shurjopay-plugins/sp-plugin-usage-examples/tree/dev/react-app-js-plugin) showing usage of the javascript plugin. 77 | 2. Vanilla [javascript sample project](https://github.com/shurjopay-plugins/sp-plugin-usage-examples/tree/dev/javascript-app-javascript-plugin) to get your feet wet with shurjopay. 78 | 3. [Sample applications and projects](https://github.com/shurjopay-plugins/sp-plugin-usage-examples) in many different languages and frameworks showing shurjopay integration. 79 | 4. [shurjoPay Postman site](https://documenter.getpostman.com/view/6335853/U16dS8ig) illustrating the request and response flow using the sandbox system. 80 | 5. [shurjopay Plugins](https://github.com/shurjopay-plugins) home page on github 81 | 82 | ## License 83 | 84 | This code is under the [MIT open source License](http://www.opensource.org/licenses/mit-license.php). 85 | 86 | #### Please [contact](https://shurjopay.com.bd/#contacts) with shurjoPay team for more detail. 87 | 88 | Copyright ©️2022 [ShurjoMukhi Limited](https://shurjomukhi.com.bd). 89 | -------------------------------------------------------------------------------- /README_react.md: -------------------------------------------------------------------------------- 1 | #### Step 3: Setup configuration parameters for shurjopay plugin correctly in your application. 2 | 3 | e.g. API endpoint URL, username, password, prefix for the order id and return URL. 4 | Create a shurjopay_config.js in the project directory with text similar like below: 5 | 6 | ```javaScript 7 | const shurjopay_config = { 8 | SP_ENDPOINT: process.env.REACT_APP_SP_ENDPOINT, 9 | SP_USERNAME: process.env.REACT_APP_SP_USERNAME, 10 | SP_PASSWORD: process.env.REACT_APP_SP_PASSWORD, 11 | SP_PREFIX: process.env.REACT_APP_SP_PREFIX, 12 | SP_RETURN_URL: process.env.REACT_APP_SP_RETURN_URL, 13 | }; 14 | 15 | export { shurjopay_config }; 16 | ``` 17 | 18 | Create a .env the react project directory with text similar like below: 19 | 20 | ```javaScript 21 | REACT_APP_SP_ENDPOINT=https://sandbox.shurjopayment.com 22 | REACT_APP_SP_USERNAME=sp_sandbox 23 | REACT_APP_SP_PASSWORD=pyyk97hu&6u6 24 | REACT_APP_SP_PREFIX=sp 25 | REACT_APP_SP_RETURN_URL=https:///shurjopay-response 26 | ``` 27 | -------------------------------------------------------------------------------- /README_vuejs.md: -------------------------------------------------------------------------------- 1 | #### Step 3: Setup configuration parameters for shurjopay plugin correctly in your application. 2 | 3 | e.g. API endpoint URL, username, password, prefix for the order id and return URL. 4 | Create a shurjopay_config.js in the project directory with text similar like below: 5 | 6 | ```javaScript 7 | const shurjopay_config = { 8 | SP_ENDPOINT: process.env.VUE_APP_SP_ENDPOINT, 9 | SP_USERNAME: process.env.VUE_APP_SP_USERNAME, 10 | SP_PASSWORD: process.env.VUE_APP_SP_PASSWORD, 11 | SP_PREFIX: process.env.VUE_APP_SP_PREFIX, 12 | SP_RETURN_URL: process.env.VUE_APP_SP_RETURN_URL, 13 | }; 14 | 15 | export { shurjopay_config }; 16 | ``` 17 | 18 | Create a .env the vue.js project directory with text similar like below: 19 | 20 | ```javaScript 21 | VUE_APP_SP_ENDPOINT=https://sandbox.shurjopayment.com 22 | VUE_APP_SP_USERNAME=sp_sandbox 23 | VUE_APP_SP_PASSWORD=pyyk97hu&6u6 24 | VUE_APP_SP_PREFIX=sp 25 | VUE_APP_SP_RETURN_URL=https:///shurjopay-response 26 | ``` 27 | -------------------------------------------------------------------------------- /_env_local: -------------------------------------------------------------------------------- 1 | REACT_APP_SP_ENDPOINT=https://sandbox.shurjopayment.com 2 | REACT_APP_SP_USERNAME=REACT_APP_sandbox 3 | REACT_APP_SP_PASSWORD=pyyk97hu&6u6 4 | REACT_APP_SP_PREFIX=SP 5 | REACT_APP_SP_RETURN_URL=https://sandbox.shurjopayment.com/reREACT_APPonse 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shurjopay-js", 3 | "version": "0.1.2", 4 | "description": "Official shurjoPay JavaScript package (plugin) for merchants or service providers to connect with shurjoPay Payment Gateway v2.1 developed and maintained by shurjoMukhi Limited.", 5 | "main": "shurjopay.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha --recursive --exit" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/shurjopay-plugins/sp-plugin-js.git" 15 | }, 16 | "keywords": [ 17 | "shurjopay", 18 | "shurjomukhi", 19 | "shurjopay-plugin", 20 | "sp-plugin", 21 | "javascript", 22 | "react", 23 | "angular", 24 | "vue.js" 25 | ], 26 | "author": "Saiful Islam Shanto", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/shurjopay-plugins/sp-plugin-js/issues", 30 | "email": "plugindev@shurjomukhi.com.bd" 31 | }, 32 | "homepage": "https://github.com/shurjopay-plugins" 33 | } 34 | -------------------------------------------------------------------------------- /shurjopay.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Plug-in service to provide shurjoPay gateway services. 4 | * 5 | * @uthor: Saiful Islam Shanto 6 | * @since 2022-11-02 7 | */ 8 | 9 | /* shurjopay_config.js is expected at root of application using this plugin. check README.md */ 10 | import { shurjopay_config } from '../../shurjopay_config.js'; 11 | 12 | /* Getting credentials from shurjopay_config.js file */ 13 | const sp_endpoint = shurjopay_config.SP_ENDPOINT; 14 | const sp_username = shurjopay_config.SP_USERNAME; 15 | const sp_password = shurjopay_config.SP_PASSWORD; 16 | const sp_prefix = shurjopay_config.SP_PREFIX; 17 | const sp_return_url = shurjopay_config.SP_RETURN_URL; 18 | 19 | /* shurpay messages */ 20 | const MSG_AUTH_FAILED = 'Merchant authentication failure'; 21 | const MSG_PAYMENT_FAILED = 'Payment processing failed'; 22 | const MSG_PAYMENT_VERIFY_FAILED = 'Payment verification failed'; 23 | 24 | /** 25 | * Return authentication token for shurjoPay payment gateway system. 26 | * Setup shurjopay.properties file. 27 | * 28 | * @return authentication details with valid token 29 | * @throws ShurjopayException while merchant username and password is invalid. 30 | */ 31 | async function authentication() { 32 | // Fetch payment URL from authenticate API 33 | const response = await fetch(`${sp_endpoint}/api/get_token`, { 34 | method: "POST", 35 | headers: { 36 | "Content-Type": "application/json", 37 | }, 38 | // Request body credentials 39 | body: JSON.stringify({ 40 | username: sp_username, 41 | password: sp_password, 42 | }), 43 | }); 44 | 45 | return response.ok ? await response.json() : MSG_AUTH_FAILED; 46 | } 47 | 48 | /** 49 | * Send payment processing request to shurjopay system. 50 | * 51 | * @param merchant_order_id Unique ID generated by merchant system 52 | * @param form_data Payment request object 53 | * @return Payment response object contains redirect URL to reach payment page,token_details,order_id, form_data to verify order in shurjoPay. 54 | * @see shurjoPay v2 integration documentation for details. 55 | */ 56 | async function makePayment(merchant_order_id, form_data) { 57 | // Getting IP from user device 58 | const ipResponse = await fetch("https://checkip.amazonaws.com/"); 59 | const client_ip = await ipResponse.text(); 60 | 61 | // Call authentication function to get token details with store ID 62 | const token_details = await authentication(); 63 | const { token, token_type, store_id } = token_details; 64 | 65 | const payLoad = { 66 | prefix: sp_prefix, 67 | store_id: store_id, 68 | token: token, 69 | return_url: sp_return_url, 70 | cancel_url: sp_return_url, 71 | order_id: merchant_order_id, 72 | client_ip: client_ip, 73 | ...form_data 74 | } 75 | 76 | // Validate amount 77 | if (form_data.amount <= 0) 78 | return "Amount cannot be less then or equal to 0"; 79 | 80 | // Fetch payment URL from makePayment API 81 | const response = await fetch(`${sp_endpoint}/api/secret-pay`, { 82 | method: "POST", 83 | headers: headerWithToken(token, token_type), 84 | body: JSON.stringify(payLoad), 85 | }); 86 | 87 | return response.ok ? await response.json() : MSG_PAYMENT_FAILED; 88 | } 89 | 90 | /** 91 | * Verify payment status using shurjopay order id. 92 | * Payment details object is returned when order status is success in shurjopay system. 93 | * 94 | * @param sp_order_id shurjopay order id 95 | * @return Payment details object if order verified successfully 96 | */ 97 | async function verifyPayment(sp_order_id) { 98 | if (!sp_order_id) return "Missing order id"; 99 | 100 | const token_details = await authentication(); 101 | const { token, token_type } = token_details; 102 | 103 | const response = await fetch(`${sp_endpoint}/api/verification`, { 104 | method: "POST", 105 | headers: headerWithToken(token, token_type), 106 | body: JSON.stringify({ 107 | order_id: sp_order_id, 108 | }), 109 | }); 110 | 111 | return response.ok ? await response.json : MSG_PAYMENT_VERIFY_FAILED; 112 | } 113 | 114 | function headerWithToken(token, token_type) { 115 | return { 116 | authorization: `${token_type} ${token}`, 117 | Accept: "application/json", 118 | "Content-Type": "application/json", 119 | } 120 | } 121 | 122 | /* Export functions */ 123 | export { makePayment, verifyPayment }; 124 | -------------------------------------------------------------------------------- /test/crud_test.js: -------------------------------------------------------------------------------- 1 | // import {authentication,makePayment,verifyPayment,paymentStatus} from '../Shurjopay.js' 2 | import { makePayment, paymentStatus, verifyPayment } from "../Shurjopay.js"; 3 | 4 | makePayment( 5 | { 6 | amount: "10", 7 | order_id: "sp315689", 8 | customer_name: "ATM Fahim", 9 | customer_address: "Dhaka", 10 | customer_phone: "01534303074", 11 | customer_city: "Dhaka", 12 | customer_post_code: "1212", 13 | client_ip: "102.101.1.1", 14 | }, 15 | function (checkout_url, resp_data) { 16 | console.log("checkout url is " + checkout_url); 17 | 18 | verifyPayment((resp_data) => { 19 | console.log("verify"); 20 | console.dir(resp_data); 21 | }); 22 | paymentStatus((resp_data) => { 23 | console.log("check status"); 24 | console.dir(resp_data); 25 | }); 26 | } 27 | ); 28 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | //test by mocha 2 | // Requiring module 3 | import assert from 'assert'; 4 | // import setting from '../setting.js' 5 | 6 | // import {authentication,makePayment,verifyPayment,paymentStatus} from '../Shurjopay.js' 7 | import { makePayment } from '../Shurjopay.js'; 8 | 9 | // We can group similar tests inside a 'describe' block 10 | describe("authentication,makePayment,verifyPayment,paymentStatus", () => { 11 | before(() => { 12 | //console.log("This part executes once before all tests"); 13 | }); 14 | 15 | after(() => { 16 | //console.log("This part executes once after all tests"); 17 | }); 18 | 19 | // We can add nested blocks for different tests 20 | describe("makePayment", () => { 21 | beforeEach(() => { 22 | console.log("Test checkout"); 23 | }); 24 | 25 | it("Is checkout working properly", () => { 26 | makePayment({ 27 | amount: '10', 28 | order_id: 'sp315689', 29 | customer_name: 'ATM Fahim', 30 | customer_address: 'Dhaka', 31 | customer_phone: '01534303074', 32 | customer_city: 'Dhaka', 33 | customer_post_code: '1212', 34 | client_ip: '102.101.1.1' 35 | }, function (resp_data) { 36 | //assert('checkout_url2' in resp_data); 37 | //assert('sp_order_id' in resp_data); 38 | //assert('transactionStatus' in resp_data); 39 | assert.equal(resp_data.transactionStatus, 'Initiated'); 40 | }); 41 | }); 42 | }); 43 | 44 | /* 45 | describe("verifyPayment", () => { 46 | beforeEach(() => { 47 | console.log("executes before every test"); 48 | }); 49 | it("Is returning 4 when adding 2 + 3", () => { 50 | assert.equal(2 + 3, 4); 51 | }); 52 | it("Is returning 8 when multiplying 2 * 4", () => { 53 | assert.equal(2 * 4, 8); 54 | }); 55 | }); 56 | describe("paymentStatus", () => { 57 | beforeEach(() => { 58 | console.log("executes before every test"); 59 | }); 60 | it("Is returning 4 when adding 2 + 3", () => { 61 | assert.equal(2 + 3, 4); 62 | }); 63 | it("Is returning 8 when multiplying 2 * 4", () => { 64 | assert.equal(2 * 4, 8); 65 | }); 66 | }); 67 | */ 68 | }); --------------------------------------------------------------------------------