Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

React.js Project to Integrate Paypal Payment Gateway Button inside Browser Using JSX & Javascript

Posted on January 30, 2023

 

 

Welcome folks today in this blog post we will be integrating paypal payment gateway button inside the browser using jsx and javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js project using the below command as shown below

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And after that you need to install the below libraries using the below command as shown below

 

 

npm i @paypal/react-paypal-js

 

 

And after that you will see the below directory structure of the react.js app as shown below

 

 

 

 

Now we need to copy paste the below code inside the App.js file as shown below

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { PayPalButtons, PayPalScriptProvider } from '@paypal/react-paypal-js';
import { useState } from 'react';
import './App.css';
function App() {
  const [show, setShow] = useState(false)
  const [success, setSuccess] = useState(false)
  const [errorMessage, setErrorMessage] = useState('')
  const [orderId, setOrderId] = useState(false)
  
  const createOrder = (data, actions) => {
    return actions.order.create({
      purchase_units: [
        {
          description: 'This is the Book Worth 100
,
          amount: {
            currency_code: 'USD',
            value:100
          },
        },  
      ],
      application_context: {
        shipping_preference:'NO_SHIPPING'
      }
    })
      .then((orderID) => {
        setOrderId(orderID)
        return orderID
    })
  }
  const onApprove = (data,actions) => {
    return actions.order.capture().then(function (details) {
      const { payer } = details
      setSuccess(true)
    })
  }
  const onError = (data, actions) => {
    setErrorMessage("An error occured with your payment")
  }
  return (
    <div className="App">
      <PayPalScriptProvider
        options={{
          "client-id":
            "###replacethiswithyourownclientid###"
        }}
      >
      <h1>Simple Book Worth 100lt;/h1>
      <span>100lt;/span>
      <button onClick={() => setShow(true) } type='submit'>
        Buy Now
      </button>
      {show ? (
          <PayPalButtons style={{ layout: 'vertical' }} createOrder={createOrder}
            onApprove={onApprove} onError={onError}/>
        ) : null}
        
        {success ? (
          <h1>Your Payment has been done successfully please check email</h1>
        ):<h2>payment is pending</h2>}
      
    </PayPalScriptProvider>
    </div>
  );
}
export default App;

 

 

Here you need to replace the clientid inside the above code. And here we are using the PaypalButtons library to render the buttons and here we are initializing the different events which is when the customer is creating the order and error happened.

 

 

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme