<?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: Abhee Hudani ☕ </title>
    <description>The latest articles on DEV Community by Abhee Hudani ☕  (@hudaniabhee).</description>
    <link>https://dev.to/hudaniabhee</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%2F488963%2F6577f922-016d-4c95-8be7-cc327f5e75bc.jpg</url>
      <title>DEV Community: Abhee Hudani ☕ </title>
      <link>https://dev.to/hudaniabhee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hudaniabhee"/>
    <language>en</language>
    <item>
      <title>Setup Nginx with HTTPS on Azure VM using Certbot</title>
      <dc:creator>Abhee Hudani ☕ </dc:creator>
      <pubDate>Sat, 20 Feb 2021 08:49:05 +0000</pubDate>
      <link>https://dev.to/hudaniabhee/setup-nginx-with-https-on-azure-vm-using-certbot-1nhp</link>
      <guid>https://dev.to/hudaniabhee/setup-nginx-with-https-on-azure-vm-using-certbot-1nhp</guid>
      <description>&lt;p&gt;We will setup HTTPS on Nginx using Certbot on an Azure Ubuntu VM&lt;/p&gt;

&lt;h4&gt;
  
  
  Steps
&lt;/h4&gt;

&lt;p&gt;1) Create VM&lt;br&gt;
2) Config Ports&lt;br&gt;
3) Install Nginx&lt;br&gt;
4) HTTPS using Certbot&lt;/p&gt;


&lt;h4&gt;
  
  
  Creating Azure Ubuntu VM
&lt;/h4&gt;



&lt;ul&gt;
&lt;li&gt;Let's get started by creating Azure VM.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1DtkvlGhZG9wVs7WN9AS28fEkz-wXQe6R" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1DtkvlGhZG9wVs7WN9AS28fEkz-wXQe6R" alt="Azure Ubuntu VM Config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; I'm using ubuntu 18.04 LTS -Ge1  as OS Image and Size :Standard_B1ls - 1vcps. 
With SSH connection on PORT 22.&lt;/li&gt;
&lt;li&gt;After that I'm going with default settings.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1mB9YxaPfMZJaTFSLExhFXORuXko2IwyT" alt="Azure Ubuntu VM SSH"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-- SAVE YOUR PRIVATE KEY, KEEP IT SECURE AND DON'T SHARE IT.&lt;/p&gt;


&lt;h4&gt;
  
  
  Azure Server DNS Config
&lt;/h4&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1IhLAHfN0fdNNleGWP6zAPJN5avO97I5G" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1IhLAHfN0fdNNleGWP6zAPJN5avO97I5G" alt="Azure VM Config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now we change Dynamic to Static Assigment&lt;/li&gt;
&lt;li&gt;We set the Assignment as Static then we choose a DNS name label. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D19SZsl9CS3w9MbRqcTJPcBmHRTlu1nEt4" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D19SZsl9CS3w9MbRqcTJPcBmHRTlu1nEt4" alt="Azure VM Config DNS "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here we choose nova-test therefore the VM will be accessible at nova-test.eastus2.cloudapp.azure.com.&lt;/li&gt;
&lt;li&gt;After it is saved let's connect our azure using SSH in terminal.&lt;/li&gt;
&lt;/ul&gt;


&lt;h4&gt;
  
  
  SSH connection to server
&lt;/h4&gt;



&lt;p&gt;Now let's connect our terminal to server using SSH.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: You can always find intructions in VM's Connect Tab&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my PC, I have saved my Private Key(Generated during VM creation) in a root folder called "Key".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection String &lt;code&gt;

ssh -i key.pem user_name@server_name 

&lt;/code&gt;
&amp;gt; Note: You can also use public IP to connect instead of server_name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1QU5zkW8ShSm6PAnCSwjOyOplKWkxRifF" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1QU5zkW8ShSm6PAnCSwjOyOplKWkxRifF" alt="Azure VM SSH Key "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;But before we access the key we need to change the permission of the private key otherwise you might face an issue saying, "Unprotected Private Key File!"&lt;/li&gt;
&lt;li&gt;In order to do that, we need to goto key's dir and fire these commands&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 chmod 600 test-vm_key.pem 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And Volla! We are connected to our server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1wVoeMm8o44kr8u9Ud9hwjW0LhP3jiCL1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1wVoeMm8o44kr8u9Ud9hwjW0LhP3jiCL1" alt="Azure VM SSH Connection "&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  Nginx Setup
&lt;/h4&gt;



