Ejemplo n.º 1
0
        //Save Transaction without saving payment details
        public async Task <dynamic> SaveCustTransaction(CustomerTransactionModel model)
        {
            Response response = new Response();
            // if paymentmethod is empty then first add payment method also check customer token as well.
            //if customer token available then search on cstmr token otherwise on id.
            //if paymentmethodid = "" then add and save card details
            // paymentmethodid from model
            string        custIntlId    = "";
            string        invoiceIntlId = "";
            string        orderIntlId   = "";
            SecurityToken token         = await GetSecurityToken();

            var client = new PaymentGateway.IeBizServiceClient();

            if (!string.IsNullOrEmpty(model.customer?.CustomerId))
            {
                try
                {
                    model.customer = await client.GetCustomerAsync(token, model.customer.CustomerId, "");

                    //getCustomer( by id customer?.CustomerId
                    //assigne to custIntlId = customerInternalID
                    custIntlId = model.customer.CustomerInternalId;
                }
                catch (Exception ex)
                {
                    if (ex.Message.ToString() == "Not Found")
                    {
                        custIntlId = "";
                    }
                }
            }
            if (string.IsNullOrEmpty(custIntlId.ToString()) && model.customer != null)
            {
                //Add customer
                response.CustomerResponse = await new CustomerManager().AddNewCustomer(model.customer, token);
                //get customertoken
                model.customer.CustomerToken = await client.GetCustomerTokenAsync(token, model.customer.CustomerId, model.customer.CustomerInternalId);

                custIntlId = response.CustomerResponse.CustomerInternalId;
            }
            if (string.IsNullOrEmpty(model.paymentMethod) && model.paymentMethodProfile != null)
            {
                //Add Payment method (use custIntlId
                //if secondrySort == 0 then call SetAsDefault
                // values must come from controller.
                response.PaymentMethodProfileResponse = await new PaymentManager().AddNewPaymentDetails(model.customer.CustomerInternalId, model.paymentMethodProfile);
                model.paymentMethod = response.PaymentMethodProfileResponse;
                // this logic also implemented in controller.
                if (model.paymentMethodProfile.SecondarySort == "0")
                {
                    PaymentProfile obj = new PaymentProfile();
                    obj.customerToken = model.customer.CustomerToken;
                    obj.paymentMethod = response.PaymentMethodProfileResponse;
                    obj.securityToken = token;
                    var status = await new PaymentManager().SetDefaultPaymentMethodProfile(obj);
                }
            }
            if (!string.IsNullOrEmpty(model.customerTransaction?.Details?.Invoice) && model.isOrder == false)
            {
                try
                {
                    model.invoice = await client.GetInvoiceAsync(token, model.customer.CustomerId, "", model.customerTransaction.Details.Invoice, "");

                    invoiceIntlId = model.invoice.InvoiceInternalId;
                }
                catch (Exception ex)
                {
                    if (ex.Message.ToString() == "Not Found")
                    {
                        invoiceIntlId = "";
                    }
                }
            }
            if (string.IsNullOrEmpty(invoiceIntlId.ToString()) && model.invoice != null && model.isOrder == false)
            {
                //Add new Invoice
                response.Invoice = await new InvoiceManager().AddInvoices(model.invoice, token);
                invoiceIntlId    = response.Invoice.InvoiceInternalId;
            }
            if (!string.IsNullOrEmpty(model.customerTransaction?.Details?.OrderID) && model.isOrder == true)
            {
                try
                {
                    model.salesOrder = await client.GetSalesOrderAsync(token, model.customer.CustomerId, model.customer.CustomerInternalId, model.customerTransaction.Details.OrderID, "");

                    orderIntlId = model.salesOrder.SalesOrderInternalId;
                }
                catch (Exception ex)
                {
                    if (ex.Message.ToString() == "Not Found")
                    {
                        orderIntlId = "";
                    }
                }
            }
            if (string.IsNullOrEmpty(orderIntlId.ToString()) && model.salesOrder != null && model.isOrder == true)
            {
                //Add new Invoice
                response.SalesOrder = await new OrderManager().AddSalesOrder(token, model.salesOrder);
                orderIntlId         = response.SalesOrder.SalesOrderInternalId;
            }

            //Call Run Customer transaction
            ///
            //end
            var result = await client.runCustomerTransactionAsync(token, model.customer.CustomerToken, model.paymentMethod, model.customerTransaction);

            if (model.isOrder == true)
            {
                if (!string.IsNullOrEmpty(model.customerTransaction.Details.OrderID))
                {
                    ApplicationTransactionRequest obj = new ApplicationTransactionRequest();
                    {
                        obj.CustomerInternalId = custIntlId;
                        obj.LinkedToInternalId = orderIntlId;                               //sales order internal id or invoice internal id
                        obj.LinkedToTypeId     = model.customerTransaction.Details.OrderID; //sales order number or invoice number
                        obj.TransactionId      = result.RefNum;
                        obj.TransactionDate    = DateTime.Now.ToString();
                        obj.TransactionTypeId  = "AuthOnly";
                        obj.SoftwareId         = ".NetApi";
                    }
                    response.AppTransResponse = await client.AddApplicationTransactionAsync(token, obj);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(model.customerTransaction.Details.Invoice))
                {
                    ApplicationTransactionRequest obj = new ApplicationTransactionRequest();
                    {
                        obj.CustomerInternalId = custIntlId;
                        obj.LinkedToInternalId = invoiceIntlId;                             //sales order internal id or invoice internal id
                        obj.LinkedToTypeId     = model.customerTransaction.Details.Invoice; //sales order number or invoice number
                        obj.TransactionId      = result.RefNum;
                        obj.TransactionDate    = DateTime.Now.ToString();
                        obj.TransactionTypeId  = "AuthOnly";
                        obj.SoftwareId         = ".NetApi";
                    }
                    response.AppTransResponse = await client.AddApplicationTransactionAsync(token, obj);
                }
            }


            if (result.ResultCode == "A")
            {
                response.TransResponse = result;
            }
            else
            {
                response.TransResponse = result;
            }
            return(response);
        }