Skip to content

Commit

Permalink
Plugin initial Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayshik committed Jan 22, 2025
1 parent 5b5dca2 commit 4de5d4e
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 2 deletions.
104 changes: 102 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,102 @@
# sp-plugin-typescript
The sp-plugin-typescript is a TypeScript-based plugin for integrating the shurjoPay payment gateway into your projects seamlessly. Designed for developers, this plugin provides a robust and easy-to-use interface to initiate and manage payment transactions using the shurjoPay platform.
# ![shurjoPay](https://shurjopay.com.bd/dev/images/shurjoPay.png) JavaScript plugin package

![Made With](https://badgen.net/badge/Made%20with/typescript)
![NPM](https://img.shields.io/npm/l/sp-plugin)
![version](https://img.shields.io/badge/version-1.0.0-blue)


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).

This plugin package is compatible with any TypeScript or JavaScript application or framework, including React, Vue.js, Angular, Next.js, and more.
Also it makes it easy for developers to integrate with shurjoPay v2.1 with just three API calls:

1. **makePayment**: create and send payment processing request
1. **verifyPayment**: verify payment status at shurjoPay

Also reduces many of the things that you had to do manually

- Handles http request and errors
- JSON serialization and deserialization
- Authentication during checkout and verification of payments

## Audience

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.

# How to use this shurjoPay Plugin

To integrate the shurjoPay Payment Gateway in your JavaScript project do the following tasks sequentially.

#### Step 1: Copy and paste the lib folder from the plugin into the root directory of your project environment.


#### Step 2: Copy and paste the shurjopay_config.js file from the plugin into the root directory of your project environment.


Checkout this [react project](https://github.com/shurjopay-plugins/sp-plugin-usage-examples/tree/main/next-app-typescript-plugin) to see this plugin in action.
#### Step 3: Set up the configuration parameters correctly in the shurjopay_config.js file for the shurjopay plugin within your application.

e.g. SP_ENDPOINT Url, SP_USERNAME, SP_PASSWORD, SP_PREFIX for the order id and SP_RETURN_URL.

Create a shurjopay_config.js in the project directory with text similar like below:

```javaScript
const shurjopay_config = {
SP_ENDPOINT: "https://sandbox.shurjopayment.com",
SP_USERNAME: "sp_sandbox",
SP_PASSWORD: "pyyk97hu&6u6",
SP_PREFIX: "sp",
SP_RETURN_URL: "https://<your.app.com>/shurjopay-response",
};

export { shurjopay_config };
```
#### To initiate a payment, import and use the makePayment function as follows:

```javascript
import { makePayment } from "../lib/shurjoPay";

const form_data = {
prefix: "NOK",
currency: "BDT",
customer_name: "Md Wali Mosnad Ayshik",
customer_phone: "01775XXXXXX",
customer_email: "[email protected]",
customer_address: "123 Street, Dhaka",
customer_city: "Dhaka",
customer_state: "Dhaka",
customer_postcode: "1212",
customer_country: "Bangladesh",
shipping_address: "123 Street, Dhaka",
shipping_city: "Dhaka",
shipping_country: "Bangladesh",
received_person_name: "Ayshik",
shipping_phone_number: "017XXXXXXXX",
amount: 100,
};
const merchantOrderId = `your order id`;
const makePayment = await makePayment(merchantOrderId, form_data);
```


#### Payment verification can be done after each transaction with shurjopay order id

```javascript
const verifyPayment_details = await verifyPayment(order_id as string);
```

## References

1. [Next sample project](https://github.com/shurjopay-plugins/sp-plugin-usage-examples/tree/main/next-app-typescript-plugin) showing usage of the javascript plugin.
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.
3. [Sample applications and projects](https://github.com/shurjopay-plugins/sp-plugin-usage-examples) in many different languages and frameworks showing shurjopay integration.
4. [shurjoPay Postman site](https://documenter.getpostman.com/view/6335853/U16dS8ig) illustrating the request and response flow using the sandbox system.
5. [shurjopay Plugins](https://github.com/shurjopay-plugins) home page on github

## License

This code is under the [MIT open source License](http://www.opensource.org/licenses/mit-license.php).

#### Please [contact](https://shurjopay.com.bd/#contacts) with shurjoPay team for more detail.

Copyright ©️2022 [ShurjoMukhi Limited](https://shurjomukhi.com.bd).
165 changes: 165 additions & 0 deletions lib/shurjoPay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Next.js Typescript plugin class to connect and integrate with the shurjoPay payment gateway API.
*
* This plugin includes three mandatory functions for interacting with shurjoPay:
* 1. `authenticate()` - Authenticates the client with shurjoPay.
* 2. `makePayment()` - Generates the payment URL for checkout.
* 3. `verifyPayment()` - Verifies the payment status.
*
*
* @author Md Wali Mosnad Ayshik
* Senior Software Engineer
* @since 22/01/2025
*/


import { shurjopay_config } from "../shurjopay_config";

// Static error messages
const MSG_AUTH_FAILED = "Merchant authentication failure";
const MSG_PAYMENT_FAILED = "Payment processing failed";
const MSG_PAYMENT_VERIFY_FAILED = "Payment verification failed";

// Type definitions for improved TypeScript support
interface PaymentRequest {
amount: number;
prefix: string;
currency: string;
customer_name: string;
customer_phone: string;
customer_email: string;
customer_address: string;
customer_city: string;
customer_state: string;
customer_postcode: string;
customer_country: string;
shipping_address: string;
shipping_city: string;
shipping_country: string;
received_person_name: string;
shipping_phone_number: string;
discount_amount?: number;
disc_percent?: number;
value1?: string;
value2?: string;
value3?: string;
value4?: string;
}

interface PaymentResponse {
checkout_url: string;
order_id: string;
status: string;
[key: string]: any; // Allow additional properties if returned by the API
}

/**
* Authenticates with shurjoPay and returns the token.
*/
async function authenticate(): Promise<any> {
try {
const response = await fetch(`${shurjopay_config.SP_ENDPOINT}/api/get_token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: shurjopay_config.SP_USERNAME,
password: shurjopay_config.SP_PASSWORD,
}),
});

if (!response.ok) throw new Error(MSG_AUTH_FAILED);
return await response.json();
} catch (error) {
if (error instanceof Error) {
throw new Error(error.message); // Throw an Error instance with its message
}
throw new Error(MSG_AUTH_FAILED);
}
}

/**
* Processes a payment request to shurjoPay.
*/
async function makePayment(
merchant_order_id: string,
form_data: PaymentRequest
): Promise<PaymentResponse | string> {
try {
const ipResponse = await fetch("https://checkip.amazonaws.com/");
const client_ip = (await ipResponse.text()).trim();

const tokenDetails = await authenticate();
const { token, token_type, store_id } = tokenDetails;

const payload = {
...form_data,
store_id,
token,
client_ip,
return_url: shurjopay_config.SP_RETURN_URL,
cancel_url: shurjopay_config.SP_RETURN_URL,
order_id: merchant_order_id,
};

const response = await fetch(
`${shurjopay_config.SP_ENDPOINT}/api/secret-pay`,
{
method: "POST",
headers: {
authorization: `${token_type} ${token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);

if (!response.ok) throw new Error(MSG_PAYMENT_FAILED);
return await response.json();
} catch (error) {
if (error instanceof Error) {
return error.message; // Return the error message if it's an instance of Error
} else {
return MSG_PAYMENT_FAILED; // Fallback message for unknown errors
}
}
}

/**
* Verifies the payment status using shurjoPay's order ID.
*/
async function verifyPayment(sp_order_id: string): Promise<any> {
if (!sp_order_id) return "Missing order ID";

try {
const tokenDetails = await authenticate();
const { token, token_type } = tokenDetails;

const response = await fetch(
`${shurjopay_config.SP_ENDPOINT}/api/verification`,
{
method: "POST",
headers: {
authorization: `${token_type} ${token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ order_id: sp_order_id }),
}
);

if (!response.ok) throw new Error(MSG_PAYMENT_VERIFY_FAILED);
return await response.json();
} catch (error) {
if (error instanceof Error) {
return error.message; // Return the error message if it's an instance of Error
} else {
return MSG_PAYMENT_VERIFY_FAILED; // Fallback message for unknown errors
}
}
}

// Export functions
export { makePayment, verifyPayment, authenticate };
8 changes: 8 additions & 0 deletions shurjopay_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const shurjopay_config = {
SP_ENDPOINT: "https://www.sandbox.shurjopayment.com",
SP_USERNAME: "sp_sandbox",
SP_PASSWORD: "pyyk97hu&6u6",
SP_PREFIX: "NOK",
SP_RETURN_URL: "https://www.sandbox.shurjopayment.com/response",
};

0 comments on commit 4de5d4e

Please sign in to comment.