&lt;p&gt;Now let's get started with our nginx server !&lt;br&gt;
Install nginx server first:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;nginx


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And Done ???&lt;br&gt;
-- Well technically, yes. Server is ready.&lt;br&gt;
-- But wait you can't access it from browser right now because we haven't added port rule.&lt;br&gt;
-- To access the server we need to add PORT 80 Access Rule to our VM.&lt;br&gt;
-- In order to add rule for port 80, goto Networking Tab and click ```Add&lt;br&gt;
&lt;br&gt;
 Inbound Port Rule&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 ![Azure VM Inbound Port ](https://drive.google.com/uc?export=view&amp;amp;id=1WFNE4p_lD-59y3bH730wjhxwcLPuP3i8)

 ![Azure VM Add New Inbound Port ](https://drive.google.com/uc?export=view&amp;amp;id=1_haaIwqWGkausry4PBgwLQMt3FJxaLqu)

&amp;gt; Note: You can always check your inbound port rules and add custom rules in VM's Networking Tab.

Now our server can be accessed by browser by visiting  nova-test.eastus2.cloudapp.azure.com

 ![Azure VM Server HTTP Nginx ](https://drive.google.com/uc?export=view&amp;amp;id=1ydU3QSmROg45HsN8SeQi17XcghFlBjQI)

As you can see our server is ready but it not secure. We can solve that by adding SSL certificate to our server.

-- And ....yes about the bookmarkbar, I forgot to hide it.

---
#### SSL Certificate using Certbot
---
In order to do that we need to add one last PORT 443 rule. As we know HTTPS runs on port 443 and without 443 access browser can't use https connection.

 ![Azure VM Server 443 Rule ](https://drive.google.com/uc?export=view&amp;amp;id=1MkZR6BWfMIP8Zayh9wq-xoBAonkJ_wEW)

Now we are ready for SSL certificate.

##### Installing Certbot
----

Fire following commands to get started:
-- Installing and updating snap
```sh


$ sudo snap install core; sudo snap refresh core


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;-- Installing Certbot&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--classic&lt;/span&gt; certbot


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: For More Info visit: &lt;a href="https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx.html" rel="noopener noreferrer"&gt;https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Nginx Server Config
&lt;/h4&gt;




&lt;p&gt;Before we initialize our certification process we need to configure our nginx default file.&lt;br&gt;
-- Fire following commands to goto nginx default file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 cd /etc/nginx/sites-available

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;-- Now edit default file. I'm using nano as text edtor.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 sudo nano default

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Add domain after &lt;code&gt;server_name

your_domain;

&lt;/code&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1N8HfSmD2SLpZ9I7xbu-1NJlNnuxw4xJD" alt="Azure VM Server Nginx Default "&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-- Save the file by pressing Ctrl+O, Enter(To Save). Ctrl+X (To Exit)&lt;/p&gt;

&lt;h4&gt;
  
  
  Certbot --nginx
&lt;/h4&gt;




&lt;p&gt;Run this command to get a certificate and have Certbot edit your Nginx configuration automatically to serve it, turning on HTTPS access in a single step.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot &lt;span class="nt"&gt;--nginx&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;-- Fill the details.&lt;br&gt;
-- Once all the steps completed, you will see message similiar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1UXvYuQ4TdO5-UqAcXvhK6NVLOBwIOzB9" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1UXvYuQ4TdO5-UqAcXvhK6NVLOBwIOzB9" alt="Azure VM Server Nginx Default "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations ! We have successfully added certificate to our server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1v4WJvMbbhzumC_ttYJlY7I-taeykwOOw" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1v4WJvMbbhzumC_ttYJlY7I-taeykwOOw" alt="Azure VM Server Nginx Default "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;VOLLA ! We did it. There is just one step remaining which is to restart nginx server.&lt;br&gt;
We can restart it by following command:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service nginx reload&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;We did it, we setup HTTPS on our server for free using Certbot. We did it by creating a VM on Azure and used a custom DNS, added port inbound rules for PORT 80 &amp;amp; 443, installed Nginx and configure default Nginx file. At the end we installed a SSL certification and configured Nginx with Certbot.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>nginx</category>
      <category>ubuntu</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Payment Handling with Stripe, Node and Express</title>
      <dc:creator>Abhee Hudani ☕ </dc:creator>
      <pubDate>Sun, 18 Oct 2020 12:48:24 +0000</pubDate>
      <link>https://dev.to/hudaniabhee/payment-handling-with-stripe-node-and-express-4gh0</link>
      <guid>https://dev.to/hudaniabhee/payment-handling-with-stripe-node-and-express-4gh0</guid>
      <description>&lt;h1&gt;
  
  
  Why do you need a payment gateway ?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Let's just assume that we are creating an e-commerce shopping website. Payment handling is one of the crucial part in e-commerce site. &lt;/li&gt;
