- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi to all,
I'm trying to add a paypal button in my React app and I found this: https://developer.paypal.com/docs/checkout/standard/customize/single-page-app/
In public/index.html I added a line like this:
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"></script>
then I try to create my custom component like this:
import React from "react";
import ReactDOM from "react-dom"
const PayPalButton = paypal.Buttons.driver("react", { React, ReactDOM });
class YourComponent extends React.Component {
createOrder(data, actions) {
return actions.order.create({
purchase_units: [
{
amount: {
value: "0.01",
},
},
],
});
}
onApprove(data, actions) {
return actions.order.capture();
}
render() {
return (
<PayPalButton
createOrder={(data, actions) => this.createOrder(data, actions)}
onApprove={(data, actions) => this.onApprove(data, actions)}
/>
);
}
}
the app fails to compile: 'paypal' is not defined. Any suggestions? Thanks
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
just added this line:let paypal = window.paypal;
before this:const PayPalButton = paypal.Buttons.driver('react', { React, ReactDOM });
and resolved
const paypal = window.paypal;
const PayPalButton = paypal.Buttons.driver('react', { React, ReactDOM });
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
just added this line:let paypal = window.paypal;
before this:const PayPalButton = paypal.Buttons.driver('react', { React, ReactDOM });
and resolved
const paypal = window.paypal;
const PayPalButton = paypal.Buttons.driver('react', { React, ReactDOM });
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I got the same error, using the same remote paypal script as the OP. This happened after I had upgraded my app from Rails 6 to 7 and switched javascript bundler from webpacker to jsbundling.
The new js bundlers for Rails don't seem to like inline javascript. As soon as I moved my client-side paypal script (starting with "paypal.Buttons...") to app/javascript, it started working. I also placed it inside
$(document).on('ready turbo:load', function(){
});
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- Selected Shipping Method amount not updating to cart for card payment in SDKs
- ExpressCheckout to REST API - Is Partner Program Now Required to Host Simple 3rd Party Transactions? in REST APIs
- paypal.ApplePay().confirmOrder() returns applepay_payment_error. in SDKs
- Venmo is not eligible - Unable to render Venmo button using SDK v5 in live mode in SDKs
- No subscription details shown to buyer in their PayPal account in REST APIs