0

我正在将 AmazonPay 集成到 React SPA 中。经典的集成依赖于脚本标签和回调(docs)。

这是按钮小部件的一个示例:

<head>
  <script type='text/javascript'>
    window.onAmazonLoginReady = function() {
      amazon.Login.setClientId('CLIENT-ID');
    };
    window.onAmazonPaymentsReady = function() {
                showButton();
    };
  </script>
    <script async="async" src='https://static-na.payments-amazon.com/OffAmazonPayments/us/sandbox/js/Widgets.js'>
  </script>
</head>

<body>
. . .
 <div id="AmazonPayButton">
 </div>
  ...
 <script type="text/javascript">
    function showButton(){
      var authRequest; 
      OffAmazonPayments.Button("AmazonPayButton", "SELLER-ID", { 
        type:  "TYPE", 
        color: "COLOR", 
        size:  "SIZE", 

        authorization: function() { 
        loginOptions = {scope: "SCOPES", 
          popup: "POPUP-PARAMETER"}; 
        authRequest = amazon.Login.authorize (loginOptions, 
          "REDIRECT-URL"); 
        }, 

        onError: function(error) { 
          // your error handling code.
          // alert("The following error occurred: " 
          //        + error.getErrorCode() 
          //        + ' - ' + error.getErrorMessage());
        } 
     });
    }; 
   </script>
   . . .
   <script type="text/javascript">
     document.getElementById('Logout').onclick = function() {
       amazon.Login.logout();
     };
   </script>

</body>

使用 React 时,只有在 React 挂载了 之后,页面上才会出现divwith ,从而导致函数失败。id="AmazonPayButton"divwindow.showButton()

为了规避这个问题,我将function showButton()定义包含在里面window.showButton()

    window.onAmazonPaymentsReady = function() {

        window.showButton = function () {

            var authRequest;

            // eslint-disable-next-line no-undef
            OffAmazonPayments.Button("AmazonPayButton", "%REACT_APP_AMAZON_SELLER_ID_SANDBOX%", {
                type: "PwA",
                color: "Gold",
                size: "medium",
                authorization: function () {
                    loginOptions = {
                        scope: "profile postal_code payments:widget payments:shipping_address",
                        popup: true
                    };
                    authRequest = amazon.Login.authorize(loginOptions, "%PUBLIC_URL%/pay-with-amazon");
                },
                onError: function (error) {
                    console.log(error.toString())
                }
            });
        };
    };

div现在可以调用包含 AmazonPay 的组件componentDidMount

import React, {Component} from 'react'

class AmazonMethod extends Component {

    componentDidMount () {
        window.showButton()
    }

    render() { return <div id="AmazonPayButton"></div>}
}

export default AmazonMethod

我很困惑如何onError从我的 React 组件内部访问回调。如何收听回调并做出适当响应?

这个问题也适用于 AddressWidget 和 WalletWidget;它们都依赖于脚本标签回调。

更新:我写了一篇文章,总结了如何将 AmazonPay 与客户端 React 集成。

4

1 回答 1

2

为什么不直接将一个函数传递给 onError 可以调用的 componentDidMount 中的 showButton 函数?

像这样的东西:

window.onAmazonPaymentsReady = function() {

    window.showButton = function (errorFunc) {

        var authRequest;

        // eslint-disable-next-line no-undef
        OffAmazonPayments.Button("AmazonPayButton", "%REACT_APP_AMAZON_SELLER_ID_SANDBOX%", {
            type: "PwA",
            color: "Gold",
            size: "medium",
            authorization: function () {
                loginOptions = {
                    scope: "profile postal_code payments:widget payments:shipping_address",
                    popup: true
                };
                authRequest = amazon.Login.authorize(loginOptions, "%PUBLIC_URL%/pay-with-amazon");
            },
            onError: function (error) {
                console.log(error.toString())
                errorFunc(error)
            }
        });
    };
};


import React, {Component} from 'react'

class AmazonMethod extends Component {

componentDidMount () {
    window.showButton(this.errorFunc)
}

errorFunc = (error) => {
    console.log(error);

   this.setState({
        amazonError: error
   });
}

render() { return <div id="AmazonPayButton"></div>}
}

export default AmazonMethod

于 2018-08-27T15:51:49.627 回答