1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package main
import ( "bytes" "crypto/sha1" "encoding/xml" "fmt" "io/ioutil" "net/http" "sort" "strings" "time" "crypto/hmac" "crypto/sha256" "github.com/google/uuid" )
const ( AppID = "wx1234567890abcdef" MchID = "1234567890" Key = "your_api_key" NotifyURL = "https://yourdomain.com/notify" TradeType = "APP" UnifiedOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder" )
type UnifiedOrderRequest struct { AppID string `xml:"appid"` MchID string `xml:"mch_id"` NonceStr string `xml:"nonce_str"` Sign string `xml:"sign"` Body string `xml:"body"` OutTradeNo string `xml:"out_trade_no"` TotalFee int `xml:"total_fee"` SpbillCreateIP string `xml:"spbill_create_ip"` NotifyURL string `xml:"notify_url"` TradeType string `xml:"trade_type"` }
type UnifiedOrderResponse struct { ReturnCode string `xml:"return_code"` ReturnMsg string `xml:"return_msg"` PrepayID string `xml:"prepay_id"` }
func main() { order := UnifiedOrderRequest{ AppID: AppID, MchID: MchID, NonceStr: generateNonceStr(), Body: "测试订单", OutTradeNo: generateOrderID(), TotalFee: 1, SpbillCreateIP: "123.12.12.123", NotifyURL: NotifyURL, TradeType: TradeType, }
order.Sign = generateSign(order)
requestXML, err := xml.Marshal(order) if err != nil { fmt.Println("XML Marshal failed:", err) return }
resp, err := http.Post(UnifiedOrderURL, "application/xml", bytes.NewBuffer(requestXML)) if err != nil { fmt.Println("Request failed:", err) return } defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Read response failed:", err) return }
var unifiedResp UnifiedOrderResponse err = xml.Unmarshal(body, &unifiedResp) if err != nil { fmt.Println("XML Unmarshal failed:", err) return }
if unifiedResp.ReturnCode == "SUCCESS" { fmt.Println("预支付ID:", unifiedResp.PrepayID) } else { fmt.Println("下单失败:", unifiedResp.ReturnMsg) } }
func generateNonceStr() string { return uuid.New().String() }
func generateOrderID() string { return time.Now().Format("20060102150405") + "000001" }
func generateSign(order UnifiedOrderRequest) string { params := make(map[string]string) params["appid"] = order.AppID params["mch_id"] = order.MchID params["nonce_str"] = order.NonceStr params["body"] = order.Body params["out_trade_no"] = order.OutTradeNo params["total_fee"] = fmt.Sprintf("%d", order.TotalFee) params["spbill_create_ip"] = order.SpbillCreateIP params["notify_url"] = order.NotifyURL params["trade_type"] = order.TradeType
keys := make([]string, 0, len(params)) for k := range params { keys = append(keys, k) } sort.Strings(keys)
var queryString string for _, key := range keys { queryString += fmt.Sprintf("%s=%s&", key, params[key]) } queryString += "key=" + Key
return calculateMD5(queryString) }
func calculateMD5(str string) string { hash := sha256.New() hash.Write([]byte(str)) return fmt.Sprintf("%x", hash.Sum(nil)) }
|