84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package do
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/common"
|
|
"PaymentCenter/app/constants/errorcode"
|
|
"PaymentCenter/app/models/paychannelmodel"
|
|
"PaymentCenter/app/services"
|
|
"PaymentCenter/app/third/paymentService/payCommon"
|
|
|
|
"PaymentCenter/app/models/ordersmodel"
|
|
"PaymentCenter/app/third/paymentService"
|
|
)
|
|
|
|
type Pay struct {
|
|
paycheck *PayCheck
|
|
Order *ordersmodel.Orders
|
|
PayCode int
|
|
Url string
|
|
}
|
|
|
|
func NewPay(paycheck *PayCheck) *Pay {
|
|
return &Pay{
|
|
paycheck: paycheck,
|
|
PayCode: errorcode.Success,
|
|
}
|
|
}
|
|
|
|
func (w *Pay) CreateOrder(order_type int) {
|
|
if _, ok := common.OrderTypeList[order_type]; !ok { //判断是否是支持的支付渠道
|
|
w.PayCode = errorcode.PayChannelNotFound
|
|
return
|
|
}
|
|
w.Order, w.PayCode = services.OrderCreate(&ordersmodel.Orders{
|
|
MerchantId: w.paycheck.Merchant.Id,
|
|
PayChannelId: w.paycheck.Channel.Id,
|
|
AppId: w.paycheck.Channel.Id,
|
|
OutTreadNo: w.paycheck.WebPayReqs.OutTradeNo,
|
|
OrderType: order_type,
|
|
Amount: w.paycheck.WebPayReqs.Amount,
|
|
ExtJson: w.paycheck.WebPayReqs.ExtJson,
|
|
Desc: w.paycheck.WebPayReqs.Desc,
|
|
Status: common.ORDER_STATUS_WAITPAY,
|
|
},
|
|
)
|
|
}
|
|
|
|
func (w *Pay) PayUrl() (url string) {
|
|
var (
|
|
payFunc func(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error
|
|
ok bool
|
|
)
|
|
thirdPay := &paymentService.PayOrderRequest{
|
|
PayChannelId: w.paycheck.WebPayReqs.PayChannelId,
|
|
OrderId: w.Order.Id,
|
|
ChannelType: w.paycheck.Channel.ChannelType,
|
|
Description: w.Order.Desc,
|
|
Amount: w.Order.Amount,
|
|
PayerClientIp: w.paycheck.AppCheck.Ip,
|
|
}
|
|
if payFunc, ok = PayWayList[w.paycheck.Channel.ChannelType]; !ok {
|
|
w.PayCode = errorcode.PayChannelNotBuild
|
|
return
|
|
}
|
|
err := payFunc(thirdPay, w.paycheck.Channel)
|
|
if err != nil {
|
|
w.PayCode = errorcode.PayChannelExtJsonError
|
|
return
|
|
}
|
|
res := paymentService.PaymentService(*w.paycheck.ctx, *thirdPay)
|
|
|
|
if res.Code == payCommon.PAY_SUCCESS_CODE {
|
|
w.Order.Status = common.ORDER_STATUS_PAYING
|
|
code := services.OrderUpdate(w.Order, "status")
|
|
if code != errorcode.Success {
|
|
w.PayCode = code
|
|
}
|
|
w.Url = res.Result
|
|
} else {
|
|
w.PayCode = errorcode.PrePayFail
|
|
}
|
|
|
|
return
|
|
}
|