<?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: Suneel Kumar</title>
    <description>The latest articles on DEV Community by Suneel Kumar (@suneelk47250768).</description>
    <link>https://dev.to/suneelk47250768</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%2F1021129%2F1a010643-219a-4aee-8807-d03624a32366.png</url>
      <title>DEV Community: Suneel Kumar</title>
      <link>https://dev.to/suneelk47250768</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suneelk47250768"/>
    <language>en</language>
    <item>
      <title>How to Create a Stripe Subscription with React and Node.js</title>
      <dc:creator>Suneel Kumar</dc:creator>
      <pubDate>Sun, 05 Feb 2023 14:17:15 +0000</pubDate>
      <link>https://dev.to/suneelk47250768/how-to-create-a-stripe-subscription-with-react-and-nodejs-1k3e</link>
      <guid>https://dev.to/suneelk47250768/how-to-create-a-stripe-subscription-with-react-and-nodejs-1k3e</guid>
      <description>&lt;p&gt;How to Create a Stripe Subscription with React and Node.js&lt;/p&gt;

&lt;p&gt;Stripe is a popular payment gateway that allows businesses to accept payments online. In this article, we'll explore how to create a subscription system using React and Node.js. Subscriptions allow businesses to charge customers recurring fees, such as monthly or annual fees, for access to their products or services.&lt;/p&gt;

&lt;p&gt;Before we begin, it's important to note that you'll need to have a Stripe account set up and a basic understanding of React and Node.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the Backend with Node.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll start by setting up the backend using Node.js. We'll use the Express framework to build our API, and the Stripe npm package to interact with the Stripe API.&lt;/p&gt;

&lt;p&gt;To get started, create a new Node.js project and install the necessary packages:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mkdir my-subscription-app&lt;br&gt;
$ cd my-subscription-app&lt;br&gt;
$ npm init -y&lt;br&gt;
$ npm install express stripe&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, create an index.js file in the root of your project and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const stripe = require("stripe")("&amp;lt;your_stripe_secret_key&amp;gt;");

const app = express();
app.use(express.json());

app.post("/create-subscription", async (req, res) =&amp;gt; {
  const customer = await stripe.customers.create({
    email: req.body.email,
    source: req.body.stripeToken,
  });

  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ plan: req.body.plan }],
  });

  res.json({ subscription });
});

app.listen(3000,() =&amp;gt; {
console.log("Server running on port 3000");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we've created an endpoint at &lt;code&gt;/create-subscription&lt;/code&gt; that will create a new customer and a new subscription in Stripe when a POST request is made to this endpoint.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Setting up the Frontend with React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll set up the frontend using React. We'll use the react-stripe-elements library to easily integrate Stripe's payment form into our React app.&lt;/p&gt;

&lt;p&gt;To get started, create a new React project and install the necessary packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx create-react-app my-subscription-app-frontend
$ cd my-subscription-app-frontend
$ npm install @stripe/react-stripe-js @stripe/stripe-js react-stripe-elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a &lt;code&gt;SubscriptionForm.js&lt;/code&gt; file in your React project and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { useStripe, useElements, CardElement } from "@stripe/react-stripe-js";
import { createSubscription } from "./api";

const SubscriptionForm = () =&amp;gt; {
const [error, setError] = useState(null);
const [success, setSuccess] = useState(false);
const stripe = useStripe();
const elements = useElements();

const handleSubmit = async (event) =&amp;gt; {
event.preventDefault();
if (!stripe || !elements) {
  return;
}

const cardElement = elements.getElement(CardElement);

try {
  const { error, paymentMethod }
= await stripe.createPaymentMethod({
type: "card",
card: cardElement,
});
  if (error) {
    setError(error.message);
    return;
  }

  const { id } = paymentMethod;
  const response = await createSubscription({
    email: event.target.email.value,
    plan: event.target.plan.value,
    stripeToken: id,
  });

  if (response.error) {
    setError(response.error);
    return;
  }

  setSuccess(true);
} catch (error) {
  setError(error.message);
}
};

return (
&amp;lt;form onSubmit={handleSubmit}&amp;gt;
&amp;lt;input type="email" name="email" placeholder="Email" required /&amp;gt;
&amp;lt;select name="plan" required&amp;gt;
&amp;lt;option value="plan_1"&amp;gt;Plan 1&amp;lt;/option&amp;gt;
&amp;lt;option value="plan_2"&amp;gt;Plan 2&amp;lt;/option&amp;gt;
&amp;lt;option value="plan_3"&amp;gt;Plan 3&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;
&amp;lt;CardElement /&amp;gt;
{error &amp;amp;&amp;amp; &amp;lt;p style={{ color: "red" }}&amp;gt;{error}&amp;lt;/p&amp;gt;}
{success &amp;amp;&amp;amp; &amp;lt;p style={{ color: "green" }}&amp;gt;Subscription created!&amp;lt;/p&amp;gt;}
&amp;lt;button type="submit" disabled={!stripe}&amp;gt;
Subscribe
&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
);
};

export default SubscriptionForm;

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

&lt;/div&gt;



&lt;p&gt;In the code above, we've created a form that allows a user to enter their email, select a plan, and enter their payment information. When the form is submitted, the payment information is passed to the Stripe API to create a payment method, and then passed to our Node.js API to create a new customer and subscription.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Putting it all together&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, we'll need to bring the frontend and backend together by making the API call from the React app to the Node.js API.&lt;/p&gt;

&lt;p&gt;Create a new file &lt;code&gt;api.js&lt;/code&gt; in your React project and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const createSubscription = async (data) =&amp;gt; {
const response = await fetch("http://localhost:3000/create-subscription", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});

return response.json();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we're using the &lt;code&gt;fetch&lt;/code&gt; API to make a POST request to the &lt;code&gt;/create-subscription&lt;/code&gt; endpoint in our Node.js API.&lt;/p&gt;

&lt;p&gt;Now, import the &lt;code&gt;SubscriptionForm&lt;/code&gt; component into your React app's main file and render it to the screen.&lt;/p&gt;

&lt;p&gt;And that's it! You now have a working subscription system using React and Node.js with Stripe. Of course, there are many other features and optimizations you can add, but this should give you a solid foundation to build upon.&lt;/p&gt;

&lt;p&gt;please follow me on &lt;a href="https://codingmoze.com/" rel="noopener noreferrer"&gt;https://codingmoze.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this article helpful. If you have any questions or comments, please feel free to reach out!&lt;/p&gt;

</description>
      <category>announcement</category>
      <category>devto</category>
      <category>offers</category>
      <category>community</category>
    </item>
  </channel>
</rss>
