A Node package for seamless integration with PhonePe Payment Gateway APIs.
- Node Version: 14.21
Install the dependency using npm:
npm i https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-node/releases/v2/phonepe-pg-sdk-node.tgzBefore using the SDK, you need to acquire your credentials from the PhonePe Merchant Portal.
You need three key pieces of information:
clientId- Your merchant identifierclientSecret- Your authentication secretclientVersion- API version to use
import {StandardCheckoutClient, Env} from 'pg-sdk-node';
const clientId = "<clientId>";
const clientSecret = "<clientSecret>";
const clientVersion = 1; //insert your client version here
const env = Env.SANDBOX; //change to Env.PRODUCTION when you go live
const client = StandardCheckoutClient.getInstance(clientId, clientSecret, clientVersion, env);To initiate a payment, create a request using StandardCheckoutPayRequest.builder():
import {StandardCheckoutPayRequest} from 'pg-sdk-node';
import {randomUUID} from 'crypto';
const merchantOrderId = randomUUID();
const amount = 100;
const redirectUrl = "https://www.merchant.com/redirect";
const request = StandardCheckoutPayRequest.builder()
.merchantOrderId(merchantOrderId)
.amount(amount)
.redirectUrl(redirectUrl)
.build();
client.pay(request).then((response)=> {
const checkoutPageUrl = response.redirectUrl;
console.log(checkoutPageUrl);
})
// Redirect the user to checkoutPageUrl to complete the paymentTo check the status of an order:
const merchantOrderId = '<MERCHANT_ORDER_ID>'; //Order Id used for creating new order
client.getOrderStatus(merchantOrderId).then((response) => {
const state = response.state;
});
// Handle the state accordingly in your applicationPhonePe sends callbacks to your configured endpoint. Validate these callbacks to ensure they're authentic:
const authorizationHeaderData = "<FETCH_SHA_256_DATA_FROM_HEADER>" // received in the response headers
const phonepeS2SCallbackResponseBodyString = "{\"type\": \"PG_ORDER_COMPLETED\",\"payload\": {}}" // receiver in response body
const usernameConfigured = "<MERCHANT_USERNAME>"
const passwordConfigured = "<MERCHANT_PASSWORD>"
try {
const callbackResponse = client.validateCallback(
usernameConfigured,
passwordConfigured,
authorizationHeaderData,
phonepeS2SCallbackResponseBodyString );
const orderId = callbackResponse.payload.orderId;
const state = callbackResponse.payload.state;
// Process the order based on its state
} catch (err) {
// Handle invalid callback - potential security issue
}For mobile SDK integration, first create an order on your server:
import {StandardCheckoutClient, Env, CreateSdkOrderRequest} from 'pg-sdk-node';
import {randomUUID} from 'crypto';
const clientId = "<clientId>";
const clientSecret = "<clientSecret>";
const clientVersion = 1; //insert your client version here
const env = Env.SANDBOX; //change to Env.PRODUCTION when you go live
const client = StandardCheckoutClient.getInstance(clientId, clientSecret, clientVersion, env);
const merchantOrderId = randomUUID();
const amount = 1000;
const redirectUrl = "https://redirectUrl.com";
const request = CreateSdkOrderRequest.StandardCheckoutBuilder()
.merchantOrderId(merchantOrderId)
.amount(amount)
.redirectUrl(redirectUrl)
.build();
client.createSdkOrder(request).then((response) => {
const token = response.token
})
// Pass this token to your mobile app to initiate payment through the PhonePe SDKFor detailed API documentation, advanced features, and integration options:
Contributions to PG Node SDK are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure your code follows the project's coding standards and includes appropriate tests.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Copyright 2025 PhonePe Private Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.