001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.util.BaseGraphQl;
021import com.echothree.model.control.payment.server.control.PaymentProcessorControl;
022import com.echothree.model.control.payment.server.control.PaymentProcessorTransactionControl;
023import com.echothree.model.control.user.server.control.UserControl;
024import com.echothree.model.data.payment.server.entity.PaymentProcessor;
025import com.echothree.model.data.payment.server.entity.PaymentProcessorDetail;
026import com.echothree.util.server.persistence.Session;
027import graphql.annotations.annotationTypes.GraphQLDescription;
028import graphql.annotations.annotationTypes.GraphQLField;
029import graphql.annotations.annotationTypes.GraphQLName;
030import graphql.annotations.annotationTypes.GraphQLNonNull;
031import graphql.schema.DataFetchingEnvironment;
032import java.util.ArrayList;
033import java.util.List;
034
035@GraphQLDescription("payment processor object")
036@GraphQLName("PaymentProcessor")
037public class PaymentProcessorObject
038        extends BaseEntityInstanceObject {
039    
040    private final PaymentProcessor paymentProcessor; // Always Present
041    
042    public PaymentProcessorObject(PaymentProcessor paymentProcessor) {
043        super(paymentProcessor.getPrimaryKey());
044        
045        this.paymentProcessor = paymentProcessor;
046    }
047
048    private PaymentProcessorDetail paymentProcessorDetail; // Optional, use getPaymentProcessorDetail()
049    
050    private PaymentProcessorDetail getPaymentProcessorDetail() {
051        if(paymentProcessorDetail == null) {
052            paymentProcessorDetail = paymentProcessor.getLastDetail();
053        }
054        
055        return paymentProcessorDetail;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("payment processor name")
060    @GraphQLNonNull
061    public String getPaymentProcessorName() {
062        return getPaymentProcessorDetail().getPaymentProcessorName();
063    }
064
065    @GraphQLField
066    @GraphQLDescription("payment processor type")
067    public PaymentProcessorTypeObject getPaymentProcessorType(final DataFetchingEnvironment env) {
068        return PaymentSecurityUtils.getHasPaymentProcessorTypeAccess(env) ? new PaymentProcessorTypeObject(getPaymentProcessorDetail().getPaymentProcessorType()) : null;
069    }
070    
071    @GraphQLField
072    @GraphQLDescription("is default")
073    @GraphQLNonNull
074    public boolean getIsDefault() {
075        return getPaymentProcessorDetail().getIsDefault();
076    }
077    
078    @GraphQLField
079    @GraphQLDescription("sort order")
080    @GraphQLNonNull
081    public int getSortOrder() {
082        return getPaymentProcessorDetail().getSortOrder();
083    }
084    
085    @GraphQLField
086    @GraphQLDescription("description")
087    @GraphQLNonNull
088    public String getDescription(final DataFetchingEnvironment env) {
089        var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class);
090        var userControl = Session.getModelController(UserControl.class);
091
092        return paymentProcessorControl.getBestPaymentProcessorDescription(paymentProcessor, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
093    }
094
095    @GraphQLField
096    @GraphQLDescription("payment processor transactions")
097    @GraphQLNonNull
098    public List<PaymentProcessorTransactionObject> getPaymentProcessorTransactions(final DataFetchingEnvironment env) {
099        List<PaymentProcessorTransactionObject> paymentProcessorTransactions = null;
100
101        if(PaymentSecurityUtils.getHasPaymentProcessorTransactionsAccess(env)) {
102            var paymentProcessorTransactionControl = Session.getModelController(PaymentProcessorTransactionControl.class);
103            var entities = paymentProcessorTransactionControl.getPaymentProcessorTransactionsByPaymentProcessor(paymentProcessor);
104            var objects = new ArrayList<PaymentProcessorTransactionObject>(entities.size());
105
106            entities.forEach((entity) -> objects.add(new PaymentProcessorTransactionObject(entity)));
107
108            paymentProcessorTransactions = objects;
109        }
110
111        return paymentProcessorTransactions;
112    }
113
114}