&lt;li&gt;There are many other payment gateways are available like PayPal, Amazon Pay, World Pay, Stripe etc. But in this tutorial we are going to stick with Stripe.&lt;/li&gt;
&lt;li&gt;And using a payment gateway is not just for transferring money, but it has other benefits as well.&lt;/li&gt;
&lt;li&gt;A payment gateway focuses on creating a secure pathway between a customer and the merchant to facilitate payments securely.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this tutorial we are going to create payment gateway service for Node JS using STRIPE.&lt;/p&gt;

&lt;h4&gt;
  
  
  Github Repo Link and Postman Collection: &lt;a href="https://github.com/Novartus/Node-Stripe" rel="noopener noreferrer"&gt;Click Here&lt;/a&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  What are we building here:
&lt;/h3&gt;

&lt;p&gt;1) Saving Customer's Details to Stripe&lt;br&gt;
2) Add customer's Card in Stripe.&lt;br&gt;
3) View Customer's Cards.&lt;br&gt;
4) Update Customer's Cards.&lt;br&gt;
5) Delete Customer's Card from stripe&lt;br&gt;
6) Payment Checkout with Saved Card.&lt;br&gt;
7) One Time Payment Checkout without saving card info.&lt;/p&gt;
&lt;h3&gt;
  
  
  How we are gonna save customer sensitive card details?
&lt;/h3&gt;

&lt;p&gt;Instead of saving card details directly by passing them to API, we are going to create a charge first and after that we are going to save the charge details to customer. This we are not going to handle card information directly and our production integration is developed in a &lt;a href="https://stripe.com/docs/security#pci-dss-guidelines" rel="noopener noreferrer"&gt;PCI Compliant&lt;/a&gt; manner. Which is also recommended by the Stripe.&lt;/p&gt;
&lt;h3&gt;
  
  
  What do we need ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Stripe API Key&lt;/li&gt;
&lt;li&gt;Node version &amp;gt;= 12 installed in your system&lt;/li&gt;
&lt;li&gt;Postman/ Insomnia (Or any other software for testing API Calls)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Getting the stripe API Key:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can get your own stripe API key by logging into &lt;a href="https://dashboard.stripe.com/" rel="noopener noreferrer"&gt;Stripe Dashboard&lt;/a&gt; and get the test key. It will look something like this 'sk_live_...3Vls'.&lt;/li&gt;
&lt;li&gt;In this tutorial we are gonna use the default sandbox key from &lt;a href="https://stripe.com/docs/api/authentication" rel="noopener noreferrer"&gt;here&lt;/a&gt; which is provided by stripe. &lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Note:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are using your own API key, then make sure to turn on "TEST MODE" from your Stripe Dashboard otherwise you might face some unexpected charges during the test and you will be able to see the test transactions in your dashboard.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Getting Started:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Stripe Charges the amount in cents so if you want to charge $200 then your amount would be 20000 ($200 x 100 = 20000).&lt;/li&gt;
&lt;li&gt;We are not using any Database here so I'm just gonna store customer ID as a Constant during this tutorial. So if you use this make sure to connect to Database if you want to store customer's stripe data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Express, Stripe-Node packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express 
&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; stripe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Index.js
&lt;/h3&gt;

&lt;p&gt;Ok now we get to create our first file called index.js . Once created, at the top of the file we want to include all of the dependencies that we will need for our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const express &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"express"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const app &lt;span class="o"&gt;=&lt;/span&gt; express&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const port &lt;span class="o"&gt;=&lt;/span&gt; 3000&lt;span class="p"&gt;;&lt;/span&gt;

