Skip to content

Commit a22cfd3

Browse files
committed
Bug fixing in the form data
The undefined fields were throwing error. So used a condition to skip those fields
1 parent 74e76c1 commit a22cfd3

3 files changed

Lines changed: 95 additions & 19 deletions

File tree

README.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SSLCOMMERZ Payment Gateway implementation in NodeJs
1+
# SSLCOMMERZ Payment Gateway itegration in NodeJs
22
It is npm package which provides functionalities to implement SSLCommerze Payment Gateway in Node Based Apps.
33

44
## Installation
@@ -7,7 +7,7 @@ Via NPM
77
npm i ssl-commerz-node
88
```
99

10-
## Instruction
10+
## Set the parameters to make a request
1111
```js
1212
const SSLCommerz = require('ssl-commerze-node');
1313
const PaymentSession = SSLCommerz.PaymentSession;
@@ -27,12 +27,82 @@ payment.setOrderInfo({
2727
total_amount: 1570, // Number field
2828
currency: 'BDT', // Must be three character string
2929
tran_id: 'ref12345667', // Unique Transaction id
30-
emi_option: 0 // 1 or 0,
30+
emi_option: 0, // 1 or 0
3131
multi_card_name: 'internetbank', // Do not Use! If you do not customize the gateway list,
3232
allowed_bin: '371598,371599,376947,376948,376949', // Do not Use! If you do not control on transaction
3333
emi_max_inst_option: 3, // Max instalment Option
3434
emi_allow_only: 0 // Value is 1/0, if value is 1 then only EMI transaction is possible
3535
});
3636

37+
// Set customer info
38+
payment.setCusInfo({
39+
name: 'Simanta Paul',
40+
email: 'simanta@bohubrihi.com',
41+
add1: '66/A Midtown',
42+
add2: 'Andarkilla',
43+
city: 'Chittagong',
44+
state: 'Optional',
45+
postcode: 4000,
46+
country: 'Bangladesh',
47+
phone: '010000000000',
48+
fax: 'Customer_fax_id'
49+
});
50+
51+
// Set shipping info
52+
payment.setShippingInfo({
53+
method: 'Courier', //Shipping method of the order. Example: YES or NO or Courier
54+
num_item: 2,
55+
name: 'Simanta Paul',
56+
add1: '66/A Midtown',
57+
add2: 'Andarkilla',
58+
city: 'Chittagong',
59+
state: 'Optional',
60+
postcode: 4000,
61+
country: 'Bangladesh',
62+
});
63+
64+
// Set Product Profile
65+
payment.setProductInfo({
66+
product_name: 'Computer',
67+
product_category: 'Electronics',
68+
product_profile: 'general'
69+
});
70+
```
71+
**See this for details:** https://developer.sslcommerz.com/doc/v4/#ready-the-parameters
72+
73+
## After setting the parameters initialize the payment
74+
```js
75+
// Initiate Payment and Get session key
76+
payment.paymentInit()
77+
.then(response => {
78+
console.log(response);
79+
});
3780
```
38-
See this for details: https://developer.sslcommerz.com/doc/v4/#ready-the-parameters
81+
**This link containes details about the response parameters:** https://developer.sslcommerz.com/doc/v4/#returned-parameters
82+
83+
## Response Parameters Examples
84+
### After Success
85+
```js
86+
console.log(response['status']);
87+
```
88+
> SUCCESS
89+
```js
90+
console.log(response['sessionkey']);
91+
```
92+
> D37CD2C0A0D322991531D217E194F981
93+
```js
94+
console.log(response['GatewayPageURL']);
95+
```
96+
> https://sandbox.sslcommerz.com/EasyCheckOut/testcded37cd2c0a0d322991531d217e194f981
97+
98+
### After Failure (Wrong Store ID)
99+
```js
100+
console.log(response['status']);
101+
```
102+
> FAILED
103+
```js
104+
console.log(response['failedreason']);
105+
```
106+
> Store Credential Error Or Store is De-active
107+
108+
The **GatewayPageURL** is the url of the payment page.

SSLCommerz/PaymentSession.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class PaymentSession extends SSLCommerz {
99
}
1010

1111
setSSLdata() {
12+
this.setSSLdata();
1213
this.postData['store_id'] = this.store_id;
1314
this.postData['store_passwd'] = this.store_pass;
1415
}
@@ -65,17 +66,21 @@ class PaymentSession extends SSLCommerz {
6566
}
6667

6768
async paymentInit() {
68-
const response = await fetch('https://sandbox.sslcommerz.com/gwprocess/v4/api.php', {
69-
method: 'POST', // *GET, POST, PUT, DELETE, etc.
70-
mode: 'cors', // no-cors, cors, *same-origin
71-
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
72-
credentials: 'same-origin', // include, *same-origin, omit
73-
redirect: 'follow', // manual, *follow, error
74-
referrer: 'no-referrer', // no-referrer, *client
75-
body: getFormData(this.postData), // body data type must match "Content-Type" header
76-
})
77-
const data = await response.json();
78-
return data;
69+
try {
70+
const response = await fetch(this.session_api, {
71+
method: 'POST', // *GET, POST, PUT, DELETE, etc.
72+
mode: 'cors', // no-cors, cors, *same-origin
73+
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
74+
credentials: 'same-origin', // include, *same-origin, omit
75+
redirect: 'follow', // manual, *follow, error
76+
referrer: 'no-referrer', // no-referrer, *client
77+
body: getFormData(this.postData), // body data type must match "Content-Type" header
78+
})
79+
const data = await response.json();
80+
return data;
81+
} catch (error) {
82+
return error;
83+
}
7984
}
8085
}
8186

SSLCommerz/processFormData.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const FormData = require('form-data');
22

33
module.exports = (postData) => {
4-
let fdata = new FormData();
5-
for (var property in postData) {
6-
fdata.append(property, postData[property])
4+
let form = new FormData();
5+
for (let key in postData) {
6+
if (!postData[key]) continue;
7+
form.append(key, postData[key]);
78
}
8-
return fdata;
9+
return form;
910
}

0 commit comments

Comments
 (0)