starmorph logo
Published on

Payment Processing in Nextjs 2023 - Stripe-React vs Commerce.js.

Authors

Stripe-react vs Commerce.js

Both Stripe and Commerce.js are popular choices for handling payments and e-commerce functionality in web applications. Here's a comparison of their pros and cons:

Stripe

(with @stripe/react-stripe-js and @stripe/stripe-js libraries):

Pros:

  1. Comprehensive payment platform: Stripe provides a wide range of features and tools for handling payments, including support for various payment methods, recurring payments, and more.
  2. Extensive documentation and resources: Stripe offers detailed documentation, guides, and examples, making it easier to integrate and customize the payment experience.
  3. Strong developer community: Stripe has a large developer community, which means you can find many resources, tutorials, and third-party libraries to help with your integration.
  4. High level of security: Stripe is PCI-compliant and handles sensitive payment information on their servers, reducing the security burden on your application.

Cons:

  1. Limited to payment processing: Stripe primarily focuses on payment processing and does not provide a full suite of e-commerce features like product management, inventory, and shipping.
  2. Potentially higher fees: Stripe's fees can be higher than some other payment processing platforms, depending on your transaction volume and business model.

Commerce.js

(with @chec/commerce.js library):

Pros:

  1. Full-featured e-commerce platform: Commerce.js provides a complete e-commerce solution, including product management, inventory, shipping, and payment processing.
  2. Flexible and customizable: Commerce.js offers a headless e-commerce platform, allowing you to build a custom front-end experience while leveraging their back-end services.
  3. Integration with multiple payment gateways: Commerce.js supports various payment gateways, including Stripe, PayPal, and others, giving you more flexibility in choosing your payment provider.
  4. Simplified development: Commerce.js handles many e-commerce-related tasks, reducing the amount of code and complexity in your application.

Cons:

  1. Less focused on payment processing: While Commerce.js supports payment processing, it may not offer the same level of features and customization as a dedicated payment platform like Stripe.
  2. Smaller developer community: Commerce.js has a smaller developer community compared to Stripe, which may result in fewer resources and third-party libraries available for integration.
  3. Additional cost: Commerce.js is a separate service with its own pricing, which may add to your overall costs if you're already using a payment processing platform like Stripe.

In summary, if you need a comprehensive e-commerce solution that handles product management, inventory, shipping, and payment processing, Commerce.js might be a better choice. However, if you're primarily focused on payment processing and require advanced features and customization, Stripe might be more suitable.

Getting Started with Commerce.js in your React / Next.js application

Let's start with Commerce.js. First, you need to install the Commerce.js SDK:

yarn add @chec/commerce.js

Next, create an instance of the Commerce.js client:

// commerce.js
import Commerce from '@chec/commerce.js'

const commerce = new Commerce(process.env.NEXT_PUBLIC_COMMERCEJS_PUBLIC_KEY, true)

export default commerce

Now, you can use the commerce instance to interact with the Commerce.js API.

Getting Started with Stripe-react in your Next.js application

For Stripe, first, install the @stripe/react-stripe-js and @stripe/stripe-js packages:

yarn add @stripe/react-stripe-js @stripe/stripe-js

Next, import the Elements component from @stripe/react-stripe-js and wrap your app or payment component with it:

// pages/_app.js
import { Elements } from '@stripe/react-stripe-js'
import { loadStripe } from '@stripe/stripe-js'

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY)

function MyApp({ Component, pageProps }) {
  return (
    <Elements stripe={stripePromise}>
      <Component {...pageProps} />
    </Elements>
  )
}

export default MyApp

Now, you can use Stripe components like CardElement and useStripe hook in your payment component:

// components/PaymentForm.js
import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js'

const PaymentForm = () => {
  const stripe = useStripe()
  const elements = useElements()

  const handleSubmit = async (event) => {
    event.preventDefault()

    if (!stripe || !elements) {
      return
    }

    const cardElement = elements.getElement(CardElement)

    const { error, paymentMethod } = await stripe.createPaymentMethod({
      type: 'card',
      card: cardElement,
    })

    if (error) {
      console.error('[error]', error)
    } else {
      console.log('[PaymentMethod]', paymentMethod)
      // Process the payment using your backend server
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <CardElement />
      <button type="submit" disabled={!stripe}>
        Pay
      </button>
    </form>
  )
}

export default PaymentForm

Now you have a basic setup for Commerce.js and Stripe React components. You can use these components to build your custom payment processing flow.

Here are some helpful links to get started with Commerce.js and Stripe React:

Commerce.js:

  1. Commerce.js Official Documentation: https://commercejs.com/docs/
  2. Commerce.js GitHub Repository: https://github.com/chec/commerce.js
  3. Commerce.js Example Projects: https://github.com/chec/commercejs-examples

Stripe React:

  1. Stripe React Official Documentation: https://stripe.com/docs/stripe-js/react
  2. Stripe React GitHub Repository: https://github.com/stripe/react-stripe-js
  3. Stripe React Example Projects: https://github.com/stripe-samples

These resources should help you get started with both Commerce.js and Stripe React. They provide comprehensive guides, examples, and best practices for integrating these libraries into your projects.