Files
baseapi/platformapi/tiktok/callback.go
邹宗楠 dad0d7bb19 1
2022-06-01 21:58:28 +08:00

91 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tiktok
import (
"encoding/json"
"git.rosy.net.cn/baseapi/utils"
"io/ioutil"
"net/http"
)
const (
PayStatus = "payment" // 回调类型标记,支付成功回调为"payment"
RefundStatus = "refund" // 回调类型标记,退款回调为"refund"
WxPayWay = "1" // 1-微信支付
ZfbPayWay = "2" // 2-支付宝支付
TtPayWay = "10" // 10-抖音支付
)
type CallBackResult struct {
Timestamp int `json:"timestamp"` // Unix 时间戳10 位,整型数
Nonce string `json:"nonce"` // 随机数
Msg string `json:"msg"` // 订单信息的 json 字符串
TypePay string `json:"type"` // 回调类型标记,支付成功回调为"payment"
Msgsignature string `json:"msgsignature"` // 签名,详见
}
// 详细信息MSG(支付回调)
type DetailCallBackMessage struct {
AppId string `json:"appid"` // 当前交易发起的小程序id
CpOrderno string `json:"cp_orderno"` // 开发者侧的订单号
CpExtra string `json:"cp_extra"` // 预下单时开发者传入字段
Way string `json:"way"` // way 字段中标识了支付渠道1-微信支付2-支付宝支付10-抖音支付
ChannelNo string `json:"channel_no"` // 支付渠道侧单号
PaymentOrderNo string `json:"payment_order_no"` // 支付渠道侧商家订单号
TotalAmount int64 `json:"total_amount"` // 支付金额,单位为分
Status string `json:"status"` // 固定SUCCESS
ItemId string `json:"item_id"` // 订单来源视频对应视频 id
SellerUid string `json:"seller_uid"` // 该笔交易卖家商户号
PaidAt int64 `json:"paid_at"` // 支付时间Unix 时间戳10 位,整型数
OrderId string `json:"order_id"` // 抖音侧订单号
}
// 详细信息MSG(退款回调)
type DetailCallBackMessage2Refund struct {
AppId string `json:"appid"` // 当前交易发起的小程序id
CpRefundno string `json:"cp_refundno"` // 开发者侧的退款订单号
CpExtra string `json:"cp_extra"` // 预下单时开发者传入字段
Status string `json:"status"` // 固定SUCCESS/FAIL
RefundAmount int64 `json:"refund_amount"` // 退款金额
RefundedAt int64 `json:"refunded_at"` // 退款时间
Message string `json:"message"` // 退款失败描述
OrderId string `json:"order_id"` // 抖音侧订单号
}
func (a *API) GetCallbackMsg(request *http.Request) (call *DetailCallBackMessage, refund *DetailCallBackMessage2Refund, payType string, err error) {
data, err := ioutil.ReadAll(request.Body)
if err != nil {
return nil, nil, "", err
}
values, err := utils.HTTPBody2Values(data, false)
if err != nil {
return nil, nil, "", err
}
payInfo := &CallBackResult{}
if err := utils.Map2StructByJson(utils.URLValues2Map(values), &payInfo, false); err != nil {
return nil, nil, "", err
}
// 支付回调
if payInfo.TypePay == PayStatus {
payDetailResult := &DetailCallBackMessage{}
if err := json.Unmarshal([]byte(payInfo.Msg), payDetailResult); err != nil {
return nil, nil, "", err
}
return payDetailResult, nil, payInfo.TypePay, err
}
// 退款回调
if payInfo.TypePay == RefundStatus {
refundDetailResult := &DetailCallBackMessage2Refund{}
if err := json.Unmarshal([]byte(payInfo.Msg), refundDetailResult); err != nil {
return nil, nil, "", err
}
return nil, refundDetailResult, payInfo.TypePay, err
}
return nil, nil, "", err
}