187 lines
5.5 KiB
Go
187 lines
5.5 KiB
Go
package app_server
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"git.rosy.net.cn/baseapi/platformapi/tonglianpayapi"
|
||
"git.rosy.net.cn/baseapi/platformapi/wxpayapi"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-print/dao"
|
||
"git.rosy.net.cn/jx-print/globals"
|
||
userModel "git.rosy.net.cn/jx-print/model"
|
||
printModel "git.rosy.net.cn/jx-print/model/app_model"
|
||
"git.rosy.net.cn/jx-print/services/api"
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/jmoiron/sqlx"
|
||
"math/rand"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// pay4OrderByWX 直接微信支付
|
||
func pay4OrderByWX(user *userModel.User, totalFell int, ip, vendorPayType string) (orderPay *printModel.OrderPay, err error) {
|
||
payCreatedAt := time.Now()
|
||
param := &wxpayapi.CreateOrderParam{
|
||
OutTradeNo: utils.Int64ToStr(time.Now().Unix()) + fmt.Sprintf("%d", rand.Intn(100000)),
|
||
Body: printModel.PayBody,
|
||
NotifyURL: globals.TongLianPayCallBack,
|
||
SpbillCreateIP: ip,
|
||
TradeType: vendorPayType,
|
||
TotalFee: totalFell,
|
||
|
||
TimeStart: wxpayapi.Time2PayTime(payCreatedAt),
|
||
ProfitSharing: wxpayapi.OptYes,
|
||
}
|
||
if user.OpenId != "" {
|
||
param.OpenID = user.OpenId
|
||
}
|
||
|
||
result, err := api.WxpayAPI.CreateUnifiedOrder(param)
|
||
if err == nil {
|
||
orderPay = &printModel.OrderPay{
|
||
CreatedAt: time.Now(),
|
||
UpdatedAt: time.Now(),
|
||
PayOrderID: param.OutTradeNo,
|
||
PayType: printModel.PayTypeWX,
|
||
VendorPayType: vendorPayType,
|
||
Status: 0,
|
||
PayCreatedAt: payCreatedAt,
|
||
PrepayID: result.PrepayID,
|
||
CodeURL: result.CodeURL,
|
||
TotalFee: int(totalFell),
|
||
}
|
||
}
|
||
return orderPay, err
|
||
}
|
||
|
||
// pay4OrderByTL 通联支付
|
||
func pay4OrderByTL(user *userModel.User, totalFell int, payType int, vendorPayType string) (orderPay *printModel.OrderPay, err error) {
|
||
payCreatedAt := time.Now()
|
||
param := &tonglianpayapi.CreateUnitorderOrderParam{
|
||
Trxamt: totalFell,
|
||
NotifyUrl: globals.TongLianPayCallBack,
|
||
Reqsn: utils.Int64ToStr(time.Now().Unix()) + fmt.Sprintf("%d", rand.Intn(100000)),
|
||
PayType: vendorPayType,
|
||
}
|
||
//暂时做兼容处理
|
||
if vendorPayType == "JSAPI" {
|
||
param.PayType = tonglianpayapi.PayTypeWxXcx
|
||
}
|
||
if vendorPayType == tonglianpayapi.PayTypeWxXcx {
|
||
param.Acct = user.OpenId
|
||
}
|
||
|
||
if vendorPayType == tonglianpayapi.PayTypeZfbJS || vendorPayType == tonglianpayapi.PayTypeZfbApp {
|
||
param.Acct = user.OpenId
|
||
if param.Acct == "" {
|
||
return nil, fmt.Errorf("未找到用户的认证ID!")
|
||
}
|
||
}
|
||
if vendorPayType == tonglianpayapi.PayTypeH5 {
|
||
param2 := &tonglianpayapi.CreateH5UnitorderOrderParam{
|
||
Trxamt: totalFell,
|
||
NotifyUrl: globals.TongLianPayCallBack,
|
||
Body: printModel.PayBody,
|
||
Charset: "UTF-8",
|
||
}
|
||
err = api.TLpayAPI.CreateH5UnitorderOrder(param2)
|
||
} else {
|
||
result, err := api.TLpayAPI.CreateUnitorderOrder(param)
|
||
if err == nil {
|
||
var result2 tonglianpayapi.PayInfo
|
||
json.Unmarshal([]byte(result.PayInfo), &result2)
|
||
prePayID := result2.Package[strings.LastIndex(result2.Package, "=")+1 : len(result2.Package)]
|
||
orderPay = &printModel.OrderPay{
|
||
PayOrderID: param.Reqsn,
|
||
PayType: payType,
|
||
VendorPayType: vendorPayType,
|
||
TransactionID: result.TrxID,
|
||
VendorOrderID: param.Reqsn,
|
||
VendorID: printModel.OrderPayVendorId,
|
||
Status: 0,
|
||
PayCreatedAt: payCreatedAt,
|
||
PrepayID: prePayID,
|
||
CodeURL: utils.LimitUTF8StringLen(result.PayInfo, 3200),
|
||
TotalFee: param.Trxamt,
|
||
}
|
||
}
|
||
}
|
||
return orderPay, err
|
||
}
|
||
|
||
// onTLPayFinished 通联微信支付回调
|
||
func onTLPayFinished(call *tonglianpayapi.CallBackResult) (err error) {
|
||
orderPay, err := dao.QueryPrinterPayRecord(call.CusorderID)
|
||
if err != nil {
|
||
globals.SugarLogger.Debugf("onTLpayFinished msg:%s, err:%v", utils.Format4Output(call, true), err)
|
||
return err
|
||
}
|
||
if orderPay.Status != 0 {
|
||
return err
|
||
}
|
||
loc, _ := time.LoadLocation("Local")
|
||
t1, _ := time.ParseInLocation("20060102150405", call.PayTime, loc)
|
||
orderPay.PayFinishedAt = t1
|
||
orderPay.OriginalData = utils.Format4Output(call, true)
|
||
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
|
||
orderPay.Status = printModel.PayStatusYes
|
||
} else {
|
||
orderPay.Status = printModel.PayStatusFailed
|
||
}
|
||
|
||
txDb := globals.GetTxDb()
|
||
if err := dao.UpdatePrinterPayRecord(txDb, orderPay, "status", "pay_finished_at", "original_data"); err != nil {
|
||
globals.SugarLogger.Debugf("UpdatePrinterPayRecord msg:%s, err:%v", utils.Format4Output(call, true), err)
|
||
txDb.Rollback()
|
||
return err
|
||
}
|
||
|
||
if call.TrxStatus == tonglianpayapi.TrxStatusSuccess {
|
||
err = onPayFinished(txDb, orderPay)
|
||
}
|
||
|
||
txDb.Commit()
|
||
return err
|
||
}
|
||
|
||
// onPayFinished 通联支付完成时,修改打印机账户余额
|
||
func onPayFinished(db *sqlx.Tx, param *printModel.OrderPay) error {
|
||
// 修改打印机余额
|
||
if err := dao.AddMoney(db, param.PrintNo, int64(param.TotalFee)); err != nil {
|
||
globals.SugarLogger.Debugf("onPayFinished update bancall err = %v", err)
|
||
db.Rollback()
|
||
return err
|
||
}
|
||
// 添加充值记录
|
||
record := &printModel.PrintBillRecord{
|
||
CreatedAt: time.Now(),
|
||
UpdatedAt: time.Now(),
|
||
PrintNo: param.PrintNo,
|
||
PayType: 1,
|
||
PayMoney: param.TotalFee,
|
||
OrderId: param.PayOrderID,
|
||
UserId: "",
|
||
}
|
||
if err := dao.AddIncomeRecord(db, record); err != nil {
|
||
globals.SugarLogger.Debugf("onPayFinished AddIncomeRecord bancall err = %v", err)
|
||
db.Rollback()
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// GetRealRemoteIP 获取ip
|
||
func (p *PrintPay) GetRealRemoteIP(ctx *gin.Context) (ip string) {
|
||
ip = ctx.ClientIP()
|
||
if ip == "" {
|
||
ip = ctx.Request.Header.Get("X-real-ip")
|
||
}
|
||
if ip == "" {
|
||
ip = strings.Split(ctx.Request.RemoteAddr, ":")[0]
|
||
} else {
|
||
ip = strings.Split(ip, ",")[0]
|
||
}
|
||
return ip
|
||
}
|