javascript - React 的 Paypal Checkout 按钮不允许登录

标签 javascript reactjs paypal

我在使用沙箱将 Paypal 与我的 React 应用程序集成时遇到问题。当我单击该按钮时,会打开一个 PayPal 弹出窗口,当我输入我的凭据进行登录时,出现以下错误:

Paypal popup Paypal error

我可以看到登录表单,但它不会让我登录,相反我会看到该消息。

App.js

import PaypalButton from './PaypalButton';

const CLIENT = {
  sandbox: 'xxxxx',
  production: 'xxxxx',
};

const ENV = process.env.NODE_ENV === 'production' ? 'production' : 'sandbox';

render() {     
  const onSuccess = (payment) =>
    console.log('Successful payment!', payment);

  const onError = (error) =>
    console.log('Erroneous payment OR failed to load script!', error);

  const onCancel = (data) =>
    console.log('Cancelled payment!', data);

  return(
    <div>
        <PaypalButton
          client={CLIENT}
          env={ENV}
          commit={true}
          currency={'USD'}
          total={500.00}
          onSuccess={onSuccess}
          onError={onError}
          onCancel={onCancel}
        />
    </div>
  )
}

PaypalButton

import React from 'react';
import ReactDOM from 'react-dom';
import scriptLoader from 'react-async-script-loader';

class PaypalButton extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      showButton: false,
    };

    window.React = React;
    window.ReactDOM = ReactDOM;
  }

  componentDidMount() {
    const {
      isScriptLoaded,
      isScriptLoadSucceed
    } = this.props;

    if (isScriptLoaded && isScriptLoadSucceed) {
      this.setState({ showButton: true });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      isScriptLoaded,
      isScriptLoadSucceed,
    } = nextProps;

    const isLoadedButWasntLoadedBefore =
      !this.state.showButton &&
      !this.props.isScriptLoaded &&
      isScriptLoaded;

    if (isLoadedButWasntLoadedBefore) {
      if (isScriptLoadSucceed) {
        this.setState({ showButton: true });
      }
    }
  }

  render() {
    const {
      total,
      currency,
      env,
      commit,
      client,
      onSuccess,
      onError,
      onCancel,
    } = this.props;

    const {
      showButton,
    } = this.state;

    const payment = () =>
      paypal.rest.payment.create(env, client, {
        transactions: [
          {
            amount: {
              total,
              currency,
            }
          },
        ],
      });

    const onAuthorize = (data, actions) =>
      actions.payment.execute()
        .then(() => {
          const payment = {
            paid: true,
            cancelled: false,
            payerID: data.payerID,
            paymentID: data.paymentID,
            paymentToken: data.paymentToken,
            returnUrl: data.returnUrl,
          };

          onSuccess(payment);
        });

    return (
      <div>
        {showButton && <paypal.Button.react
          env={env}
          client={client}
          commit={commit}
          payment={payment}
          onAuthorize={onAuthorize}
          onCancel={onCancel}
          onError={onError}
        />}
      </div>
    );
  }
}

export default scriptLoader('https://www.paypalobjects.com/api/checkout.js')(PaypalButton);

有人可以帮我解决这个问题吗?

最佳答案

我上周遇到了同样的问题。工作了一段时间后,沙箱开始给我这个错误。我恢复了所有提交以确保这不是我的代码的问题。一两天后,它又开始工作了。

这似乎是 PayPal 沙盒环境的问题。 (Apparently it happens to the best of us)。

如果您发送的数据不正确,您会看到错误的 console.log

关于javascript - React 的 Paypal Checkout 按钮不允许登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060943/

相关文章:

javascript - jQuery Ajax OPTIONS 调用中的自定义 header

javascript - 无法更新 ReactJS 中现有的输入组件值

reactjs - 在无限循环中 react Downshift 自动完成请求

paypal - 授权我的应用创建 Paypal 自适应付款退款

paypal - 无法使用 "PayPal Payments Standard"与匿名支付集成

php - Paypal/cgi-bin/webscr 表单数据

javascript - 如何在 Javascript 中克隆一个正方形

javascript - 如何在 HTML 和 CSS 中创建一个全屏视频背景,让我的所有组件都覆盖在它上面?

javascript - jQuery 仅适用于 Firefox,不适用于 Chrome 或 IE

javascript - TypeScript 不知道组件从 HOC 获取了 required prop