yuleduiPay/service/payservice.go

185 lines
6.0 KiB
Go
Raw Normal View History

2024-11-05 08:32:10 +08:00
package service
import (
2024-11-05 17:34:58 +08:00
"encoding/json"
2024-11-05 08:32:10 +08:00
"errors"
2024-11-05 17:34:58 +08:00
"fmt"
2024-11-05 08:32:10 +08:00
"strconv"
2024-11-05 17:34:58 +08:00
"time"
2024-11-07 18:07:35 +08:00
"yuleduiPay/common"
2024-11-05 08:32:10 +08:00
"yuleduiPay/repo"
"yuleduiPay/service/po"
"yuleduiPay/service/vo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
2024-11-05 09:49:02 +08:00
"github.com/gogf/gf/v2/os/gtime"
2024-11-05 08:32:10 +08:00
"github.com/gogf/gf/v2/util/guid"
)
type Pay struct {
payOrderRepo repo.PayOrder
2024-11-05 09:49:02 +08:00
shopRepo repo.Shop
2024-11-05 08:32:10 +08:00
}
2024-11-05 17:34:58 +08:00
// 微信小程序,扫码支付
2024-11-05 08:32:10 +08:00
func (t *Pay) PayQrCode(r *ghttp.Request) {
2024-11-05 17:34:58 +08:00
g.Log().Line().Print(nil, r.Request) //todo
2024-11-05 08:32:10 +08:00
req := vo.PayQrCodeReq{}
err := r.Parse(&req)
if err != nil {
2024-11-05 09:49:02 +08:00
r.SetError(err)
2024-11-05 08:32:10 +08:00
return
}
g.Log().Line().Printf(nil, "%+v", req)
//获取订单信息
2024-11-05 09:49:02 +08:00
shop, err := t.shopRepo.GetShopById(req.ShopId)
if err != nil {
r.SetError(err)
return
}
2024-11-05 08:32:10 +08:00
//创建订单记录
payOrder := po.PayOrder{}
payOrder.OrderId = guid.S()
payOrder.Price, err = strconv.ParseFloat(req.Price, 64)
if err != nil {
r.SetError(err)
return
}
payOrder.Amount, err = strconv.ParseFloat(req.Amount, 64)
if err != nil {
r.SetError(err)
return
}
payOrder.Bean, err = strconv.ParseFloat(req.Bean, 64)
if err != nil {
r.SetError(err)
return
}
payOrder.OpenId = req.OpenId
payOrder.Mobile = req.Mobile
payOrder.ShopId = req.ShopId
2024-11-07 18:07:35 +08:00
payOrder.Channel = req.Channel
2024-11-05 09:49:02 +08:00
payOrder.Created = gtime.Now()
2024-11-05 08:32:10 +08:00
payOrder.Updated = payOrder.Created
err = t.payOrderRepo.CreatePayOrder(&payOrder)
if err != nil {
r.SetError(err)
return
}
2024-11-05 17:34:58 +08:00
2024-11-08 15:19:11 +08:00
yseRespBytes, err := t.ysePayRequestJson(shop, &payOrder) //访问java项目获取银盛返回的结果
2024-11-05 17:34:58 +08:00
if err != nil {
r.SetError(err)
return
}
2024-11-08 15:19:11 +08:00
fmt.Println("------:", string(yseRespBytes))
2024-11-07 18:07:35 +08:00
2024-11-08 15:19:11 +08:00
yseResp, err := t.yseParseResp(payOrder.Channel, yseRespBytes) //解析报文返回的报文
2024-11-05 17:34:58 +08:00
if err != nil {
r.SetError(err)
return
}
2024-11-08 15:19:11 +08:00
fmt.Println(yseResp)
2024-11-05 17:34:58 +08:00
if yseResp.OutTradeNo != payOrder.OpenId {
errStr := fmt.Sprintf("银盛返回订单号错误,银盛订单号:%s,余乐兑订单号:%s", yseResp.OutTradeNo, payOrder.OpenId)
r.SetError(errors.New(errStr))
return
}
//更新支付订单表字段
updates := g.Map{"ysePayStatus": yseResp.TradeStatus, "updated": time.Now()}
err = t.payOrderRepo.UpdatePayOrderByOrderId(updates, payOrder.OpenId)
if err != nil {
r.SetError(err)
}
2024-11-05 08:32:10 +08:00
return
2024-11-05 17:34:58 +08:00
}
2024-11-08 15:19:11 +08:00
func (t *Pay) yseParseResp(channel int, respBytes []byte) (*vo.BusPayResp, error) {
yepayOnlineWeixinPayResponse := &vo.BusPayResp{}
if channel == 1 { //微信
ysePayResp := &vo.WxPayResp{}
err := json.Unmarshal(respBytes, ysePayResp)
if err != nil {
return nil, err
}
fmt.Println("银盛返回的应答:", ysePayResp)
yepayOnlineWeixinPayResponse = &ysePayResp.YsepayOnlineWeixinPayResponse
} else { //支付宝
ysePayResp := &vo.ZfbPayResp{}
err := json.Unmarshal(respBytes, ysePayResp)
2024-11-07 18:07:35 +08:00
if err != nil {
return nil, err
}
2024-11-08 15:19:11 +08:00
fmt.Println("银盛返回的应答:", ysePayResp)
yepayOnlineWeixinPayResponse = &ysePayResp.YsepayOnlineAlijsapiPayResponse
2024-11-05 17:34:58 +08:00
2024-11-08 15:19:11 +08:00
}
if yepayOnlineWeixinPayResponse.Code != "10000" {
return nil, errors.New("银盛应答返回错误:" + yepayOnlineWeixinPayResponse.Msg)
}
return yepayOnlineWeixinPayResponse, nil
2024-11-07 18:07:35 +08:00
}
2024-11-08 15:19:11 +08:00
func (t *Pay) ysePayRequestJson(shop *po.Shop, payOrder *po.PayOrder) ([]byte, error) {
2024-11-05 17:34:58 +08:00
//业务请求参数
2024-11-07 18:07:35 +08:00
busReq := vo.PayBusReq{}
busReq.OutTradeNo = payOrder.OrderId //订单编号
busReq.Shopdate = payOrder.Created.Format("20060102") //商户系统的交易发生日期格式
busReq.Subject = "余乐兑小程序订单" //订单备注
busReq.TotalAmount = fmt.Sprintf("%.2f", payOrder.Price) //该笔订单的资金总额,保留两位小数
busReq.Currency = "CNY" //默认人民币
2024-11-08 15:19:11 +08:00
busReq.SellerId = shop.ChildAccountNumber //银盛商户ID //订单备注
2024-11-07 18:07:35 +08:00
busReq.SellerName = shop.ShopName //店铺名称
busReq.TimeoutExpress = "1h" //设置未付款交易的超时时间,一个小时
busReq.BusinessCode = g.Cfg().MustGet(nil, "ysepay.busCode").String() //业务代码
var bjson []byte
var err error
if payOrder.Channel == 1 { //微信渠道
wxBusReq := vo.PayWxBusReq{PayBusReq: busReq}
wxBusReq.SubOpenid = payOrder.OpenId //微信用户ID
wxBusReq.IsMinipg = "1" //微信小程序支付:1
wxBusReq.Appid = g.Cfg().MustGet(nil, "weixin.appId").String() //微信小程序APPID
bjson, err = json.Marshal(wxBusReq)
if err != nil {
return nil, err
}
} else { //支付宝渠道
zfBusReq := vo.PayZfBusReq{PayBusReq: busReq}
zfBusReq.BuyerId = payOrder.OpenId //支付宝用户ID
bjson, err = json.Marshal(zfBusReq)
if err != nil {
return nil, err
}
}
commonReq := vo.PayCommonReq{}
if payOrder.Channel == 1 { //微信渠道
commonReq.Method = "ysepay.online.weixin.pay"
} else { //支付宝渠道
commonReq.Method = "ysepay.online.alijsapi.pay"
}
commonReq.PartnerId = g.Cfg().MustGet(nil, "ysepay.CERTID").String() //在银盛支付开设的服务商商户号
commonReq.Timestamp = time.Now().Format("2006-01-02 15:04:05") //发送请求的时间
commonReq.Charset = "UTF-8" //商户网站使用的编码格式
commonReq.SignType = "SM" //报文签名算法
commonReq.NotifyUrl = g.Cfg().MustGet(nil, "ysepay.notifyUrl").String() //交易成功异步通知到商户的后台地址
commonReq.Version = "3.0" //接口版本
2024-11-08 15:19:11 +08:00
commonReq.BizContent = string(bjson)
2024-11-07 18:07:35 +08:00
bjson, err = json.Marshal(&commonReq)
2024-11-05 17:34:58 +08:00
if err != nil {
return nil, err
}
2024-11-07 18:07:35 +08:00
url := g.Cfg().MustGet(nil, "ysepay.url").String()
2024-11-08 15:19:11 +08:00
xxx := string(bjson)
fmt.Println(xxx)
//发送给java项目让其访问银盛接口
bjson, err = common.HttpPost(url, "application/json", bjson)
2024-11-07 18:07:35 +08:00
if err != nil {
return nil, err
}
2024-11-08 15:19:11 +08:00
return bjson, err
2024-11-05 08:32:10 +08:00
}