PaymentCenter/app/services/thirdpay/do/pay.go

122 lines
3.2 KiB
Go

package do
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/models/merchantmodel"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/third/paymentService/payCommon"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/third/paymentService"
)
type Pay struct {
Merchant *merchantmodel.Merchant
Channel *paychannelmodel.PayChannel
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.Reqs.AppId,
OutTreadNo: w.paycheck.Reqs.OutTradeNo,
OrderType: order_type,
Amount: w.paycheck.Reqs.Amount,
ExtJson: w.paycheck.Reqs.ExtJson,
Desc: w.paycheck.Reqs.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.Reqs.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
}
func (w *Pay) Refund() {
var (
refundFunc func(commonRefundInfo *paymentService.OrderRefundRequest, channel *paychannelmodel.PayChannel) error
ok bool
)
thirdPayRefund := &paymentService.OrderRefundRequest{
OrderId: w.Order.Id,
RefundOrderId: w.paycheck.OldOrder.Id,
RefundReason: w.paycheck.Reqs.Desc,
RefundAmount: int64(w.paycheck.Reqs.Amount),
PayChannel: w.paycheck.Channel.ChannelType,
}
if refundFunc, ok = RefundWayList[w.paycheck.Channel.ChannelType]; !ok {
w.PayCode = errorcode.PayChannelNotBuild
return
}
err := refundFunc(thirdPayRefund, w.paycheck.Channel)
if err != nil {
w.PayCode = errorcode.PayChannelExtJsonError
return
}
res := paymentService.OrderRefund(*w.paycheck.ctx, *thirdPayRefund)
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
}
} else {
w.PayCode = errorcode.PreRefundFail
}
return
}