0

I am using a payment merchant in my React app which requires that I import their v1.js script and then use it to create an object.

First I tried to download the script and import it via import ./v1.js, but that left me with plenty of errors:

enter image description here

I've then imported the script in my index.html file instead via <script type="text/javascript" src="https://tpgw.trustpay.eu/js/v1.js"></script>.

This didn't incur any errors, however, the next step requires me to create an object referenced in the script using var trustPayApi = new TrustPayApi({secrets});.

When I run this code in my React app (substituting secrets for the correct value), I get an error saying

'TrustPayApi' is not defined

How can I correctly implement the script and use the TrustPayApi object defined within it?

Sam
  • 1,130
  • 12
  • 36
  • The messages in the image are not code errors, but linting errors. They can be ignored for running the code – evolutionxbox May 03 '22 at 08:10
  • Apologies, I'm not familiar with those. How can I get around them? – Sam May 03 '22 at 08:11
  • Does this address your issue: https://stackoverflow.com/a/50802718/3977134 ? – r3dst0rm May 03 '22 at 08:12
  • @r3dst0rm how does that relate here? – evolutionxbox May 03 '22 at 08:23
  • 1
    It does indeed. I had to use the `window.` prefix when creating the `trustPayApi` object. Thank you. – Sam May 03 '22 at 08:27
  • 1
    @Sam in case you want to use the downloaded file rather than including a script tag in your index.html, you can exclude v1.js from linting. In your eslint config you can place a `ignorePatterns` property (for reference: https://eslint.org/docs/user-guide/configuring/ignoring-code) – r3dst0rm May 03 '22 at 09:19
  • @evolutionxbox OP wants to include an external script, loading it via a script tag - accessing the window property solves the "Foobar is not defined" issue. – r3dst0rm May 03 '22 at 09:21
  • Thanks @r3dst0rm It's good to know I can still use the script if I download it locally – Sam May 03 '22 at 09:51

0 Answers0