Home > database >  react-paypal-button-v2 cannot authorize payment
react-paypal-button-v2 cannot authorize payment

Time:11-28

I am trying to authorize a payment and capture funds later. Basically, I want to place a hold on the customer’s card for the funds. Then process the order and finally capture the amount when the order is ready. I followed the paypal guide to do that : https://developer.paypal.com/docs/business/checkout/add-capabilities/authorization/ However, no matter what I've tried I always end up "capturing" the full amount and not placing a hold. I am using react-paypal-button-v2 for the paypal buttons Here is my code :

import { PayPalButton } from "react-paypal-button-v2";

const addPayPalScript = () => {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.src =
      "https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&intent=authorize";
    //script.src = "https://www.paypal.com/sdk/js";
    script.async = true;
    script.onload = () => {
      setSdkReady(true);
    };
    document.body.appendChild(script);
  };
  useEffect(() => {
   
      if (!window.paypal) {
        addPayPalScript();
        setSdkReady(true);
      } else {
        setSdkReady(true);
      }
    }
  }, []);

return  {!sdkReady ? (
                    <Loader />
                  ) : (
                    <PayPalButton
                       amount={100}         
                      onSuccess={successPaymentHandler}

                    />
                  )}

I have also tried the following:

const addPayPalScript = () => {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.src =
      "https://www.paypal.com/sdk/js";
    script.async = true;
    script.onload = () => {
      setSdkReady(true);
    };
    document.body.appendChild(script);
  };
 const initialOptions = {
    clientId:
      MyPaypalID,
    intent: "authorize",
  };

return  {!sdkReady ? (
                    <Loader />
                  ) : (
                    <PayPalButton
                       amount={100}         
                      onSuccess={successPaymentHandler}
    options={initialOptions}

                    />
                  )}

CodePudding user response:

Use the official react-paypal-js and specify an onApprove function that does actions.order.authorize() rather than the default capture.

  • Related