How to use external object in react?











up vote
3
down vote

favorite












A third party sent me this script. Basically,




  1. include a script

  2. call the object

  3. onAuthorize will feedback data, then do something with data


Is it a way to integrate it with react? I think I need the data from onAuthorize to update my react state



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Payment Gateway Test Page</title>
<script src="https://service.com/widget.js">
</script>
<style type="text/css">
iframe{border: 0;height: 50px;}
</style>
</head>

<body>
<div>
* Demo for widget
</div>
<br/>
<script>
// widget
mywidget.Button.render({
Client: {
id: "1234",
name: "testme"
},
payment: function (actions) {
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
return actions.createQuote(amountValue)
},
onAuthorize: function (data) {
// err
if (data.errorCode) {
this.onError(data);
return;
}

// money we need to pay
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
// we have such points, converted to money
var pointsDollars = parseFloat(data.pointsBurned * 0.005, 10);

// points to convert
document.getElementById('qp').innerText = data.pointsBurned;

// origPay - new_money = pay_now
document.getElementById('bal').innerText = '$' + (amountValue - pointsDollars);
},
onError: function (data) {
console.log(data);
},
onClicked: function (data) {
// on click
console.log(data);
}
}, "#container"); // container
</script>

<div id="container"></div>
<br/>
<div id="amount">
Checkout: $<span id="inp-amount">1543</span> <br>
Points to redeem: <span id="qp"></span> <br>

<hr>
Balance to pay: <span id="bal"></span> <br>
</div>
</body>

</html>









share|improve this question






















  • You might be interested in something like: github.com/schiehll/react-globally
    – mkaatman
    Nov 12 at 4:13















up vote
3
down vote

favorite












A third party sent me this script. Basically,




  1. include a script

  2. call the object

  3. onAuthorize will feedback data, then do something with data


Is it a way to integrate it with react? I think I need the data from onAuthorize to update my react state



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Payment Gateway Test Page</title>
<script src="https://service.com/widget.js">
</script>
<style type="text/css">
iframe{border: 0;height: 50px;}
</style>
</head>

<body>
<div>
* Demo for widget
</div>
<br/>
<script>
// widget
mywidget.Button.render({
Client: {
id: "1234",
name: "testme"
},
payment: function (actions) {
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
return actions.createQuote(amountValue)
},
onAuthorize: function (data) {
// err
if (data.errorCode) {
this.onError(data);
return;
}

// money we need to pay
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
// we have such points, converted to money
var pointsDollars = parseFloat(data.pointsBurned * 0.005, 10);

// points to convert
document.getElementById('qp').innerText = data.pointsBurned;

// origPay - new_money = pay_now
document.getElementById('bal').innerText = '$' + (amountValue - pointsDollars);
},
onError: function (data) {
console.log(data);
},
onClicked: function (data) {
// on click
console.log(data);
}
}, "#container"); // container
</script>

<div id="container"></div>
<br/>
<div id="amount">
Checkout: $<span id="inp-amount">1543</span> <br>
Points to redeem: <span id="qp"></span> <br>

<hr>
Balance to pay: <span id="bal"></span> <br>
</div>
</body>

</html>









share|improve this question






















  • You might be interested in something like: github.com/schiehll/react-globally
    – mkaatman
    Nov 12 at 4:13













up vote
3
down vote

favorite









up vote
3
down vote

favorite











A third party sent me this script. Basically,




  1. include a script

  2. call the object

  3. onAuthorize will feedback data, then do something with data


Is it a way to integrate it with react? I think I need the data from onAuthorize to update my react state



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Payment Gateway Test Page</title>
<script src="https://service.com/widget.js">
</script>
<style type="text/css">
iframe{border: 0;height: 50px;}
</style>
</head>

<body>
<div>
* Demo for widget
</div>
<br/>
<script>
// widget
mywidget.Button.render({
Client: {
id: "1234",
name: "testme"
},
payment: function (actions) {
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
return actions.createQuote(amountValue)
},
onAuthorize: function (data) {
// err
if (data.errorCode) {
this.onError(data);
return;
}

// money we need to pay
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
// we have such points, converted to money
var pointsDollars = parseFloat(data.pointsBurned * 0.005, 10);

// points to convert
document.getElementById('qp').innerText = data.pointsBurned;

// origPay - new_money = pay_now
document.getElementById('bal').innerText = '$' + (amountValue - pointsDollars);
},
onError: function (data) {
console.log(data);
},
onClicked: function (data) {
// on click
console.log(data);
}
}, "#container"); // container
</script>

