Initiate Payment with Node.js SDK


The Initiate Payment step allows you to start a payment transaction by creating a payment request with essential details like order ID, amount, and redirect URL. This request prepares the transaction on PhonePe’s platform and generates a redirect URL where users complete their payment securely.

Use StandardCheckoutPayRequest.build_request() to create the payment request. Below are the key attributes you can set:

Parameter NameData TypeMandatory
(Yes/No)
DescriptionConstraints
merchantOrderIdStringYesUnique order ID assigned by youMax length: 63 characters, no special characters except “_” and “-”
amountLongYesOrder amount in paisaMinimum value: 100 (in paisa)
metaInfoObjectNoMeta information is defined by you to store additional information. The same data will be returned in status and callback response.
metaInfo.udf1-5StringNoOptional details you can add for more information.Maximum length = 256 characters 
redirectUrlStringNoURL to which the user will be redirected after the payment (success or failure)
Sample Request
import { StandardCheckoutClient, Env, MetaInfo, StandardCheckoutPayRequest } from 'pg-sdk-node';
import { randomUUID } from 'crypto';
 
const clientId = "<clientId>";
const clientSecret = "<clientSecret>";
const clientVersion = <clientVersion>;    //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 = 100;
const redirectUrl = "https://www.merchant.com/redirect";
const metaInfo = MetaInfo.builder()
                    .udf1("udf1")
                    .udf2("udf2")
                    .build();
 
const request = StandardCheckoutPayRequest.builder()
        .merchantOrderId(merchantOrderId)
        .amount(amount)
        .redirectUrl(redirectUrl)
        .metaInfo(metaInfo)
        .build();
 
client.pay(request).then((response)=> {
    const checkoutPageUrl = response.redirectUrl;
})

The function returns a StandardCheckoutPayResponse object with the following properties:

AttributeData TypeDescription
stateStringState of the order created, expected value is PENDING.
redirect_urlStringURL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment.
order_idStringA unique internal order ID generated by PhonePe PG.
expire_atStringOrder expiry timestamp in epoch.

After using the pay method to initiate a payment via the PhonePe PG, you can create a payment request and start the payment process. The next step is to create the order SDK.

Proceed to the next section to learn how to Create Order SDK .

Is this article helpful?