fusenapi/utils/handlers/payHandler.go
2023-08-03 17:37:25 +08:00

65 lines
1.7 KiB
Go

package handlers
import (
"fusenapi/constants"
"fusenapi/utils/pay"
"github.com/zeromicro/go-zero/core/mr"
)
type (
PayInfo struct {
TradeNo string `json:"trade_no"`
PaymentMethod int64 `json:"payment_method"`
Key string `json:"key"`
}
PayRefundHandlerReq struct {
PayInfoList []PayInfo
}
PayRefundHandlerRes struct {
}
)
// 申请第三方退款
func PayRefundHandler(req *PayRefundHandlerReq) (res PayRefundHandlerRes, err error) {
_, err = mr.MapReduce(func(source chan<- interface{}) {
for _, payInfo := range req.PayInfoList {
source <- payInfo
}
}, func(item interface{}, writer mr.Writer[interface{}], cancel func(error)) {
payConfig := new(pay.Config)
payInfo := item.(PayInfo)
switch payInfo.PaymentMethod {
case int64(constants.PAYMETHOD_STRIPE):
// stripe 支付
payConfig.Stripe.Key = payInfo.Key
}
payDriver := pay.NewPayDriver(payInfo.PaymentMethod, payConfig)
_, err = payDriver.PayRefund(&pay.PayRefundReq{
TradeNo: payInfo.TradeNo,
})
if err != nil {
// Notice 如果不加 cancel(err),会返回校验成功的id; 如果加上cancel(err),返回的结果会是一个空列表
// Notice 实际上,如果这里返回错误,其他协程直接就退出了!
// Notice 看实际中业务的需求情况来定了...
cancel(err)
}
// Notice 这个必须加!
writer.Write(payInfo)
}, func(pipe <-chan interface{}, writer mr.Writer[interface{}], cancel func(error)) {
var payInfoList []PayInfo
for p := range pipe {
payInfoList = append(payInfoList, p.(PayInfo))
}
// Notice 这个必须加!
writer.Write(payInfoList)
})
if err != nil {
return res, err
}
return res, nil
}