Skip to content

PhonePe/phonepe-pg-sdk-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PhonePe B2B Payment Gateway SDK for Node

npm Node License

A Node package for seamless integration with PhonePe Payment Gateway APIs.

Table of Contents

Requirements

  • Node Version: 14.21

Installation

Node

Install the dependency using npm:

npm i https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-node/releases/v2/phonepe-pg-sdk-node.tgz

Quick Start

Initialization

Before using the SDK, you need to acquire your credentials from the PhonePe Merchant Portal.

You need three key pieces of information:

  1. clientId - Your merchant identifier
  2. clientSecret - Your authentication secret
  3. clientVersion - 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);

Standard Checkout Flow

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 payment

Checking Order Status

To 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 application

Handling Callbacks

PhonePe 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
}

SDK Order Integration

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 SDK

Documentation

For detailed API documentation, advanced features, and integration options:

Contributing

Contributions to PG Node SDK are welcome! Here's how you can contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure your code follows the project's coding standards and includes appropriate tests.

License

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.

About

Sample App to demonstrate the integration of PhonePe PG SDK Node package.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •