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.logic; 018 019import com.echothree.control.user.payment.common.spec.PaymentProcessorUniversalSpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.payment.common.exception.DuplicatePaymentProcessorNameException; 025import com.echothree.model.control.payment.common.exception.UnknownDefaultPaymentProcessorException; 026import com.echothree.model.control.payment.common.exception.UnknownPaymentProcessorNameException; 027import com.echothree.model.control.payment.server.control.PaymentProcessorControl; 028import com.echothree.model.data.core.server.entity.EntityInstance; 029import com.echothree.model.data.party.server.entity.Language; 030import com.echothree.model.data.payment.server.entity.PaymentProcessor; 031import com.echothree.model.data.payment.server.entity.PaymentProcessorType; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.persistence.BasePK; 034import com.echothree.util.server.control.BaseLogic; 035import com.echothree.util.server.message.ExecutionErrorAccumulator; 036import com.echothree.util.server.persistence.EntityPermission; 037import com.echothree.util.server.persistence.Session; 038 039public class PaymentProcessorLogic 040 extends BaseLogic { 041 042 private PaymentProcessorLogic() { 043 super(); 044 } 045 046 private static class PaymentProcessorLogicHolder { 047 static PaymentProcessorLogic instance = new PaymentProcessorLogic(); 048 } 049 050 public static PaymentProcessorLogic getInstance() { 051 return PaymentProcessorLogicHolder.instance; 052 } 053 054 public PaymentProcessor createPaymentProcessor(final ExecutionErrorAccumulator eea, final String paymentProcessorName, 055 PaymentProcessorType paymentProcessorType, final Boolean isDefault, final Integer sortOrder, 056 final Language language, final String description, final BasePK createdBy) { 057 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 058 PaymentProcessor paymentProcessor = paymentProcessorControl.getPaymentProcessorByName(paymentProcessorName); 059 060 if(paymentProcessor == null) { 061 paymentProcessor = paymentProcessorControl.createPaymentProcessor(paymentProcessorName, paymentProcessorType, isDefault, 062 sortOrder, createdBy); 063 064 if(description != null) { 065 paymentProcessorControl.createPaymentProcessorDescription(paymentProcessor, language, description, createdBy); 066 } 067 } else { 068 handleExecutionError(DuplicatePaymentProcessorNameException.class, eea, ExecutionErrors.DuplicatePaymentProcessorName.name(), paymentProcessorName); 069 } 070 071 return paymentProcessor; 072 } 073 074 public PaymentProcessor getPaymentProcessorByName(final ExecutionErrorAccumulator eea, final String paymentProcessorName, 075 final EntityPermission entityPermission) { 076 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 077 PaymentProcessor paymentProcessor = paymentProcessorControl.getPaymentProcessorByName(paymentProcessorName, entityPermission); 078 079 if(paymentProcessor == null) { 080 handleExecutionError(UnknownPaymentProcessorNameException.class, eea, ExecutionErrors.UnknownPaymentProcessorName.name(), paymentProcessorName); 081 } 082 083 return paymentProcessor; 084 } 085 086 public PaymentProcessor getPaymentProcessorByName(final ExecutionErrorAccumulator eea, final String paymentProcessorName) { 087 return getPaymentProcessorByName(eea, paymentProcessorName, EntityPermission.READ_ONLY); 088 } 089 090 public PaymentProcessor getPaymentProcessorByNameForUpdate(final ExecutionErrorAccumulator eea, final String paymentProcessorName) { 091 return getPaymentProcessorByName(eea, paymentProcessorName, EntityPermission.READ_WRITE); 092 } 093 094 public PaymentProcessor getPaymentProcessorByUniversalSpec(final ExecutionErrorAccumulator eea, 095 final PaymentProcessorUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 096 PaymentProcessor paymentProcessor = null; 097 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 098 String paymentProcessorName = universalSpec.getPaymentProcessorName(); 099 var parameterCount = (paymentProcessorName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 100 101 switch(parameterCount) { 102 case 0: 103 if(allowDefault) { 104 paymentProcessor = paymentProcessorControl.getDefaultPaymentProcessor(entityPermission); 105 106 if(paymentProcessor == null) { 107 handleExecutionError(UnknownDefaultPaymentProcessorException.class, eea, ExecutionErrors.UnknownDefaultPaymentProcessor.name()); 108 } 109 } else { 110 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 111 } 112 break; 113 case 1: 114 if(paymentProcessorName == null) { 115 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 116 ComponentVendors.ECHO_THREE.name(), EntityTypes.PaymentProcessor.name()); 117 118 if(!eea.hasExecutionErrors()) { 119 paymentProcessor = paymentProcessorControl.getPaymentProcessorByEntityInstance(entityInstance, entityPermission); 120 } 121 } else { 122 paymentProcessor = getPaymentProcessorByName(eea, paymentProcessorName, entityPermission); 123 } 124 break; 125 default: 126 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 127 break; 128 } 129 130 return paymentProcessor; 131 } 132 133 public PaymentProcessor getPaymentProcessorByUniversalSpec(final ExecutionErrorAccumulator eea, 134 final PaymentProcessorUniversalSpec universalSpec, boolean allowDefault) { 135 return getPaymentProcessorByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 136 } 137 138 public PaymentProcessor getPaymentProcessorByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 139 final PaymentProcessorUniversalSpec universalSpec, boolean allowDefault) { 140 return getPaymentProcessorByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 141 } 142 143 public void deletePaymentProcessor(final ExecutionErrorAccumulator eea, final PaymentProcessor paymentProcessor, 144 final BasePK deletedBy) { 145 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 146 147 paymentProcessorControl.deletePaymentProcessor(paymentProcessor, deletedBy); 148 } 149 150}