<div id="container"></div>
<br/>
<div id="amount">
Checkout: $<span id="inp-amount">1543</span> <br>
Points to redeem: <span id="qp"></span> <br>

<hr>
Balance to pay: <span id="bal"></span> <br>
</div>
</body>

</html>









share|improve this question













A third party sent me this script. Basically,




  1. include a script

  2. call the object

  3. onAuthorize will feedback data, then do something with data


Is it a way to integrate it with react? I think I need the data from onAuthorize to update my react state



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Payment Gateway Test Page</title>
<script src="https://service.com/widget.js">
</script>
<style type="text/css">
iframe{border: 0;height: 50px;}
</style>
</head>

<body>
<div>
* Demo for widget
</div>
<br/>
<script>
// widget
mywidget.Button.render({
Client: {
id: "1234",
name: "testme"
},
payment: function (actions) {
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
return actions.createQuote(amountValue)
},
onAuthorize: function (data) {
// err
if (data.errorCode) {
this.onError(data);
return;
}

// money we need to pay
var amountValue = parseFloat(document.getElementById("inp-amount").innerText);
// we have such points, converted to money
var pointsDollars = parseFloat(data.pointsBurned * 0.005, 10);

// points to convert
document.getElementById('qp').innerText = data.pointsBurned;

// origPay - new_money = pay_now
document.getElementById('bal').innerText = '$' + (amountValue - pointsDollars);
},
onError: function (data) {
console.log(data);
},
onClicked: function (data) {
// on click
console.log(data);
}
}, "#container"); // container
</script>

<div id="container"></div>
<br/>
<div id="amount">
Checkout: $<span id="inp-amount">1543</span> <br>
Points to redeem: <span id="qp"></span> <br>

<hr>
Balance to pay: <span id="bal"></span> <br>
</div>
</body>

</html>






javascript html reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 4:08









kenpeter

1,38762137




1,38762137












  • You might be interested in something like: github.com/schiehll/react-globally
    – mkaatman
    Nov 12 at 4:13


















  • You might be interested in something like: github.com/schiehll/react-globally
    – mkaatman
    Nov 12 at 4:13
















You might be interested in something like: github.com/schiehll/react-globally
– mkaatman
Nov 12 at 4:13




You might be interested in something like: github.com/schiehll/react-globally
– mkaatman
Nov 12 at 4:13












2 Answers
2






active

oldest

votes

















up vote
2
down vote













You could create an event and listen for that event. In onAuthorize you can trigger the event and pass the data.



Add an event in your page (not necessarily in React)



// Create the event
var event = new CustomEvent("authroize-me", { "detail": "some event info" });


React component



 constructor() {
super();
this.handleAuthroizeMe = this.handleAuthroizeMe.bind(this);
}
handleAuthroizeMe(data) {
console.log(data);
}

componentDidMount() {
document.addEventListener('authroize-me', this.handleAuthroizeMe);
}
componentWillUnmount() {
document.removeEventListener("authroize-me", this.handleAuthroizeMe);
}


In onAuthorize



onAuthorize: function (data) {
// Dispatch event
document.dispatchEvent(event, data);
}


Another quick and dirty fix.



Expose a function from react component outside the react scope.



window.setAuthorizeState = (data)=> {
this.setState({authorizeState: data});
}


Call setAuthorizeState from onAuthorize






