From e58fccd6d5731ddec418815e7446b925ab11cd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Tue, 4 Feb 2020 17:16:45 +0800 Subject: [PATCH] weixin --- platformapi/wxpayapi/wxpayapi.go | 89 ++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/platformapi/wxpayapi/wxpayapi.go b/platformapi/wxpayapi/wxpayapi.go index 732e0a45..c9bc3457 100644 --- a/platformapi/wxpayapi/wxpayapi.go +++ b/platformapi/wxpayapi/wxpayapi.go @@ -2,7 +2,7 @@ package wxpayapi import ( "bytes" - "crypto/md5" + "crypto/sha256" "crypto/tls" "encoding/xml" "fmt" @@ -28,7 +28,7 @@ const ( sigKey = "sign" sigTypeKey = "sign_type" - sigType = "MD5" + sigType = "HMAC-SHA256" ) const ( @@ -64,6 +64,17 @@ type RequestBase struct { SignType string `json:"sign_type,omitempty" xml:"sign_type,omitempty"` } +type RequestBase2 struct { + XMLName xml.Name `json:"-" xml:"xml"` + + AppID string `json:"mch_appid" xml:"mch_appid"` + DeviceInfo string `json:"device_info,omitempty" xml:"device_info,omitempty"` + MchID string `json:"mchid" xml:"mchid"` + NonceStr string `json:"nonce_str" xml:"nonce_str"` + Sign string `json:"sign" xml:"sign"` + SignType string `json:"sign_type,omitempty" xml:"sign_type,omitempty"` +} + type IRequestBase interface { SetAppID(appID string) SetMchID(mchID string) @@ -222,6 +233,58 @@ type PayRefundResult struct { TransactionID string `json:"transaction_id"` } +type MultiProfitSharing struct { + RequestBase + TransactionID string `json:"transaction_id" xml:"transaction_id"` + OutOrderNo string `json:"out_order_no" xml:"out_order_no"` + Receivers CData `json:"receivers" xml:"receivers"` +} + +type MultiProfitSharingResult struct { + ReturnCode string `json:"return_code" xml:"return_code"` + ReturnMsg string `json:"return_msg" xml:"return_msg"` + + ResultCode string `json:"result_code" xml:"result_code"` + ErrCode string `json:"err_code,omitempty" xml:"err_code,omitempty"` + ErrCodeDes string `json:"err_code_des,omitempty" xml:"err_code_des,omitempty"` + AppID string `json:"appid" xml:"appid"` + MchID string `json:"mch_id" xml:"mch_id"` + NonceStr string `json:"nonce_str" xml:"nonce_str"` + Sign string `json:"sign" xml:"sign"` + + TransactionID string `json:"transaction_id" xml:"transaction_id"` + OutOrderNo string `json:"out_order_no" xml:"out_order_no"` + OrderID string `json:"order_id" xml:"order_id"` +} + +type Transfers struct { + RequestBase2 + PartnerTradeNo string `json:"partner_trade_no" xml:"partner_trade_no"` + OpenID string `json:"openid" xml:"openid"` + CheckName string `json:"check_name" xml:"check_name"` + ReUserName string `json:"re_user_name,omitempty" xml:"re_user_name,omitempty"` + Amount int `json:"amount" xml:"amount"` + Desc string `json:"desc" xml:"desc"` + SpbillCreateIP string `json:"spbill_create_ip" xml:"spbill_create_ip"` +} + +type TransfersResult struct { + ReturnCode string `json:"return_code" xml:"return_code"` + ReturnMsg string `json:"return_msg" xml:"return_msg"` + + ResultCode string `json:"result_code" xml:"result_code"` + ErrCode string `json:"err_code,omitempty" xml:"err_code,omitempty"` + ErrCodeDes string `json:"err_code_des,omitempty" xml:"err_code_des,omitempty"` + AppID string `json:"appid" xml:"appid"` + MchID string `json:"mch_id" xml:"mch_id"` + NonceStr string `json:"nonce_str" xml:"nonce_str"` + DeviceInfo string `json:"device_info,omitempty" xml:"device_info,omitempty"` + + PartnerTradeNo string `json:"partner_trade_no" xml:"partner_trade_no"` + PaymentNo string `json:"payment_no" xml:"payment_no"` + PaymentTime string `json:"payment_time" xml:"payment_time"` +} + func New(appID, appKey, mchID string, config ...*platformapi.APIConfig) *API { curConfig := platformapi.DefAPIConfig if len(config) > 0 { @@ -277,7 +340,7 @@ func (a *API) signParam(params map[string]interface{}) (sig string) { sort.Sort(sort.StringSlice(valueList)) valueList = append(valueList, fmt.Sprintf("key=%s", a.appKey)) sig = strings.Join(valueList, "&") - sig = fmt.Sprintf("%X", md5.Sum([]byte(sig))) + sig = fmt.Sprintf("%X", sha256.Sum256([]byte(sig))) return sig } @@ -420,3 +483,23 @@ func (a *API) PayRefund(param *PayRefundParam) (refundResult *PayRefundResult, e } return refundResult, err } + +//请求多次分账 +//https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_6&index=2 +func (a *API) MultiProfitSharing(param *MultiProfitSharing) (result *MultiProfitSharingResult, err error) { + retVal, err := a.AccessAPI("secapi/pay/multiprofitsharing", param) + if err == nil { + err = a.translateResult(retVal, &result) + } + return result, err +} + +//企业付款 +//https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2 +func (a *API) Transfers(param *Transfers) (result *TransfersResult, err error) { + retVal, err := a.AccessAPI("mmpaymkttransfers/promotion/transfers", param) + if err == nil { + err = a.translateResult(retVal, &result) + } + return result, err +}