PaymentCenter/app/services/thirdpay/wx_mini.go

144 lines
3.6 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 thirdpay
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/do"
"PaymentCenter/app/third/paymentService"
"PaymentCenter/app/third/paymentService/payCommon"
"PaymentCenter/app/utils"
"context"
"encoding/json"
"strconv"
"time"
)
// 小程序支付 通过code换取网页授权access_tokenopenid,
func GetWxAuthMini(param front.GetWxAuthRequest) (rsp front.GetWxAuthMiniResponse, code int) {
wx := do.NewWxMini()
rsp, err := wx.GetWxAuthMini(param)
if err != nil {
code = handErr(err)
return
}
if !rsp.IsSuccess() && rsp.Openid == "" {
code = errorcode.GetOpenIdErr
} else {
code = errorcode.Success
}
return
}
// 小程序 获取sechema 链接
func GetWxMiniSchemeService(param front.GetWxMiniSchemeRequest) (scheme string, code int) {
var err error
wx := do.NewWxMini()
scheme, err = wx.GetWxMiniScheme(param)
if err != nil {
code = handErr(err)
}
return scheme, code
}
// 微信小程序支付
func WxMiniPay(param front.WxJsApiPayRequest) (response front.WxMiniPayResponse, code int) {
var (
ctx = context.Background()
wxConfig = paymentService.WxPay{}
)
task := newWxJsapiPay(param)
// 1 获取订单
task.getOrder()
if task.code != errorcode.Success {
return response, task.code
}
// 获取支付渠道配置
task.getPayChannel()
if task.code != errorcode.Success {
return response, task.code
}
// 配置支付的解析
err := json.Unmarshal([]byte(task.payChannel.ExtJson), &wxConfig)
if err != nil {
task.code = handErr(err)
return
}
task.wxConfig = wxConfig
task.wxConfig.AppId = task.payChannel.AppId
// 获取下单的请求参数
task.getOrderRequestLogs()
if task.code != errorcode.Success {
return response, task.code
}
// 2 通过code获取openid
rsp, code := GetWxAuthMini(front.GetWxAuthRequest{
Code: param.Code,
PayChannelId: strconv.Itoa(int(task.order.PayChannelId)),
})
if code != errorcode.Success {
response.ThirdMsg = rsp.Errmsg
return
}
task.openId = rsp.Openid
order := task.order
// 通过openid订单数据请求微信下单, 获取prepay_id
orderRequest := paymentService.PayOrderRequest{
PayChannelId: order.PayChannelId,
OrderId: order.Id,
ChannelType: task.payChannel.ChannelType,
Description: order.Desc,
Amount: order.Amount,
PayerClientIp: param.ClientIp,
ReturnUrl: task.orderPayRequest.ReturnUrl,
Wx: task.wxConfig,
OpenId: task.openId,
}
res := paymentService.PaymentService(ctx, orderRequest)
// 下单失败
if res.Code != payCommon.PAY_SUCCESS_CODE {
task.code = errorcode.PrePayFail
response.ThirdMsg = res.ErrorMsg
return response, task.code
}
// 更新订单状态
order.Status = common.ORDER_STATUS_PAYING
task.code = services.OrderUpdate(order, "status")
if task.code != errorcode.Success {
utils.Log(nil, "成功下单,更新订单状态失败", order)
}
// 3 组装返回数据
response = front.WxMiniPayResponse{
AppId: task.payChannel.AppId,
TimeStamp: strconv.Itoa(int(time.Now().Unix())),
NonceStr: strconv.Itoa(int(time.Now().Unix())),
Package: "prepay_id=" + res.Result,
SignType: "RSA",
ThirdMsg: res.ErrorMsg,
}
// 4 签名
signValue, err := task.Sign(response.AppId, response.TimeStamp, response.NonceStr, response.Package)
if err != nil {
utils.Log(nil, "签名失败", err)
return response, errorcode.WechatAuthFail
}
response.PaySign = signValue
response.ReturnUrl = task.orderPayRequest.ReturnUrl
return response, task.code
}