protected V2.TranProfile GetCustomerProfileFromDoc(ICCPayment pDoc)
        {
            V2.TranProfile ret = null;
            if (pDoc.PMInstanceID > 0)
            {
                var query = new PXSelectJoin <CustomerPaymentMethod,
                                              InnerJoin <CustomerPaymentMethodDetail, On <CustomerPaymentMethodDetail.pMInstanceID, Equal <CustomerPaymentMethod.pMInstanceID> >,
                                                         InnerJoin <PaymentMethodDetail, On <PaymentMethodDetail.detailID, Equal <CustomerPaymentMethodDetail.detailID> > > >,
                                              Where <CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> >,
                                                     And <PaymentMethodDetail.isCCProcessingID, Equal <True> > > >(Base);
                PXResultset <CustomerPaymentMethod> result = query.Select(pDoc.PMInstanceID);

                foreach (PXResult <CustomerPaymentMethod, CustomerPaymentMethodDetail> item in result)
                {
                    CustomerPaymentMethod       cpm    = (CustomerPaymentMethod)item;
                    CustomerPaymentMethodDetail cpmDet = (CustomerPaymentMethodDetail)item;
                    ret = new V2.TranProfile()
                    {
                        CustomerProfileId = cpm.CustomerCCPID, PaymentProfileId = cpmDet.Value
                    };
                    break;
                }
            }
            return(ret);
        }
        protected int?GetInstanceId(V2.TranProfile input)
        {
            int?    instanceID = PaymentTranExtConstants.NewPaymentProfile;
            PXCache cpmCache   = Base.Caches[typeof(CustomerPaymentMethod)];

            cpmCache.ClearQueryCacheObsolete();
            PXSelectBase <CustomerPaymentMethod> query = new PXSelectReadonly2 <CustomerPaymentMethod,
                                                                                InnerJoin <CustomerPaymentMethodDetail, On <CustomerPaymentMethod.pMInstanceID, Equal <CustomerPaymentMethodDetail.pMInstanceID> >,
                                                                                           InnerJoin <PaymentMethodDetail, On <CustomerPaymentMethodDetail.detailID, Equal <PaymentMethodDetail.detailID>,
                                                                                                                               And <CustomerPaymentMethodDetail.paymentMethodID, Equal <PaymentMethodDetail.paymentMethodID> > > > >,
                                                                                Where <CustomerPaymentMethod.cCProcessingCenterID, Equal <Required <CustomerPaymentMethod.cCProcessingCenterID> >,
                                                                                       And <CustomerPaymentMethod.customerCCPID, Equal <Required <CustomerPaymentMethod.customerCCPID> >,
                                                                                            And <PaymentMethodDetail.isCCProcessingID, Equal <True>, And <PaymentMethodDetail.useFor, Equal <PaymentMethodDetailUsage.useForARCards> > > > > >(Base);

            PXResultset <CustomerPaymentMethod> queryResult = query.Select(SelectedProcessingCenter, input.CustomerProfileId);

            foreach (PXResult <CustomerPaymentMethod, CustomerPaymentMethodDetail> item in queryResult)
            {
                CustomerPaymentMethodDetail checkCpmd = (CustomerPaymentMethodDetail)item;

                if (checkCpmd.Value == input.PaymentProfileId)
                {
                    instanceID = checkCpmd.PMInstanceID;
                    break;
                }
            }
            CustomerPaymentMethod cpm = NewCpm.Select();

            if (cpm != null && cpm.PMInstanceID != null && cpm.PMInstanceID >= 0)
            {
                instanceID = cpm.PMInstanceID.Value;
            }
            return(instanceID);
        }
        protected virtual int?GetOrCreatePaymentProfilrByTran(V2.TransactionData tranData, ICCPayment pDoc)
        {
            int?instanceID;

            try
            {
                V2.TranProfile profile = null;
                if (tranData.CustomerId != null && tranData.PaymentId != null)
                {
                    profile = new V2.TranProfile()
                    {
                        CustomerProfileId = tranData.CustomerId, PaymentProfileId = tranData.PaymentId
                    };
                    ICCPaymentProfile cpm = null;
                    try
                    {
                        cpm = PrepeareCpmRecord();
                    }
                    catch (Exception ex)
                    {
                        var v = ex;
                    }
                    cpm.CustomerCCPID = tranData.CustomerId;
                }

                ExternalTransactionState state = GetActiveTransactionState();
                if (profile == null && state.IsPreAuthorized)
                {
                    profile = GetCustomerProfileFromDoc(pDoc);
                }

                if (profile == null)
                {
                    profile = GetOrCreateCustomerProfileByTranId(tranData.TranID);
                }

                instanceID = GetInstanceId(profile);

                using (PXTransactionScope tran = new PXTransactionScope())
                {
                    if (instanceID == PaymentTranExtConstants.NewPaymentProfile)
                    {
                        instanceID = CreatePaymentProfile(profile);
                    }
                    CreateCustomerProcessingCenterRecord(profile);
                    tran.Complete();
                }
            }
            finally
            {
                NewCpm.Cache.Clear();
                NewCpmd.Cache.Clear();
            }
            pDoc.PMInstanceID = instanceID;
            return(instanceID);
        }
        protected void CreateCustomerProcessingCenterRecord(V2.TranProfile input)
        {
            PXCache customerProcessingCenterCache = Base.Caches[typeof(CustomerProcessingCenterID)];

            customerProcessingCenterCache.ClearQueryCacheObsolete();
            PXSelectBase <CustomerProcessingCenterID> checkRecordExist = new PXSelectReadonly <CustomerProcessingCenterID,
                                                                                               Where <CustomerProcessingCenterID.cCProcessingCenterID, Equal <Required <CustomerProcessingCenterID.cCProcessingCenterID> >,
                                                                                                      And <CustomerProcessingCenterID.bAccountID, Equal <Required <CustomerProcessingCenterID.bAccountID> >,
                                                                                                           And <CustomerProcessingCenterID.customerCCPID, Equal <Required <CustomerProcessingCenterID.customerCCPID> > > > > >(Base);

            CustomerProcessingCenterID cProcessingCenter = checkRecordExist.SelectSingle(SelectedProcessingCenter, SelectedBAccount, input.CustomerProfileId);

            if (cProcessingCenter == null)
            {
                cProcessingCenter                      = customerProcessingCenterCache.CreateInstance() as CustomerProcessingCenterID;
                cProcessingCenter.BAccountID           = SelectedBAccount;
                cProcessingCenter.CCProcessingCenterID = SelectedProcessingCenter;
                cProcessingCenter.CustomerCCPID        = input.CustomerProfileId;
                customerProcessingCenterCache.Insert(cProcessingCenter);
                customerProcessingCenterCache.Persist(PXDBOperation.Insert);
            }
        }
        protected int?CreatePaymentProfile(V2.TranProfile input)
        {
            var cpmSelect  = NewCpm;
            var cpmdSelect = NewCpmd;
            var pmdSelect  = PaymentMethodDet;

            foreach (PaymentMethodDetail item in pmdSelect.Select())
            {
                CustomerPaymentMethodDetail cpmd = new CustomerPaymentMethodDetail();
                cpmd.PaymentMethodID = SelectedPaymentMethod;
                cpmd.DetailID        = item.DetailID;
                if (item.IsCCProcessingID == true)
                {
                    cpmd.Value = input.PaymentProfileId;
                }
                cpmdSelect.Insert(cpmd);
            }

            CCCustomerInformationManagerGraph infoManagerGraph = PXGraph.CreateInstance <CCCustomerInformationManagerGraph>();
            GenericCCPaymentProfileAdapter <CustomerPaymentMethod> cpmAdapter = new GenericCCPaymentProfileAdapter <CustomerPaymentMethod>(cpmSelect);
            var cpmdAdapter = new GenericCCPaymentProfileDetailAdapter <CustomerPaymentMethodDetail, PaymentMethodDetail>(cpmdSelect, pmdSelect);

            infoManagerGraph.GetPaymentProfile(Base, cpmAdapter, cpmdAdapter);
            var   cardIdent         = pmdSelect.Select().RowCast <PaymentMethodDetail>().Where(i => i.IsIdentifier == true).First();
            var   detailWithCardNum = cpmdSelect.Select().RowCast <CustomerPaymentMethodDetail>().Where(i => i.DetailID == cardIdent.DetailID).First();
            Match match             = new Regex("[\\d]+").Match(detailWithCardNum.Value);

            CustomerPaymentMethod cpm = cpmSelect.Select();

            if (match.Success)
            {
                cpm.Descr = SelectedPaymentMethod + ":" + maskedCardTmpl + match.Value;
            }

            cpmSelect.Cache.Persist(PXDBOperation.Insert);
            cpmdSelect.Cache.Persist(PXDBOperation.Insert);
            return(cpm.PMInstanceID);
        }
        protected V2.TranProfile GetOrCreateCustomerProfileByTranId(string tranId)
        {
            PXSelectBase <CustomerPaymentMethod> query = new PXSelectReadonly <CustomerPaymentMethod,
                                                                               Where <CustomerPaymentMethod.bAccountID, Equal <Required <CustomerPaymentMethod.bAccountID> >,
                                                                                      And <CustomerPaymentMethod.cCProcessingCenterID, Equal <Required <CustomerPaymentMethod.cCProcessingCenterID> > > >,
                                                                               OrderBy <Desc <CustomerPaymentMethod.createdDateTime> > >(Base);

            IEnumerable <CustomerPaymentMethod> cpmRes = query.Select(SelectedBAccount, SelectedProcessingCenter).RowCast <CustomerPaymentMethod>();
            CustomerPaymentMethod searchCpm            = cpmRes.FirstOrDefault();
            ICCPaymentProfile     cpm = PrepeareCpmRecord();

            if (searchCpm != null)
            {
                cpm.CustomerCCPID = searchCpm.CustomerCCPID;
            }

            CCCustomerInformationManagerGraph infoManagerGraph = PXGraph.CreateInstance <CCCustomerInformationManagerGraph>();
            GenericCCPaymentProfileAdapter <CustomerPaymentMethod> cpmAdapter =
                new GenericCCPaymentProfileAdapter <CustomerPaymentMethod>(NewCpm);

            V2.TranProfile ret = infoManagerGraph.GetOrCreatePaymentProfileByTran(Base, cpmAdapter, tranId);
            cpm.CustomerCCPID = ret.CustomerProfileId;
            return(ret);
        }
 public V2.TranProfile GetOrCreatePaymentProfileFromTransaction(string transactionId, V2.CreateTranPaymentProfileParams cParams)
 {
     V2.ICCProfileCreator processor = GetProcessor <V2.ICCProfileCreator>();
     V2.TranProfile       result    = V2PluginErrorHandler.ExecuteAndHandleError(() => processor.GetOrCreatePaymentProfileFromTransaction(transactionId, cParams));
     return(result);
 }