I have a GraphQL Websocket based application in which I need to wait for customer payment. The subscription handler is supposed to
- Send a request to the payment gateway to activate the payment terminal.
- Wait for a webhook that gets called after payment for further processing.
I am sorry I am not able to post any code since, after step 1, I am unable to find details on which constructs (like a Mono or PubSub) to use to "wait" in this use-case.
How can I associate the outgoing request with the webhook call?
I can provide any other details needed to make my question clearer.
CodePudding user response:
There is still too little information but let me give you a rough sketch of how it could work.
After a request to the payment gateway has been sent we need to do two things.
- Create a stream (e.g. a
Mono
) to which you can send a signal - Create a mapping between the (device or transaction) ID and the stream. For starting you can use a simple
Map
that is also accessible (through a service) by the webhook. For production, you may think about a more sophisticated way.
When the webhook receives a request you look up the stream for the ID and send a signal to it (could also be an error signal). Whoever has subscribed to the stream now receives that signal and can continue.
Now to what construct to use. There may be several alternatives, one is to use a Sink. Here is some pseudo-code(!):
In the method that sends the payment request:
activateTerminal(paymentDetails);
transactionSink = Sinks.empty();
transactionMap.put(deviceId, transactionSink);
return transactionSink.asMono();
Inside the webhook:
transactionSink = transactionMap.get(deviceId);
transactionSink.tryEmitEmpty();
The last statement completes the Mono
, so whoever "waits" for it gets notified.
Please understand this answer only as a concept. The things you need to take a closer look at are:
- How to store the association
- The most appropriate way for you to send a signal to a stream (
Mono
).
If you have any specific question after that I recommend to create a new one.