147 lines
3.8 KiB
Go
147 lines
3.8 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"
|
|
"context"
|
|
|
|
"PaymentCenter/app/models/ordersmodel"
|
|
"PaymentCenter/app/third/paymentService"
|
|
)
|
|
|
|
type PayParam struct {
|
|
Merchant *merchantmodel.Merchant
|
|
Channel *paychannelmodel.PayChannel
|
|
App_id int64
|
|
OutTradeNo string
|
|
Amount int
|
|
ExtJson string
|
|
Desc string
|
|
ClientIp string
|
|
}
|
|
|
|
type Pay struct {
|
|
ctx *context.Context
|
|
PayParam *PayParam
|
|
RelationOrder *ordersmodel.Orders
|
|
Order *ordersmodel.Orders
|
|
PayCode int
|
|
Url string
|
|
}
|
|
|
|
func NewPayWithPayCheck(paycheck *PayCheck) *Pay {
|
|
return &Pay{
|
|
ctx: paycheck.ctx,
|
|
PayParam: &PayParam{
|
|
Merchant: paycheck.Merchant,
|
|
Channel: paycheck.Channel,
|
|
App_id: paycheck.Reqs.AppId,
|
|
OutTradeNo: paycheck.Reqs.OutTradeNo,
|
|
Amount: paycheck.Reqs.Amount,
|
|
ExtJson: paycheck.Reqs.ExtJson,
|
|
Desc: paycheck.Reqs.Desc,
|
|
ClientIp: paycheck.AppCheck.Ip,
|
|
},
|
|
RelationOrder: paycheck.OldOrder,
|
|
PayCode: errorcode.Success,
|
|
}
|
|
}
|
|
|
|
func (w *Pay) CreateOrder(order_type int) {
|
|
if _, ok := common.OrderTypeList[order_type]; !ok { //判断是否是支持的支付渠道
|
|
w.PayCode = errorcode.PayChannelNotFound
|
|
return
|
|
}
|
|
order := &ordersmodel.Orders{
|
|
MerchantId: w.PayParam.Merchant.Id,
|
|
PayChannelId: w.PayParam.Channel.Id,
|
|
AppId: w.PayParam.App_id,
|
|
OutTradeNo: w.PayParam.OutTradeNo,
|
|
OrderType: order_type,
|
|
Amount: w.PayParam.Amount,
|
|
ExtJson: w.PayParam.ExtJson,
|
|
Desc: w.PayParam.Desc,
|
|
Status: common.ORDER_STATUS_WAITPAY,
|
|
}
|
|
if order_type == common.ORDER_TYPE_REFUND {
|
|
order.RefundOrderId = w.RelationOrder.Id
|
|
}
|
|
w.Order, w.PayCode = services.OrderCreate(order)
|
|
}
|
|
|
|
func (w *Pay) PayUrl() (url string) {
|
|
var (
|
|
payFunc func(commonPayInfo *paymentService.PayOrderRequest, channel *paychannelmodel.PayChannel) error
|
|
ok bool
|
|
)
|
|
thirdPay := &paymentService.PayOrderRequest{
|
|
PayChannelId: w.PayParam.Channel.Id,
|
|
OrderId: w.Order.Id,
|
|
ChannelType: w.PayParam.Channel.ChannelType,
|
|
Description: w.Order.Desc,
|
|
Amount: w.Order.Amount,
|
|
PayerClientIp: w.PayParam.ClientIp,
|
|
}
|
|
if payFunc, ok = PayWayList[w.PayParam.Channel.ChannelType]; !ok {
|
|
w.PayCode = errorcode.PayChannelNotBuild
|
|
return
|
|
}
|
|
err := payFunc(thirdPay, w.PayParam.Channel)
|
|
if err != nil {
|
|
w.PayCode = errorcode.PayChannelExtJsonError
|
|
return
|
|
}
|
|
res := paymentService.PaymentService(*w.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.RelationOrder.Id,
|
|
RefundReason: w.PayParam.Desc,
|
|
RefundAmount: int64(w.PayParam.Amount),
|
|
}
|
|
if refundFunc, ok = RefundWayList[w.PayParam.Channel.ChannelType]; !ok {
|
|
w.PayCode = errorcode.PayChannelNotBuild
|
|
return
|
|
}
|
|
err := refundFunc(thirdPayRefund, w.PayParam.Channel)
|
|
if err != nil {
|
|
w.PayCode = errorcode.PayChannelExtJsonError
|
|
return
|
|
}
|
|
res := paymentService.OrderRefund(*w.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
|
|
return
|
|
}
|
|
} else {
|
|
w.PayCode = errorcode.PreRefundFail
|
|
}
|
|
return
|
|
}
|