Files
kami_gateway/internal/service/supplier/third_party/apple_shark.go
2025-02-24 21:45:06 +08:00

444 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package third_party
import (
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"gateway/internal/config"
"gateway/internal/otelTrace"
"gateway/internal/service/supplier"
"net/http"
"gateway/internal/models/merchant"
"gateway/internal/models/order"
"gateway/internal/models/payfor"
"gateway/internal/models/road"
"gateway/internal/models/supply_model"
"gateway/internal/service"
"gateway/internal/utils"
"strconv"
"strings"
"time"
"github.com/beego/beego/v2/client/httplib"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/beego/beego/v2/server/web"
"github.com/bytedance/sonic"
"github.com/widuu/gojson"
)
type AppleCardSharkImpl struct {
web.Controller
}
// AppleRechargeOrderStatus 充值编码
//type AppleRechargeOrderStatus int
//
//const (
// AppleRechargeOrderFail AppleRechargeOrderStatus = 0 // 充值失败
// AppleRechargeOrderSuccess AppleRechargeOrderStatus = 1 // 充值成功
// AppleRechargeOrderProcessing AppleRechargeOrderStatus = 2 // 正在充值
// AppleRechargeOrderWaiting AppleRechargeOrderStatus = 3 // 待充值
// AppleRechargeOrderLimited AppleRechargeOrderStatus = 4 // 充值受限
// AppleRechargeOrderExpired AppleRechargeOrderStatus = 5 // 充值过期
// AppleRechargeOrderCanceled AppleRechargeOrderStatus = 6 // 充值取消
// AppleRechargeOrderRefunded AppleRechargeOrderStatus = 7 // 充值退款
// AppleRechargeOrderRefunding AppleRechargeOrderStatus = 8 // 充值退款中
// AppleRechargeOrderRefundFailed AppleRechargeOrderStatus = 9 // 充值退款失败
// AppleRechargeOrderRefundSuccess AppleRechargeOrderStatus = 10 // 充值退款成功
// AppleRechargeOrderRefundWaiting AppleRechargeOrderStatus = 11 // 充值退款待处理
// AppleRechargeOrderRefundProcessing AppleRechargeOrderStatus = 12 // 充值退款处理中
// AppleRechargeOrderAccountOverLimited AppleRechargeOrderStatus = 13 // 账户枯竭
// AppleRechargeOrderCardNoOrCardPassDuplicated AppleRechargeOrderStatus = 14 // 卡号或密码重复
// AppleRechargeOrderAmountDifferent AppleRechargeOrderStatus = 15 // 充值金额与标定金额不一致
//)
func (c *AppleCardSharkImpl) verifyCardNo(cardNo string) bool {
// 验证卡号是否正常
// 限制出现 A、B、E、I、O、S、U、1、0的卡密
forbiddenList := []string{"A", "B", "E", "I", "O", "S", "U", "1", "0"}
for _, s := range forbiddenList {
if strings.Contains(cardNo, s) {
return false
}
}
return true
}
// HasDependencyHTML 是否有单独的支付页面
func (c *AppleCardSharkImpl) HasDependencyHTML() bool {
return false
}
func (c *AppleCardSharkImpl) SendCard(ctx context.Context, jsonStr string, cardInfo supplier.RedeemCardInfo, attach string, merchantId string) (bool, string) {
cfg := config.Config{}
params := map[string]string{
"attach": attach,
"cardNo": cardInfo.CardNo,
"cardPass": cardInfo.Data,
"channelType": "",
"faceValue": cardInfo.FaceType,
"merchantId": "",
"notifyUrl": cfg.GetAppleSharkNotifyUrl(),
"orderAmount": cardInfo.FaceType,
"orderId": attach,
//"sign": utils.TmpEncrypt(attach + cardInfo.Data + strconv.FormatInt()),
}
//加密
oriStr := ""
for s, s2 := range params {
oriStr += s + "=" + s2 + "&"
}
oriStr += "key=12345"
req := httplib.NewBeegoRequestWithCtx(ctx, "http://haocai.just168.vip/api/newOrder", "POST")
req.SetTransport(otelhttp.NewTransport(http.DefaultTransport))
marshal, err := json.Marshal(params)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("Map转化为byte数组失败,异常。", zap.Error(err))
return false, "内部错误请稍后再试试01"
}
req.SetTimeout(time.Second*30, time.Second*30)
req.Header("Content-Type", "application/json")
req.Body(marshal)
req.Header("Accept-Charset", "utf-8")
req.Header("tokenFrom", "iframe")
response, err := req.String()
otelTrace.Logger.WithContext(ctx).Info(response)
return true, ""
}
func (c *AppleCardSharkImpl) Scan(ctx context.Context, orderInfo order.OrderInfo, roadInfo road.RoadInfo, merchantInfo merchant.MerchantInfo) supplier.ScanData {
ctx, cancel := otelTrace.Span(ctx, "苹果鲨鱼", "Scan", trace.WithAttributes(
attribute.String("BankOrderId", orderInfo.BankOrderId),
attribute.String("MerchantUid", orderInfo.MerchantUid),
attribute.String("ExValue", orderInfo.ExValue),
))
defer cancel()
cdata := supplier.RedeemCardInfo{}
err := json.Unmarshal([]byte(orderInfo.ExValue), &cdata)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("格式化数据失败", zap.String("ExValue", orderInfo.ExValue))
return supplier.ScanData{Status: "-1", Msg: "订单有有误,请稍后再试"}
}
ok, str := c.SendCard(ctx, roadInfo.Params, cdata, orderInfo.BankOrderId, orderInfo.MerchantOrderId)
var scanData supplier.ScanData
if !ok {
scanData = supplier.ScanData{
Status: "-1",
Msg: "订单有有误,请稍后再试:" + str,
BankNo: orderInfo.MerchantOrderId,
OrderNo: orderInfo.BankOrderId,
ReturnData: str,
}
return scanData
}
scanData.Status = "00"
scanData.OrderNo = orderInfo.BankOrderId
scanData.BankNo = orderInfo.MerchantOrderId
scanData.OrderPrice = strconv.FormatFloat(orderInfo.OrderAmount, 'f', 2, 64)
scanData.ReturnData = str
return scanData
}
// KMEncrypt 加密卡密
func (c *AppleCardSharkImpl) kMEncrypt(ctx context.Context, kf, appSecret string) (string, error) {
secret := utils.GetMD5LOWER(appSecret)[:16] // 加密秘钥
block, err := aes.NewCipher([]byte(secret))
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("Joker: AesDecrypt failed to NewCipher")
return "", err
}
// 数据填充
plaintext := utils.PadType(kf)
iv := "0102030405060708"
mode := cipher.NewCBCEncrypter(block, []byte(iv))
mode.CryptBlocks(plaintext, plaintext)
return base64.StdEncoding.EncodeToString(plaintext), nil
}
func (c *AppleCardSharkImpl) PayNotify() {
ctx, cancel := otelTrace.Span(c.Ctx.Request.Context(), "苹果鲨鱼", "PayNotify")
defer cancel()
attach := strings.TrimSpace(c.GetString("attach"))
orderInfo := order.GetOrderByBankOrderId(ctx, attach) // OrderId
if orderInfo.BankOrderId == "" || len(orderInfo.BankOrderId) == 0 {
otelTrace.Logger.WithContext(ctx).Error("【APPLE】回调的订单号不存在订单号=", zap.String("attach", attach))
c.Ctx.WriteString("FAIL")
return
}
roadInfo := road.GetRoadInfoByRoadUid(ctx, orderInfo.RoadUid)
if roadInfo.RoadUid == "" || len(roadInfo.RoadUid) == 0 {
otelTrace.Logger.WithContext(ctx).Error("【APPLE】支付通道已经关系或者删除不进行回调")
c.Ctx.WriteString("FAIL")
return
}
merchantUid := orderInfo.MerchantUid
merchantInfo := merchant.GetMerchantByUid(ctx, merchantUid)
if merchantInfo.MerchantUid == "" || len(merchantInfo.MerchantUid) == 0 {
otelTrace.Logger.WithContext(ctx).Error("【APPLE】快付回调失败该商户不存在或者已经删除商户uid=", zap.String("merchantUid", merchantUid))
c.Ctx.WriteString("FAIL")
return
}
params := map[string]string{
"merchantId": strings.TrimSpace(c.GetString("merchantId")),
"amount": strings.TrimSpace(c.GetString("amount")), // 时间戳
"status": strings.TrimSpace(c.GetString("status")),
"sign": strings.TrimSpace(c.GetString("sign")),
"remark": strings.TrimSpace(c.GetString("remark")),
}
orderInfo.BankTransId = params["merchantId"]
if params["status"] == "1" {
// TODO 订单支付成功
isOk := service.SolvePaySuccess(ctx, orderInfo.BankOrderId, orderInfo.FactAmount, params["merchantId"], "支付成功")
if isOk {
c.Ctx.WriteString("SUCCESS")
} else {
c.Ctx.WriteString("FAIL")
}
} else {
isOk := service.SolvePayFail(ctx, orderInfo.BankOrderId, "", "")
if isOk {
c.Ctx.WriteString("SUCCESS")
} else {
c.Ctx.WriteString("FAIL")
}
}
order.InsertCardReturnData(orderInfo.MerchantOrderId, params["remark"])
return
}
func (c *AppleCardSharkImpl) PayQuery(orderInfo order.OrderInfo, roadInfo road.RoadInfo) bool {
params := map[string]any{}
ctx, cancel := otelTrace.Span(c.Ctx.Request.Context(), "苹果鲨鱼", "PayQuery")
defer cancel()
cardData, err := sonic.GetFromString(orderInfo.CardReturnData)
if err != nil {
return false
}
orderId, err := cardData.Get("order_id").String()
if err != nil {
return false
}
params["order_id"] = orderId
params["app_key"] = gojson.Json(roadInfo.Params).Get("appKey").Tostring()
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
params["sign"] = utils.GetMD5SignMF(params, gojson.Json(roadInfo.Params).Get("appSecret").Tostring())
cfg := config.Config{}
url, err := cfg.GetMFCardQueryUrl()
req := httplib.Post(url)
marshal, err := json.Marshal(params)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("Map转化为byte数组失败,异常。", zap.Error(err))
// fmt.Printf("Map转化为byte数组失败,异常:%s\n", err)
return false
}
otelTrace.Logger.WithContext(ctx).Info("请求参数:" + string(marshal))
req.Header("Content-Type", "application/json")
req.Body(marshal)
req.Header("Accept-Charset", "utf-8")
response, err := req.String()
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("MF GetToken 请求失败:", zap.Error(err))
return false
}
otelTrace.Logger.WithContext(ctx).Info("远端请求返回数据:" + response)
resData, err := sonic.GetFromString(response)
if err != nil {
return false
}
resStatus, err := resData.Get("data").Get("status").Int64()
if err != nil {
return false
}
resCode, err := resData.Get("code").Int64()
if err != nil {
return false
}
if resCode == 0 && resStatus == 9 {
return true
}
return false
}
func (c *AppleCardSharkImpl) PayQueryV2(orderInfo order.OrderInfo, roadInfo road.RoadInfo) supply_model.MsgModel {
params := map[string]any{}
ctx, cancel := otelTrace.Span(c.Ctx.Request.Context(), "苹果鲨鱼", "AppleCardSharkImpl")
defer cancel()
cardData, err := sonic.GetFromString(orderInfo.CardReturnData)
if err != nil {
return supply_model.CardMsgErr
}
orderId, err := cardData.Get("order_id").String()
if err != nil {
return supply_model.CardMsgErr
}
params["order_id"] = orderId
params["app_key"] = gojson.Json(roadInfo.Params).Get("appKey").Tostring()
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
params["sign"] = utils.GetMD5SignMF(params, gojson.Json(roadInfo.Params).Get("appSecret").Tostring())
cfg := config.Config{}
url, err := cfg.GetMFCardQueryUrl()
req := httplib.Post(url)
marshal, err := json.Marshal(params)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("Map转化为byte数组失败,异常。", zap.Error(err))
// fmt.Printf("Map转化为byte数组失败,异常:%s\n", err)
return supply_model.DataErr
}
otelTrace.Logger.WithContext(ctx).Info("请求参数:" + string(marshal))
req.Header("Content-Type", "application/json")
req.Body(marshal)
req.Header("Accept-Charset", "utf-8")
response, err := req.String()
if err != nil {
return supply_model.RemoteDataErr
}
otelTrace.Logger.WithContext(ctx).Info("远端请求返回数据:" + response)
resData, err := sonic.GetFromString(response)
if err != nil {
return supply_model.RemoteDataErr
}
resStatus, err := resData.Get("data").Get("status").Int64()
if err != nil {
return supply_model.RemoteDataErr
}
resCode, err := resData.Get("code").Int64()
if err != nil {
return supply_model.RemoteDataErr
}
if resCode == 0 {
switch resStatus {
case 9:
return supply_model.RemoteSuccess
case 2, 3, 4:
return supply_model.RemoteDataDealing
case 7:
return supply_model.RemoteDataHandErr
case 8:
return supply_model.RemoteDataHealingErr
}
}
return supply_model.RemoteDataErr
}
func (c *AppleCardSharkImpl) PayFor(info payfor.PayforInfo) string {
return ""
}
func (c *AppleCardSharkImpl) PayForQuery(payFor payfor.PayforInfo) (string, string) {
cfg := config.Config{}
ctx, cancel := otelTrace.Span(c.Ctx.Request.Context(), "苹果鲨鱼", "PayForQuery")
defer cancel()
url, err := cfg.GetMFCardQueryUrl()
if err != nil {
return config.PAYFOR_FAIL, ""
}
params := map[string]string{}
params["order_id"] = payFor.BankOrderId
params["app_key"] = gojson.Json("").Get("appKey").Tostring()
req := httplib.Post(url)
marshal, err := json.Marshal(params)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("Map转化为byte数组失败,异常。", zap.Error(err))
// fmt.Printf("Map转化为byte数组失败,异常:%s\n", err)
return config.PAYFOR_FAIL, "内部错误请稍后再试试01"
}
otelTrace.Logger.WithContext(ctx).Info("请求参数:" + string(marshal))
req.Header("Content-Type", "application/json")
req.Body(marshal)
req.Header("Accept-Charset", "utf-8")
response, err := req.String()
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("MF GetToken 请求失败:", zap.Error(err))
return config.PAYFOR_FAIL, ""
}
otelTrace.Logger.WithContext(ctx).Info("远端请求返回数据:" + response)
if gojson.Json(response).Get("code").Tostring() == "" {
otelTrace.Logger.WithContext(ctx).Error("远程调用失败")
return config.PAYFOR_BANKING, ""
}
if gojson.Json(response).Get("code").Tostring() == "0" {
type data struct {
OrderID int64 `json:"order_id"`
CardNo string `json:"card_no"`
CardPwd string `json:"card_pwd"`
Status int `json:"status"`
RspInfo string `json:"rsp_info"`
FaceVal int `json:"face_val"`
Amount int `json:"amount"`
Discount string `json:"discount"`
}
var d data
err2 := json.Unmarshal([]byte(gojson.Json(response).Get("data").Tostring()), &d)
if err2 != nil {
return config.PAYFOR_FAIL, ""
}
if d.Status == 9 {
return config.PAYFOR_SUCCESS, ""
}
if d.Status == 4 {
return config.PAYFOR_BANKING, ""
}
if d.Status == 7 || d.Status == 8 {
return config.PAYFOR_FAIL, ""
}
}
otelTrace.Logger.WithContext(ctx).Error("远程调用失败")
return config.PAYFOR_BANKING, ""
}
func (c *AppleCardSharkImpl) BalanceQuery(roadInfo road.RoadInfo) float64 {
return 0.00
}
func (c *AppleCardSharkImpl) PayForNotify() string {
return ""
}