<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aritra Dutta</title>
    <description>The latest articles on DEV Community by Aritra Dutta (@aritra_dutta_07526ad4f6bb).</description>
    <link>https://dev.to/aritra_dutta_07526ad4f6bb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3211243%2F330165a4-ce4f-46f5-b480-72ba7ee5c822.jpg</url>
      <title>DEV Community: Aritra Dutta</title>
      <link>https://dev.to/aritra_dutta_07526ad4f6bb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aritra_dutta_07526ad4f6bb"/>
    <language>en</language>
    <item>
      <title>💳 PhonePe Payment Gateway Integration – A Complete Guide</title>
      <dc:creator>Aritra Dutta</dc:creator>
      <pubDate>Mon, 26 May 2025 19:55:38 +0000</pubDate>
      <link>https://dev.to/aritra_dutta_07526ad4f6bb/phonepe-payment-gateway-integration-a-complete-guide-49l8</link>
      <guid>https://dev.to/aritra_dutta_07526ad4f6bb/phonepe-payment-gateway-integration-a-complete-guide-49l8</guid>
      <description>&lt;p&gt;In today’s digital-first world, enabling seamless online payments is a must-have for any modern application. Whether you're running an e-commerce platform, a service-based website, or a mobile app, integrating a reliable and secure payment gateway is crucial. &lt;strong&gt;PhonePe&lt;/strong&gt; offers a robust &lt;strong&gt;Payment Gateway API&lt;/strong&gt;  for development and business purposes. Recently, I have integrated the same into my project, and I would like to share the steps to do it. I have shown the process using the UAT API and credentials of PhonePe, but I will also mention the steps to do the same using a merchant Id.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting with the integration steps, let's consider some of the prerequisites for the API to function properly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A registered PhonePe business account.&lt;/li&gt;
&lt;li&gt;A test API key and merchant credentials (from the PhonePe dashboard).&lt;/li&gt;
&lt;li&gt;Basic understanding of REST APIs, Node.js/Java/PHP (or your preferred backend language).&lt;/li&gt;
&lt;li&gt;Your application URL (for redirect and webhook setup).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting the credentials
&lt;/h2&gt;

&lt;p&gt;Go to the PhonePe Business Dashboard and sign up as a merchant. On approval of your account, PhonePe will give you the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merchant ID&lt;/li&gt;
&lt;li&gt;Salt Key&lt;/li&gt;
&lt;li&gt;Salt Index&lt;/li&gt;
&lt;li&gt;API Credentials&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Payment API Integration
&lt;/h2&gt;

&lt;p&gt;I will show the integration process step by step with UAT credentials. When integrating for a business application, replace the UAT credentials with those given by PhonePe.&lt;/p&gt;

&lt;p&gt;Payment API URL for Testing -&lt;br&gt;
&lt;a href="https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay" rel="noopener noreferrer"&gt;https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay&lt;/a&gt;&lt;br&gt;
Salt Key - 96434309-7796-489d-8924-ab56988a6076&lt;br&gt;
PhonePe Merchannt Id - PGTESTPAYUAT86.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request Headers -&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Header Name&lt;/th&gt;
      &lt;th&gt;Header Value&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Content-Type&lt;/td&gt;
      &lt;td&gt;X-VERIFY&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;application/json&lt;/td&gt;
      &lt;td&gt;SHA256(base64 encoded payload + “/pg/v1/pay” +
salt key) + ### + salt index&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Request/Payload Parameters-&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Parameter Name&lt;/th&gt;
      &lt;th&gt;Datatype&lt;/th&gt;
&lt;th&gt;Mandatory/Optional&lt;/th&gt;
&lt;th&gt;Possible Values&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;merchantId&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;Unique MerchantID assigned to the merchant by PhonePe
Note: merchantId length will be less than 38 characters.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;merchantTransactionId&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;Unique TransactionID generated by the merchant to track request to PhonePe
Note:
– merchantTransactionId length should be less than 35 characters.&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;amount&lt;/td&gt;
      &lt;td&gt;Long&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;Transaction amount in Paise.
Note: Amount should be greater than 100 (in Paise)&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;merchantUserId&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;**Mandatory** if paymentInstrument.type is:
● PAY_PAGE
● CARD
● SAVED_CARD
**Optional** for if paymentInstrument.type is:
● UPI_INTENT
● UPI_COLLECT
● UPI_QR
● TOKEN
● NET_BANKING&lt;/td&gt;
&lt;td&gt;Unique UserID of the users generated by the merchant.
Note:
– merchantUserId length should be less than 36 characters

