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.• For udf1 to udf10, there is no constraint and Maximum length for Udf1-10 = 256 characters
• For udf11 to udf15, alphanumeric + _, -, @, ., + only are allowed and Maximum length for Udf11-15 = 50 characters
• It is mandatory to keep the parameter names udf1udf2, etc., exactly as they are in the metainfo block. Renaming these key values will result in a production error.
redirectUrlStringNoURL to which the user will be redirected after the payment (success or failure)
expireAfterLongNoSet a orders expiry time in seconds.
messageStringNoPayment message shown in APP for collect requests.
Sample Request
import { StandardCheckoutClient, Env, MetaInfo, StandardCheckoutPayRequest } from '@phonepe-pg/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; // Amount in paise (100 = ₹1.00)
const prefillUserLoginDetails = PrefillUserLoginDetails.builder()
    .phoneNumber("<PhonepeNumber>")
const metaInfo = MetaInfo.builder()
    .udf1("udf1")
    .udf2("udf2")
    .udf3("udf3")
    .build();

const orderRequest = CreateSdkOrderRequest.StandardCheckoutBuilder()
    .merchantOrderId(merchantOrderId)
    .amount(amount)
    .prefillUserLoginDetails(prefillUserLoginDetails)
    .metaInfo(metaInfo)
    .redirectUrl("https://www.merchant.com/redirect")
    .expireAfter(3600)
    .message("Message that will be shown for UPI collect transaction")
    .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?