Initiate Payment with Java 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.builder() 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.
redirectUrlStringNoURL where user will be redirected after success/failed payment.
paymentModeConfigObjectNoUsed to dynamically enable only specific instruments on the checkout page.

Sample Request
import java.util.UUID;
import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.payments.v2.StandardCheckoutClient;
import com.phonepe.sdk.pg.payments.v2.models.request.StandardCheckoutPayRequest;
import com.phonepe.sdk.pg.payments.v2.models.response.StandardCheckoutPayResponse;
import com.phonepe.sdk.pg.common.models.MetaInfo;
 
String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = clientVersion;  //insert your client version here
Env env = Env.SANDBOX;      //change to Env.PRODUCTION when you go live
 
StandardCheckoutClient client = StandardCheckoutClient.getInstance(clientId, clientSecret,
        clientVersion, env);
 
String merchantOrderId = UUID.randomUUID().toString();
long amount = 100;
String redirectUrl = "https://redirectUrl.com";
MetaInfo metaInfo = MetaInfo.builder()
            .udf1("udf1")
            .udf2("udf2")
            .udf3("udf3")
            .udf4("udf4")
            .udf5("udf5")
            .build();
 
StandardCheckoutPayRequest standardCheckoutPayRequest = StandardCheckoutPayRequest.builder()
        .merchantOrderId(merchantOrderId)
        .amount(amount)
        .redirectUrl(redirectUrl)
        .metaInfo(metaInfo)
        .build();
 
StandardCheckoutPayResponse standardCheckoutPayResponse = client.pay(standardCheckoutPayRequest);
String checkoutPageUrl = standardCheckoutPayResponse.getRedirectUrl();

Example using paymentModeConfig:
Pass either enabledPaymentModes or disabledPaymentModes in the request. Avoid setting both, as doing so will result in an error.
The following example demonstrates usage with only enabledPaymentModes.

Sample Request
import java.util.UUID;
import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.payments.v2.StandardCheckoutClient;
import com.phonepe.sdk.pg.payments.v2.models.request.StandardCheckoutPayRequest;
import com.phonepe.sdk.pg.payments.v2.models.response.StandardCheckoutPayResponse;
import com.phonepe.sdk.pg.common.models.MetaInfo;
import com.phonepe.sdk.pg.payments.v2.models.request.PaymentModeConfig;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.CardPaymentModeConstraint;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.CardType;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.NetBankingPaymentModeConstraint;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.PaymentModeConstraint;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.UpiCollectPaymentModeConstraint;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.UpiIntentPaymentModeConstraint;
import com.phonepe.sdk.pg.common.models.request.paymentmodeconstraints.UpiQrPaymentModeConstraint;


String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = clientVersion; //insert your client version here
Env env = Env.SANDBOX; //change to Env.PRODUCTION when you go live

StandardCheckoutClient client = StandardCheckoutClient.getInstance(clientId, clientSecret,
    clientVersion, env);

String merchantOrderId = UUID.randomUUID().toString();
long amount = 100;
String redirectUrl = "https://redirectUrl.com";
MetaInfo metaInfo = MetaInfo.builder()
    .udf1("udf1")
    .udf2("udf2")
    .udf3("udf3")
    .udf4("udf4")
    .udf5("udf5")
    .build();

Set < CardType > allowedCardTypes = new HashSet < > ();
allowedCardTypes.add(CardType.DEBIT_CARD);
allowedCardTypes.add(CardType.CREDIT_CARD);
PaymentModeConstraint cardPaymentModeConstraint = CardPaymentModeConstraint.builder()
    .cardTypes(allowedCardTypes)
    .build();
PaymentModeConstraint netbanking = NetBankingPaymentModeConstraint.builder().build();
PaymentModeConstraint upiIntent = UpiIntentPaymentModeConstraint.builder().build();
PaymentModeConstraint upiQr = UpiQrPaymentModeConstraint.builder().build();
PaymentModeConstraint upiCollect = UpiCollectPaymentModeConstraint.builder().build();
PaymentModeConfig paymentModeConfigEnabled = PaymentModeConfig.builder()
    .enabledPaymentModes(Arrays.asList(cardPaymentModeConstraint, netbanking, upiIntent, upiQr))
    .build();

StandardCheckoutPayRequest standardCheckoutPayRequest = StandardCheckoutPayRequest.builder()
    .merchantOrderId(merchantOrderId)
    .amount(amount)
    .redirectUrl(redirectUrl)
    .metaInfo(metaInfo)
    .paymentModeConfig(paymentModeConfigEnabled)
    .build();

StandardCheckoutPayResponse standardCheckoutPayResponse = client.pay(standardCheckoutPayRequest);
String checkoutPageUrl = standardCheckoutPayResponse.getRedirectUrl();

The function returns a StandardCheckoutPayResponse object with the following properties:

Parameter NameData TypeDescription
orderIdStringOrder Id created by PhonePe.
stateStringState of the order. Initially it will be PENDING.
expireAtLongOrder expiry timestamp in epoch format.
redirectUrlStringURL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment.

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?