Skip to content

Commit

Permalink
main functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
rseyf committed Aug 14, 2021
1 parent 864a78c commit 79f6a7c
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 16 deletions.
11 changes: 11 additions & 0 deletions CHANGELIST.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Change Log
All notable changes to this project is documented in this file.

# v0.2.2 - 2021 14 Aug (main functionality)
### Added
- checkTransaction() method
- verifyPayment() method
- refundPayment() method
### Changes
- `URL_REFUND` address changed to correct value.

# v0.1.0 (dev/main)
- initializing main structure
- initializing requestBuilder
Expand Down
153 changes: 139 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,144 @@
# NodeJS SDK for Pasargad
# NodeJS SDK for Pasargad IPG
NodeJS package to connect your application to Pasargad Internet Payment Gateway through RESTful API

## Redirect To payment
# Installation
For installation, use `npm` package:

```bash
$ npm install @pepco/nodejs-rest-sdk
```

# Usage
- To Read API Documentation, [Click Here! (دانلود مستندات کامل درگاه پرداخت)](https://www.pep.co.ir/wp-content/uploads/2019/06/1-__PEP_IPG_REST-13971020.Ver3_.00.pdf)
- Save your private key into an `.xml` file inside your project directory.

## Redirect User to Payment Gateway
```js
// Tip! Initialize this property in your payment service constructor method!
const PasargadApi = require('@pepco/nodejs-rest-sdk');
const pasargad = new PasargadApi(
"YOUR_MERCHANT_CODE",
"YOUR_TERMINAL_ID",
"http://yoursite.com/redirect-url-here/",
"certificate_file_location.xml");
//e.q:
// const pasargad = new PasargadApi(xxxxxx,xxxxx,"https://pep.co.ir/ipgtest","cert.xml");

const pasargad = new PasargadApi(xxxxxx,xxxxx,"https://pep.co.ir/ipgtest","cert.xml");
try {
pasargad.amount = 10000;
pasargad.invoiceNumber = "5002";
pasargad.invoiceDate = "2021/08/12 17:31:00";
pasargad.redirect().then(redirectUrl => {
// redirect Here
console.log(redirectUrl);
});
} catch(e) {
throw e;
// Set Amount
pasargad.amount = 15000;

// Set Invoice Number (it MUST BE UNIQUE)
pasargad.invoiceNumber = "4029";

// set Invoice Date with below format (Y/m/d H:i:s)
pasargad.invoiceDate = "2021/08/08 11:54:03";

// get the Generated RedirectUrl from Pasargad API (async request):
// output example: https://pep.shaparak.ir/payment.aspx?n=bPo+Z8GLB4oh5W0KVNohihxCu1qBB3kziabGvO1xqg8Y=
pasargad.redirect().then(redirectURL => {
// redirect user to `redirectURL`
console.log(redirectURL);
});
```

## Checking and Verifying Transaction
After Payment Process, User is going to be returned to your redirect_url.

payment gateway is going to answer the payment result with sending below parameters to your redirectURL (as `QueryString` parameters):
- InvoiceNumber (IN field)
- InvoiceDate (ID field)
- TransactionReferenceID (tref field)

Store this information in a proper data storage and let's check transaction result by sending a check api request to the Bank:

```js
// Set Transaction refrence id received in
pasargad.transactionReferenceID = "636843820118990203";

// Set Unique Invoice Number that you want to check the result
pasargad.invoiceNumber = 4029;

// set Invoice Date of your Invoice
pasargad.invoiceDate = "2021/08/08 11:54:03";

// check Transaction result
pasargad.checkTransaction().then(response => {
// you can handle the response here:
console.log(response);
});
```

Successful result:
```json
{
"TraceNumber": 13,
"ReferenceNumber": 100200300400500,
"TransactionDate": "2021/08/08 11:58:23",
"Action": "1003",
"TransactionReferenceID": "636843820118990203",
"InvoiceNumber": "4029",
"InvoiceDate": "2021/08/08 11:54:03",
"MerchantCode": 100123,
"TerminalCode": 200123,
"Amount": 15000,
"IsSuccess": true,
"Message": " "
}
```
```
If you got `IsSuccess` with `true` value, so everything is O.K!

Now, for your successful transaction, you should call `verifyPayment()` method to keep the money and Bank makes sure the checking process was done properly:


```js
// Set Amount
pasargad.amount = 15000;

// Set Invoice Number (it MUST BE UNIQUE)
pasargad.invoiceNumber = "4029";

// set Invoice Date with below format (Y/m/d H:i:s)
pasargad.invoiceDate = "2021/08/08 11:54:03";

// verify payment:
pasargad.verifyPayment().then(response => {
// response
console.log(response);
});
```

...and the successful response looks like this response:
```json
{
"IsSuccess": true,
"Message": " ",
"MaskedCardNumber": "5022-29**-****-2328",
"HashedCardNumber": "2DDB1E270C598677AE328AA37C2970E3075E1DB6665C5AAFD131C59F7FAD99F23680536B07C140D24AAD8355EA9725A5493AC48E0F48E39D50B54DB906958182",
"ShaparakRefNumber": "100200300400500"
}

```

## Payment Refund
If for any reason, you decided to cancel an order in early hours after taking the order (maximum 2 hours later), you can refund the client payment to his/her bank card.

for this, use `refundPayment()` method:

```js
// Set Unique Invoice Number that you want to check the result
pasargad.invoiceNumber = "4029";

// set Invoice Date of your Invoice
pasargad.invoiceDate = "2021/08/08 11:54:03";

// check Transaction result
pasargad.refundPayment().then(response => {
// handle response here:
console.log(response);
});
```

# Support
Please use your credentials to login into [Support Panel](https://my.pep.co.ir)

Contact Author/Maintainer: [Reza Seyf](https://twitter.com/seyfcode)
77 changes: 76 additions & 1 deletion lib/Pasargad.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const URL_GET_TOKEN = "https://pep.shaparak.ir/Api/v1/Payment/GetToken";

const URL_CHECK_TRANSACTION = 'https://pep.shaparak.ir/Api/v1/Payment/CheckTransactionResult';
const URL_VERIFY_PAYMENT = 'https://pep.shaparak.ir/Api/v1/Payment/VerifyPayment';
const URL_REFUND = 'https://pep.shaparak.ir/Api/v1/Payment/VerifyPayment';
const URL_REFUND = 'https://pep.shaparak.ir/Api/v1/Payment/RefundPayment';



Expand Down Expand Up @@ -89,6 +89,17 @@ module.exports = class Pasargad
}



/**
* payment reference id setter
*
* @param {string} transactionReferenceID Payment reference id
*/
set transactionReferenceID (transactionReferenceID) {
this._transactionReferenceID = transactionReferenceID;
}


/**
* invoiceDate setter
*
Expand Down Expand Up @@ -225,4 +236,68 @@ module.exports = class Pasargad
};
return this.getToken(params);
}


async checkTransaction() {
const params = {
transactionReferenceID: this._transactionReferenceID,
invoiceNumber: this._invoiceNumber,
invoiceDate: this._invoiceDate,
merchantCode: `${this._merchantcode}`,
terminalCode: `${this._terminalId}`
};
this.signData(params);
let response;
await this._requestBuilder({
url: URL_CHECK_TRANSACTION,
method: "POST",
body: (JSON.stringify(params))
}).then(data => {
response = data;
});
return response;
}

async verifyPayment()
{
const params = {
amount: this._amount,
invoiceNumber: this._invoiceNumber,
invoiceDate: this._invoiceDate,
merchantCode: `${this._merchantcode}`,
terminalCode: `${this._terminalId}`,
timeStamp: this.getTimestamp()
};
this.signData(params);
let response;
await this._requestBuilder({
url: URL_VERIFY_PAYMENT,
method: "POST",
body: (JSON.stringify(params))
}).then(data => {
response = data;
});
return response;
}

async refundPayment()
{
const params = {
invoiceNumber: this._invoiceNumber,
invoiceDate: this._invoiceDate,
merchantCode: `${this._merchantcode}`,
terminalCode: `${this._terminalId}`,
timeStamp: this.getTimestamp()
};
this.signData(params);
let response;
await this._requestBuilder({
url: URL_REFUND,
method: "POST",
body: (JSON.stringify(params))
}).then(data => {
response = data;
});
return response;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pepco/nodejs-rest-sdk",
"version": "0.1.0",
"version": "0.2.2",
"description": "NodeJS package to connect your application to Pasargad Bank Internet Payment Gateway through RESTful API",
"homepage": "https://github.com/pepco-api/nodejs-rest-sdk-sdk",
"repository": {
Expand Down

0 comments on commit 79f6a7c

Please sign in to comment.