0

Here is the situation - I'm working with the Mastercard payment gateway api in an angular based application. The api takes a callback for success and error and the callback is passed in the data-error and data-success attributes of the script tag to load the Mastercard api.

<script src="https://eu-gateway.mastercard.com/checkout/version/49/checkout.js" 
        data-error="errorCallback" 
        data-cancel="cancelCallback">
</script>

Details here.

I have a solution which works quite well in Firefox and Chrome but absolutely fails in IE11. I've uncommented all the polyfills imports but nothing is working no matter how much I try.

Here is what I have done so far:

  export class AppComponent implements OnInit {
    constructor(private ngZone: NgZone, private router:Router) {
    var _self = this;

    (<any>window).errorPaymentCallback = function(error){
      console.log(error);
    };

    (<any>window).cancelPaymentCallback = function(){
      console.log('cancel');
    };      
}

No matter what I try the callbacks are not fired and instead the api returns an error. Any ideas?

DarkNeuron
  • 7,981
  • 2
  • 43
  • 48
Ali
  • 7,353
  • 20
  • 103
  • 161

1 Answers1

1

You can dispatch an event and then catch it in AppComponent

This is how you do this:

<script src="https://eu-gateway.mastercard.com/checkout/version/49/checkout.js" 
        data-error="errorCallback" 
        data-cancel="cancelCallback">
</script>

    <script type="text/javascript">
        function errorCallback(error) { document.dispatchEvent(new Event('payment-error', { bubbles: true })); }
        function cancelCallback() { document.dispatchEvent(new Event('payment-error', { bubbles: true })); }
        window.global = window;
      </script>

In AppComponent

@HostListener('document:payment-error', ['$event'])
  paymentError(event){
    //do your work
  }
Hassan Al Bourji
  • 185
  • 1
  • 13
  • Whoa - havent checked out events yet - I'll give this a try and get back to you :) - lot of Angular left to learn – Ali Oct 04 '18 at 15:24
  • Its not really working - IE says Object doesnt support this action :( – Ali Oct 08 '18 at 13:06
  • Got it - this polyfill in this answer sorted it https://stackoverflow.com/a/26596324/89752 thanks for pointing me in the right direction :) Rockstar! – Ali Oct 08 '18 at 16:59