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.
372 lines
11 KiB
372 lines
11 KiB
package pay_service
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
pay_core "github.com/chanxuehong/wechat/mch/core"
|
|
"github.com/chanxuehong/wechat/mch/pay"
|
|
"hudongzhuanjia/logger"
|
|
"hudongzhuanjia/models"
|
|
"hudongzhuanjia/utils"
|
|
"io"
|
|
"io/ioutil"
|
|
"math"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
go loopUnifiedOrder()
|
|
}
|
|
|
|
var orderDelayQueue = make(chan *models.UserOrder, math.MaxInt8)
|
|
|
|
func PutOrderDelayQueue(order *models.UserOrder) {
|
|
orderDelayQueue <- order
|
|
}
|
|
|
|
func loopUnifiedOrder() {
|
|
orders, err := models.GetUserOrdersByStatus(0, 3)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, order := range orders {
|
|
PutOrderDelayQueue(order)
|
|
}
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
logger.Error("订单轮询查询: panic 恢复错误", err)
|
|
}
|
|
// 重启
|
|
time.Sleep(5 * time.Second)
|
|
loopUnifiedOrder()
|
|
}()
|
|
for {
|
|
select {
|
|
case order, ok := <-orderDelayQueue:
|
|
if !ok {
|
|
panic("通道异常关闭")
|
|
}
|
|
if order.ExpireAt <= time.Now().Unix() { // 订单超时
|
|
if order.Status == 0 {
|
|
err = Close(order.OutTradeNo) // 超时关闭订单
|
|
if err != nil {
|
|
logger.Error("订单关闭出现错误: 错误原因->", err)
|
|
}
|
|
order.Status = 5
|
|
go HandleFailed(order)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if order.Status == 0 {
|
|
res, err := OrderQuery(order.OutTradeNo)
|
|
if err != nil {
|
|
logger.Error("查询订单出现错误: 错误原因-->", err.Error(), "交易订单号-->", order.OutTradeNo)
|
|
orderDelayQueue <- order // 重新进入队列
|
|
continue
|
|
}
|
|
|
|
if res["trade_state"] == CODE_TRADE_SUCCESS {
|
|
go HandleSuccess(order)
|
|
} else if res["trade_state"] == CODE_TRADE_REFUND {
|
|
order.Status = 3
|
|
orderDelayQueue <- order
|
|
continue
|
|
} else {
|
|
orderDelayQueue <- order
|
|
continue
|
|
}
|
|
} else if order.Status == 3 {
|
|
res, err := QueryRefund(order.OutTradeNo)
|
|
if err != nil {
|
|
logger.Error("退款订单查询错误: 错误原因-->", err.Error(), "交易订单号-->", order.OutTradeNo)
|
|
continue
|
|
}
|
|
order.RefundAccount = res.RefundList[0].RefundAccount
|
|
order.RefundRecvAccount = res.RefundList[0].RefundRecvAccout
|
|
order.Status = 4
|
|
_, err = models.Update(order.Id, order, "refund_account", "refund_recv_account", "status")
|
|
if err != nil {
|
|
logger.Error("退款状态改变错误: 错误原因-->", err, "交易订单号-->", order.OutTradeNo)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const CallbackOrderUrl = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackOrder"
|
|
|
|
func UnifiedOrder(content, openId string, fee, goodType, userId, activityId, expireAt int64) (map[string]interface{}, error) {
|
|
client, err := Client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
outTradeNo := utils.RandomStr(32)
|
|
nonceStr := utils.RandomStr(32)
|
|
body := make(map[string]string, 0)
|
|
body["body"] = content
|
|
body["out_trade_no"] = outTradeNo
|
|
body["total_fee"] = fmt.Sprint(fee)
|
|
body["notify_url"] = CallbackOrderUrl
|
|
body["trade_type"] = "JSAPI"
|
|
body["device_info"] = "WEB"
|
|
body["nonce_str"] = nonceStr
|
|
body["sign_type"] = pay_core.SignType_MD5
|
|
body["open_id"] = openId
|
|
body["time_expire"] = pay_core.FormatTime(time.Unix(expireAt, 0))
|
|
|
|
resp, err := pay.UnifiedOrder(client, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 记录这次订单
|
|
userOrder := new(models.UserOrder)
|
|
userOrder.DeviceInfo = "WEB"
|
|
userOrder.Body = content
|
|
userOrder.UserId = userId
|
|
userOrder.ActivityId = activityId
|
|
userOrder.FeeType = "CNY"
|
|
userOrder.GoodType = goodType
|
|
userOrder.OpenId = openId
|
|
userOrder.OutTradeNo = outTradeNo
|
|
userOrder.ExpireAt = expireAt
|
|
userOrder.TotalFee = fee
|
|
userOrder.TradeType = "JSAPI"
|
|
userOrder.PrepayId = resp["prepay_id"]
|
|
userOrder.Status = 0
|
|
if _, err = models.Add(userOrder); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//获取H5支付需要的paySign
|
|
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
|
|
pac := "prepay_id=" + resp["prepay_id"]
|
|
paySign := pay_core.JsapiSign(client.AppId(), timestamp, nonceStr, pac, pay_core.SignType_MD5, ApiKey)
|
|
|
|
go PutOrderDelayQueue(userOrder)
|
|
return map[string]interface{}{
|
|
"appid": Appid,
|
|
"timestamp": timestamp,
|
|
"nonce_str": nonceStr,
|
|
"package": pac,
|
|
"sign_type": pay_core.SignType_MD5,
|
|
"pay_sign": paySign,
|
|
"out_trade_no": outTradeNo,
|
|
"user_order_id": userOrder.Id,
|
|
}, nil
|
|
}
|
|
|
|
func ReOrder(outTradeNo string) (map[string]interface{}, error) {
|
|
userOrder := new(models.UserOrder)
|
|
exist, err := userOrder.GetByOutTradeNo(outTradeNo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exist {
|
|
return nil, fmt.Errorf("订单不存在")
|
|
}
|
|
|
|
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
|
|
nonceStr := utils.RandomStr(32)
|
|
//获取H5支付需要的paySign
|
|
pac := "prepay_id=" + userOrder.PrepayId
|
|
paySign := pay_core.JsapiSign(Appid, timestamp, nonceStr, pac, pay_core.SignType_MD5, ApiKey)
|
|
go PutOrderDelayQueue(userOrder)
|
|
return map[string]interface{}{
|
|
"appid": Appid,
|
|
"timestamp": timestamp,
|
|
"nonce_str": nonceStr,
|
|
"package": pac,
|
|
"sign_type": pay_core.SignType_MD5,
|
|
"pay_sign": paySign,
|
|
"out_trade_no": outTradeNo,
|
|
"user_order_id": userOrder.Id,
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Notify
|
|
type NotifyRequest struct {
|
|
ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`
|
|
ReturnMsg string `xml:"return_msg,omitempty" json:"return_msg,omitempty"`
|
|
ResultCode string `xml:"result_code,omitempty" json:"result_code,omitempty"`
|
|
ErrCode string `xml:"err_code,omitempty" json:"err_code,omitempty"`
|
|
ErrCodeDes string `xml:"err_code_des,omitempty" json:"err_code_des,omitempty"`
|
|
Appid string `xml:"appid,omitempty" json:"appid,omitempty"`
|
|
MchId string `xml:"mch_id,omitempty" json:"mch_id,omitempty"`
|
|
DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
|
|
NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
|
|
Sign string `xml:"sign,omitempty" json:"sign,omitempty"`
|
|
SignType string `xml:"sign_type,omitempty" json:"sign_type,omitempty"`
|
|
Openid string `xml:"openid,omitempty" json:"openid,omitempty"`
|
|
IsSubscribe string `xml:"is_subscribe,omitempty" json:"is_subscribe,omitempty"`
|
|
TradeType string `xml:"trade_type,omitempty" json:"trade_type,omitempty"`
|
|
BankType string `xml:"bank_type,omitempty" json:"bank_type,omitempty"`
|
|
TotalFee int `xml:"total_fee,omitempty" json:"total_fee,omitempty"`
|
|
SettlementTotalFee int `xml:"settlement_total_fee,omitempty" json:"settlement_total_fee,omitempty"`
|
|
FeeType string `xml:"fee_type,omitempty" json:"fee_type,omitempty"`
|
|
CashFee int `xml:"cash_fee,omitempty" json:"cash_fee,omitempty"`
|
|
CashFeeType string `xml:"cash_fee_type,omitempty" json:"cash_fee_type,omitempty"`
|
|
CouponFee int `xml:"coupon_fee,omitempty" json:"coupon_fee,omitempty"`
|
|
CouponCount int `xml:"coupon_count,omitempty" json:"coupon_count,omitempty"`
|
|
CouponType0 string `xml:"coupon_type_0,omitempty" json:"coupon_type_0,omitempty"`
|
|
CouponType1 string `xml:"coupon_type_1,omitempty" json:"coupon_type_1,omitempty"`
|
|
CouponId0 string `xml:"coupon_id_0,omitempty" json:"coupon_id_0,omitempty"`
|
|
CouponId1 string `xml:"coupon_id_1,omitempty" json:"coupon_id_1,omitempty"`
|
|
CouponFee0 int `xml:"coupon_fee_0,omitempty" json:"coupon_fee_0,omitempty"`
|
|
CouponFee1 int `xml:"coupon_fee_1,omitempty" json:"coupon_fee_1,omitempty"`
|
|
TransactionId string `xml:"transaction_id,omitempty" json:"transaction_id,omitempty"`
|
|
OutTradeNo string `xml:"out_trade_no,omitempty" json:"out_trade_no,omitempty"`
|
|
Attach string `xml:"attach,omitempty" json:"attach,omitempty"`
|
|
TimeEnd string `xml:"time_end,omitempty" json:"time_end,omitempty"`
|
|
}
|
|
|
|
func NotifyOrder(w io.Reader) (order *models.UserOrder, err error) {
|
|
var exist bool
|
|
res := NotifyRequest{}
|
|
body, err := ioutil.ReadAll(w)
|
|
if err != nil {
|
|
err = fmt.Errorf("body read all error:%+v", err)
|
|
return nil, err
|
|
}
|
|
err = xml.Unmarshal(body, &res)
|
|
if err != nil {
|
|
err = fmt.Errorf("xml unmarsal error:%+v", err)
|
|
return
|
|
}
|
|
|
|
if res.ReturnCode != CODE_SUCCESS {
|
|
return nil, fmt.Errorf("network error, retrun_code: %+v and return_msg: %+v", res.ReturnCode, res.ReturnMsg)
|
|
}
|
|
|
|
if res.ResultCode == CODE_SUCCESS && res.TradeType == CODE_TRADE_SUCCESS {
|
|
userOrder := new(models.UserOrder)
|
|
userOrder.TransactionId = res.TransactionId
|
|
userOrder.Status = 1
|
|
_, err = userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
exist, err = userOrder.GetByOutTradeNo(res.OutTradeNo)
|
|
if err == nil || !exist {
|
|
err = fmt.Errorf("user order get by out_trade_no: %+v, error: %+v, exist: %+v", res.OutTradeNo, err, exist)
|
|
return
|
|
}
|
|
|
|
err = HandleSuccess(userOrder)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("handle success: %v", err))
|
|
return
|
|
}
|
|
return userOrder, nil
|
|
} else {
|
|
err = fmt.Errorf("trade error, err_code: %+v and err_code_des: %+v", res.ErrCode, res.ErrCodeDes)
|
|
return
|
|
}
|
|
}
|
|
|
|
type OrderQueryResult struct {
|
|
Order *models.UserOrder
|
|
Query *pay.OrderQueryResponse
|
|
}
|
|
|
|
func OrderQuery(outTradeNo string) (map[string]string, error) {
|
|
client, err := Client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
body := make(map[string]string)
|
|
body["out_trade_no"] = outTradeNo
|
|
body["nonce_str"] = utils.RandomStr(32)
|
|
body["sign_type"] = pay_core.SignType_MD5
|
|
|
|
res, err := pay.OrderQuery(client, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func Close(outTradeNo string) error {
|
|
client, err := Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body := make(map[string]string)
|
|
body["out_trade_no"] = outTradeNo
|
|
body["nonce_str"] = utils.RandomStr(32)
|
|
body["sign_type"] = pay_core.SignType_MD5
|
|
|
|
_, err = pay.CloseOrder(client, body)
|
|
// 请求关闭订单,成功后得到结果
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//const CALLBACK_REFUND_URL = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund"
|
|
|
|
func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) {
|
|
userOrder := new(models.UserOrder)
|
|
exist, err := userOrder.GetByOutTradeNo(outTradeNo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exist {
|
|
return nil, fmt.Errorf("订单不存在")
|
|
}
|
|
client, err := Client()
|
|
|
|
outRefundNo := utils.RandomStr(64)
|
|
nonceStr := utils.RandomStr(32)
|
|
res, err := pay.Refund2(client, &pay.RefundRequest{
|
|
TransactionId: "",
|
|
OutTradeNo: outTradeNo,
|
|
OutRefundNo: outRefundNo,
|
|
TotalFee: userOrder.TotalFee,
|
|
RefundFee: userOrder.TotalFee,
|
|
NonceStr: nonceStr,
|
|
SignType: pay_core.SignType_MD5,
|
|
RefundFeeType: "CNY",
|
|
RefundDesc: reason,
|
|
})
|
|
//
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
userOrder.Status = 3
|
|
_, err = userOrder.UpdateStatusByOutTradeNo(outTradeNo, userOrder.Status)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
PutOrderDelayQueue(userOrder) // 退款查询
|
|
return res, nil
|
|
}
|
|
|
|
func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) {
|
|
client, err := Client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res, err := pay.RefundQuery2(client, &pay.RefundQueryRequest{
|
|
OutTradeNo: outTradeNo,
|
|
NonceStr: utils.RandomStr(32),
|
|
SignType: pay_core.SignType_MD5,
|
|
})
|
|
//请求申请退款
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|