&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;redirectUrl&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;The url where the user will be redirected after transaction completion. Note - The redirectUrl should be an existing page of the application.&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;redirectMode&lt;/td&gt;
      &lt;td&gt;Enum&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;● REDIRECT and
● POST&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;callbackUrl&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;The URL where server to server callback will be posted. Always POST&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;paymentInstrument&lt;/td&gt;
      &lt;td&gt;Object&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;PayMent Details&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;paymentInstrument.type&lt;/td&gt;
      &lt;td&gt;Enum&lt;/td&gt;
&lt;td&gt;Mandatory&lt;/td&gt;
&lt;td&gt;● PAY_PAGE
● CARD
● UPI_INTENT
● SAVED_CARD
● TOKEN
● UPI_COLLECT
● UPI_QR
● NET_BANKING&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;mobileNumber&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Optional&lt;/td&gt;
&lt;td&gt;Mobile number of the user
Note: There should not be any space.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Payload-&lt;/strong&gt;&lt;br&gt;
Firstly, we need to prepare the payload for the request. We will create an object with the fields which are mentioned above. Replace the PHONEPE_MERCHANT_ID with the merchant id provided by PhonePe. Also note that the amount should be in paise so we are multiplying it with 100.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let data = {
        merchantId: PHONEPE_MERCHANT_ID,
        merchantTransactionId: merchantTransactionId,
        name: customerName,
        mobileNumber: customerMobileNumber,
        amount: totalAmount * 100,
        redirectUrl:
          &amp;lt;YOUR_REDIRECT_URL&amp;gt;,
        callbackUrl:
          &amp;lt;YOUR_CALLBACK_URL&amp;gt;,
        redirectMode: "REDIRECT",
        paymentInstrument: {
          type: "PAY_PAGE",
        },
      };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have to convert this data to a string and take the base64 encoded value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const payload = JSON.stringify(data);
const payloadMain = Buffer.from(payload).toString("base64");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will concatenate the base64 encoded payload with "/pg/v1/pay" and the Salt key given by PhonePe. Now, hash the string using sha256 algorithm and prepare the checksum. The Salt index is 1 for UAT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const string = payloadMain + "/pg/v1/pay" + PHONEPE_SALT_KEY;
let sha256hash = crypto.createHash("sha256").update(string).digest("hex");
let checkSum = sha256hash + "###" + process.env.SALT_INDEX;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, prepare an object for axios to hit the payment API.&lt;br&gt;
Payment API URL for Testing -&lt;br&gt;
&lt;a href="https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay" rel="noopener noreferrer"&gt;https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let options = {
        method: "POST",
        URL: "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay",
        headers: {
          accept: "application/json",
          "Content-Type": "application/json",
          "X-VERIFY": checkSum,
        },
        data: {
          request: payloadMain,
        },
      };
let phonePayRes = await axios(options);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Payment Status API Integration
&lt;/h2&gt;

&lt;p&gt;Payment Staus API URL for Testing -&lt;br&gt;
&lt;a href="https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status" rel="noopener noreferrer"&gt;https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, it's time to check the status of the payment made.&lt;br&gt;
Create a string combining "/pg/v1/status/", the Merchant ID, and the transaction ID.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const string ="/pg/v1/status/" + PHONEPE_MERCHANT_ID + YOUR_TRANSACTION_ID + PHONEPE_SALT_KEY;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we will create the hash and checksum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sha256hash = crypto.createHash("sha256").update(string).digest("hex");
let checkSum = sha256hash + "###" + process.env.SALT_INDEX;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we will prepare the object to be passed to axios for hitting the API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let options = {
        method: "GET",
        url: https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/{PHONEPE_MERCHANT_ID}/{YOUR_TRANSACTION_ID},
        headers: {
          accept: "application/json",
          "Content-Type": "application/json",
          "X-VERIFY": checkSum,
        },
      };

let phonePeStsRes = await axios(options);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give the status of the transaction, whether it was a failure or a success.&lt;/p&gt;

&lt;p&gt;I hope this blog will help you integrate these APIs into your own application. I would be pleased to know if any points remained uncovered, so that I can further add them.&lt;/p&gt;

</description>
      <category>node</category>
      <category>backenddevelopment</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
