gatewaycash / gateway

Gateway - Simple Bitcoin Payments
GNU Affero General Public License v3.0
18 stars 10 forks source link

Improvements to Payment Processing, Responsiveness and Size #95

Open ty-everett opened 5 years ago

ty-everett commented 5 years ago

The current model has the client connecting to the block explorer, listening for transactions and sending a POST /paid request to the server when a transaction is received. This is non-ideal for several reasons:

Proposal

We should set up a WebSocket (not Socket.IO) server on the Gateway API server. PayButton requests POST /pay and then gets an address back from the server. The PayButton connects to the WebSocket server.

The server adds the new invoice address to its internal list of addresses to watch for. Any transactions concerning any watched address (which can be thousands of addresses) are processed. The server has no need to query address balances every 12 hours, because any transactions are handled live.

The PayButton listens for several things from the server: paid, overpaid and underpaid. The PayButton responds accordingly. PayButton no longer connects with Socket.IO to a block explorer, saving on payload size and client bandwidth.

Merchants on the gateway.cash site can connect to the socket and see new payments come in live from the server.

Confidentiality can be added to the WebSocket server by requiring merchants to "subscribe" to their API key's channel so that they can only see their transactions. PayButton clients should receive a socketKey with their paymentAddress which they will use when connecting to the socket in order to see their relevant transactions.

On the backend, the API server is always connected to a block explorer via Socket.IO and is listening for addresses it is concerned with. After the appropriate action is performed (processing the payment, notifying customer of overpaid funds etc), the server sends alerts to the WebSocket channels. Merchant API keys serve as channels for the specific merchant, and socketKeys should be stored in the payments table.

Occasionally, broken payments will still happen so we might consider querying past address balances at startup or maybe very slowly over the course of a week, from various explorers so as to be good citizens.

I'd love to hear some feedback on this

ty-everett commented 5 years ago

oops thought this was #66