PaymentCenter/app/services/thirdpay/pay.go

146 lines
3.9 KiB
Go

package thirdpay
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/api"
thirdpay "PaymentCenter/app/services/thirdpay/do"
"PaymentCenter/app/services/thirdpay/types"
"PaymentCenter/app/third/paymentService/payCommon"
"context"
"github.com/jinzhu/copier"
)
func ThirdPayUrl(check *thirdpay.PayCheck) *thirdpay.Pay {
pay := ThirdPay(check)
// 支付
pay.PayUrl()
return pay
}
func NewOrdersResp(db *ordersmodel.Orders) *api.OrdersResp {
return &api.OrdersResp{
OrderNo: db.Id,
OutTradeNo: db.OutTradeNo,
Status: db.Status,
OrderType: db.OrderType,
Amount: db.Amount,
PayerTotal: db.PayerTotal,
Desc: db.Desc,
CreateTime: db.CreateTime.Format("2006-01-02 15:04:05"),
}
}
func ThirdPayInfoCheck(ctx context.Context, payReq *front.PayReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
var req types.Reqs
copier.Copy(&req, payReq)
check = thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
// 校验表单
check.CheckPayInfo()
if check.CheckCode != errorcode.Success {
return
}
return
}
func ThirdPayRefundCheck(ctx context.Context, refundReq *front.RefundReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck, errorCode int) {
var req types.Reqs
// 外部商户单号,支付商户单号和退款商户单号不能相同
if refundReq.OutTradeNo == refundReq.RefundOutTradeNo {
return check, errorcode.RefundOutTradeNoSame
}
copier.Copy(&req, refundReq)
check = thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
// 校验表单
check.CheckPayInfo()
if check.CheckCode != errorcode.Success {
return check, check.CheckCode
}
check.CheckOrderRefund()
return check, check.CheckCode
}
func ThirdPayRefund(check *thirdpay.PayCheck) (refund *thirdpay.Pay, errorCode int) {
refund = thirdpay.NewPayWithPayCheck(check)
if refund.CheckRefundOrder() {
refund.CreateOrder(common.ORDER_TYPE_REFUND)
if refund.PayCode != errorcode.Success {
return refund, refund.PayCode
}
}
refund.Refund()
return refund, refund.PayCode
}
func ThirdPay(check *thirdpay.PayCheck) (pay *thirdpay.Pay) {
pay = thirdpay.NewPayWithPayCheck(check)
// 创建订单
if check.OldOrder != nil {
pay.Order = check.OldOrder
} else {
pay.CreateOrder(common.ORDER_TYPE_PAY)
if pay.PayCode != errorcode.Success {
return pay
}
}
return
}
func thirdPayCloseCheck(ctx context.Context, payReq *front.CloseReqs, appCheck *services.AppCheck, ip string) (check *thirdpay.PayCheck) {
var req types.Reqs
copier.Copy(&req, payReq)
check = thirdpay.NewPayCheck(&ctx, &req, appCheck, ip)
// 校验表单
check.CheckPayInfo()
return
}
func ThirdCloseOrder(ctx context.Context, closeReq front.CloseReqs, appCheck *services.AppCheck, ip string) (code int, message string) {
check := thirdPayCloseCheck(ctx, &closeReq, appCheck, ip)
if check.CheckCode != errorcode.Success {
return
}
check.GetOldOrder(&types.OrderFindOne{
OutTradeNo: check.Reqs.OutTradeNo,
})
if check.OldOrder != nil {
switch check.OldOrder.Status {
// 1待支付,2支付中, 4支付失败
case common.ORDER_STATUS_WAITPAY, common.ORDER_STATUS_PAYING, common.ORDER_STATUS_FAILED:
// 调用上游关闭订单
client := thirdpay.NewPayWithPayCheck(check)
client.Order = check.OldOrder
client.CloseOrder()
code = client.PayCode
message = client.ThirdMsg
// 修改订单状态为已关闭
if client.PayCode == payCommon.PAY_SUCCESS_CODE {
check.OldOrder.Status = common.ORDER_STATUS_CLOSE
code = services.OrderUpdate(check.OldOrder, "status")
}
// 3支付成功
case common.ORDER_STATUS_PAYED:
code = errorcode.CloseOrderPayed
// 5已关闭
case common.ORDER_STATUS_CLOSE:
code = errorcode.Success
default:
code = errorcode.SystemError
}
} else {
code = errorcode.OrdersNotFound
}
return
}