互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.6 KiB

package pay_service
import (
"fmt"
"github.com/chanxuehong/wechat/mch/mmpaymkttransfers"
"github.com/chanxuehong/wechat/mch/mmpaymkttransfers/promotion"
"hudongzhuanjia/models"
"hudongzhuanjia/utils"
"time"
)
// 企业向微信用户个人付款(不支持沙箱环境)
type TransferResponse struct {
DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
PartnerTradeNo string `xml:"partner_trade_no,omitempty" json:"partner_trade_no,omitempty"`
PaymentNo string `xml:"payment_no,omitempty" json:"payment_no,omitempty"`
PaymentTime string `xml:"payment_time,omitempty" json:"payment_time,omitempty"`
}
func Transfer(desc, ip, openId string, amount int) (*TransferResponse, error) {
nonceStr := utils.RandomStr(32)
partnerTradeNo := utils.RandomStr(32)
// 初始化参数结构体
body := make(map[string]string)
body["nonce_str"] = nonceStr
body["partner_trade_no"] = partnerTradeNo
body["openid"] = openId
body["check_name"] = "NO_CHECK" // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
body["amount"] = fmt.Sprintf("%d", amount) // 企业付款金额,单位为分
body["desc"] = desc // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
body["spbill_create_ip"] = ip
body["mchid"] = Mchid
body["mch_appid"] = Appid
client, err := Client()
m, err := promotion.Transfers(client, body)
if err != nil {
return nil, err
}
res := &TransferResponse{
DeviceInfo: m["device_info"],
NonceStr: m["nonce_str"],
PartnerTradeNo: m["partner_trade_no"],
PaymentNo: m["payment_no"],
PaymentTime: m["payment_time"],
}
return res, nil
}
type RedPackResult struct {
MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
ReOpenid string `xml:"re_openid,omitempty" json:"re_openid,omitempty"`
TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
SendListid string `xml:"send_listid,omitempty" json:"send_listid,omitempty"`
}
// 发送红包
func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmount, totalNum, scene int) (*RedPackResult, error) {
mchBillNo := utils.RandomStr(28)
nonceStr := utils.RandomStr(32)
body := make(map[string]string, 0)
body["nonce_str"] = nonceStr
body["mch_billno"] = mchBillNo
body["send_name"] = sendName
body["re_openid"] = openId
body["total_amount"] = fmt.Sprintf("%d", totalAmount)
body["total_num"] = fmt.Sprintf("%d", totalNum)
body["wishing"] = wishing
body["client_ip"] = ip
body["act_name"] = actName
body["remark"] = remark
body["scene_id"] = fmt.Sprintf("PRODUCT_%d", scene)
body["ppid"] = Appid
body["mch_id"] = Mchid
client, err := Client()
m, err := mmpaymkttransfers.SendRedPack(client, body)
if err != nil {
return nil, err
}
res := &RedPackResult{
MchBillno: m["mch_billno"],
ReOpenid: m["re_openid"],
TotalAmount: totalAmount,
SendListid: m["send_listid"],
}
userRedPack := new(models.UserRedPack)
userRedPack.MchBillno = mchBillNo
userRedPack.ReOpenid = openId
userRedPack.SceneId = scene
userRedPack.SendListid = res.SendListid
userRedPack.TotalAmount = totalAmount
userRedPack.TotalNum = totalNum
userRedPack.IsDelete = false
userRedPack.UpdatedAt = time.Now()
userRedPack.CreatedAt = time.Now()
_, err = userRedPack.AddRedPack()
if err != nil {
return nil, err
}
return res, nil
}
type QueryRedPackResult struct {
CommonReturn
CommonResult
MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
Mchid string `xml:"mchid,omitempty" json:"mchid,omitempty"`
DetailId string `xml:"detail_id,omitempty" json:"detail_id,omitempty"`
Status string `xml:"status,omitempty" json:"status,omitempty"`
SendType string `xml:"send_type,omitempty" json:"send_type,omitempty"`
HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
TotalNum int `xml:"total_num,omitempty" json:"total_num,omitempty"`
TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
Reason string `xml:"reason,omitempty" json:"reason,omitempty"`
SendTime string `xml:"send_time,omitempty" json:"send_time,omitempty"`
RefundTime string `xml:"refund_time,omitempty" json:"refund_time,omitempty"`
RefundAmount int `xml:"refund_amount,omitempty" json:"refund_amount,omitempty"`
Wishing string `xml:"wishing,omitempty" json:"wishing,omitempty"`
Remark string `xml:"remark,omitempty" json:"remark,omitempty"`
ActName string `xml:"act_name,omitempty" json:"act_name,omitempty"`
}