pay.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package pay
  2. import (
  3. "encoding/xml"
  4. "errors"
  5. "fmt"
  6. "github.com/silenceper/wechat/context"
  7. "github.com/silenceper/wechat/util"
  8. )
  9. var payGateway = "https://api.mch.weixin.qq.com/pay/unifiedorder"
  10. // Pay struct extends context
  11. type Pay struct {
  12. *context.Context
  13. }
  14. // Params was NEEDED when request unifiedorder
  15. // 传入的参数,用于生成 prepay_id 的必需参数
  16. type Params struct {
  17. TotalFee string
  18. CreateIP string
  19. Body string
  20. OutTradeNo string
  21. OpenID string
  22. }
  23. // Config 是传出用于 jsdk 用的参数
  24. type Config struct {
  25. Timestamp int64
  26. NonceStr string
  27. PrePayID string
  28. SignType string
  29. Sign string
  30. }
  31. // payResult 是 unifie order 接口的返回
  32. type payResult struct {
  33. ReturnCode string `xml:"return_code"`
  34. ReturnMsg string `xml:"return_msg"`
  35. AppID string `xml:"appid,omitempty"`
  36. MchID string `xml:"mch_id,omitempty"`
  37. NonceStr string `xml:"nonce_str,omitempty"`
  38. Sign string `xml:"sign,omitempty"`
  39. ResultCode string `xml:"result_code,omitempty"`
  40. TradeType string `xml:"trade_type,omitempty"`
  41. PrePayID string `xml:"prepay_id,omitempty"`
  42. CodeURL string `xml:"code_url,omitempty"`
  43. ErrCode string `xml:"err_code,omitempty"`
  44. ErrCodeDes string `xml:"err_code_des,omitempty"`
  45. }
  46. //payRequest 接口请求参数
  47. type payRequest struct {
  48. AppID string `xml:"appid"`
  49. MchID string `xml:"mch_id"`
  50. DeviceInfo string `xml:"device_info,omitempty"`
  51. NonceStr string `xml:"nonce_str"`
  52. Sign string `xml:"sign"`
  53. SignType string `xml:"sign_type,omitempty"`
  54. Body string `xml:"body"`
  55. Detail string `xml:"detail,omitempty"`
  56. Attach string `xml:"attach,omitempty"` //附加数据
  57. OutTradeNo string `xml:"out_trade_no"` //商户订单号
  58. FeeType string `xml:"fee_type,omitempty"` //标价币种
  59. TotalFee string `xml:"total_fee"` //标价金额
  60. SpbillCreateIP string `xml:"spbill_create_ip"` //终端IP
  61. TimeStart string `xml:"time_start,omitempty"` //交易起始时间
  62. TimeExpire string `xml:"time_expire,omitempty"` //交易结束时间
  63. GoodsTag string `xml:"goods_tag,omitempty"` //订单优惠标记
  64. NotifyURL string `xml:"notify_url"` //通知地址
  65. TradeType string `xml:"trade_type"` //交易类型
  66. ProductID string `xml:"product_id,omitempty"` //商品ID
  67. LimitPay string `xml:"limit_pay,omitempty"` //
  68. OpenID string `xml:"openid,omitempty"` //用户标识
  69. SceneInfo string `xml:"scene_info,omitempty"` //场景信息
  70. }
  71. // NewPay return an instance of Pay package
  72. func NewPay(ctx *context.Context) *Pay {
  73. pay := Pay{Context: ctx}
  74. return &pay
  75. }
  76. // PrePayID will request wechat merchant api and request for a pre payment order id
  77. func (pcf *Pay) PrePayID(p *Params) (prePayID string, err error) {
  78. nonceStr := util.RandomStr(32)
  79. tradeType := "JSAPI"
  80. template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s&notify_url=%s&openid=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s&key=%s"
  81. str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OpenID, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType, pcf.PayKey)
  82. sign := util.MD5Sum(str)
  83. request := payRequest{
  84. AppID: pcf.AppID,
  85. MchID: pcf.PayMchID,
  86. NonceStr: nonceStr,
  87. Sign: sign,
  88. Body: p.Body,
  89. OutTradeNo: p.OutTradeNo,
  90. TotalFee: p.TotalFee,
  91. SpbillCreateIP: p.CreateIP,
  92. NotifyURL: pcf.PayNotifyURL,
  93. TradeType: tradeType,
  94. OpenID: p.OpenID,
  95. }
  96. rawRet, err := util.PostXML(payGateway, request)
  97. if err != nil {
  98. return "", errors.New(err.Error() + " parameters : " + str)
  99. }
  100. payRet := payResult{}
  101. err = xml.Unmarshal(rawRet, &payRet)
  102. if err != nil {
  103. return "", errors.New(err.Error())
  104. }
  105. if payRet.ReturnCode == "SUCCESS" {
  106. //pay success
  107. if payRet.ResultCode == "SUCCESS" {
  108. return payRet.PrePayID, nil
  109. }
  110. return "", errors.New(payRet.ErrCode + payRet.ErrCodeDes)
  111. }
  112. return "", errors.New("[msg : xmlUnmarshalError] [rawReturn : " + string(rawRet) + "] [params : " + str + "] [sign : " + sign + "]")
  113. }