Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

javascript - How to use PayPal's Express In-Context Checkout with ReactJS?

I'm following this PayPal tutorial about how to generate a PayPal button, but nothing works. The code it provides to make the button appear mysteriously worked only once for me, but after a refresh, it disappear and there's no Christ to make it appear again.

This is the code being executed inside of a React component

  class Storefronts extends Component {
    render() {
      return (
        <div className="layout-wrapper">
          {this.props.location.pathname === '/shops' ? <Shops {...this.props}/> : <Basic {...this.props}/>}
          <script>
            window.paypalCheckoutReady = function() {
              paypal.checkout.setup('MERCHANTID', {
                environment: 'sandbox',
                container: 'test1',
              })
            }
          </script>
        </div>
      );
    }
  }

This is a Storefront component that holds a Shop, and inside this one has a Card component. Basically, it's a shop that shows its products, and each product (Card) needs to have a button:

class Card extends Editor {
  render() {
    const {list} = this.props;
    let img = '/images/logo-v2-small.jpg';

    return (
      <Row>
      {list.map(item =>{
        return (
          <Col xs={6} md={3}>
            <Link to={{ pathname: '/shops/' + item.id }}>
              <Thumbnail src={img} alt={item.name}>
                <h3>{item.name}</h3>
                <p>{this.parseHtmlToReact(item.description)}</p>
                <p>{item.address}</p>
                <p>
                  <Button bsStyle="primary">Book</Button>
                  <a id="test1" href="/checkout"/> // The button should appear here.
                  <p className="pull-right">
                    {item.rating} 
                  </p>
                </p>
              </Thumbnail>
            </Link>
          </Col>
        )
      })}
      </Row>
    );
  }
}

There's nothing saying about its usage with React and no recent module for it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could create your own PayPal Button component.

class PayPalButton extends React.Component {
  constructor() {
    super();
    // you can take this value from a config.js module for example.
    this.merchantId = '6XF3MPZBZV6HU';
  }

  componentDidMount() {
    let container = this.props.id;
    let merchantId = this.merchantId;
    window.paypalCheckoutReady = function() {
      paypal.checkout.setup(merchantId, {
        locale: 'en_US',
        environment: 'sandbox',
        container: container,
      });
    }
  }

  render() {
    return(
      <a id={this.props.id} href="/checkout" />
    );
  }
}

ReactDOM.render(<PayPalButton id="button" />, document.getElementById('View'));

Working example on JSFiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...