Browse Source

app & mch

master
黄梓健 5 years ago
parent
commit
f216b939e3
  1. 2
      controllers/client/wx.go
  2. 2
      libs/wechat/wx.go
  3. 8
      libs/wx/wx.go
  4. 82
      services/pay/bindata.go
  5. 8
      services/pay/client.go
  6. 4
      services/pay/handle.go
  7. 6
      services/pay/order.go
  8. 16
      services/pay/order_test.go
  9. 8
      services/pay/transfer.go
  10. 17
      test/order_test.go
  11. 21
      utils/define/define.go

2
controllers/client/wx.go

@ -57,7 +57,7 @@ func (t *WxCtl) Ticket() {
"noncestr": nonceStr,
"timestamp": timestamp,
"ticket": cache.Ticket,
"appid": define.AppId,
"appid": define.SubAppId,
})
}

2
libs/wechat/wx.go

@ -6,7 +6,7 @@ import (
"hudongzhuanjia/utils/define"
)
var Endpoint = mpoauth2.NewEndpoint(define.AppId, define.Secret)
var Endpoint = mpoauth2.NewEndpoint(define.SubAppId, define.Secret)
func GetToken(code string) (*oauth2.Token, error) {
client := oauth2.Client{

8
libs/wx/wx.go

@ -10,15 +10,15 @@ import (
func GetCodeUrl() string {
uid := helper.CreateUUID()
return fmt.Sprintf(CodeUrl, define.AppId, url.PathEscape(RedirectUri), uid)
return fmt.Sprintf(CodeUrl, define.SubAppId, url.PathEscape(RedirectUri), uid)
}
func GetAccessTokenUrl(code string) string {
return fmt.Sprintf(AccessTokenUrl, define.AppId, define.Secret, code)
return fmt.Sprintf(AccessTokenUrl, define.SubAppId, define.Secret, code)
}
func GetRefreshTokenUrl(refreshToken string) string {
return fmt.Sprintf(RefreshTokenUrl, define.AppId, refreshToken)
return fmt.Sprintf(RefreshTokenUrl, define.SubAppId, refreshToken)
}
func GetAuthUrl(accessToken string, openid string) string {
@ -34,7 +34,7 @@ func GetTicketUrl(ticketToken string) string {
}
func GetTicketTokenUrl() string {
return fmt.Sprintf(TicketTokenUrl, define.AppId, define.Secret)
return fmt.Sprintf(TicketTokenUrl, define.SubAppId, define.Secret)
}
///

82
services/pay/bindata.go
File diff suppressed because it is too large
View File

8
services/pay/client.go

@ -12,15 +12,15 @@ import (
// 配置 client
func Client() (*core.Client, error) {
certP12, err := Asset("cacert/apiclient_cert.p12")
certP12, err := Asset("apiclient_cert.p12")
if err != nil {
return nil, err
}
certPem, err := Asset("cacert/apiclient_cert.pem")
certPem, err := Asset("apiclient_cert.pem")
if err != nil {
return nil, err
}
keyPem, err := Asset("cacert/apiclient_key.pem")
keyPem, err := Asset("apiclient_key.pem")
if err != nil {
return nil, err
}
@ -45,5 +45,5 @@ func Client() (*core.Client, error) {
},
}
return core.NewSubMchClient(define.AppId, define.MchId, define.ApiKey, "", define.SubMchId, httpClient), nil
return core.NewSubMchClient(define.PayAppId, define.MchId, define.ApiKey, "", define.SubMchId, httpClient), nil
}

4
services/pay/handle.go

@ -247,7 +247,7 @@ func HandleTimeout(order *models.UserOrder) error {
if order.GoodType == 4 {
err := HandleCancelOrder(order.OutTradeNo)
if err != nil {
logger.Error("下订单", err)
logger.Error(err)
}
}
return nil
@ -281,7 +281,7 @@ func HandleReward() error {
func HandleCancelOrder(outTradeNo string) error {
return models.Commit(func(session *xorm.Session) error {
order := new(models.CustomerOrder)
exist, err := session.Where("is_delete=0 and out_trade_no=?",
exist, err := session.Where("is_delete=0 and status=0 and out_trade_no=?",
outTradeNo).Get(order)
if err != nil {
return err

6
services/pay/order.go

@ -21,6 +21,10 @@ func UnifiedOrder(content, openId string, fee, goodType, userId, activityId, exp
return nil, err
}
fmt.Println(client.AppId)
fmt.Println(client.MchId)
fmt.Println(client.SubMchId)
outTradeNo := utils.RandomStr(32)
nonceStr := utils.RandomStr(32)
body := make(map[string]string, 0)
@ -32,7 +36,7 @@ func UnifiedOrder(content, openId string, fee, goodType, userId, activityId, exp
body["nonce_str"] = nonceStr
body["sign_type"] = pay_core.SignType_MD5
body["openid"] = openId
body["time_expire"] = pay_core.FormatTime(time.Unix(expireAt, 0))
// body["time_expire"] = pay_core.FormatTime(time.Unix(expireAt, 0))
resp, err := pay.UnifiedOrder(client, body)
if err != nil {

16
services/pay/order_test.go

@ -1,16 +0,0 @@
package pay_service
import (
"fmt"
"testing"
"time"
)
func TestUnifiedOrder(t *testing.T) {
res, err := UnifiedOrder("欧轩互动测试", "o9XM41s_NN8Y0QK6_MbM-aYMV3TE",
2, 1, 1, 101, time.Now().Add(1*time.Hour).Unix())
if err != nil {
t.Error(err)
}
fmt.Println(res)
}

8
services/pay/transfer.go

@ -137,7 +137,10 @@ func PutTransferDelayQueue(desc, openId, partnerTradeNo string, amount, retries,
}
func Transfer(desc, openId, partnerTradeNo string, amount int) (*TransferResponse, error) {
client, err := Client()
if err != nil {
return nil, err
}
nonceStr := utils.RandomStr(32)
if partnerTradeNo == "" { // 需要提前存入
partnerTradeNo = utils.RandomStr(32)
@ -152,9 +155,8 @@ func Transfer(desc, openId, partnerTradeNo string, amount int) (*TransferRespons
body["amount"] = fmt.Sprintf("%d", amount) // 企业付款金额,单位为分
body["desc"] = desc // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
body["mchid"] = define.MchId
body["mch_appid"] = define.AppId
body["mch_appid"] = client.AppId()
client, err := Client()
m, err := promotion.Transfers(client, body)
// 记录错误信息
if err != nil {

17
test/order_test.go

@ -0,0 +1,17 @@
package test
import (
"fmt"
pay_service "hudongzhuanjia/services/pay"
"testing"
"time"
)
func TestUnifiedOrder(t *testing.T) {
res, err := pay_service.UnifiedOrder("欧轩互动测试", "o9XM41s_NN8Y0QK6_MbM-aYMV3TE",
200, 0, 1, 101, time.Now().Add(1*time.Hour).Unix())
if err != nil {
t.Error(err)
}
fmt.Println(res)
}

21
utils/define/define.go

@ -90,21 +90,22 @@ var DefaultOrderNo = 10000000000
// 微信常量
const (
// 欧轩互动 -> 普通商户
ApiKey = `2c82c64ceec6ba89ffc9f593c671a12f`
AppId = "wx7b0bcf476552c5e9"
// ApiKey = `2c82c64ceec6ba89ffc9f593c671a12f`
SubAppId = `wx7b0bcf476552c5e9`
Secret = `f6aabdd40ea25272f4442603a7dc8028`
MchId = `1394404502`
SubMchId = ``
// MchId = `1394404502`
// SubMchId = ``
// 欧轩 -> 服务商
//ApiKey = `5x201c8zn43cg675204594cab5423320`
AppId = `wx662a1633304bfd42`
ApiKey = `6e281c8b5430c674034594cab789334F`
//AppId = `wx662a1633304bfd42`
//Secret = `7e4ecfe06ad1e075c210059d5b0162a3`
//MchId = `1441266702`
//SubMchId = `1594049151`
// 认证服务号:欧轩(用于申请微信服务商)
//AppID:wx662a1633304bfd42
//密钥:7e4ecfe06ad1e075c210059d5b0162a3
MchId = `1441266702`
SubMchId = `1394404502`
// 认证服务号:欧轩(用于申请微信服务商)
// AppID:wx662a1633304bfd42
// 密钥:7e4ecfe06ad1e075c210059d5b0162a3
WxHost = `https://api.mch.weixin.qq.com`
WxBack = `https://api2.mch.weixin.qq.com`

Loading…
Cancel
Save