I am trying to follow the example here
https://github.com/paypal-examples/docs-examples/tree/main/standard-integration
except I modified the front end to use reach via react-paypal-js
https://github.com/paypal/react-paypal-js
But I dont understand how to launch the backend server.js
script
I am running the page with npm start
command,
Is there something I need to add to automatically launch the server.js
script?
export default function App() {
return (
<PayPalScriptProvider options={{ "client-id": "test" }}>
<PayPalButtons
createOrder={(data, actions) => {
return actions.order.create({
purchase_units: [
{
amount: {
value: "1.99",
},
},
],
});
}}
onApprove={(data, actions) => {
return actions.order.capture().then((details) => {
const name = details.payer.name.given_name;
alert(`Transaction completed by ${name}`);
});
}}
/>
</PayPalScriptProvider>
);
}
2
Answers
I guess it depends on you. It is not necessary to have a server script if you do not have anything like stock level or recording some data.
For example, when a user pay to buy 1 item of A product. Then you will need a server script to update the table data of A product to reduce the stock you have.
If you want a server then you may need to build a server using express node.js to integrate with the backend or maybe any other framework will do too.
There’s a similar question and answer here you can look into it.
Link: Post
server.js
will only be used if your app has routes that call into it, which it should. If it doesn’t, you need to create them. The first example you link to has them.Once your backend routes are running and listening for requests, you need to modify your frontend to make use of them.
actions.order.create
andactions.order.capture
are deprecated and should not be used in any new integrations. Those methods within the frontendcreateOrder
andonApprove
callback methods need to be replaced with fetches to your server routes. Again the first example you link has code that does this.