Senior
Is there experience integrating with external services (banks, payment gateways, APIs)?
sobes.tech AI
Answer from AI
Yes, experience with integrating external services in Go typically involves working with REST APIs of banks and payment gateways via HTTP clients, handling JSON/XML, authentication (OAuth, API keys), as well as error and timeout handling.
Example of integration with a payment API:
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type PaymentRequest struct {
Amount float64 `json:"amount"`
Currency string `json:"currency"`
CardID string `json:"card_id"`
}
type PaymentResponse struct {
Status string `json:"status"`
Message string `json:"message"`
}
func makePayment() error {
reqBody := PaymentRequest{
Amount: 100.0,
Currency: "USD",
CardID: "card_123",
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return err
}
client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest("POST", "https://api.paymentgateway.com/pay", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer your_api_token")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var paymentResp PaymentResponse
if err := json.NewDecoder(resp.Body).Decode(&paymentResp); err != nil {
return err
}
fmt.Println("Payment status:", paymentResp.Status)
return nil
}
func main() {
if err := makePayment(); err != nil {
fmt.Println("Error making payment:", err)
}
}
This approach allows flexible working with external APIs, handling responses and errors, and integrating with various payment systems.