2023-08-03 09:37:25 +00:00
|
|
|
package handlers
|
2023-07-28 11:03:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|