app.use&lt;span class="o"&gt;(&lt;/span&gt;express.json&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we want to test and make sure our server is working, we will run this function to listen on port 3000 and log a string if its successful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;app.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;, &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  res.send&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello World!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.listen&lt;span class="o"&gt;(&lt;/span&gt;port, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;App listening at http://localhost:&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you open your browser and type: &lt;a href="http://localhost:3000/" rel="noopener noreferrer"&gt;http://localhost:3000/&lt;/a&gt; you will see ' Hello World!' in your browser.&lt;/p&gt;

&lt;p&gt;At this point our index.js file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const express &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"express"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const app &lt;span class="o"&gt;=&lt;/span&gt; express&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const port &lt;span class="o"&gt;=&lt;/span&gt; 3000&lt;span class="p"&gt;;&lt;/span&gt;

app.use&lt;span class="o"&gt;(&lt;/span&gt;express.json&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;, &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    res.send&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello World!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.listen&lt;span class="o"&gt;(&lt;/span&gt;port, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;App listening at http://localhost:&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stripe.js
&lt;/h3&gt;

&lt;p&gt;Let's create a new file called strie.js which will be used for handling all stripe calls. Once created we are going to create a simple GET request and exporting our route file so index.js can access it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const express &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"express"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const router &lt;span class="o"&gt;=&lt;/span&gt; express.Router&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

router.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;, &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.json&lt;span class="o"&gt;({&lt;/span&gt;
    message: &lt;span class="s2"&gt;"Stripe Hello World!"&lt;/span&gt;,
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

module.exports &lt;span class="o"&gt;=&lt;/span&gt; router&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Adding stripe.js route to index.js
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const stripe &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./stripe"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
app.use&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/"&lt;/span&gt;, stripe&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the index.js will something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const express &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"express"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const app &lt;span class="o"&gt;=&lt;/span&gt; express&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const stripe &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./stripe"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const port &lt;span class="o"&gt;=&lt;/span&gt; 3000&lt;span class="p"&gt;;&lt;/span&gt;

app.use&lt;span class="o"&gt;(&lt;/span&gt;express.json&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.use&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/api/"&lt;/span&gt;, stripe&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;, &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  res.send&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello World!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

app.listen&lt;span class="o"&gt;(&lt;/span&gt;port, &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;App listening at http://localhost:&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you open your browser and type: &lt;a href="http://localhost:3000/api/" rel="noopener noreferrer"&gt;http://localhost:3000/api/&lt;/a&gt; you will see ' Stripe Hello World!' in your browser.&lt;/p&gt;




&lt;h4&gt;
  
  
  Creating a new Stripe Customer
&lt;/h4&gt;




&lt;p&gt;In Stripe.js file let's add the stripe key as a constant and adding stripe module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const Stripe_Key &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'sk_test_....jQb'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const stripe &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"stripe"&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;Stripe_Key&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and let's create an api which will create a new customer id from user's email address&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D18tF1PL-b-g8Modnsd1gKGs9IyEm_zWe0" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D18tF1PL-b-g8Modnsd1gKGs9IyEm_zWe0" alt="Creating a new Stripe Customer Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note&lt;br&gt;
If you are using your own API key then you can add your organization account Secret Key. So it will link the customer with your organization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can add some extra fileds during creating the new customer using stripe API which you can found here on &lt;a href="https://stripe.com/docs/api/customers/object" rel="noopener noreferrer"&gt;Stripe Documents&lt;/a&gt;&lt;br&gt;
Currently we are not using any Database so we have to note down the customer id somewhere from our response message.&lt;/p&gt;
&lt;h5&gt;
  
  
  Response :
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1zDaVceP-8w6a22J-Pl1fbuQEqDWb3Xcv" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1zDaVceP-8w6a22J-Pl1fbuQEqDWb3Xcv" alt="New Customer Response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's Note Down Our Customer ID here: 'cus_IDxxDETTAorXTO'&lt;/li&gt;
&lt;/ul&gt;


&lt;h4&gt;
  
  
  Adding a card to the existing customer
&lt;/h4&gt;



&lt;p&gt;For testing purposes we don't need to use real card data. Stripe has given some list of the card for the testing purposes and we are going to use it in this. You can find it here &lt;a href="https://stripe.com/docs/testing" rel="noopener noreferrer"&gt;Stripe Test Cards&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Card Number, Expiry Month, Year, CVC these are the required parameters for adding a new card. So if those parameters are not passed in the body then we are going throw a bad request error as the response.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1rwMkJuFDP_-0y69AQAvb5kzHTOhPX-HG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1rwMkJuFDP_-0y69AQAvb5kzHTOhPX-HG" alt="Add Card Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Response :
&lt;/h5&gt;
&lt;h2&gt;
  
  
  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D167gGIh2k51PtekWDGJNAAxI_B0ybhw0Q" alt="Add Card Response"&gt;
&lt;/h2&gt;
&lt;h4&gt;
  
  
  View Customer's All Cards
&lt;/h4&gt;



&lt;ul&gt;
&lt;li&gt;To get the list of customer's saved cards we need to just pass the customerID which was generated by the stripe earlier.&lt;/li&gt;
&lt;li&gt;In this case we only need cardId, Card Type, Expiry Details and Last 4 digit of the saved card. But if you need more data of the saved card you can find it here on &lt;a href="https://stripe.com/docs/api/cards/list" rel="noopener noreferrer"&gt;Stripe Docs for View saved Cards&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1fGIzMqH2-okWeIxdcePn4h9AHlxB2xlM" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1fGIzMqH2-okWeIxdcePn4h9AHlxB2xlM" alt="View Customer's All Cards Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Response :
&lt;/h5&gt;
&lt;h2&gt;
  
  
  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1H55Z8rs2NNLbL6kbTDx02SzwqUUxb2Vg" alt="View Cards Response"&gt;
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Update Card Details
&lt;/h4&gt;



&lt;ul&gt;
&lt;li&gt;From customer's saved card details we will get the cardID and from cardID we can update the saved card details like card holder name, expiry month, expiry year, address details etc. All the details except cardId is optional for update operation.&lt;/li&gt;
&lt;li&gt;If you need to update more fields then you can find it here on &lt;a href="https://stripe.com/docs/api/cards/update" rel="noopener noreferrer"&gt;Stripe Docs for Updating Card Details&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1g9u0Rsrfqw6K38szfBSBqAIbo3f0Afvi" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1g9u0Rsrfqw6K38szfBSBqAIbo3f0Afvi" alt="Update Card Details Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Response :
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1ICVw-3Bhf3uz6ur6HVomEbWkKWtNoZ-p" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1ICVw-3Bhf3uz6ur6HVomEbWkKWtNoZ-p" alt="Update Card Details Response"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  Delete Saved Card
&lt;/h4&gt;



&lt;p&gt;To delete a saved card we need to pass the ID of the savedCard&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1b38Xsre4uMfOrO3_OSmBByQP7dvx65TB" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1b38Xsre4uMfOrO3_OSmBByQP7dvx65TB" alt="Delete Saved Card Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Response :
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1AcFneB80phfB-wMaWfcR08rlfsqlSelw" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1AcFneB80phfB-wMaWfcR08rlfsqlSelw" alt="Delete Saved Card Response"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  Creating A Payment Charge
&lt;/h4&gt;



&lt;ul&gt;
&lt;li&gt;We have two choices over here 
-- Customer can pay from existing card.
-- Customer can pay without saving the new card or without existing cards.&lt;/li&gt;
&lt;li&gt;So we are going to use 'oneTime' parameter from request.body as a toggle switch between them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  One Time Payment with new Card
&lt;/h5&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const &lt;span class="o"&gt;{&lt;/span&gt; amount, email &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;
    const &lt;span class="o"&gt;{&lt;/span&gt;
      cardNumber,
      cardExpMonth,
      cardExpYear,
      cardCVC,
      country,
      postalCode,
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;cardNumber &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpMonth &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpYear &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardCVC&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: &lt;span class="s2"&gt;"Necessary Card Details are required for One Time Payment"&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    try &lt;span class="o"&gt;{&lt;/span&gt;
      const cardToken &lt;span class="o"&gt;=&lt;/span&gt; await stripe.tokens.create&lt;span class="o"&gt;({&lt;/span&gt;
        card: &lt;span class="o"&gt;{&lt;/span&gt;
          number: cardNumber,
          exp_month: cardExpMonth,
          exp_year: cardExpYear,
          cvc: cardCVC,
          address_state: country,
          address_zip: postalCode,
        &lt;span class="o"&gt;}&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      const charge &lt;span class="o"&gt;=&lt;/span&gt; await stripe.charges.create&lt;span class="o"&gt;({&lt;/span&gt;
        amount: amount,
        currency: &lt;span class="s2"&gt;"usd"&lt;/span&gt;,
        &lt;span class="nb"&gt;source&lt;/span&gt;: cardToken.id,
        receipt_email: email,
        description: &lt;span class="sb"&gt;`&lt;/span&gt;Stripe Charge Of Amount &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;One Time Payment&lt;span class="sb"&gt;`&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;charge.status &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"succeeded"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt; Success: charge &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res
          .status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;
          .send&lt;span class="o"&gt;({&lt;/span&gt; Error: &lt;span class="s2"&gt;"Please try again later for One Time Payment"&lt;/span&gt; &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: error.raw.message,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Payment with an existing Card
&lt;/h5&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const &lt;span class="o"&gt;{&lt;/span&gt; amount, cardId,  email &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;

try &lt;span class="o"&gt;{&lt;/span&gt;
      const createCharge &lt;span class="o"&gt;=&lt;/span&gt; await stripe.charges.create&lt;span class="o"&gt;({&lt;/span&gt;
        amount: amount,
        currency: &lt;span class="s2"&gt;"usd"&lt;/span&gt;,
        receipt_email: email,
        customer: customerId,
        card: cardId,
        description: &lt;span class="sb"&gt;`&lt;/span&gt;Stripe Charge Of Amount &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;Payment&lt;span class="sb"&gt;`&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;createCharge.status &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"succeeded"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt; Success: createCharge &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res
          .status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;
          .send&lt;span class="o"&gt;({&lt;/span&gt; Error: &lt;span class="s2"&gt;"Please try again later for payment"&lt;/span&gt; &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: error.raw.message,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Final Charge Payment Code
&lt;/h4&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1IOQ04__-aRWSmsCHQqxCsWhT1ZHpTE5O" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1IOQ04__-aRWSmsCHQqxCsWhT1ZHpTE5O" alt="Final Charge Payment Code Snippet"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  One Time Payment Response :
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1WdupkSpKNzReAIgb94Q3f_mXNeIs6VhT" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1WdupkSpKNzReAIgb94Q3f_mXNeIs6VhT" alt="One Time Payment Response"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Saved Card Payment Response :
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1ZsKl1lFAd2vyuIFkLCIexXsJZ-rDV7EE" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1ZsKl1lFAd2vyuIFkLCIexXsJZ-rDV7EE" alt="One Time Payment Response"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Final Stripe.JS
&lt;/h3&gt;



&lt;p&gt;Now the stripe.js will something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const express &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"express"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const router &lt;span class="o"&gt;=&lt;/span&gt; express.Router&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const Stripe_Key &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="s2"&gt;"sk_test_....Qb"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const stripe &lt;span class="o"&gt;=&lt;/span&gt; require&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"stripe"&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;Stripe_Key&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const customerId &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"cus_IDxx....orXTO"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

router.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;, &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.json&lt;span class="o"&gt;({&lt;/span&gt;
    message: &lt;span class="s2"&gt;"Stripe Hello World!"&lt;/span&gt;,
  &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Create a new customer &lt;span class="k"&gt;for &lt;/span&gt;stripe
router.post&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/newCustomer"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt; Body Passed:"&lt;/span&gt;, req.body&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    const customer &lt;span class="o"&gt;=&lt;/span&gt; await stripe.customers.create&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;{&lt;/span&gt;
        email: req.body.email,
      &lt;span class="o"&gt;}&lt;/span&gt;
      // &lt;span class="o"&gt;{&lt;/span&gt;
      //   // If you are using your own api &lt;span class="k"&gt;then &lt;/span&gt;you can add your organization account here. So it will &lt;span class="nb"&gt;link &lt;/span&gt;the customer with your organization
      //   stripeAccount: process.env.StripeAccountId,
      //&lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      //   customerDetails: customer,
      customerId: customer.id,
      customerEmail: customer.email,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt; Error: error.raw.message &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Add a new card of the customer
router.post&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/addNewCard"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt; Body Passed:"&lt;/span&gt;, req.body&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  const &lt;span class="o"&gt;{&lt;/span&gt;
    cardNumber,
    cardExpMonth,
    cardExpYear,
    cardCVC,
    cardName,
    country,
    postal_code,
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;cardNumber &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpMonth &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpYear &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardCVC&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: &lt;span class="s2"&gt;"Please Provide All Necessary Details to save the card"&lt;/span&gt;,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    const cardToken &lt;span class="o"&gt;=&lt;/span&gt; await stripe.tokens.create&lt;span class="o"&gt;({&lt;/span&gt;
      card: &lt;span class="o"&gt;{&lt;/span&gt;
        name: cardName,
        number: cardNumber,
        exp_month: cardExpMonth,
        exp_year: cardExpYear,
        cvc: cardCVC,
        address_country: country,
        address_zip: postal_code,
      &lt;span class="o"&gt;}&lt;/span&gt;,
      // customer: customer.stripe_id,
      // stripe_account: StripeAccountId,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    const card &lt;span class="o"&gt;=&lt;/span&gt; await stripe.customers.createSource&lt;span class="o"&gt;(&lt;/span&gt;customerId, &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;source&lt;/span&gt;: &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;cardToken&lt;/span&gt;&lt;span class="p"&gt;.id&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      card: card.id,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: error.raw.message,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Get List of all saved card of the customers
router.get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/viewAllCards"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;let &lt;/span&gt;cards &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    const savedCards &lt;span class="o"&gt;=&lt;/span&gt; await stripe.customers.listSources&lt;span class="o"&gt;(&lt;/span&gt;customerId, &lt;span class="o"&gt;{&lt;/span&gt;
      object: &lt;span class="s2"&gt;"card"&lt;/span&gt;,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    const cardDetails &lt;span class="o"&gt;=&lt;/span&gt; Object.values&lt;span class="o"&gt;(&lt;/span&gt;savedCards.data&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    cardDetails.forEach&lt;span class="o"&gt;((&lt;/span&gt;cardData&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;let &lt;/span&gt;obj &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        cardId: cardData.id,
        cardType: cardData.brand,
        cardExpDetails: &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;cardData&lt;/span&gt;&lt;span class="p"&gt;.exp_month&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;/&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;cardData&lt;/span&gt;&lt;span class="p"&gt;.exp_year&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;,
        cardLast4: cardData.last4,
      &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      cards.push&lt;span class="o"&gt;(&lt;/span&gt;obj&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      cardDetails: cards,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: error.raw.message,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Update saved card details of the customer
router.post&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/updateCardDetails"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  const &lt;span class="o"&gt;{&lt;/span&gt; cardName, cardExpMonth, cardExpYear, cardId &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;cardId&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: &lt;span class="s2"&gt;"CardID is Required to update"&lt;/span&gt;,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    const card &lt;span class="o"&gt;=&lt;/span&gt; await stripe.customers.updateSource&lt;span class="o"&gt;(&lt;/span&gt;customerId, cardId, &lt;span class="o"&gt;{&lt;/span&gt;
      name: cardName,
      exp_month: cardExpMonth,
      exp_year: cardExpYear,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      updatedCard: card,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: error.raw.message,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Delete a saved card of the customer
router.post&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/deleteCard"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt; Body Passed:"&lt;/span&gt;, req.body&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  const &lt;span class="o"&gt;{&lt;/span&gt; cardId &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;cardId&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: &lt;span class="s2"&gt;"CardId is required to delete Card"&lt;/span&gt;,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  try &lt;span class="o"&gt;{&lt;/span&gt;
    const deleteCard &lt;span class="o"&gt;=&lt;/span&gt; await stripe.customers.deleteSource&lt;span class="o"&gt;(&lt;/span&gt;customerId, cardId&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;(&lt;/span&gt;deleteCard&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
      Error: error.raw.message,
    &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Create a payment charge
router.post&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/createCharge"&lt;/span&gt;, async &lt;span class="o"&gt;(&lt;/span&gt;req, res&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  const &lt;span class="o"&gt;{&lt;/span&gt; amount, cardId, oneTime, email &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;oneTime&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    const &lt;span class="o"&gt;{&lt;/span&gt;
      cardNumber,
      cardExpMonth,
      cardExpYear,
      cardCVC,
      country,
      postalCode,
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; req.body&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;cardNumber &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpMonth &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardExpYear &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;cardCVC&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: &lt;span class="s2"&gt;"Necessary Card Details are required for One Time Payment"&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    try &lt;span class="o"&gt;{&lt;/span&gt;
      const cardToken &lt;span class="o"&gt;=&lt;/span&gt; await stripe.tokens.create&lt;span class="o"&gt;({&lt;/span&gt;
        card: &lt;span class="o"&gt;{&lt;/span&gt;
          number: cardNumber,
          exp_month: cardExpMonth,
          exp_year: cardExpYear,
          cvc: cardCVC,
          address_state: country,
          address_zip: postalCode,
        &lt;span class="o"&gt;}&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      const charge &lt;span class="o"&gt;=&lt;/span&gt; await stripe.charges.create&lt;span class="o"&gt;({&lt;/span&gt;
        amount: amount,
        currency: &lt;span class="s2"&gt;"usd"&lt;/span&gt;,
        &lt;span class="nb"&gt;source&lt;/span&gt;: cardToken.id,
        receipt_email: email,
        description: &lt;span class="sb"&gt;`&lt;/span&gt;Stripe Charge Of Amount &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;One Time Payment&lt;span class="sb"&gt;`&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;charge.status &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"succeeded"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt; Success: charge &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res
          .status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;
          .send&lt;span class="o"&gt;({&lt;/span&gt; Error: &lt;span class="s2"&gt;"Please try again later for One Time Payment"&lt;/span&gt; &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: error.raw.message,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    try &lt;span class="o"&gt;{&lt;/span&gt;
      const createCharge &lt;span class="o"&gt;=&lt;/span&gt; await stripe.charges.create&lt;span class="o"&gt;({&lt;/span&gt;
        amount: amount,
        currency: &lt;span class="s2"&gt;"usd"&lt;/span&gt;,
        receipt_email: email,
        customer: customerId,
        card: cardId,
        description: &lt;span class="sb"&gt;`&lt;/span&gt;Stripe Charge Of Amount &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;Payment&lt;span class="sb"&gt;`&lt;/span&gt;,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;createCharge.status &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"succeeded"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;200&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt; Success: charge &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return &lt;/span&gt;res
          .status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;
          .send&lt;span class="o"&gt;({&lt;/span&gt; Error: &lt;span class="s2"&gt;"Please try again later for payment"&lt;/span&gt; &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; catch &lt;span class="o"&gt;(&lt;/span&gt;error&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;res.status&lt;span class="o"&gt;(&lt;/span&gt;400&lt;span class="o"&gt;)&lt;/span&gt;.send&lt;span class="o"&gt;({&lt;/span&gt;
        Error: error.raw.message,
      &lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

module.exports &lt;span class="o"&gt;=&lt;/span&gt; router&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This is the totally working code to handle payment and its related data. All you have to do is replace the sandbox key with your Strip API key and it will work perfectly. Let me know if it is not working or anything I would really appreciate your feedbacks.&lt;/p&gt;

</description>
      <category>stripe</category>
      <category>node</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Dealing with Timezone in JavaScript</title>
      <dc:creator>Abhee Hudani ☕ </dc:creator>
      <pubDate>Tue, 13 Oct 2020 17:58:13 +0000</pubDate>
      <link>https://dev.to/hudaniabhee/dealing-with-timezone-javascript-47p8</link>
      <guid>https://dev.to/hudaniabhee/dealing-with-timezone-javascript-47p8</guid>
      <description>&lt;h3&gt;
  
  
  Dealing With Different Timezone in JavaScript
&lt;/h3&gt;

&lt;h5&gt;
  
  
  At some point in developer's life he has to deal with different timezones. And I guess it was my turn.
&lt;/h5&gt;

&lt;h4&gt;
  
  
  Why we need to deal with timezone ?
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;So let's just say that, one user from India, one from Canada, and one from Australia makes a new request for 10th January 2020. But the thing is that date is not same for all the users because of the different timezone which they are following.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's frame this in another way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are creating an international cab service, which accepts user's request date and time and according to that cab driver will provide service. If no cab driver accepts user's service before 8 hours then it will be marked as expired.
&lt;/li&gt;
&lt;li&gt;Now you get a call from Australia and a customer will say, by the way we are  9.5 hour ahead from your country. And you will be like okay we can make an exception. Now after 3 hours a customer calls from India and says that by the way we are 4 hours ahead from your country. Now if you think about that, then you will realize  that there are more than  24 timezone. So at that time request expiration or deletion would not be easy because at that time we have to deal with all the timezone according to your customer.
Because at that time you use the same 10th Jan 2020 -12 Hours Logic then it won't work for the all the customers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How to deal with timezone ?
&lt;/h4&gt;

&lt;p&gt;So one way to deal this problem is with JavaScript's &lt;strong&gt;getTimezoneOffset()&lt;/strong&gt; function which returns the difference of request date and utc date in minutes.   &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getTimezoneOffset(): JavaScript getTimezoneOffset() method method returns the time difference between UTC time and local time, in minutes. It can either positive or negative depending upon user's local timezone from his machine.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;So what one can do is just store the 10th Jan 2020 for all the users as UTC Timestamp and in another DB column you can store the offSet Difference value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So whenever we want to check date for expiration or deletion purpose or any date related operations, we can just subtract the value (which is in minutes) from the original requested date. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So this way we can get the requested date in our current date and time. And after that we can find the difference between user's request date (which is now converted to our local timestamp) and current date(local time).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One Another way to deal this is using &lt;strong&gt;UNIX Timestamp&lt;/strong&gt;.   &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UNIX Time : Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds, the Unix epoch is 00:00:00 UTC on 1 January 1970.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;You can convert user's local time into UNIX time and then you can store it in the database and whenever you want to check for expiration or deletion you will just get the difference between request data's UNIX time and current time UNIX time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;I'm not saying this is 100% fail proof but I this solution worked for me. And I hope it will help you to give you a kick start. It might not be the best solution, but it gets the job done.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>timezone</category>
      <category>webde</category>
      <category>date</category>
    </item>
  </channel>
</rss>
