All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
398 lines
8.8 KiB
Go
398 lines
8.8 KiB
Go
package service_kyc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/internal/kyc"
|
|
"nixcn-cms/service/shared"
|
|
"nixcn-cms/tracer"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// cnrid: {"legal_name":"", "resident_id":""}
|
|
// passport: {"id": ""}
|
|
type KycSessionData struct {
|
|
Type string `json:"type" validate:"required"` // cnrid | passport
|
|
Identity string `json:"identity" validate:"required"` // base64 json
|
|
UserId string `json:"user_id" swaggerignore:"true"`
|
|
}
|
|
|
|
type KycSessionPayload struct {
|
|
Context context.Context
|
|
Data *KycSessionData
|
|
}
|
|
|
|
type KycSessionResponse struct {
|
|
Status string `json:"status" validate:"required"` // 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) {
|
|
ctx, span := tracer.StartSpan(
|
|
payload.Context,
|
|
"service_kyc",
|
|
"session",
|
|
)
|
|
defer span.End()
|
|
|
|
ctx = exception.ContextWithService(ctx, exception.ServiceKycSession)
|
|
|
|
var err error
|
|
|
|
decodedIdentityByte, err := base64.StdEncoding.DecodeString(payload.Data.Identity)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorBase64DecodeFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
switch payload.Data.Type {
|
|
case "cnrid":
|
|
var info kyc.CNRidInfo
|
|
if err := json.Unmarshal(decodedIdentityByte, &info); err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorJsonDecodeFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycInfo := kyc.CNRidInfo{
|
|
LegalName: info.LegalName,
|
|
ResidentId: info.ResidentId,
|
|
}
|
|
|
|
aliCloudAuth, err := kyc.CNRidMD5AliEnc(&kycInfo)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInternal),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycResult, err := kyc.AliId2MetaVerify(aliCloudAuth)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInternal),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if *kycResult != "1" {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeSpecific),
|
|
exception.WithOriginal(exception.KycSessionFailed),
|
|
exception.WithError(errors.New("KYC verification failed")),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
userId, err := uuid.Parse(payload.Data.UserId)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
encodedKycInfo, err := kyc.EncodeAES(kycInfo)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorAesEncodeFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycIdOrig, err := data.NewKyc(
|
|
data.WithKycType("cnrid"),
|
|
data.WithKycUserId(userId),
|
|
data.WithKycInfo(*encodedKycInfo),
|
|
).Create(ctx)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorDatabase),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycId := kycIdOrig.String()
|
|
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusSuccess),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonSuccess),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exc,
|
|
},
|
|
Data: &KycSessionResponse{
|
|
Status: "success",
|
|
KycId: &kycId,
|
|
RedirectUri: nil,
|
|
},
|
|
}
|
|
|
|
case "passport":
|
|
var info kyc.PassportInfo
|
|
if err := json.Unmarshal(decodedIdentityByte, &info); err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorJsonDecodeFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
sessionResponse, err := kyc.CreateSession(ctx)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInternal),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
userId, err := uuid.Parse(payload.Data.UserId)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycIdOrig, err := data.NewKyc(
|
|
data.WithKycType("passport"),
|
|
data.WithKycUserId(userId),
|
|
).Create(ctx)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorDatabase),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if err := kyc.SavePassportSession(ctx, kycIdOrig, kyc.PassportSessionBinding{
|
|
SessionId: sessionResponse.ID,
|
|
PassportId: info.ID,
|
|
}); err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInternal),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
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()
|
|
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusSuccess),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonSuccess),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exc,
|
|
},
|
|
Data: &KycSessionResponse{
|
|
Status: "processing",
|
|
KycId: &kycId,
|
|
RedirectUri: &redirectUri,
|
|
},
|
|
}
|
|
|
|
default:
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &KycSessionResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|