Coder Social home page Coder Social logo

Comments (6)

guanacone avatar guanacone commented on July 19, 2024 1

Ok, so the amount is not updating because the button is only created on first render.

  useEffect(() => {
    if (typeof window !== `undefined`) {
      window.paypal
        .Buttons({
          ...props,
          createOrder: (props.createOrder ? props.createOrder : createOrderPaypal),
          onApprove: (props.onApprove ? props.onApprove : onApprovePaypal),
          onError: (props.onError ? props.onError : onErrorPaypal),
        })
        .render('#paypal-button');
    }
  }, []);

Could you fix that?

from gatsby-plugin-paypal.

lawrencejberry avatar lawrencejberry commented on July 19, 2024 1

In the meantime, a very ugly workaround is to set the value as a key on the component, which forces the component to re-mount whenever the value changes:

<Paypal
    // change the key to force a remount of the component which is needed for it to pick up new props
    key={value}
    amount={value}
    style={{
      color: "blue",
    }}
/>

from gatsby-plugin-paypal.

guanacone avatar guanacone commented on July 19, 2024 1

I ended up using react-paypal-button-v2

from gatsby-plugin-paypal.

nNevermore avatar nNevermore commented on July 19, 2024 1

Hello, I have also a problem with this solution, so I did it in this way

 useEffect(() => {
     window.paypal
       .Buttons({
         createOrder: (data, actions) => {
           return actions.order.create({
             purchase_units: [
               {
                 description: product.description,
                 amount: {
                   currency_code: 'PLN',
                   value: productPrice,
                 },
               },
             ],
             "application_context": {
                shipping_preference: "NO_SHIPPING"
            },

           });
         },
         onApprove: async (data, actions) => {
           const order = await actions.order.capture();
           setPaidFor(true);
           console.log(order);
         },
         onError: err => {
           setError(err);
           console.error(err);
         },
       })
       .render(
           paypalRef.current
       );
   }, [productDescription, productPrice]);

and:
<div id="paypalDiv"ref={paypalRef} />
have a nice day!

from gatsby-plugin-paypal.

alexislepresle avatar alexislepresle commented on July 19, 2024 1

Hello everyone,

Sorry I don't have much time at the moment. I will try to make the necessary updates soon. If you find a solution, you can make a PR :)

Have a nice day :)

from gatsby-plugin-paypal.

lawrencejberry avatar lawrencejberry commented on July 19, 2024

+1 on this issue. As @guanacone said, you can't dynamically change the amount (or pass in any new props for that matter) as the PayPal button is only created on component mount due to the empty [] dependencies array. Unfortunately it's not as simple as just adding props to the dependencies array, since this would cause additional buttons to be rendered. We need to find some way to clean up the old PayPal button before re-creating it with the latest props, but I can't see anything of the sort in the PayPal Checkout button API. It's possible using the special PayPal React button might do the trick?

from gatsby-plugin-paypal.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.