退款
This commit is contained in:
@@ -52,9 +52,11 @@ var (
|
||||
CodeSafeValueRangeErr = &StatusResponse{5040, "value not in range"} // 值不在范围内
|
||||
CodeTemplateErr = &StatusResponse{5040, "template parsed error"} // 模板解析错误
|
||||
|
||||
CodeOrderNotFoundErr = &StatusResponse{5030, "order not found"} //未找到订单
|
||||
CodeCloudOrderNotFoundErr = &StatusResponse{5031, "cloud order not found"} //未找到云仓订单
|
||||
CodeOrderNotCancelledErr = &StatusResponse{5032, "current order cannot be cancelled"} // 当前订单无法取消
|
||||
CodeOrderNotFoundErr = &StatusResponse{5030, "order not found"} //未找到订单
|
||||
CodeCloudOrderNotFoundErr = &StatusResponse{5031, "cloud order not found"} //未找到云仓订单
|
||||
CodeOrderNotCancelledErr = &StatusResponse{5032, "current order cannot be cancelled"} // 当前订单无法取消
|
||||
CodeOrderCancelledNotOk = &StatusResponse{5033, "current order cancelled failed"} // 当前订单取消失败
|
||||
CodeOrderCancelledOk = &StatusResponse{5034, "current order cancelled successful"} // 当前订单取消成功
|
||||
|
||||
CodePayNotFoundErr = &StatusResponse{5020, "pay info not found"} // 支付信息无法查询
|
||||
CodePayCancelOk = &StatusResponse{5021, "cancellation successful"} // 支付取消成功
|
||||
|
||||
64
utils/handler/payHandler.go
Normal file
64
utils/handler/payHandler.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package handler
|
||||
|
||||
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
|
||||
}
|
||||
@@ -20,7 +20,12 @@ func NewPayDriver(PayMethod int64, config *Config) Pay {
|
||||
|
||||
// Pay 支付集成接口
|
||||
type Pay interface {
|
||||
|
||||
// 支付预处理
|
||||
GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error)
|
||||
|
||||
// 支付退款申请
|
||||
PayRefund(req *PayRefundReq) (res *PayRefundRes, err error)
|
||||
}
|
||||
|
||||
type GeneratePrepaymentReq struct {
|
||||
@@ -41,3 +46,10 @@ type GeneratePrepaymentRes struct {
|
||||
ClientSecret string `json:"clientSecret"` //交易密钥
|
||||
SessionId string `json:"session_id"` //SessionId
|
||||
}
|
||||
|
||||
type PayRefundReq struct {
|
||||
TradeNo string `json:"trade_no"` // 交易编号
|
||||
}
|
||||
|
||||
type PayRefundRes struct {
|
||||
}
|
||||
|
||||
@@ -3,12 +3,25 @@ package pay
|
||||
import (
|
||||
"github.com/stripe/stripe-go/v74"
|
||||
"github.com/stripe/stripe-go/v74/checkout/session"
|
||||
"github.com/stripe/stripe-go/v74/refund"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type Stripe struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
// 生成退款
|
||||
func (stripePay *Stripe) PayRefund(req *PayRefundReq) (res *PayRefundRes, err error) {
|
||||
stripe.Key = stripePay.Key
|
||||
params := &stripe.RefundParams{PaymentIntent: stripe.String(req.TradeNo)}
|
||||
_, err = refund.New(params)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// 生成预付款
|
||||
func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) {
|
||||
var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams
|
||||
|
||||
Reference in New Issue
Block a user