Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Created September 23, 2023 14:59
Show Gist options
  • Save alirezaarzehgar/37325bb294aa031ef4dcf3c70a7de35e to your computer and use it in GitHub Desktop.
Save alirezaarzehgar/37325bb294aa031ef4dcf3c70a7de35e to your computer and use it in GitHub Desktop.
Connect golang application to payment gateway
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// https://aqayepardakht.ir/api/
func main() {
http.HandleFunc("/payment", func(w http.ResponseWriter, r *http.Request) {
var data map[string]string
body, _ := json.Marshal(map[string]any{
"pin": "sandbox",
"amount": 20000,
"callback": "http://localhost:8000/callback",
})
res, _ := http.Post("https://panel.aqayepardakht.ir/api/v2/create", "application/json", bytes.NewReader(body))
json.NewDecoder(res.Body).Decode(&data)
fmt.Fprintf(w, "https://panel.aqayepardakht.ir/startpay/sandbox/%s", data["transid"])
})
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Fprintf(w, "status: %s; 1 for success", r.PostFormValue("status"))
})
http.HandleFunc("/verify", func(w http.ResponseWriter, r *http.Request) {
var data map[string]string
body, _ := json.Marshal(map[string]any{
"pin": "sandbox",
"transid": r.URL.Query().Get("transid"),
"amount": 20000,
})
res, _ := http.Post("https://panel.aqayepardakht.ir/api/v2/verify", "application/json", bytes.NewReader(body))
json.NewDecoder(res.Body).Decode(&data)
fmt.Fprintf(w, "status code: %s; 2 for verified", data["code"])
})
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment