pay.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package pay
  2. import (
  3. "crypto/md5"
  4. "strings"
  5. "github.com/silenceper/wechat/context"
  6. "github.com/silenceper/wechat/util"
  7. )
  8. var payGateway := "https://api.mch.weixin.qq.com/pay/unifiedorder"
  9. // Pay struct extends context
  10. type Pay struct {
  11. *context.Context
  12. }
  13. // PayParams was NEEDED when request unifiedorder
  14. type PayParams struct {
  15. TotalFee string
  16. CreateIP string
  17. Body string
  18. OutTradeNo string
  19. }
  20. type PayResult struct {
  21. Success bool
  22. PrePayID string
  23. }
  24. //PayRequest
  25. type payRequest struct {
  26. AppID string `xml:"appid"`
  27. MchID string `xml:"mch_id"`
  28. NotifyUrl string `xml:"notify_url"` //通知地址
  29. DeviceInfo string `xml:"device_info,omitempty"`
  30. NonceStr string `xml:"nonce_str"`
  31. Sign string `xml:"sign"`
  32. SignType string `xml:"sign_type,omitempty"`
  33. Body string `xml:"body"`
  34. Detail string `xml:"detail,omitempty"`
  35. Attach string `xml:"attach,omitempty"` //附加数据
  36. OutTradeNo string `xml:"out_trade_no"` //商户订单号
  37. FeeType string `xml:"fee_type,omitempty"` //标价币种
  38. TotalFee string `xml:"total_fee"` //标价金额
  39. SpbillCreateIp string `xml:"spbill_create_ip"` //终端IP
  40. TimeStart string `xml:"time_start,omitempty"` //交易起始时间
  41. TimeExpire string `xml:"time_expire,omitempty"` //交易结束时间
  42. GoodsTag string `xml:"goods_tag,omitempty"` //订单优惠标记
  43. TradeType string `xml:"trade_type"` //交易类型
  44. ProductId string `xml:"product_id,omitempty"` //商品ID
  45. LimitPay string `xml:"limit_pay,omitempty"` //
  46. OpenID string `xml:"openid,omitempty"` //用户标识
  47. SceneInfo string `xml:"scene_info,omitempty"` //场景信息
  48. }
  49. type payResponse struct {
  50. }
  51. // NewPay return an instance of Pay package
  52. func NewPay(ctx *context.Context) *Pay {
  53. pay := Pay{Context: ctx}
  54. return &pay
  55. }
  56. // PrePayId will request wechat merchant api and request for a pre payment order id
  57. func (pcf *Pay) PrePayId(p *PayParams) payResult *PayResult {
  58. nonceStr := util.RandomStr(32)
  59. pType = "JSAPI"
  60. template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s&notify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type"
  61. stringA := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.MchID, nonceStr, pcf.NotifyUrl, p.OutTradeNo, p.CreateIP, p.TotalFee, pType)
  62. signature := md5.Sum(stringA + pcf.PayKey)
  63. sign := strings.ToUpper(signature)
  64. request := payRequest{
  65. AppID: pcf.AppID,
  66. MchID: pcf.MchID,
  67. NotifyUrl: pcf.NotifyUrl,
  68. NonceStr: util.RandomStr(32),
  69. Sign: sign,
  70. Body: p.Body,
  71. OutTradeNo: p.OutTradeNo,
  72. TotalFee: p.TotalFee,
  73. SpbillCreateIp: params.CreateIP,
  74. OpenID: params.OpenID,
  75. }
  76. ret, err := util.PostXML(payGateway, request)
  77. if err != nil {
  78. }
  79. fmt.Println(string(ret))
  80. }