github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/http_pay.go (about) 1 package internal 2 3 import ( 4 "common" 5 "encoding/json" 6 "encoding/xml" 7 "fmt" 8 "game/pay/alipay" 9 "game/pay/wxpay" 10 "io/ioutil" 11 "net/http" 12 "net/url" 13 "reflect" 14 "strconv" 15 "strings" 16 17 "github.com/name5566/leaf/log" 18 ) 19 20 func handleAliPay(w http.ResponseWriter, r *http.Request) { 21 switch r.Method { 22 case "GET": 23 total_amount := r.URL.Query().Get("total_amount") 24 account_id := r.URL.Query().Get("account_id") 25 if total_amount == "" || account_id == "" { 26 w.WriteHeader(http.StatusNotFound) 27 fmt.Fprintf(w, "%v", "no total_amount or account_id") 28 return 29 } 30 outTradeNo := common.GetOutTradeNo() 31 request := alipay.NewAlipayTradeAppPayRequest(total_amount, outTradeNo) 32 data := alipay.DoRequest(request) 33 fmt.Fprintf(w, "%s", data) 34 totalAmount, _ := strconv.ParseFloat(total_amount, 64) 35 accountID, _ := strconv.Atoi(account_id) 36 startAliPayOrder(outTradeNo, accountID, totalAmount, nil) 37 case "POST": 38 result, _ := ioutil.ReadAll(r.Body) 39 r.Body.Close() 40 log.Debug("result: %s", result) 41 m, err := url.ParseQuery(string(result)) 42 if err == nil && alipay.Check(m) { 43 // 需要验证 out_trade_no 和 total_amount 44 fmt.Fprintf(w, "%v", "success") 45 totalAmount, _ := strconv.ParseFloat(m.Get("total_amount"), 64) 46 finishAliPayOrder(m.Get("out_trade_no"), totalAmount, true) 47 } else { 48 fmt.Fprintf(w, "%v", "failure") 49 } 50 } 51 } 52 53 func handleWXPay(w http.ResponseWriter, r *http.Request) { 54 switch r.Method { 55 case "GET": 56 total_fee := r.URL.Query().Get("total_fee") 57 account_id := r.URL.Query().Get("account_id") 58 if total_fee == "" || account_id == "" { 59 w.WriteHeader(http.StatusNotFound) 60 fmt.Fprintf(w, "%v", "no total_fee or account_id") 61 return 62 } 63 totalFee, _ := strconv.Atoi(total_fee) 64 accountID, _ := strconv.Atoi(account_id) 65 if totalFee < 600 { 66 w.WriteHeader(http.StatusNotFound) 67 fmt.Fprintf(w, "%v", "充值金额不能低于6元") 68 return 69 } 70 ip := strings.Split(r.RemoteAddr, ":")[0] 71 p := wxpay.NewWXTradeAppPayParameter(total_fee, ip) 72 data, err := json.Marshal(p) 73 if err != nil { 74 log.Error("marshal message %v error: %v", reflect.TypeOf(p), err) 75 data = []byte{} 76 } 77 w.Header().Set("Access-Control-Allow-Origin", "*") 78 fmt.Fprintf(w, "%s", data) 79 startWXPayOrder(p["out_trade_no"], accountID, totalFee, nil) 80 case "POST": 81 result, _ := ioutil.ReadAll(r.Body) 82 r.Body.Close() 83 log.Debug("result: %s", result) 84 payResult := new(wxpay.WXPayResult) 85 xml.Unmarshal(result, &payResult) 86 if wxpay.VerifyPayResult(payResult) { 87 // 需要验证 out_trade_no 和 total_fee 88 fmt.Fprintf(w, "%v", wxpay.ReturnWXSuccess) 89 finishWXPayOrder(payResult.OutTradeNo, payResult.TotalFee, true) 90 } else { 91 fmt.Fprintf(w, "%v", wxpay.ReturnWXFail) 92 } 93 } 94 } 95 96 97 func handleFakerAliPay(w http.ResponseWriter, r *http.Request) { 98 switch r.Method { 99 case "GET": 100 total_amount := r.URL.Query().Get("total_amount") 101 account_id := r.URL.Query().Get("account_id") 102 fmt.Fprintf(w, "%v", total_amount+" "+account_id) 103 if total_amount == "" || account_id == "" { 104 w.WriteHeader(http.StatusNotFound) 105 fmt.Fprintf(w, "%v", "no total_amount or account_id") 106 return 107 } 108 outTradeNo := common.GetOutTradeNo() 109 fmt.Fprintf(w, "faker alipay request outTradeNo:%v", outTradeNo) 110 totalAmount, _ := strconv.ParseFloat(total_amount, 64) 111 accountID, _ := strconv.Atoi(account_id) 112 startAliPayOrder(outTradeNo, accountID, totalAmount, nil) 113 case "POST": 114 result, _ := ioutil.ReadAll(r.Body) 115 r.Body.Close() 116 log.Debug("result: %s", result) 117 // 需要验证 out_trade_no 和 total_amount 118 fmt.Fprintf(w, "%v", "faker alipay response") 119 totalAmount, _ := strconv.ParseFloat(r.FormValue("total_amount"), 64) 120 finishAliPayOrder(r.FormValue("out_trade_no"), totalAmount, true) 121 } 122 } 123 124 func handleFakerWXPay(w http.ResponseWriter, r *http.Request) { 125 switch r.Method { 126 case "GET": 127 total_fee := r.URL.Query().Get("total_fee") 128 account_id := r.URL.Query().Get("account_id") 129 if total_fee == "" || account_id == "" { 130 w.WriteHeader(http.StatusNotFound) 131 fmt.Fprintf(w, "%v", "no total_fee or account_id") 132 return 133 } 134 totalFee, _ := strconv.Atoi(total_fee) 135 accountID, _ := strconv.Atoi(account_id) 136 if totalFee < 600 { 137 w.WriteHeader(http.StatusNotFound) 138 fmt.Fprintf(w, "%v", "充值金额不能低于6元") 139 return 140 } 141 ip := strings.Split(r.RemoteAddr, ":")[0] 142 p := wxpay.NewWXTradeAppPayParameter(total_fee, ip) 143 data, err := json.Marshal(p) 144 if err != nil { 145 log.Error("marshal message %v error: %v", reflect.TypeOf(p), err) 146 data = []byte{} 147 } 148 w.Header().Set("Access-Control-Allow-Origin", "*") 149 fmt.Fprintf(w, "faker wxpay %s", data) 150 startWXPayOrder(p["out_trade_no"], accountID, totalFee, nil) 151 case "POST": 152 result, _ := ioutil.ReadAll(r.Body) 153 r.Body.Close() 154 log.Debug("result: %s", result) 155 // 需要验证 out_trade_no 和 total_fee 156 fmt.Fprintf(w, "kafer wxpay response %v %v", r.FormValue("total_fee"),r.FormValue("out_trade_no")) 157 total_fee,_ := strconv.Atoi(r.FormValue("total_fee")) 158 finishWXPayOrder(r.FormValue("out_trade_no"), total_fee, true) 159 } 160 } 161 162 func handleEdyWXPay(w http.ResponseWriter, r *http.Request) { 163 switch r.Method { 164 case "GET": 165 total_fee := r.URL.Query().Get("total_fee") 166 account_id := r.URL.Query().Get("account_id") 167 if total_fee == "" || account_id == "" { 168 w.WriteHeader(http.StatusNotFound) 169 fmt.Fprintf(w, "%v", "no total_fee or account_id") 170 return 171 } 172 totalFee, _ := strconv.Atoi(total_fee) 173 accountID, _ := strconv.Atoi(account_id) 174 //if totalFee < 600 { 175 // w.WriteHeader(http.StatusNotFound) 176 // fmt.Fprintf(w, "%v", "充值金额不能低于6元") 177 // return 178 //} 179 ip := strings.Split(r.RemoteAddr, ":")[0] 180 p := wxpay.NewWXTradeAppPayParameter(total_fee, ip) 181 data, err := json.Marshal(p) 182 if err != nil { 183 log.Error("marshal message %v error: %v", reflect.TypeOf(p), err) 184 data = []byte{} 185 } 186 w.Header().Set("Access-Control-Allow-Origin", "*") 187 fmt.Fprintf(w, "%s", data) 188 startEdyWXPayOrder(p["out_trade_no"], accountID, totalFee, nil) 189 } 190 } 191 192 func handleEdyAliPay(w http.ResponseWriter, r *http.Request) { 193 switch r.Method { 194 case "GET": 195 total_amount := r.URL.Query().Get("total_amount") 196 account_id := r.URL.Query().Get("account_id") 197 fmt.Println("二打一支付宝支付,接收参数:total_amount=",total_amount, "account_id=", account_id) 198 if total_amount == "" || account_id == "" { 199 w.WriteHeader(http.StatusNotFound) 200 fmt.Fprintf(w, "%v", "no total_amount or account_id") 201 return 202 } 203 outTradeNo := common.GetOutTradeNo() 204 request := alipay.NewAlipayTradeAppPayRequest(total_amount, outTradeNo) 205 data := alipay.DoRequest(request) 206 fmt.Fprintf(w, "%s", data) 207 totalAmount, _ := strconv.ParseFloat(total_amount, 64) 208 accountID, _ := strconv.Atoi(account_id) 209 startEdyAliPayOrder(outTradeNo, accountID, totalAmount, nil) 210 } 211 }