f
This commit is contained in:
@@ -46,7 +46,7 @@ service main {
|
|||||||
type (
|
type (
|
||||||
PaymentReq {
|
PaymentReq {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
PayMethod string `json:"pay_method"`
|
PayMethod string `json:"pay_method" validate:"required,oneof=wechat alipay appleiap test"`
|
||||||
PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"`
|
PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"`
|
||||||
}
|
}
|
||||||
PaymentResp {
|
PaymentResp {
|
||||||
|
|||||||
@@ -78,7 +78,14 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
|||||||
// 支付宝:按订单写入的商户标识(one/two)创建支付订单
|
// 支付宝:按订单写入的商户标识(one/two)创建支付订单
|
||||||
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.payMerchantID, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.payMerchantID, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
||||||
} else if req.PayMethod == "appleiap" {
|
} else if req.PayMethod == "appleiap" {
|
||||||
prepayData = l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo)
|
iap := l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo)
|
||||||
|
prepayData = iap
|
||||||
|
if iap == "" {
|
||||||
|
createOrderErr = fmt.Errorf("获取 IAP 参数失败")
|
||||||
|
}
|
||||||
|
} else if req.PayMethod == "test" {
|
||||||
|
// 开发环境测试支付已在上方分支写入 prepayData;若走到此处说明非 development
|
||||||
|
return errors.Wrapf(xerr.NewErrMsg("测试支付仅在开发环境可用"), "ENV!=development 且 pay_method=test")
|
||||||
}
|
}
|
||||||
if createOrderErr != nil {
|
if createOrderErr != nil {
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
|
||||||
@@ -89,6 +96,12 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.PayMethod == "wechat" || req.PayMethod == "alipay" {
|
||||||
|
if prepayData == nil {
|
||||||
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "创建支付失败: 未生成支付参数")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 开发环境测试支付模式:事务提交后处理订单状态更新和后续流程(仅 pay_method=test 且 query 类型 orderID>0)
|
// 开发环境测试支付模式:事务提交后处理订单状态更新和后续流程(仅 pay_method=test 且 query 类型 orderID>0)
|
||||||
isDevTestPayment := os.Getenv("ENV") == "development" && req.PayMethod == "test"
|
isDevTestPayment := os.Getenv("ENV") == "development" && req.PayMethod == "test"
|
||||||
if isDevTestPayment && paymentTypeResp != nil && paymentTypeResp.orderID != 0 {
|
if isDevTestPayment && paymentTypeResp != nil && paymentTypeResp.orderID != 0 {
|
||||||
|
|||||||
@@ -180,6 +180,36 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
|
|||||||
return *resp.PrepayId, nil
|
return *resp.PrepayId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// jsapiRequestPaymentToMap 将 JSAPI 调起参数转为 map[string]string,便于 JSON 序列化给前端 WeixinJSBridge
|
||||||
|
func jsapiRequestPaymentToMap(resp *jsapi.PrepayWithRequestPaymentResponse) (map[string]string, error) {
|
||||||
|
if resp == nil {
|
||||||
|
return nil, fmt.Errorf("微信下单返回为空")
|
||||||
|
}
|
||||||
|
m := map[string]string{}
|
||||||
|
if resp.Appid != nil {
|
||||||
|
m["appId"] = *resp.Appid
|
||||||
|
}
|
||||||
|
if resp.TimeStamp != nil {
|
||||||
|
m["timeStamp"] = *resp.TimeStamp
|
||||||
|
}
|
||||||
|
if resp.NonceStr != nil {
|
||||||
|
m["nonceStr"] = *resp.NonceStr
|
||||||
|
}
|
||||||
|
if resp.Package != nil {
|
||||||
|
m["package"] = *resp.Package
|
||||||
|
}
|
||||||
|
if resp.SignType != nil {
|
||||||
|
m["signType"] = *resp.SignType
|
||||||
|
}
|
||||||
|
if resp.PaySign != nil {
|
||||||
|
m["paySign"] = *resp.PaySign
|
||||||
|
}
|
||||||
|
if len(m) != 6 {
|
||||||
|
return nil, fmt.Errorf("微信 JSAPI 调起参数不完整")
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CreateWechatMiniProgramOrder 创建微信小程序支付订单
|
// CreateWechatMiniProgramOrder 创建微信小程序支付订单
|
||||||
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (interface{}, error) {
|
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (interface{}, error) {
|
||||||
totalAmount := lzUtils.ToWechatAmount(amount)
|
totalAmount := lzUtils.ToWechatAmount(amount)
|
||||||
@@ -206,8 +236,7 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
// 返回预支付交易会话标识
|
return jsapiRequestPaymentToMap(resp)
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWechatH5Order 创建微信H5支付订单
|
// CreateWechatH5Order 创建微信H5支付订单
|
||||||
@@ -237,14 +266,16 @@ func (w *WechatPayService) CreateWechatH5Order(ctx context.Context, amount float
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
// 返回预支付交易会话标识
|
return jsapiRequestPaymentToMap(resp)
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWechatOrder 创建微信支付订单(集成 APP、H5、小程序)
|
// CreateWechatOrder 创建微信支付订单(集成 APP、H5、小程序)
|
||||||
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
|
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
|
||||||
// 根据 ctx 中的 platform 判断平台
|
platformVal := ctx.Value("platform")
|
||||||
platform := ctx.Value("platform").(string)
|
platform, ok := platformVal.(string)
|
||||||
|
if !ok || platform == "" {
|
||||||
|
return "", fmt.Errorf("缺少 X-Platform 请求头(微信内请传 wxh5)")
|
||||||
|
}
|
||||||
|
|
||||||
var prepayData interface{}
|
var prepayData interface{}
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@@ -1851,7 +1851,7 @@ type PaymentCheckResp struct {
|
|||||||
|
|
||||||
type PaymentReq struct {
|
type PaymentReq struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
PayMethod string `json:"pay_method"`
|
PayMethod string `json:"pay_method" validate:"required,oneof=wechat alipay appleiap test"`
|
||||||
PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"`
|
PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user