share|improve this answer






























    up vote
    0
    down vote













    The code can be embedded in a component which renders the container. And in componentDidMount, the script can be placed.



    class Widget extends Component {
    componentDidMount() {
    // script here
    }

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





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255844%2fhow-to-use-external-object-in-react%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      You could create an event and listen for that event. In onAuthorize you can trigger the event and pass the data.



      Add an event in your page (not necessarily in React)



      // Create the event
      var event = new CustomEvent("authroize-me", { "detail": "some event info" });


      React component



       constructor() {
      super();
      this.handleAuthroizeMe = this.handleAuthroizeMe.bind(this);
      }
      handleAuthroizeMe(data) {
      console.log(data);
      }

      componentDidMount() {
      document.addEventListener('authroize-me', this.handleAuthroizeMe);
      }
      componentWillUnmount() {
      document.removeEventListener("authroize-me", this.handleAuthroizeMe);
      }


      In onAuthorize



      onAuthorize: function (data) {
      // Dispatch event
      document.dispatchEvent(event, data);
      }


      Another quick and dirty fix.



      Expose a function from react component outside the react scope.



      window.setAuthorizeState = (data)=> {
      this.setState({authorizeState: data});
      }


      Call setAuthorizeState from onAuthorize






      share|improve this answer



























        up vote
        2
        down vote













        You could create an event and listen for that event. In onAuthorize you can trigger the event and pass the data.



        Add an event in your page (not necessarily in React)



        // Create the event
        var event = new CustomEvent("authroize-me", { "detail": "some event info" });


        React component



         constructor() {
        super();
        this.handleAuthroizeMe = this.handleAuthroizeMe.bind(this);
        }
        handleAuthroizeMe(data) {
        console.log(data);
        }

        componentDidMount() {
        document.addEventListener('authroize-me', this.handleAuthroizeMe);
        }
        componentWillUnmount() {
        document.removeEventListener("authroize-me", this.handleAuthroizeMe);
        }


        In onAuthorize



        onAuthorize: function (data) {
        // Dispatch event
        document.dispatchEvent(event, data);
        }


        Another quick and dirty fix.



        Expose a function from react component outside the react scope.



        window.setAuthorizeState = (data)=> {
        this.setState({authorizeState: data});
        }


        Call setAuthorizeState from onAuthorize






        share|improve this answer

























          up vote
          2
          down vote










          up vote
          2
          down vote









          You could create an event and listen for that event. In onAuthorize you can trigger the event and pass the data.



          Add an event in your page (not necessarily in React)



          // Create the event
          var event = new CustomEvent("authroize-me", { "detail": "some event info" });


          React component



           constructor() {
          super();
          this.handleAuthroizeMe = this.handleAuthroizeMe.bind(this);
          }
          handleAuthroizeMe(data) {
          console.log(data);
          }

          componentDidMount() {
          document.addEventListener('authroize-me', this.handleAuthroizeMe);
          }
          componentWillUnmount() {
          document.removeEventListener("authroize-me", this.handleAuthroizeMe);
          }


          In onAuthorize



          onAuthorize: function (data) {
          // Dispatch event
          document.dispatchEvent(event, data);
          }


          Another quick and dirty fix.



          Expose a function from react component outside the react scope.



          window.setAuthorizeState = (data)=> {
          this.setState({authorizeState: data});
          }


          Call setAuthorizeState from onAuthorize






          share|improve this answer














          You could create an event and listen for that event. In onAuthorize you can trigger the event and pass the data.



          Add an event in your page (not necessarily in React)



          // Create the event
          var event = new CustomEvent("authroize-me", { "detail": "some event info" });


          React component



           constructor() {
          super();
          this.handleAuthroizeMe = this.handleAuthroizeMe.bind(this);
          }
          handleAuthroizeMe(data) {
          console.log(data);
          }

          componentDidMount() {
          document.addEventListener('authroize-me', this.handleAuthroizeMe);
          }
          componentWillUnmount() {
          document.removeEventListener("authroize-me", this.handleAuthroizeMe);
          }


          In onAuthorize



          onAuthorize: function (data) {
          // Dispatch event
          document.dispatchEvent(event, data);
          }


          Another quick and dirty fix.



          Expose a function from react component outside the react scope.



          window.setAuthorizeState = (data)=> {
          this.setState({authorizeState: data});
          }


          Call setAuthorizeState from onAuthorize







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 5:21

























          answered Nov 12 at 5:13









          kiranvj

          12.2k23350




          12.2k23350
























              up vote
              0
              down vote













              The code can be embedded in a component which renders the container. And in componentDidMount, the script can be placed.



              class Widget extends Component {
              componentDidMount() {
              // script here
              }

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





              share|improve this answer

























                up vote
                0
                down vote













                The code can be embedded in a component which renders the container. And in componentDidMount, the script can be placed.



                class Widget extends Component {
                componentDidMount() {
                // script here
                }

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





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The code can be embedded in a component which renders the container. And in componentDidMount, the script can be placed.



                  class Widget extends Component {
                  componentDidMount() {
                  // script here
                  }

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





                  share|improve this answer












                  The code can be embedded in a component which renders the container. And in componentDidMount, the script can be placed.



                  class Widget extends Component {
                  componentDidMount() {
                  // script here
                  }

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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 4:19









                  vijayst

                  5,90893675




                  5,90893675






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255844%2fhow-to-use-external-object-in-react%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly