124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package thirdpay
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/common"
|
|
"PaymentCenter/app/constants/errorcode"
|
|
"PaymentCenter/app/services"
|
|
|
|
"PaymentCenter/app/http/entities/front"
|
|
"PaymentCenter/app/models/merchantmodel"
|
|
"PaymentCenter/app/models/orderrequestlogmodel"
|
|
"PaymentCenter/app/models/ordersmodel"
|
|
"PaymentCenter/app/models/paychannelmodel"
|
|
"PaymentCenter/app/third/paymentService"
|
|
"context"
|
|
"github.com/bytedance/sonic"
|
|
)
|
|
|
|
type WebPay struct {
|
|
ctx *context.Context
|
|
WebPayReqs *front.PayWebReqs
|
|
AppCheck *services.AppCheck
|
|
Order *ordersmodel.Orders
|
|
Channel *paychannelmodel.PayChannel
|
|
Merchant *merchantmodel.Merchant
|
|
PayCode int
|
|
}
|
|
|
|
func NewWebPay(ctx *context.Context, reqs *front.PayWebReqs, appCheck *services.AppCheck, ip string) *WebPay {
|
|
if appCheck == nil {
|
|
appCheck = services.AppGetAndCheck(&services.AppCheck{
|
|
AppId: reqs.AppId,
|
|
Ip: ip,
|
|
Code: errorcode.Success,
|
|
})
|
|
}
|
|
return &WebPay{
|
|
ctx: ctx,
|
|
WebPayReqs: reqs,
|
|
AppCheck: appCheck,
|
|
PayCode: appCheck.Code,
|
|
}
|
|
}
|
|
|
|
func (w *WebPay) CheckForm() {
|
|
if _, ok := common.OrderTypeList[w.WebPayReqs.OrderType]; !ok { //判断是否是支持的支付渠道
|
|
w.PayCode = errorcode.PayChannelNotFound
|
|
return
|
|
}
|
|
|
|
}
|
|
|
|
func (w *WebPay) AddPayLog() {
|
|
requestJson, err := sonic.Marshal(w.WebPayReqs)
|
|
if err != nil {
|
|
w.PayCode = errorcode.RequestLogErrors
|
|
return
|
|
}
|
|
code := services.RequestLogCreate(&orderrequestlogmodel.OrderRequestLog{
|
|
IpAddress: w.AppCheck.Ip,
|
|
MerchantRequest: string(requestJson),
|
|
})
|
|
w.PayCode = code
|
|
return
|
|
}
|
|
|
|
func (w *WebPay) CheckPayChannel() {
|
|
w.Channel, w.PayCode = services.GetAndCheckPayChannel(&paychannelmodel.PayChannel{Id: w.WebPayReqs.PayChannelId}, nil)
|
|
}
|
|
|
|
func (w *WebPay) CheckMerchant() {
|
|
w.Merchant, w.PayCode = services.GetAndCheckMerchant(&merchantmodel.Merchant{Id: w.AppCheck.App.MerchantId}, nil)
|
|
}
|
|
|
|
func (w *WebPay) CheckOrder() {
|
|
exist, code := services.HadSameValueOrder(&ordersmodel.Orders{OutTreadNo: w.WebPayReqs.OutTradeNo})
|
|
if exist {
|
|
w.PayCode = code
|
|
}
|
|
}
|
|
|
|
func (w *WebPay) CreateOrder() {
|
|
w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{
|
|
MerchantId: w.Merchant.Id,
|
|
PayChannelId: w.Channel.Id,
|
|
AppId: w.Channel.Id,
|
|
OutTreadNo: w.WebPayReqs.OutTradeNo,
|
|
OrderType: w.WebPayReqs.OrderType,
|
|
Amount: w.WebPayReqs.Amount,
|
|
ExtJson: w.WebPayReqs.ExtJson,
|
|
Desc: w.WebPayReqs.Desc,
|
|
},
|
|
)
|
|
}
|
|
|
|
func (w *WebPay) Pay() {
|
|
thirdPay := &paymentService.PayOrderRequest{
|
|
PayChannelId: w.WebPayReqs.PayChannelId,
|
|
OrderId: w.Order.Id,
|
|
ChannelType: w.Channel.ChannelType,
|
|
Description: w.Order.Desc,
|
|
Amount: w.Order.Amount,
|
|
PayerClientIp: w.AppCheck.Ip,
|
|
}
|
|
//{"api_v_3_key": "131231231", "app_id": "123", "mch_id": "123123", "private_key": "2131312", "serial_no": "123213213"}
|
|
switch w.WebPayReqs.OrderType {
|
|
case common.PAY_CHANNEL_WECHAT_H5:
|
|
var wx paymentService.WxPay
|
|
err := sonic.Unmarshal([]byte(w.Channel.ExtJson), &wx)
|
|
if err != nil {
|
|
w.PayCode = errorcode.PayChannelExtJsonError
|
|
return
|
|
}
|
|
wx.AppId = w.Channel.AppId
|
|
case common.PAY_CHANNEL_ALIPAY_WEB:
|
|
//{"ali_public_key": "123", "private_key_path": "123", "sign_type": "123"}
|
|
//thirdPay.Ali = &paymentService.AliPay{}
|
|
default:
|
|
w.PayCode = errorcode.PayChannelNotBuild
|
|
return
|
|
}
|
|
_ = paymentService.PaymentService(*w.ctx, *thirdPay)
|
|
return
|
|
}
|