pay.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package wechat
  2. import (
  3. "encoding/xml"
  4. "git.chuangxin1.com/cx/myth"
  5. )
  6. // PayConfig WeChat pay configure
  7. type PayConfig struct {
  8. AppID string `json:"appid"`
  9. MchID string `json:"mchid"`
  10. Key string `json:"key"`
  11. AppSecret string `json:"appsecret"`
  12. SSLCert string `json:"sslcert"`
  13. SSLKey string `json:"sslkey"`
  14. ContractID string `json:"contractid"`
  15. NotifyURL string `json:"notify_url"`
  16. }
  17. // PayUnifiedOrder https://api.mch.weixin.qq.com/pay/unifiedorder
  18. type PayUnifiedOrder struct {
  19. XMLName xml.Name `xml:"xml"`
  20. AppID string `xml:"appid"`
  21. MchID string `xml:"mch_id"`
  22. Body string `xml:"body"`
  23. NonceStr string `xml:"nonce_str"`
  24. NotifyURL string `xml:"notify_url"`
  25. TradeType string `xml:"trade_type"`
  26. OpenID string `xml:"openid"`
  27. SpbillCreateIP string `xml:"spbill_create_ip"`
  28. TimeStart string `xml:"time_start"`
  29. TotalFee int `xml:"total_fee"`
  30. OutTradeNo string `xml:"out_trade_no"`
  31. Attach string `xml:"attach"`
  32. Sign string `xml:"sign"`
  33. }
  34. // PayOrderQuery https://api.mch.weixin.qq.com/pay/orderquery
  35. type PayOrderQuery struct {
  36. XMLName xml.Name `xml:"xml"`
  37. AppID string `xml:"appid"`
  38. MchID string `xml:"mch_id"`
  39. NonceStr string `xml:"nonce_str"`
  40. TransactionID string `xml:"transaction_id,omitempty"`
  41. OutTradeNo string `xml:"out_trade_no,omitempty"`
  42. Sign string `xml:"sign"`
  43. }
  44. // PayReply pay reply
  45. type PayReply struct {
  46. XMLName xml.Name `xml:"xml" json:"_,omitempty"`
  47. ReturnCode string `xml:"return_code"`
  48. ReturnMsg string `xml:"return_msg"`
  49. AppID string `xml:"appid"`
  50. MchID string `xml:"mch_id"`
  51. NonceStr string `xml:"nonce_str"`
  52. Sign string `xml:"sign"`
  53. ResultCode string `xml:"result_code"`
  54. PrePayID string `xml:"prepay_id"`
  55. TradeType string `xml:"trade_type"`
  56. URL string `xml:"code_url"`
  57. ErrCode string `xml:"err_code"`
  58. ErrCodeDes string `xml:"err_code_des"`
  59. OpenID string `xml:"openid"`
  60. Attach string `xml:"attach"`
  61. IsSubscribe string `xml:"is_subscribe"`
  62. BankType string `xml:"bank_type"`
  63. CashFee string `xml:"cash_fee"`
  64. FeeType string `xml:"fee_type"`
  65. OutTradeNo string `xml:"out_trade_no"`
  66. TimeEnd string `xml:"time_end"`
  67. TotalFee string `xml:"total_fee"`
  68. TradeState string `xml:"trade_state"`
  69. TradeStateDesc string `xml:"trade_state_desc"`
  70. TransactionID string `xml:"transaction_id"`
  71. ContractID string `form:"contract_id" xml:"contract_id"`
  72. }
  73. // PayQuery order query 订单查询
  74. func PayQuery(cpyid int, config PayConfig, order PayOrderQuery) (reply PayReply, err error) {
  75. m := make(map[string]interface{})
  76. m["appid"] = order.AppID
  77. m["mch_id"] = order.MchID
  78. m["out_trade_no"] = order.OutTradeNo
  79. m["nonce_str"] = order.NonceStr
  80. //m["transaction_id"] = order.TransactionID
  81. order.Sign = Sign(m, config.Key)
  82. var msg myth.HTTPMessage
  83. msg, err = postXML(PayHost+`/pay/orderquery`, order)
  84. if err == nil {
  85. err = msg.XML(&reply)
  86. }
  87. return
  88. }