Subscribe to real-time payment, refund, and freeze events as a payer
The Client SDK provides methods to subscribe to blockchain events in real-time using viem’s watchContractEvent under the hood. All subscription methods return an object with an unsubscribe function that you should call when you no longer need the watcher.
Watch for state changes on a specific payment. This subscribes to ReleaseExecuted, RefundInEscrowExecuted, and RefundPostEscrowExecuted events on the PaymentOperator contract.
Copy
const { unsubscribe } = client.watchPaymentState( paymentInfoHash, (event) => { console.log(`Payment event: ${event.eventName}`); switch (event.eventName) { case 'ReleaseExecuted': console.log('Funds released to merchant'); console.log('Amount:', event.args.amount); break; case 'RefundInEscrowExecuted': console.log('Funds refunded from escrow'); break; case 'RefundPostEscrowExecuted': console.log('Funds refunded after escrow period'); break; } });// Stop watching when doneunsubscribe();
Watch for refund request lifecycle events. This subscribes to RefundRequested, RefundRequestStatusUpdated, and RefundRequestCancelled events on the RefundRequest contract.
Copy
const { unsubscribe } = client.watchRefundRequests((event) => { switch (event.eventName) { case 'RefundRequested': console.log('New refund request submitted'); console.log('Payment:', event.args.paymentInfoHash); console.log('Amount:', event.args.amount); break; case 'RefundRequestStatusUpdated': console.log('Refund status changed:', event.args.status); // 1 = Approved, 2 = Denied break; case 'RefundRequestCancelled': console.log('Refund request cancelled'); break; }});// Stop watching when doneunsubscribe();
Watch for new payment authorizations where the connected wallet is the payer. This subscribes to AuthorizationCreated events on the PaymentOperator contract, filtered by the wallet’s address.
All subscription methods use viem’s watchContractEvent under the hood. For reliable real-time delivery, configure your publicClient with a WebSocket transport.