001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.payment.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.graphql.count.Connections;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.payment.server.control.PaymentMethodControl;
027import com.echothree.model.control.payment.server.control.PaymentProcessorControl;
028import com.echothree.model.control.payment.server.control.PaymentProcessorTransactionControl;
029import com.echothree.model.control.user.server.control.UserControl;
030import com.echothree.model.data.payment.common.PaymentMethodConstants;
031import com.echothree.model.data.payment.common.PaymentProcessorTransactionConstants;
032import com.echothree.model.data.payment.server.entity.PaymentProcessor;
033import com.echothree.model.data.payment.server.entity.PaymentProcessorDetail;
034import com.echothree.util.server.persistence.Session;
035import graphql.annotations.annotationTypes.GraphQLDescription;
036import graphql.annotations.annotationTypes.GraphQLField;
037import graphql.annotations.annotationTypes.GraphQLName;
038import graphql.annotations.annotationTypes.GraphQLNonNull;
039import graphql.annotations.connection.GraphQLConnection;
040import graphql.schema.DataFetchingEnvironment;
041import java.util.ArrayList;
042import java.util.stream.Collectors;
043
044@GraphQLDescription("payment processor object")
045@GraphQLName("PaymentProcessor")
046public class PaymentProcessorObject
047        extends BaseEntityInstanceObject {
048    
049    private final PaymentProcessor paymentProcessor; // Always Present
050    
051    public PaymentProcessorObject(PaymentProcessor paymentProcessor) {
052        super(paymentProcessor.getPrimaryKey());
053        
054        this.paymentProcessor = paymentProcessor;
055    }
056
057    private PaymentProcessorDetail paymentProcessorDetail; // Optional, use getPaymentProcessorDetail()
058    
059    private PaymentProcessorDetail getPaymentProcessorDetail() {
060        if(paymentProcessorDetail == null) {
061            paymentProcessorDetail = paymentProcessor.getLastDetail();
062        }
063        
064        return paymentProcessorDetail;
065    }
066
067    @GraphQLField
068    @GraphQLDescription("payment processor name")
069    @GraphQLNonNull
070    public String getPaymentProcessorName() {
071        return getPaymentProcessorDetail().getPaymentProcessorName();
072    }
073
074    @GraphQLField
075    @GraphQLDescription("payment processor type")
076    public PaymentProcessorTypeObject getPaymentProcessorType(final DataFetchingEnvironment env) {
077        return PaymentSecurityUtils.getHasPaymentProcessorTypeAccess(env) ? new PaymentProcessorTypeObject(getPaymentProcessorDetail().getPaymentProcessorType()) : null;
078    }
079    
080    @GraphQLField
081    @GraphQLDescription("is default")
082    @GraphQLNonNull
083    public boolean getIsDefault() {
084        return getPaymentProcessorDetail().getIsDefault();
085    }
086    
087    @GraphQLField
088    @GraphQLDescription("sort order")
089    @GraphQLNonNull
090    public int getSortOrder() {
091        return getPaymentProcessorDetail().getSortOrder();
092    }
093    
094    @GraphQLField
095    @GraphQLDescription("description")
096    @GraphQLNonNull
097    public String getDescription(final DataFetchingEnvironment env) {
098        var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class);
099        var userControl = Session.getModelController(UserControl.class);
100
101        return paymentProcessorControl.getBestPaymentProcessorDescription(paymentProcessor, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
102    }
103
104    @GraphQLField
105    @GraphQLDescription("payment processor transactions")
106    @GraphQLNonNull
107    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
108    public CountingPaginatedData<PaymentProcessorTransactionObject> getPaymentProcessorTransactions(final DataFetchingEnvironment env) {
109        if(PaymentSecurityUtils.getHasPaymentProcessorTransactionsAccess(env)) {
110            var paymentProcessorTransactionControl = Session.getModelController(PaymentProcessorTransactionControl.class);
111            var totalCount = paymentProcessorTransactionControl.countPaymentProcessorTransactionByPaymentProcessor(paymentProcessor);
112
113            try(var objectLimiter = new ObjectLimiter(env, PaymentProcessorTransactionConstants.COMPONENT_VENDOR_NAME, PaymentProcessorTransactionConstants.ENTITY_TYPE_NAME, totalCount)) {
114                var entities = paymentProcessorTransactionControl.getPaymentProcessorTransactionsByPaymentProcessor(paymentProcessor);
115                var paymentProcessorTransactions = entities.stream().map(PaymentProcessorTransactionObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
116
117                return new CountedObjects<>(objectLimiter, paymentProcessorTransactions);
118            }
119        } else {
120            return Connections.emptyConnection();
121        }
122    }
123
124    @GraphQLField
125    @GraphQLDescription("payment methods")
126    @GraphQLNonNull
127    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
128    public CountingPaginatedData<PaymentMethodObject> getPaymentMethods(final DataFetchingEnvironment env) {
129        if(PaymentSecurityUtils.getHasPaymentMethodsAccess(env)) {
130            var paymentMethodControl = Session.getModelController(PaymentMethodControl.class);
131            var totalCount = paymentMethodControl.countPaymentMethodsByPaymentProcessor(paymentProcessor);
132
133            try(var objectLimiter = new ObjectLimiter(env, PaymentMethodConstants.COMPONENT_VENDOR_NAME, PaymentMethodConstants.ENTITY_TYPE_NAME, totalCount)) {
134                var entities = paymentMethodControl.getPaymentMethodsByPaymentProcessor(paymentProcessor);
135                var paymentMethods = entities.stream().map(PaymentMethodObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
136
137                return new CountedObjects<>(objectLimiter, paymentMethods);
138            }
139        } else {
140            return Connections.emptyConnection();
141        }
142    }
143
144}