Skip to content

Listen to Payments

Now that we know how much to pay to which address we need some sort of feedback. Obviously we don't want to wait until the customer tells us he paid, we want to know right on the spot if a payment happened.

There are multiple ways to solve this particular issue. For example you could poll the Item smart contract. You could watch the address on a low-level for incoming payments. But that's not what we want to do.

What we want is to wait for the event "SupplyChainStep" to trigger with _step == 1 (Paid).

Let's add another function to the App.js file:

listenToPaymentEvent = () => {
    let self = this;"data", async function(evt) {
      if(evt.returnValues._step == 1) {
        let item = await self.itemManager.methods.items(evt.returnValues._itemIndex).call();
        alert("Item " + item._identifier + " was paid, deliver it now!");

And call this function when we initialize the app in "componentDidMount":

this.item = new this.web3.eth.Contract(
        ItemContract.networks[this.networkId] && ItemContract.networks[this.networkId].address,

      // Set web3, accounts, and contract to the state, and then proceed with an
      // example of interacting with the contract's methods.
      this.setState({ loaded:true });
    } catch (error) {
      // Catch any errors for any of the above operations.
        `Failed to load web3, accounts, or contract. Check console for details.`,

Whenever someone pays the item a new popup will appear telling you to deliver. You could also add this to a separate page, but for simplicity we just add it as an alert popup to showcase the trigger-functionality:

Take the address, give it to someone telling them to send 100 wei (0.0000000000000001 Ether) and a bit more gas to the specified address. You can do this either via MetaMask or via the truffle console:

web3.eth.sendTransaction({to: "ITEM_ADDRESS", value: 100, from: accounts[1], gas: 2000000});

Then a popup should appear on the website

Last update: March 28, 2022