Skip to content

feat(1.增加服务商模式支持2.增加分账api): #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import (

type Account struct {
appID string
subAppId string
mchID string
subMchId string
apiKey string
certData []byte
isSandbox bool
}

// 创建微信支付账号
func NewAccount(appID string, mchID string, apiKey string, isSanbox bool) *Account {
func NewAccount(appID string, subAppId string, mchID string, subMchId string, apiKey string, isSandbox bool) *Account {
return &Account{
appID: appID,
subAppId: subAppId,
mchID: mchID,
subMchId: subMchId,
apiKey: apiKey,
isSandbox: isSanbox,
isSandbox: isSandbox,
}
}

Expand Down
86 changes: 86 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (c *Client) SetAccount(account *Account) {
func (c *Client) fillRequestData(params Params) Params {
params["appid"] = c.account.appID
params["mch_id"] = c.account.mchID
if c.account.subAppId != "" {
params["sub_appid"] = c.account.subAppId
}
if c.account.subMchId != "" {
params["sub_mch_id"] = c.account.subMchId
}
params["nonce_str"] = nonceStr()
params["sign_type"] = c.signType
params["sign"] = c.Sign(params)
Expand Down Expand Up @@ -386,3 +392,83 @@ func (c *Client) AuthCodeToOpenid(params Params) (Params, error) {
}
return c.processResponseXml(xmlStr)
}

// 添加分账接收方
func (c *Client) ProfitSharingAddReceiver(params Params) (Params, error) {
url := ProfitSharingAddReceiverUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 删除分账接收方
func (c *Client) ProfitSharingRemoveReceiver(params Params) (Params, error) {
url := ProfitSharingRemoveReceiverUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 提交单次分账
func (c *Client) ProfitSharing(params Params) (Params, error) {
url := ProfitSharingUrl
xmlStr, err := c.postWithCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 查询分账结果
func (c *Client) ProfitSharingQuery(params Params) (Params, error) {
url := ProfitSharingQueryUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 分账回退
func (c *Client) ProfitSharingReturn(params Params) (Params, error) {
url := ProfitSharingReturnUrl
xmlStr, err := c.postWithCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 分账回退结果查询
func (c *Client) ProfitSharingReturnQuery(params Params) (Params, error) {
url := ProfitSharingReturnQueryUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 分账完结
func (c *Client) ProfitSharingFinish(params Params) (Params, error) {
url := ProfitSharingFinishUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}

// 请求多次分账
func (c *Client) MultiProfitSharing(params Params) (Params, error) {
url := MultiProfitSharingUrl
xmlStr, err := c.postWithoutCert(url, params)
if err != nil {
return nil, err
}
return c.processResponseXml(xmlStr)
}
8 changes: 8 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const (
ReportUrl = "https://api.mch.weixin.qq.com/payitil/report"
ShortUrl = "https://api.mch.weixin.qq.com/tools/shorturl"
AuthCodeToOpenidUrl = "https://api.mch.weixin.qq.com/tools/authcodetoopenid"
ProfitSharingUrl = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing"
MultiProfitSharingUrl = "https://api.mch.weixin.qq.com/secapi/pay/multiprofitsharing"
ProfitSharingQueryUrl = "https://api.mch.weixin.qq.com/pay/profitsharingquery"
ProfitSharingAddReceiverUrl= "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"
ProfitSharingRemoveReceiverUrl= "https://api.mch.weixin.qq.com/pay/profitsharingremovereceiver"
ProfitSharingReturnUrl = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingreturn"
ProfitSharingReturnQueryUrl = "https://api.mch.weixin.qq.com/pay/profitsharingreturnquery"
ProfitSharingFinishUrl = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish"
SandboxMicroPayUrl = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
SandboxUnifiedOrderUrl = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
SandboxOrderQueryUrl = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/objcoding/wxpay

go 1.14

require golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
9 changes: 9 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wxpay
import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/pem"
"encoding/xml"
"golang.org/x/crypto/pkcs12"
Expand Down Expand Up @@ -57,6 +58,14 @@ func MapToXml(params Params) string {
return buf.String()
}

func MapToJson(params Params) string {
paramData, err := json.Marshal(params)
if err != nil {
return "{}"
}
return string(paramData)
}

// 用时间戳生成随机字符串
func nonceStr() string {
return strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
Expand Down