Files
cms-server/service/service_kyc/session_kyc.go
Asai Neko 8f1d5280f7
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Fix kyc info and data and api handler
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-02-01 13:58:12 +08:00

401 lines
9.3 KiB
Go

package service_kyc
import (
"context"
"encoding/base64"
"encoding/json"
"net/url"
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/internal/kyc"
"nixcn-cms/service/shared"
"github.com/google/uuid"
)
// cnrid: {"legal_name":"", "resident_id":""}
// passport: {"id": ""}
type KycSessionData struct {
Type string `json:"type"` // cnrid | passport
Identity string `json:"identity"` // base64 json
UserId string `json:"user_id" swaggerignore:"true"`
}
type KycSessionPayload struct {
Context context.Context
Data *KycSessionData
}
type KycSessionResponse struct {
Status string `json:"status"` // success | processing
KycId *string `json:"kyc_id"`
RedirectUri *string `json:"redirect_uri"`
}
type KycSessionResult struct {
Common shared.CommonResult
Data *KycSessionResponse
}
func (self *KycServiceImpl) SessionKyc(payload *KycSessionPayload) (result *KycSessionResult) {
var err error
decodedIdentityByte, err := base64.StdEncoding.DecodeString(payload.Data.Identity)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorBase64DecodeFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
switch payload.Data.Type {
case "cnrid":
var info kyc.CNRidInfo
if err := json.Unmarshal(decodedIdentityByte, &info); err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorJsonDecodeFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
kycInfo := kyc.CNRidInfo{
LegalName: info.LegalName,
ResidentId: info.ResidentId,
}
aliCloudAuth, err := kyc.CNRidMD5AliEnc(&kycInfo)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInternal).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
kycResult, err := kyc.AliId2MetaVerify(aliCloudAuth)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInternal).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
if *kycResult != "1" {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeSpecific).
SetOriginal(exception.KycSessionFailed).
SetError(nil).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
userId, err := uuid.Parse(payload.Data.UserId)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
encodedKycInfo, err := kyc.EncodeAES(kycInfo)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeSpecific).
SetOriginal(exception.KycSessionFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
kycIdOrig, err := new(data.Kyc).
SetType("cnrid").
SetUserId(userId).
SetKycInfo(*encodedKycInfo).
Create(payload.Context)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
kycId := kycIdOrig.String()
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: exception,
},
Data: &KycSessionResponse{
Status: "success",
KycId: &kycId,
RedirectUri: nil,
},
}
case "passport":
var info kyc.PassportInfo
if err := json.Unmarshal(decodedIdentityByte, &info); err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorJsonDecodeFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
self.PassportId = info.ID
sessionResponse, err := kyc.CreateSession(payload.Context)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInternal).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
self.PassportReaderSessionId = sessionResponse.ID
userId, err := uuid.Parse(payload.Data.UserId)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
kycIdOrig, err := new(data.Kyc).
SetType("passport").
SetUserId(userId).
Create(payload.Context)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
kycId := kycIdOrig.String()
kycBaseURL := "https://passportreader.app/open"
kycUrl, _ := url.Parse(kycBaseURL)
kycQuery := kycUrl.Query()
kycQuery.Set("token", sessionResponse.Token)
kycUrl.RawQuery = kycQuery.Encode()
redirectUri := kycUrl.String()
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: exception,
},
Data: &KycSessionResponse{
Status: "processing",
KycId: &kycId,
RedirectUri: &redirectUri,
},
}
default:
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceKyc).
SetEndpoint(exception.EndpointKycServiceSession).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(err).
Throw(payload.Context)
result = &KycSessionResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
return
}