Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

@paypal/react-paypal-js

npm install @paypal/react-paypal-js
Comment

paypal in react js

import {
    PayPalScriptProvider,
    PayPalHostedFieldsProvider,
    PayPalHostedField,
    usePayPalHostedFields,
} from "@paypal/react-paypal-js";

const SubmitPayment = () => {
    // Here declare the variable containing the hostedField instance
    const hostedFields = usePayPalHostedFields();

    const submitHandler = () => {
        if (!typeof hostedFields.submit !== "function") return; // validate that `submit()` exists before using it
        hostedFields
            .submit({
                // The full name as shown in the card and billing address
                cardholderName: "John Wick",
            })
            .then((order) => {
                fetch(
                    "/your-server-side-integration-endpoint/capture-payment-info"
                )
                    .then((response) => response.json())
                    .then((data) => {
                        // Inside the data you can find all the information related to the payment
                    })
                    .catch((err) => {
                        // Handle any error
                    });
            });
    };

    return <button onClick={submitHandler}>Pay</button>;
};

export default function App() {
    return (
        <PayPalScriptProvider
            options={{
                "client-id": "your-client-id",
                "data-client-token": "your-data-client-token",
            }}
        >
            <PayPalHostedFieldsProvider
                createOrder={() => {
                    // Here define the call to create and order
                    return fetch(
                        "/your-server-side-integration-endpoint/orders"
                    )
                        .then((response) => response.json())
                        .then((order) => order.id)
                        .catch((err) => {
                            // Handle any error
                        });
                }}
            >
                <PayPalHostedField
                    id="card-number"
                    hostedFieldType="number"
                    options={{ selector: "#card-number" }}
                />
                <PayPalHostedField
                    id="cvv"
                    hostedFieldType="cvv"
                    options={{ selector: "#cvv" }}
                />
                <PayPalHostedField
                    id="expiration-date"
                    hostedFieldType="expirationDate"
                    options={{
                        selector: "#expiration-date",
                        placeholder: "MM/YY",
                    }}
                />
                <SubmitPayment />
            </PayPalHostedFieldsProvider>
        </PayPalScriptProvider>
    );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: return object list in find js 
Javascript :: media queries generator script 
Javascript :: if keypress javascript 
Javascript :: datatable index column server side 
Javascript :: animated node with tag 2 does not exist 
Javascript :: magento 2 translate js 
Javascript :: how to get url parameter using jquery or plain javascript 
Javascript :: make button inside datatable 
Javascript :: babel start command nodejs 
Javascript :: run function on page resize javascript 
Javascript :: how click button and redirect angular 
Javascript :: jquery check if document loaded 
Javascript :: localstorage in js 
Javascript :: javascript regex stop at first match 
Javascript :: javascript filter map array of objects 
Javascript :: jquery do something if toggle open and close 
Javascript :: javascript event currenttarget 
Javascript :: javascript return string 
Javascript :: await loop javascript 
Javascript :: js read a ini file 
Javascript :: months js 
Javascript :: reverse array js 
Javascript :: eliminar comillas de un string javascript 
Javascript :: js find duplicates in array 
Javascript :: json length javascript 
Javascript :: parseint javascript 
Javascript :: chrome storage set example 
Javascript :: discord.js clear console 
Javascript :: react toastify is not working 
Javascript :: jquery change h1 text 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =