Giter Site home page Giter Site logo

thawani-react's Introduction

๐Ÿ› Thawani ECommerce Checkout ๐Ÿ›’

A simple React checkout component using Thawani ECommerce API.

Author: SkroooB

Thawani Checkout Docs

https://docs.thawani.om/

โš™ Installation

To use this component in your own project, simply clone the repository and install the required dependencies:

git clone https://github.com/SkroooB/thawani-react.git
cd thawani-react
npm install

๐Ÿ“š Usage

In your main application file, import the Checkout component and include it in your component tree with the necessary props:

import "./App.css";
import Checkout from "./Checkout";

function App() {
  return (
    <div>
      <h1>Thawani ECommerce Checkout Example</h1>
      <Checkout
        apiKey="your-api-key"
        pubKey="your-public-key"
        client_reference_id="your-client-reference-id"
        products={[{ name: "product 1", quantity: 1, unit_amount: 10000 }]}
        success_url="http://localhost:3000/success"
        cancel_url="http://localhost:3000/canceled"
        metadata={{ "Customer name": "somename", "order id": 0 }}
      />
    </div>
  );
}

export default App;

Replace your-api-key, your-public-key, and your-client-reference-id with your actual Thawani API key, public key, and client reference ID, respectively.

The products prop should be an array of objects with name, quantity, and unit_amount properties.

The success_url and cancel_url props should contain the URLs to redirect the customer to after a successful or canceled payment, respectively.

The metadata prop should be an object containing any additional data you want to associate with the checkout session.

๐Ÿ’ก Example Component

The Checkout creates a checkout session and redirects the customer to Thawani's checkout page.

import React, { useState } from "react";

const Checkout = ({
  apiKey,
  pubKey,
  client_reference_id,
  products,
  success_url,
  cancel_url,
  metadata,
}) => {
  const [sessionID, setSessionID] = useState(null);

  const createCheckoutSession = async () => {
    const requestOptions = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "thawani-api-key": apiKey,
      },
      body: JSON.stringify({
        client_reference_id,
        mode: "payment",
        products,
        success_url,
        cancel_url,
        metadata,
      }),
    };

    try {
      const response = await fetch(
        "https://uatcheckout.thawani.om/api/v1/checkout/session",
        requestOptions
      );
      const data = await response.json();

      if (data.success) {
        setSessionID(data.data.session_id);
        window.location.href = `https://uatcheckout.thawani.om/pay/${data.data.session_id}?key=${pubKey}`;
      } else {
        console.error("Error creating checkout session:", data.description);
      }
    } catch (error) {
      console.error("Error creating checkout session:", error);
    }
  };

  return (
    <div className="checkout">
      <button onClick={createCheckoutSession}>๐Ÿ› Proceed to Checkout</button>
      {sessionID && <p>๐Ÿ”‘ Session ID: {sessionID}</ID}</p>}
</div>
);
};

export default Checkout;

๐Ÿ“„ License

MIT License.

๐ŸŒŸ Acknowledgements

Special thanks to SkroooB and Social Mania Oman for their contributions.

thawani-react's People

Contributors

skrooob avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.