Senior
How will we receive a response from the payment provider? The interviewer suggests using a webhook.
sobes.tech AI
Answer from AI
Using a webhook to receive responses from a payment provider is a common and convenient way to get asynchronous notifications about the payment status.
How it works:
- You register your server URL (webhook) with the provider.
- After processing the payment, the provider sends an HTTP POST request to this URL with the result information.
- Your server receives the request, verifies the signature/authorization for security, and updates the payment status in the system.
Example in Go (simplified):
func paymentWebhookHandler(w http.ResponseWriter, r *http.Request) {
// Check method
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Read request body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// Signature validation (depends on provider)
if !validateSignature(r.Header, body) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Parse data
var paymentStatus PaymentStatus
if err := json.Unmarshal(body, &paymentStatus); err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// Update payment status in database
updatePaymentStatus(paymentStatus)
w.WriteHeader(http.StatusOK)
}
Thus, a webhook allows real-time notifications without the need to poll the provider.