Using Paypal in React with NextJS and it does work and go through
I am getting this error, when I click the PayPal Button:
I have implemented it in react via the react-paypal-js
package:
import React, { useState } from 'react';
import { PayPalScriptProvider, PayPalButtons } from '@paypal/react-paypal-js';
import Wrapper from '@/components/Wrapper';
import NoHeader from '@/layouts/NoHeader';
const product = {
description: 'Bread',
price: 29
};
const CheckoutButton = () => {
const [completed, setCompleted] = useState(false);
const handleApprove = (orderID) => {
// call backend to fulfill order
console.log('handle approve call');
setCompleted(true);
};
return (
<>
{completed && Thanks a ton}
{
return fetch('/api/paypal/pay10', {
method: 'post'
})
.then(function (res) {
console.log(res);
return res.json();
})
.then(function (orderData) {
console.log(orderData);
return orderData.id;
});
}}
onApprove={async (data, actions) => {
return fetch(`/api/paypal/success/${data.orderID}`, {
method: 'post'
})
.then(function (res) {
console.log(res);
return res.json();
})
.then(function (orderData) {
console.log(orderData);
handleApprove(data.orderID);
return orderData.id;
});
}}
onError={(err) => {
console.log(err);
}}
/>
);
};
const CheckoutPage = () => {
return (
<>
Paypal
);
};
CheckoutPage.layout = NoHeader;
export default CheckoutPage;
