Created
September 23, 2023 14:59
-
-
Save alirezaarzehgar/37325bb294aa031ef4dcf3c70a7de35e to your computer and use it in GitHub Desktop.
Connect golang application to payment gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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