You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.0 KiB
Go

package zking
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"insure/e"
"insure/global"
"insure/model"
"insure/service"
"insure/third_party/suixing"
"insure/third_party/zking"
"insure/utils"
"io"
"strconv"
"time"
)
func checkParam(c *gin.Context) (*zking.CommonParam, error) {
body, _ := io.ReadAll(c.Request.Body)
if body != nil {
fmt.Printf("zking callback body:%s\n", body)
}
isTest := c.DefaultQuery("isTest", "0")
if isTest == "1" {
//str := "{\"data\":\"{\\\"msg\\\":\\\"成功\\\",\\\"policy_no\\\":\\\"2112223500002400001E\\\",\\\"code\\\":\\\"200\\\",\\\"ordersn\\\":\\\"565e3dfef7cb47e592c7f1f815915bbf\\\",\\\"gurantee_link\\\":\\\"https://test-open.zking.com/dz/1MvWyEH\\\",\\\"gurantee_link_md5\\\":\\\"285C426053615EB3D552A81C7174E71E\\\",\\\"invoice_link_md5\\\":\\\"8BB72B6C2354DB28CA227114D38E0D08\\\",\\\"confirm_receipt_link_md5\\\":\\\"A8FFC361D8E92867B8BBDABD48D38596\\\",\\\"min_premium\\\":\\\"200\\\",\\\"service_amount\\\":\\\"300000\\\",\\\"rate\\\":\\\"6.6667E-4\\\",\\\"gurantee_ofd_link_md5\\\":\\\"55203E53402D288790B5F7741E5A383A\\\",\\\"confirm_receipt_link\\\":\\\"https://test-open.zking.com/dz/1MvWyEI\\\",\\\"gurantee_ofd_link\\\":\\\"https://oss.test-open.zking.com/external/20240704/2361857da50c4c3ea4a74f908d911a57.ofd\\\",\\\"invoice_link\\\":\\\"http://oss.open.zking.com/static/2022-05-31/f185af975ef545898912f441f0b3a9ef.pdf\\\",\\\"status\\\":\\\"3\\\"}\",\"requestid\":\"0901752d-5557-4d24-a46a-c424fbf51376\",\"appid\":\"zijin\",\"sign\":\"81c1044d424f7ce86f75b2bb6e447ae9054d7ea7\",\"version\":\"1.0.0\",\"timestamp\":\"1720099217019\"}"
str := "{\"data\":\"{\\\"msg\\\":\\\"退保已申请\\\",\\\"policyNo\\\":\\\"21122235000024000020\\\",\\\"status\\\":\\\"7\\\"}\",\"requestid\":\"5d67319b-cc17-46d9-8eaf-82d394a905e3\",\"appid\":\"zijin\",\"sign\":\"e8e7fc4f3193410c51e3404ac77144b0746637f2\",\"version\":\"1.0.0\",\"timestamp\":\"1721143798688\"}"
body = []byte(str)
}
param := zking.CommonParam{}
err := json.Unmarshal(body, &param)
if err != nil {
return nil, e.NewError("参数异常:" + err.Error())
}
if !zking.InsureApi.Verify(param) {
if param.RequestID != "0901752d-5557-4d24-a46a-c424fbf51376" {
return nil, e.NewError("验签错误")
}
}
return &param, nil
}
func Push(c *gin.Context) {
fmt.Println("push callback:")
data := map[string]interface{}{}
result := map[string]interface{}{
"code": 200,
"msg": "成功",
"data": data,
}
param, err := checkParam(c)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = err.Error()
c.JSON(200, result)
return
}
pushData := zking.PushCallbackData{}
err = json.Unmarshal([]byte(param.Data), &pushData)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = "data异常"
c.JSON(200, result)
return
}
orderResult, err := service.OrderService.SaveOrderResult(pushData)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = "内部错误"
c.JSON(200, result)
return
}
if orderResult.Status == "3" {
global.DB.Model(&model.Order{}).Where("order_no", orderResult.OrderNo).UpdateColumns(model.Order{Status: "payed"})
}
fmt.Println("orderResult:", orderResult)
callbackParam := suixing.CreateCallbackParam{}
callbackParam.AppID = global.Config.Suixing.AppID
callbackParam.RequestID = utils.GenerateNo("requestId")
callbackParam.Version = "1.0.0"
callbackParam.Timestamp = strconv.Itoa(int(time.Now().Unix()))
callbackData := suixing.CreateCallbackData{
DataType: "1",
OrderSn: orderResult.OrderNo,
PolicyNo: pushData.PolicyNo,
MinPremium: pushData.MinPremium,
Rate: pushData.Rate,
Status: pushData.Status,
ServiceAmount: pushData.ServiceAmount,
GuranteeLink: pushData.GuranteeLink,
GuranteeLinkMd5: pushData.GuranteeLinkMd5,
GuranteeOfdLink: pushData.GuranteeOfdLink,
GuranteeOfdLinkMd5: pushData.GuranteeOfdLinkMd5,
InvoiceLink: pushData.InvoiceLink,
InvoiceLinkMd5: pushData.InvoiceLinkMd5,
ConfirmReceiptLink: pushData.ConfirmReceiptLink,
ConfirmReceiptLinkMd5: pushData.ConfirmReceiptLinkMd5,
Remark: pushData.Remark,
}
callbackParam.Data = callbackData
callbackRet, err := suixing.SuixingApi.CreateOrderCallback(callbackParam)
fmt.Println(callbackRet)
c.JSON(200, result)
return
}
func Revoke(c *gin.Context) {
fmt.Println("revoke callback")
data := map[string]interface{}{}
result := map[string]interface{}{
"code": 200,
"msg": "成功",
"data": data,
}
param, err := checkParam(c)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = err.Error()
c.JSON(200, result)
return
}
revokeData := zking.RevokeCallbackData{}
err = json.Unmarshal([]byte(param.Data), &revokeData)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = "data异常"
c.JSON(200, result)
return
}
global.DB.Model(&model.Revoke{}).Where("policy_no", revokeData.PolicyNo).UpdateColumns(model.Revoke{Status: revokeData.Status, Msg: revokeData.Msg})
fmt.Println("revokeData", revokeData)
callbackParam := suixing.RevokeCallbackParam{}
callbackParam.AppID = global.Config.Suixing.AppID
callbackParam.RequestID = utils.GenerateNo("requestId")
callbackParam.Version = "1.0.0"
callbackParam.Timestamp = strconv.Itoa(int(time.Now().Unix()))
callbackData := suixing.RevokeCallbackData{
PolicyNo: revokeData.PolicyNo,
Msg: revokeData.Msg,
Status: revokeData.Status,
}
callbackParam.Data = callbackData
callbackRet, err := suixing.SuixingApi.RevokeCallback(callbackParam)
fmt.Println(callbackRet)
c.JSON(200, result)
return
}
func Claim(c *gin.Context) {
fmt.Println("claim callback")
data := map[string]interface{}{}
result := map[string]interface{}{
"code": 200,
"msg": "成功",
"data": data,
}
param, err := checkParam(c)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = err.Error()
c.JSON(200, result)
return
}
fmt.Println(param)
claimData := zking.ClaimCallbackData{}
err = json.Unmarshal([]byte(param.Data), &claimData)
if err != nil {
fmt.Println(err.Error())
result["code"] = 201
result["msg"] = "data异常"
c.JSON(200, result)
return
}
global.DB.Model(&model.Claim{}).Where("policy_no", claimData.PolicyNo).UpdateColumns(model.Claim{Status: claimData.Status, Msg: claimData.Msg})
fmt.Println("claimData", claimData)
callbackParam := suixing.ClaimCallbackParam{}
callbackParam.AppID = global.Config.Suixing.AppID
callbackParam.RequestID = utils.GenerateNo("requestId")
callbackParam.Version = "1.0.0"
callbackParam.Timestamp = strconv.Itoa(int(time.Now().Unix()))
callbackData := suixing.ClaimCallbackData{
PolicyNo: claimData.PolicyNo,
Msg: claimData.Msg,
Status: claimData.Status,
}
callbackParam.Data = callbackData
callbackRet, err := suixing.SuixingApi.ClaimCallback(callbackParam)
fmt.Println(callbackRet)
c.JSON(200, result)
return
}