|
|
|
|
@@ -3,6 +3,7 @@ package paymethodsimp
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
@@ -16,13 +17,21 @@ import (
|
|
|
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
|
|
|
"github.com/tech/sendico/pkg/model"
|
|
|
|
|
"github.com/tech/sendico/pkg/mservice"
|
|
|
|
|
archivablev1 "github.com/tech/sendico/pkg/proto/common/archivable/v1"
|
|
|
|
|
describablev1 "github.com/tech/sendico/pkg/proto/common/describable/v1"
|
|
|
|
|
oboundv1 "github.com/tech/sendico/pkg/proto/common/organization_bound/v1"
|
|
|
|
|
paginationv2 "github.com/tech/sendico/pkg/proto/common/pagination/v2"
|
|
|
|
|
pboundv1 "github.com/tech/sendico/pkg/proto/common/permission_bound/v1"
|
|
|
|
|
storablev1 "github.com/tech/sendico/pkg/proto/common/storable/v1"
|
|
|
|
|
endpointv1 "github.com/tech/sendico/pkg/proto/payments/endpoint/v1"
|
|
|
|
|
methodsv1 "github.com/tech/sendico/pkg/proto/payments/methods/v1"
|
|
|
|
|
eapi "github.com/tech/sendico/server/interface/api"
|
|
|
|
|
"github.com/tech/sendico/server/interface/api/sresponse"
|
|
|
|
|
mutil "github.com/tech/sendico/server/internal/mutil/param"
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -98,17 +107,25 @@ func (a *PaymentMethodsAPI) create(r *http.Request, account *model.Account, toke
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.BadPayload(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
pm, err := decodePaymentMethodJSON(payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.BadPayload(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
method, err := encodePaymentMethodProto(pm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := a.client.CreatePaymentMethod(r.Context(), &methodsv1.CreatePaymentMethodRequest{
|
|
|
|
|
AccountRef: account.ID.Hex(),
|
|
|
|
|
OrganizationRef: orgRef.Hex(),
|
|
|
|
|
PaymentMethodJson: payload,
|
|
|
|
|
AccountRef: account.ID.Hex(),
|
|
|
|
|
OrganizationRef: orgRef.Hex(),
|
|
|
|
|
PaymentMethod: method,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return grpcErrorResponse(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pm, err := decodePaymentMethod(resp.GetPaymentMethodJson())
|
|
|
|
|
pm, err = decodePaymentMethodRecord(resp.GetPaymentMethodRecord())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
@@ -140,7 +157,7 @@ func (a *PaymentMethodsAPI) list(r *http.Request, account *model.Account, token
|
|
|
|
|
return grpcErrorResponse(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items, err := decodePaymentMethods(resp.GetPaymentMethodsJson())
|
|
|
|
|
items, err := decodePaymentMethods(resp.GetPaymentMethods())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
@@ -161,7 +178,7 @@ func (a *PaymentMethodsAPI) get(r *http.Request, account *model.Account, token *
|
|
|
|
|
return grpcErrorResponse(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pm, err := decodePaymentMethod(resp.GetPaymentMethodJson())
|
|
|
|
|
pm, err := decodePaymentMethodRecord(resp.GetPaymentMethodRecord())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
@@ -173,16 +190,24 @@ func (a *PaymentMethodsAPI) update(r *http.Request, account *model.Account, toke
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.BadPayload(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
pm, err := decodePaymentMethodJSON(payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.BadPayload(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
record, err := encodePaymentMethodRecord(pm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := a.client.UpdatePaymentMethod(r.Context(), &methodsv1.UpdatePaymentMethodRequest{
|
|
|
|
|
AccountRef: account.ID.Hex(),
|
|
|
|
|
PaymentMethodJson: payload,
|
|
|
|
|
AccountRef: account.ID.Hex(),
|
|
|
|
|
PaymentMethodRecord: record,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return grpcErrorResponse(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pm, err := decodePaymentMethod(resp.GetPaymentMethodJson())
|
|
|
|
|
pm, err = decodePaymentMethodRecord(resp.GetPaymentMethodRecord())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.Internal(a.logger, a.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
@@ -300,7 +325,7 @@ func toProtoCursor(cursor *model.ViewCursor) *paginationv2.ViewCursor {
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decodePaymentMethod(payload []byte) (*model.PaymentMethod, error) {
|
|
|
|
|
func decodePaymentMethodJSON(payload []byte) (*model.PaymentMethod, error) {
|
|
|
|
|
var pm model.PaymentMethod
|
|
|
|
|
if err := json.Unmarshal(payload, &pm); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
@@ -308,13 +333,78 @@ func decodePaymentMethod(payload []byte) (*model.PaymentMethod, error) {
|
|
|
|
|
return &pm, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decodePaymentMethods(items [][]byte) ([]model.PaymentMethod, error) {
|
|
|
|
|
func decodePaymentMethodRecord(record *endpointv1.PaymentMethodRecord) (*model.PaymentMethod, error) {
|
|
|
|
|
if record == nil {
|
|
|
|
|
return nil, merrors.InvalidArgument("payment_method_record is required")
|
|
|
|
|
}
|
|
|
|
|
pm, err := decodePaymentMethodProto(record.GetPaymentMethod())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := applyPermissionBound(pm, record.GetPermissionBound()); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return pm, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decodePaymentMethodProto(method *endpointv1.PaymentMethod) (*model.PaymentMethod, error) {
|
|
|
|
|
if method == nil {
|
|
|
|
|
return nil, merrors.InvalidArgument("payment_method is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recipientRef, err := parseRequiredObjectID(method.GetRecipientRef(), "payment_method.recipient_ref")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
pt, err := paymentTypeFromProto(method.GetType(), "payment_method.type")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &model.PaymentMethod{
|
|
|
|
|
Describable: describableFromProto(method.GetDescribable()),
|
|
|
|
|
RecipientRef: recipientRef,
|
|
|
|
|
Type: pt,
|
|
|
|
|
Data: cloneBytes(method.GetData()),
|
|
|
|
|
IsMain: method.GetIsMain(),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encodePaymentMethodProto(pm *model.PaymentMethod) (*endpointv1.PaymentMethod, error) {
|
|
|
|
|
if pm == nil {
|
|
|
|
|
return nil, merrors.InvalidArgument("payment method is required")
|
|
|
|
|
}
|
|
|
|
|
pt, err := paymentTypeToProto(pm.Type)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &endpointv1.PaymentMethod{
|
|
|
|
|
Describable: describableToProto(pm.Describable),
|
|
|
|
|
RecipientRef: toObjectHex(pm.RecipientRef),
|
|
|
|
|
Type: pt,
|
|
|
|
|
Data: cloneBytes(pm.Data),
|
|
|
|
|
IsMain: pm.IsMain,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encodePaymentMethodRecord(pm *model.PaymentMethod) (*endpointv1.PaymentMethodRecord, error) {
|
|
|
|
|
method, err := encodePaymentMethodProto(pm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &endpointv1.PaymentMethodRecord{
|
|
|
|
|
PermissionBound: permissionBoundFromModel(pm),
|
|
|
|
|
PaymentMethod: method,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decodePaymentMethods(items []*endpointv1.PaymentMethodRecord) ([]model.PaymentMethod, error) {
|
|
|
|
|
if len(items) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
res := make([]model.PaymentMethod, 0, len(items))
|
|
|
|
|
for i := range items {
|
|
|
|
|
pm, err := decodePaymentMethod(items[i])
|
|
|
|
|
pm, err := decodePaymentMethodRecord(items[i])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
@@ -323,6 +413,183 @@ func decodePaymentMethods(items [][]byte) ([]model.PaymentMethod, error) {
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func paymentTypeFromProto(value endpointv1.PaymentMethodType, field string) (model.PaymentType, error) {
|
|
|
|
|
switch value {
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_IBAN:
|
|
|
|
|
return model.PaymentTypeIban, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CARD:
|
|
|
|
|
return model.PaymentTypeCard, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CARD_TOKEN:
|
|
|
|
|
return model.PaymentTypeCardToken, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_BANK_ACCOUNT:
|
|
|
|
|
return model.PaymentTypeBankAccount, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_WALLET:
|
|
|
|
|
return model.PaymentTypeWallet, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CRYPTO_ADDRESS:
|
|
|
|
|
return model.PaymentTypeCryptoAddress, nil
|
|
|
|
|
case endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_LEDGER:
|
|
|
|
|
return model.PaymentTypeLedger, nil
|
|
|
|
|
default:
|
|
|
|
|
return model.PaymentTypeIban, merrors.InvalidArgument(fmt.Sprintf("%s has unsupported value: %s", field, value.String()), field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func paymentTypeToProto(value model.PaymentType) (endpointv1.PaymentMethodType, error) {
|
|
|
|
|
switch value {
|
|
|
|
|
case model.PaymentTypeIban:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_IBAN, nil
|
|
|
|
|
case model.PaymentTypeCard:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CARD, nil
|
|
|
|
|
case model.PaymentTypeCardToken:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CARD_TOKEN, nil
|
|
|
|
|
case model.PaymentTypeBankAccount:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_BANK_ACCOUNT, nil
|
|
|
|
|
case model.PaymentTypeWallet:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_WALLET, nil
|
|
|
|
|
case model.PaymentTypeCryptoAddress:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_CRYPTO_ADDRESS, nil
|
|
|
|
|
case model.PaymentTypeLedger:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_LEDGER, nil
|
|
|
|
|
default:
|
|
|
|
|
return endpointv1.PaymentMethodType_PAYMENT_METHOD_TYPE_UNSPECIFIED, merrors.InvalidArgument(fmt.Sprintf("unsupported payment method type: %s", value.String()), "type")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func describableFromProto(src *describablev1.Describable) model.Describable {
|
|
|
|
|
if src == nil {
|
|
|
|
|
return model.Describable{}
|
|
|
|
|
}
|
|
|
|
|
res := model.Describable{Name: src.GetName()}
|
|
|
|
|
if src.Description != nil {
|
|
|
|
|
v := src.GetDescription()
|
|
|
|
|
res.Description = &v
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func describableToProto(src model.Describable) *describablev1.Describable {
|
|
|
|
|
if strings.TrimSpace(src.Name) == "" && src.Description == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
res := &describablev1.Describable{
|
|
|
|
|
Name: src.Name,
|
|
|
|
|
}
|
|
|
|
|
if src.Description != nil {
|
|
|
|
|
v := *src.Description
|
|
|
|
|
res.Description = &v
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cloneBytes(src []byte) []byte {
|
|
|
|
|
if len(src) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
dst := make([]byte, len(src))
|
|
|
|
|
copy(dst, src)
|
|
|
|
|
return dst
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func permissionBoundFromModel(pm *model.PaymentMethod) *pboundv1.PermissionBound {
|
|
|
|
|
if pm == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &pboundv1.PermissionBound{
|
|
|
|
|
Storable: &storablev1.Storable{
|
|
|
|
|
Id: toObjectHex(pm.ID),
|
|
|
|
|
CreatedAt: toProtoTime(pm.CreatedAt),
|
|
|
|
|
UpdatedAt: toProtoTime(pm.UpdatedAt),
|
|
|
|
|
},
|
|
|
|
|
Archivable: &archivablev1.Archivable{
|
|
|
|
|
IsArchived: pm.Archived,
|
|
|
|
|
},
|
|
|
|
|
OrganizationBound: &oboundv1.OrganizationBound{
|
|
|
|
|
OrganizationRef: toObjectHex(pm.GetOrganizationRef()),
|
|
|
|
|
},
|
|
|
|
|
PermissionRef: toObjectHex(pm.GetPermissionRef()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applyPermissionBound(pm *model.PaymentMethod, src *pboundv1.PermissionBound) error {
|
|
|
|
|
if pm == nil || src == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if storable := src.GetStorable(); storable != nil {
|
|
|
|
|
if methodRef, err := parseOptionalObjectID(storable.GetId(), "payment_method_record.permission_bound.storable.id"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if methodRef != bson.NilObjectID {
|
|
|
|
|
pm.ID = methodRef
|
|
|
|
|
}
|
|
|
|
|
pm.CreatedAt = fromProtoTime(storable.GetCreatedAt())
|
|
|
|
|
pm.UpdatedAt = fromProtoTime(storable.GetUpdatedAt())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if archivable := src.GetArchivable(); archivable != nil {
|
|
|
|
|
pm.Archived = archivable.GetIsArchived()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if orgBound := src.GetOrganizationBound(); orgBound != nil {
|
|
|
|
|
if orgRef, err := parseOptionalObjectID(orgBound.GetOrganizationRef(), "payment_method_record.permission_bound.organization_bound.organization_ref"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if orgRef != bson.NilObjectID {
|
|
|
|
|
pm.SetOrganizationRef(orgRef)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if permissionRef, err := parseOptionalObjectID(src.GetPermissionRef(), "payment_method_record.permission_bound.permission_ref"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if permissionRef != bson.NilObjectID {
|
|
|
|
|
pm.SetPermissionRef(permissionRef)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseOptionalObjectID(value, field string) (bson.ObjectID, error) {
|
|
|
|
|
trimmed := strings.TrimSpace(value)
|
|
|
|
|
if trimmed == "" {
|
|
|
|
|
return bson.NilObjectID, nil
|
|
|
|
|
}
|
|
|
|
|
ref, err := bson.ObjectIDFromHex(trimmed)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return bson.NilObjectID, merrors.InvalidArgument(fmt.Sprintf("%s must be a valid object id", field), field)
|
|
|
|
|
}
|
|
|
|
|
return ref, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseRequiredObjectID(value, field string) (bson.ObjectID, error) {
|
|
|
|
|
ref, err := parseOptionalObjectID(value, field)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return bson.NilObjectID, err
|
|
|
|
|
}
|
|
|
|
|
if ref == bson.NilObjectID {
|
|
|
|
|
return bson.NilObjectID, merrors.InvalidArgument(field+" is required", field)
|
|
|
|
|
}
|
|
|
|
|
return ref, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toObjectHex(value bson.ObjectID) string {
|
|
|
|
|
if value == bson.NilObjectID {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return value.Hex()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toProtoTime(value time.Time) *timestamppb.Timestamp {
|
|
|
|
|
if value.IsZero() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return timestamppb.New(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fromProtoTime(value *timestamppb.Timestamp) time.Time {
|
|
|
|
|
if value == nil {
|
|
|
|
|
return time.Time{}
|
|
|
|
|
}
|
|
|
|
|
return value.AsTime()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func grpcErrorResponse(logger mlogger.Logger, source mservice.Type, err error) http.HandlerFunc {
|
|
|
|
|
statusErr, ok := status.FromError(err)
|
|
|
|
|
if !ok {
|
|
|
|
|
|