worldpay implements the useTemplateForm callback function

My experience with implementing worldpay on my one-page Angular app (Angular 1.x) has been mostly positive. I have been using the useTemplateForm() method to generate a credit card form and retrieve a token successfully. However, I have encountered an issue where my callback function, set in useTemplateForm, is being called multiple times after retrieving the token.

This problem arises from the fact that I am calling useTemplateForm every time the payment window pops up. It seems that Worldpay.js is stacking these callbacks instead of clearing them, and I am unsure of how to prevent this behavior. Unfortunately, I am required to call useTemplateForm each time the window appears because the element (div) containing the iframe is destroyed when the payment window closes, but destroying this element does not reset the stacked callbacks.

There are various ways I could handle this issue, but I would prefer to find a method to prevent the infinite stacking of callbacks as it will ultimately consume unnecessary resources.

If anyone has any suggestions on how to address this problem, I would greatly appreciate it.

Answer №1

While there may not be a foolproof solution to this issue, I have discovered a method that can prevent the stacking of callbacks.


Alternative Solution (not for production use)

To stop your registered callback from being called, you can remove the WorldPay event listener:

window.removeEventListener("message", Worldpay.template.messageListener, false);

1) Using Success Callback (not recommended)
If you always unload the templateForm on success, you can include this in the success callback of the Worldpay.useTemplateForm config:

'callback': function (obj) {
    window.removeEventListener("message", Worldpay.template.messageListener, false);
    worldPaySuccessCallBack(obj)
},

2) After Removing the WorldPay Form from the DOM
If you no longer have the Worldpay form rendered in your Angular application, remember to call the removeEventListener line.

3) In componentWillUnmount (React)
For those encountering this issue with react, placing the removeEventListener line in the componentWillUnmount of the Worldpay component could be beneficial.


Important Note

I advise against using this approach in production because if you are loading the WorldPay library via CDN (such as ), it utilizes an unversioned library.

The latest deployment of WorldpayJS is:

WorldpayJS v0.75.1 - Wed, 17 Jan 2018 12:10:44 GMT

Keep in mind that the path to

Worldpay.template.messageListener
object could change anytime without adhering to standard versioning rules like semver.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that you accurately maintain object ids following the creation of elements using ng-repeat

I have a set of items listed with unique objects within my controller $scope.itemsList = [ {"id": 0, "item": "sw", "category": 'A' }, {"id": 1, "item": "mlr", "category": 'B'}, {"id": 2, "item": "lvm", "category": 'C&a ...

Discover the hidden content within a div by hovering over it

Here is an example of a div: <div style="width:500px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce quis pulvinar dui. Nulla ut metus molestie, dignissim metus et, tinc ...

The pie chart fails to fully display

Check out the repro example I made here. The legend is displaying properly, but the graph is not showing up. What could be causing this unexpected issue? ...

Unable to stop the default form submission in Vue 3

Using the latest version of Vue 3 I am encountering an issue with preventing form submission. Here is my HTML form: <form id="app" @submit="onSubmit"> <input type="text"> <input type="text"> ...

Create a connection between a div and a React component, allowing for the seamless transfer of

I'm looking to implement a feature where clicking on a specific div will redirect the user to another page, similar to React Router. However, I currently lack the knowledge to make it happen. Below is the code snippet in question: const Card: React.FC ...

What is the best way to limit data loading when tabs are clicked in an AngularJS application?

My application has 2 tabs and all data is fetched from an API response. Initially, all data is loaded at once. The two tabs are: Tab 1 Tab 2 By default, Tab 1 is always active. I only want to load data for Tab 1 initially and not for Tab 2. When I c ...

Caution: Attempting to access a non-existent 'sequelize' property within a circular dependency in the module exports

Issue Nodemon server.js [nodemon] 2.0.15 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node server.js` Warning: connect.session() MemoryStore is not designe ...

What is the best way to export several 'sub-components' from a single node.js module?

Here's what I mean by exporting 'sub-modules': var fibers = require('fibers'); // this is functional as the 'main' module var future = require('fibers/future'); // also operational as a 'sub' ...

AngularJS HTTP service

When using AngularJS to call $http services for fetching JSON data, I consistently encounter the following error: NetworkError: 500 Internal Server Error Below is the snippet of code that I am currently using: app.controller('activeUserController ...

Exploring the Observable object within Angular

As I delve into learning Angular through various tutorials, I encountered a perplexing issue regarding my console displaying an error message: ERROR in src/app/employee/employee.component.ts:17:24 - error TS2322: Type 'IEmployee' is not assignab ...

Create the key's value in a dynamic manner within localforage

When utilizing localForage to store data offline, I encountered an issue with generating unique key values for each form submission. My goal is to have the key value generated dynamically as 'activity_1', 'activity_2', 'activity_3& ...

Mapping JSON to Java objects in Spring MVC causes a 400 error (Bad Request)

Whenever I try to submit the form, I encounter a 400 (Bad Request) error. My setup includes Jackson API jackson-core-asl-1.9.10.jar and jackson-mapper-asl-1.9.10.jar. Although I am able to receive JSON data, I am facing issues while submitting it. myjava ...

What is the best way to utilize eslint in Vue components?

I'm facing an issue with my .vue file that defines a component. Other .vue files are unable to see it properly due to linter errors. I keep getting ES Lint errors like: Cannot find module '../components/LinkButton'. I have tried several st ...

Avoid positioning issues when dropping a DIV onto the Canvas

Utilizing the JavaScript code below allows me to drag a DIV onto a canvas. I am currently loading a multi-page PDF, which consists of image files, in a pop-up window and positioning canvas layers over them. In these canvas layers, shapes are then loaded at ...

Using a datepicker to calculate dates within a computed function in Vue

I'm currently exploring the most efficient method to dynamically generate fields based on date count or display a default option. Currently, I have successfully implemented the default setting of 11 days. However, my goal is to calculate the differen ...

Navigating with Express and Vue

Currently, I am working on a basic web page that has an index '/' and a 404 page to handle errors at '/404'. In my express app setup, I have the following configuration: // Entry Point app.use("/", express.static(resolve(__di ...

Steps for aligning items in a column horizontally in Bootstrap 5

After creating a Grid system with only 2 columns, I placed text in the first column and a carousel in the second. Despite setting a custom size for the carousel image, I'm facing difficulty centering it horizontally within the column. .title-sec { ...

Clearing Input Values in Text Boxes, Dropdowns, and Checkboxes in ASP.NET MVC

Currently, I am working with Asp.net MVC 4 using C#. On one of my pages, I have a Text box, Drop down, and checkbox all together as search filters. There is also a "Clear" button to reset the values. When this button is clicked, I want the textbox value to ...

Tips for saving an image that has been dragged onto the browser locally

I recently started working with Angular and decided to use the angular-file-upload project from GitHub here. I'm currently in the process of setting up the backend for my application, but I'd like to be able to display dropped files locally in th ...

What is the best way to display a selected checkbox in an AJAX editing modal?

Can someone assist me in loading the checked checkboxes in an edit modal? I am using checkboxes to assign multiple roles in my system. Everything was working fine, but I encountered an issue with my edit modal where the checkboxes appear blank and unchecke ...