pay.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // FormPayNotify notify
  45. type FormPayNotify struct {
  46. XMLName xml.Name `xml:"xml" json:"_,omitempty"`
  47. AppID string `form:"appid" xml:"appid"`
  48. Attach string `form:"attach" xml:"attach"`
  49. BankType string `form:"bank_type" xml:"bank_type"`
  50. CashFee int `form:"cash_fee" xml:"cash_fee"`
  51. FeeType string `form:"fee_type" xml:"fee_type"`
  52. MchID string `form:"mch_id" xml:"mch_id"`
  53. IsSubscribe string `form:"is_subscribe" xml:"is_subscribe"`
  54. NonceStr string `form:"nonce_str" xml:"nonce_str"`
  55. OpenID string `form:"openid" xml:"openid"`
  56. OutTradeNo string `form:"out_trade_no" xml:"out_trade_no"`
  57. ResultCode string `form:"result_code" xml:"result_code"`
  58. ReturnMsg string `form:"return_msg" xml:"return_msg"`
  59. ReturnCode string `form:"return_code" xml:"return_code"`
  60. ErrCodeDes string `form:"err_code_des" xml:"err_code_des"`
  61. ErrCode string `form:"err_code" xml:"err_code"`
  62. Sign string `form:"sign" xml:"sign"`
  63. TimeEnd string `form:"time_end" xml:"time_end"`
  64. TotalFee int `form:"total_fee" xml:"total_fee"`
  65. TradeType string `form:"trade_type" xml:"trade_type"`
  66. TransactionID string `form:"transaction_id" xml:"transaction_id"`
  67. ContractID string `form:"contract_id" xml:"contract_id"`
  68. }
  69. // PayReply pay reply
  70. type PayReply struct {
  71. XMLName xml.Name `xml:"xml" json:"_,omitempty"`
  72. ReturnCode string `xml:"return_code"`
  73. ReturnMsg string `xml:"return_msg"`
  74. AppID string `xml:"appid"`
  75. MchID string `xml:"mch_id"`
  76. NonceStr string `xml:"nonce_str"`
  77. Sign string `xml:"sign"`
  78. ResultCode string `xml:"result_code"`
  79. PrePayID string `xml:"prepay_id"`
  80. TradeType string `xml:"trade_type"`
  81. URL string `xml:"code_url"`
  82. ErrCode string `xml:"err_code"`
  83. ErrCodeDes string `xml:"err_code_des"`
  84. OpenID string `xml:"openid"`
  85. Attach string `xml:"attach"`
  86. IsSubscribe string `xml:"is_subscribe"`
  87. BankType string `xml:"bank_type"`
  88. CashFee string `xml:"cash_fee"`
  89. FeeType string `xml:"fee_type"`
  90. OutTradeNo string `xml:"out_trade_no"`
  91. TimeEnd string `xml:"time_end"`
  92. TotalFee string `xml:"total_fee"`
  93. TradeState string `xml:"trade_state"`
  94. TradeStateDesc string `xml:"trade_state_desc"`
  95. TransactionID string `xml:"transaction_id"`
  96. ContractID string `form:"contract_id" xml:"contract_id"`
  97. }
  98. // PayQuery order query 订单查询
  99. func PayQuery(cpyid int, config PayConfig, order PayOrderQuery) (reply PayReply, err error) {
  100. m := make(map[string]interface{})
  101. m["appid"] = order.AppID
  102. m["mch_id"] = order.MchID
  103. m["out_trade_no"] = order.OutTradeNo
  104. m["nonce_str"] = order.NonceStr
  105. //m["transaction_id"] = order.TransactionID
  106. order.Sign = Sign(m, config.Key)
  107. var msg myth.HTTPMessage
  108. msg, err = postXML(PayHost+`/pay/orderquery`, order)
  109. if err == nil {
  110. err = msg.XML(&reply)
  111. }
  112. return
  113. }