Skip to content

"bc:onpaid" event not reaching the button element #306

@Dunsin-cyber

Description

@Dunsin-cyber

also @rolznz , I noticed that when using <bc-pay-button>, the bc:onpaid event doesn’t fire when listening directly on the button element. it only fires globally on window. This is because the event is dispatched from within launchPaymentModal (specifically from the bc-payment component), but it doesn't bubble up through the bc-pay-button itself.

To improve modularity and make it easier to listen for events in scoped components or apps using Web Components (or frameworks like React/Vue), I suggest forwarding the event from bc-pay-button like this:

const { setPaid } = launchPaymentModal({
  onPaid: (response) => {
    this._paid = true;
    this.dispatchEvent(new CustomEvent('bc:onpaid', {
      detail: response,
      bubbles: true,
      composed: true,
    }));
  },
  invoice: this.invoice,
  paymentMethods: this.paymentMethods,
});

This allows users to do:

payButton.addEventListener('bc:onpaid', (e) => {
  console.log('got paid', e.detail);
});

instead of having to rely on a global window listener, which can be problematic in apps with multiple buttons or more complex scopes.

Originally posted by @Dunsin-cyber in #301 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions