{"id":22583,"date":"2021-12-29T12:23:20","date_gmt":"2021-12-29T12:23:20","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=22583"},"modified":"2025-04-22T09:53:12","modified_gmt":"2025-04-22T09:53:12","slug":"integrate-paytm-payment-gateway-using-reactjs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/integrate-paytm-payment-gateway-using-reactjs","title":{"rendered":"How to Integrate Paytm Payment Gateway using ReactJS in Seven Easy Steps"},"content":{"rendered":"<p>Are you stuck with a requirement of integrating a payment gateway into your project? Are you aggressively looking for a simple tutorial regarding the same? If yes, this tutorial: Integrate Paytm payment gateway using ReactJs is for you! In this step-by-step guide, we will learn how to implement a payment gateway in seven easy steps. So, without further ado, let\u2019s get started with our tutorial.<\/p>\n<h2>Tutorial Goal: Integrate Paytm payment gateway using ReactJs<\/h2>\n<p>You might be wondering what we are going to do in this tutorial. We will build a small demo application consisting of a button \u2018Pay Now\u2019. With the click of the button a modal will be displayed as shown below. The library allows you to customize the modal and add various payment methods the way you want.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Paytm-payment-gateway-using-ReactJs-min-1024x559.jpg\" alt=\"Paytm payment gateway using ReactJs\" width=\"1024\" height=\"559\" class=\"aligncenter size-large wp-image-22590\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Paytm-payment-gateway-using-ReactJs-min-1024x559.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Paytm-payment-gateway-using-ReactJs-min-300x164.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Paytm-payment-gateway-using-ReactJs-min-768x419.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Paytm-payment-gateway-using-ReactJs-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Create a Paytm Developer Account<\/h2>\n<p>We would need to create new secret keys by creating a new developer account. <\/p>\n<p>Go to https:\/\/developer.paytm.com\/, login if you are already a Paytm user, or you can create a new account.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-Paytm-Developer-Account-min-1024x545.jpg\" alt=\"Create a Paytm Developer Account\" width=\"1024\" height=\"545\" class=\"aligncenter size-large wp-image-22591\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-Paytm-Developer-Account-min-1024x545.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-Paytm-Developer-Account-min-300x160.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-Paytm-Developer-Account-min-768x408.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-Paytm-Developer-Account-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Collect the API keys<\/h2>\n<p>Once you are logged in successfully, you can directly go to <a href=\"https:\/\/dashboard.paytm.com\/next\/apikeys\" target=\"_blank\" rel=\"noopener\">Dashboard<\/a> and check the Test API Details. Feel free to test the APIs using the test API keys. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Collect-the-API-keys-min-1024x545.jpg\" alt=\"Collect the API keys\" width=\"1024\" height=\"545\" class=\"aligncenter size-large wp-image-22592\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Collect-the-API-keys-min-1024x545.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Collect-the-API-keys-min-300x160.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Collect-the-API-keys-min-768x408.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Collect-the-API-keys-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Create a ReactJS Application<\/h2>\n<p>Use the following command to create a new reactJs application.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-ReactJS-Application-300x162.png\" alt=\"Create ReactJS Application\" width=\"600\" height=\"800\" class=\"aligncenter size-medium wp-image-22652\" \/><\/p>\n<p>A new project would be created with the default folder structure as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-ReactJS-Application-min-1024x559.jpg\" alt=\"Create a ReactJS Application\" width=\"1024\" height=\"559\" class=\"aligncenter size-large wp-image-22593\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-ReactJS-Application-min-1024x559.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-ReactJS-Application-min-300x164.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-ReactJS-Application-min-768x419.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Create-a-ReactJS-Application-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Adding the scripts to index.html<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-7-min-1024x162.jpg\" alt=\"Adding the scripts to index.html\" width=\"1024\" height=\"162\" class=\"aligncenter size-large wp-image-22611\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-7-min-1024x162.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-7-min-300x47.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-7-min-768x121.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-7-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><a href=\"https:\/\/www.bacancytechnology.com\/hire-reactjs-developer\" target=\"_blank\" rel=\"noopener\">Hire ReactJS developer<\/a> from us to add varied skillset to your existing in-house development. We can help you develop next-gen web solution from ideation to development to final delivery.<\/p>\n<h2>Logic &#038; UI: Paytm Payment Integration<\/h2>\n<p>Create a new file <mark>paytmButton.js<\/mark> inside <mark>\/paytm-button<\/mark> folder. This file will contain the main logic and functionality for the Paytm payment integration using ReactJS.<\/p>\n<p>The user interface would have a \u201cPay Now\u201d button that will trigger the Paytm checkout modal.<\/p>\n<p>Create a new function <mark>initializePaytm()<\/mark> that will contain all the initialization configurations and token generation steps that will be triggered on page load using useEffect.<\/p>\n<pre>const initialize = () => {\r\n   let orderId = \"Order_\" + new Date().getTime();\r\n \r\n   \/\/ Sandbox Credentials\r\n   let mid = \"\"; \/\/ Merchant ID\r\n   let mkey = \"\"; \/\/ Merchant Key\r\n   var paytmParams = {};\r\n \r\n   paytmParams.body = {\r\n     requestType: \"Payment\",\r\n     mid: mid,\r\n     websiteName: \"WEBSTAGING\",\r\n     orderId: orderId,\r\n     callbackUrl: \"https:\/\/merchant.com\/callback\",\r\n     txnAmount: {\r\n       value: 100,\r\n       currency: \"INR\",\r\n     },\r\n     userInfo: {\r\n       custId: \"1001\",\r\n     },\r\n   };\r\n \r\n   PaytmChecksum.generateSignature(\r\n     JSON.stringify(paytmParams.body),\r\n     mkey\r\n   ).then(function (checksum) {\r\n     console.log(checksum);\r\n     paytmParams.head = {\r\n       signature: checksum,\r\n     };\r\n \r\n     var post_data = JSON.stringify(paytmParams);\r\n \r\n     var options = {\r\n       \/* for Staging *\/\r\n       \/\/ hostname: \"securegw-stage.paytm.in\" \/* for Production *\/,\r\n \r\n       hostname: \"securegw.paytm.in\",\r\n \r\n       port: 443,\r\n       path: `\/theia\/api\/v1\/initiateTransaction?mid=${mid}&orderId=${orderId}`,\r\n       method: \"POST\",\r\n       headers: {\r\n         \"Content-Type\": \"application\/json\",\r\n         \"Content-Length\": post_data.length,\r\n       },\r\n     };\r\n \r\n     var response = \"\";\r\n     var post_req = https.request(options, function (post_res) {\r\n       post_res.on(\"data\", function (chunk) {\r\n         response += chunk;\r\n       });\r\n       post_res.on(\"end\", function () {\r\n         console.log(\"Response: \", response);\r\n         \/\/ res.json({data: JSON.parse(response), orderId: orderId, mid: mid, amount: amount});\r\n         setPaymentData({\r\n           ...paymentData,\r\n           token: JSON.parse(response).body.txnToken,\r\n           order: orderId,\r\n           mid: mid,\r\n           amount: 100,\r\n         });\r\n       });\r\n     });\r\n \r\n     post_req.write(post_data);\r\n     post_req.end();\r\n   });\r\n };\r\n\r\n<\/pre>\n<p><strong>Explanation<\/strong><\/p>\n<ul class=\"bullets text-left\">\n<li>This method is called on page load which will return a transaction token from Paytm which will be later used for initiating the Paytm checkout modal.<\/li>\n<li>To retrieve the token, we will make an API call to \/theia\/api\/v1\/initiateTransaction which will expect a basic transaction object in the request body along with a hashed value created using the transaction object. The paytmParam.body is a transaction object with basic fields like orderId, value, and currency.\n<pre>paytmParams.body = { ... };<\/pre>\n<\/li>\n<li>A hash value should be using this transaction object. For that Paytm provides a library &#8211; PaytmChecksum using which we can generate a hashed value by passing the transaction object as arguments.\n<pre>PaytmChecksum.generateSignature(\r\nJSON.stringify(paytmParams.body),mkey ).then(function(checksum){  ... \/\/ logic };<\/pre>\n<\/li>\n<li>This hash value will be sent along with the transaction object in the initiateTransaction API and will provide the transaction token in response. Later this token will be used when the user clicks on the \u2018Pay Now\u2019 button and will be helpful for triggering the payment checkout modal.<\/li>\n<\/ul>\n<p>Create a new function <mark>makePayment()<\/mark> that will be triggered on click of the button, which will use the already generated token and display the checkout modal to the user. In this function, you can modify the style of the Paytm checkout modal and change the color code and add your logo.<\/p>\n<pre>const makePayment = () => {\r\n        var config = {\r\n            \"root\":\"\",\r\n            \"style\": {\r\n              \"bodyBackgroundColor\": \"#fafafb\",\r\n              \"bodyColor\": \"\",\r\n              \"themeBackgroundColor\": \"#0FB8C9\",\r\n              \"themeColor\": \"#ffffff\",\r\n              \"headerBackgroundColor\": \"#284055\",\r\n              \"headerColor\": \"#ffffff\",\r\n              \"errorColor\": \"\",\r\n              \"successColor\": \"\",\r\n              \"card\": {\r\n                \"padding\": \"\",\r\n                \"backgroundColor\": \"\"\r\n              }\r\n            },\r\n            \"data\": {\r\n              \"orderId\": paymentData.order,\r\n              \"token\": paymentData.token,\r\n              \"tokenType\": \"TXN_TOKEN\",\r\n              \"amount\": paymentData.amount \/* update amount *\/\r\n            },\r\n            \"payMode\": {\r\n              \"labels\": {},\r\n              \"filter\": {\r\n                \"exclude\": []\r\n              },\r\n              \"order\": [\r\n                  \"CC\",\r\n                  \"DC\",\r\n                  \"NB\",\r\n                  \"UPI\",\r\n                  \"PPBL\",\r\n                  \"PPI\",\r\n                  \"BALANCE\"\r\n              ]\r\n            },\r\n            \"website\": \"WEBSTAGING\",\r\n            \"flow\": \"DEFAULT\",\r\n            \"merchant\": {\r\n              \"mid\": paymentData.mid,\r\n              \"redirect\": false\r\n            },\r\n            \"handler\": {\r\n              \"transactionStatus\":\r\nfunction transactionStatus(paymentStatus){\r\n                console.log(paymentStatus);\r\n              },\r\n              \"notifyMerchant\":\r\nfunction notifyMerchant(eventName,data){\r\n                console.log(\"Closed\");\r\n              }\r\n            }\r\n        };\r\n      \r\n        if (window.Paytm && window.Paytm.CheckoutJS) {\r\n       \twindow.Paytm.CheckoutJS.init(config).\r\nthen(function onSuccess() {\r\n      window.Paytm.CheckoutJS.invoke();\r\n}).catch(function onError(error) {\r\n      console.log(\"Error => \", error);\r\n});\r\n}}\t\t\r\n<\/pre>\n<p>Call the <mark>makePayment()<\/mark> method on click event of the button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-6-min-1024x279.jpg\" alt=\"Call the makePayment\" width=\"1024\" height=\"279\" class=\"aligncenter size-large wp-image-22612\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-6-min-1024x279.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-6-min-300x82.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-6-min-768x209.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-6-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Import PaytmButton in App.js<\/h2>\n<p>Once we are done with the main logic implementation it\u2019s time to import the file into App.js. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-5-min-1024x344.jpg\" alt=\"Import PaytmButton in App.js\" width=\"1024\" height=\"344\" class=\"aligncenter size-large wp-image-22613\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-5-min-1024x344.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-5-min-300x101.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-5-min-768x258.jpg 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/12\/Inner-image-5-min.jpg 1100w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>Run the Server<\/h2>\n<p>So, finally, we are done with the tutorial: How to Integrate Paytm Payment Gateway using ReactJS. Now using the below command, run your server.<\/p>\n<pre>npm start<\/pre>\n<p>Visit http:\/\/localhost:3000\/ and test the demo app.<br \/>\nThe entire source code is available on the github repo: <a href=\"https:\/\/github.com\/neelkhamar\/react-paytm-checkout\" target=\"_blank\" rel=\"noopener\">paytm-payment-gateway-integration<\/a>. Feel free to clone the repository and play around with the code.<\/p>\n<h2>Conclusion <\/h2>\n<p>I hope the tutorial has helped you learn how to integrate Paytm payment gateway using ReactJS. We value your feedback and suggestions, so feel free to write back. Bacancy being the best <a href=\"https:\/\/www.bacancytechnology.com\/reactjs-development\" target=\"_blank\" rel=\"noopener\">ReactJS development company<\/a> intend to explore more about basic and advanced ReactJS knowledge. If you are a ReactJS enthusiast, visit the <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/reactjs\" target=\"_blank\" rel=\"noopener\">ReactJS tutorials<\/a> page without wasting your time, clone the github repository, and polish your technical side.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you stuck with a requirement of integrating a payment gateway into your project? Are you aggressively looking for a simple tutorial regarding the same? If yes, this tutorial: Integrate Paytm payment gateway using ReactJs is for you! In this step-by-step guide, we will learn how to implement a payment gateway in seven easy steps. [&hellip;]<\/p>\n","protected":false},"author":151,"featured_media":22595,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[988],"tags":[],"coauthors":[2429],"class_list":["post-22583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"modified_by":"Binal Prajapati","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/22583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/151"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=22583"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/22583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/22595"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=22583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=22583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=22583"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=22583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}