Exemplo n.º 1
0
        public ActionResult TryChargeAgain(InvoiceVM model)
        {
            if ((!ModelState.IsValid) && (CastleClub.BusinessLogic.Data.GlobalParameters.TryChargeAgain))
            {
                return(Json(new { Result = false }));
            }

            bool resp = PaymentsManager.ProcessInvoice(model.Id);

            InvoiceDT     invoice     = PaymentsManager.GetInvoice(model.Id);
            TransactionDT transaction = invoice.Transactions.OrderByDescending(x => x.SubmitDate).FirstOrDefault();

            /* InvoiceDT invoice = null;
             * TransactionDT transaction = null;
             * resp = false;
             * return Json(new { Result = false, Info = new { Authorize = "", SubmitDate = DateTime.Now.ToString(), Type = "", Message ="hola", Status = "FAIL", Amount = "222", Parent = string.Empty } }); */
            if (resp)
            {
                return(Json(new { Result = true, Info = new { Authorize = transaction.AuthorizeTransactionId.ToString(), SubmitDate = transaction.SubmitDate.ToString(), Type = transaction.Type.ToString(), Message = transaction.Message, Status = transaction.Status.ToString(), Amount = invoice.Amount.ToString(), Parent = string.Empty } }));
            }
            else
            {
                if (transaction != null)
                {
                    return(Json(new { Result = false, Info = new { Authorize = transaction.AuthorizeTransactionId.ToString(), SubmitDate = transaction.SubmitDate.ToString(), Type = transaction.Type.ToString(), Message = transaction.Message, Status = transaction.Status.ToString(), Amount = invoice.Amount.ToString(), Parent = string.Empty } }));
                }
                else
                {
                    return(Json(new { Result = false }));
                }
            }
        }
Exemplo n.º 2
0
        public static bool CreateCustomerProfileTransactionAuthCapture(SiteDT site, InvoiceDT invoice, CustomerDT customer, CreditCardDT creditCard, out long transactionId, out string message, out string code)
        {
            MerchantAuthenticationType merchantAT = new MerchantAuthenticationType();

            merchantAT.name           = site.AuthorizeLoginId;
            merchantAT.transactionKey = site.AuthorizeTransactionKey;

            ProfileTransactionType      profileTT   = new ProfileTransactionType();
            ProfileTransAuthCaptureType profileTACT = new ProfileTransAuthCaptureType();

            OrderExType orderET = new OrderExType();

            orderET.invoiceNumber = invoice.Id.ToString();

            profileTACT.amount                   = invoice.Amount;
            profileTACT.customerProfileId        = customer.AuthorizeProfileId;
            profileTACT.customerPaymentProfileId = creditCard.AuthorizePaymentProfileId;
            profileTACT.order = orderET;

            profileTT.Item = profileTACT;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServiceSoapClient client = new ServiceSoapClient();
            CreateCustomerProfileTransactionResponseType resp = client.CreateCustomerProfileTransaction(merchantAT, profileTT, String.Empty);

            code = string.Empty;

            if (resp.directResponse != null)
            {
                PaymentGatewayResponse paymentGatewayResponse = new PaymentGatewayResponse(resp.directResponse);
                transactionId = paymentGatewayResponse.TransactionId;
                message       = paymentGatewayResponse.ResponseReasonText;

                code = resp.messages != null && resp.messages.Count() > 0 ? resp.messages.Select(x => x.code + " - " + x.text).Aggregate((a, b) => a + "; " + b) : string.Empty;
            }
            else
            {
                transactionId = 0;
                if (resp.messages.Length > 0)
                {
                    message = resp.messages[0].text;
                }
                else
                {
                    message = "";
                }
            }

            return(resp.resultCode == MessageTypeEnum.Ok);
        }
Exemplo n.º 3
0
        public InvoiceDT GetDT()
        {
            InvoiceDT res = new InvoiceDT();

            res.Id           = Id;
            res.CustomerId   = CustomerId;
            res.Amount       = Amount;
            res.Status       = Status;
            res.CreatedAt    = CreatedAt;
            res.BilledDate   = BilledDate.HasValue ? BilledDate.Value : DateTime.MinValue;
            res.RefundedDate = RefundedDate.HasValue ? RefundedDate.Value : DateTime.MinValue;
            res.RefundReason = RefundReason;
            res.Credit       = Status == InvoiceStatus.BILLED || Status == InvoiceStatus.REFUNDED || Status == InvoiceStatus.REFUNDEDFAIL ? Amount : 0;
            res.Debit        = Status == InvoiceStatus.REFUNDED ? Amount : 0;
            res.Balance      = res.Credit - res.Debit;
            res.Transactions = Transactions.ToList().Select(t => t.GetDT()).ToList();
            return(res);
        }
Exemplo n.º 4
0
        public static bool CreateCustomerProfileTransactionRefund(SiteDT site, InvoiceDT invoice, TransactionDT transaction, CustomerDT customer, CreditCardDT creditCard, out long transactionId, out string message)
        {
            //This datetime is when system change.
            int deployYear  = CastleClub.BusinessLogic.Data.GlobalParameters.DeployYear;
            int deployMonth = CastleClub.BusinessLogic.Data.GlobalParameters.DeployMonth;
            int deployDay   = CastleClub.BusinessLogic.Data.GlobalParameters.DeployDay;

            if (invoice.BilledDate >= new DateTime(deployYear, deployMonth, deployDay))
            {
                MerchantAuthenticationType merchantAT = new MerchantAuthenticationType();
                merchantAT.name           = site.AuthorizeLoginId;
                merchantAT.transactionKey = site.AuthorizeTransactionKey;

                ProfileTransactionType profileTT  = new ProfileTransactionType();
                ProfileTransRefundType profileTRT = new ProfileTransRefundType();

                OrderExType orderET = new OrderExType();
                orderET.invoiceNumber = invoice.Id.ToString();

                profileTRT.amount                            = invoice.Amount;
                profileTRT.transId                           = transaction.AuthorizeTransactionId.ToString();
                profileTRT.customerProfileId                 = customer.AuthorizeProfileId;
                profileTRT.customerProfileIdSpecified        = true;
                profileTRT.customerPaymentProfileId          = creditCard.AuthorizePaymentProfileId;
                profileTRT.customerPaymentProfileIdSpecified = true;
                profileTRT.order = orderET;


                profileTT.Item = profileTRT;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                ServiceSoapClient client = new ServiceSoapClient();
                CreateCustomerProfileTransactionResponseType resp = client.CreateCustomerProfileTransaction(merchantAT, profileTT, String.Empty);

                if (resp.directResponse != null)
                {
                    PaymentGatewayResponse paymentGatewayResponse = new PaymentGatewayResponse(resp.directResponse);
                    transactionId = paymentGatewayResponse.TransactionId;
                    message       = paymentGatewayResponse.ResponseReasonText;
                }
                else
                {
                    transactionId = 0;
                    if (resp.messages.Length > 0)
                    {
                        message = resp.messages[0].text;
                    }
                    else
                    {
                        message = "";
                    }
                }

                return(resp.resultCode == MessageTypeEnum.Ok);
            }
            else
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                using (ServiceSoapClient client = new ServiceSoapClient())
                {
                    MerchantAuthenticationType merchant = new MerchantAuthenticationType()
                    {
                        name           = site.AuthorizeLoginId,
                        transactionKey = site.AuthorizeTransactionKey
                    };

                    ProfileTransactionType profileTT  = new ProfileTransactionType();
                    ProfileTransRefundType profileTRT = new ProfileTransRefundType();

                    profileTRT.amount  = invoice.Amount;
                    profileTRT.transId = transaction.AuthorizeTransactionId.ToString();
                    profileTRT.creditCardNumberMasked = "XXXX" + creditCard.LastFourDigit;

                    profileTT.Item = profileTRT;

                    var resp = client.CreateCustomerProfileTransaction(merchant, profileTT, string.Empty);
                    if (resp.resultCode == MessageTypeEnum.Ok)
                    {
                        PaymentGatewayResponse paymentGatewayResponse = new PaymentGatewayResponse(resp.directResponse);
                        transactionId = paymentGatewayResponse.TransactionId;
                        message       = paymentGatewayResponse.ResponseReasonText;
                    }
                    else
                    {
                        transactionId = 0;
                        message       = string.Empty;
                        message       = resp.messages != null && resp.messages.Count() > 0 ? resp.messages.Select(a => a.text).Aggregate((a, b) => a + " - " + b) : string.Empty;
                    }

                    return(resp.resultCode == MessageTypeEnum.Ok);
                }
            }
        }
Exemplo n.º 5
0
        public static void ProcessInvoices()
        {
            using (CastleClubEntities entities = new CastleClubEntities())
            {
                if (CastleClub.BusinessLogic.Data.GlobalParameters.Procces)
                {
                    int            maxFailCount = CastleClub.BusinessLogic.Data.GlobalParameters.FailCount;
                    int            yearMin      = CastleClub.BusinessLogic.Data.GlobalParameters.YearMin;
                    DateTime       min          = new DateTime(yearMin, 1, 1);
                    List <Invoice> list         = entities.Invoices.Where(i => (i.Customer.CancelledDate == null) && (i.CreatedAt >= min) && (!i.FailCount.HasValue || i.FailCount.Value < maxFailCount) && (i.StatusId == InvoiceStatus.NEW.ToString() || i.StatusId == InvoiceStatus.BILLEDFAIL.ToString())).ToList();
                    foreach (Invoice invoice in list)
                    {
                        try
                        {
                            SiteDT       siteDT       = invoice.Customer.Site.GetDT();
                            InvoiceDT    invoiceDT    = invoice.GetDT();
                            CustomerDT   customerDT   = invoice.Customer.GetDT(false);
                            CreditCardDT creditCardDT = invoice.Customer.CreditCards.FirstOrDefault().GetDT();

                            Transaction transaction = new Transaction();
                            transaction.InvoiceId              = invoiceDT.Id;
                            transaction.CreditCardId           = creditCardDT.Id;
                            transaction.AuthorizeTransactionId = 0;
                            transaction.TypeId     = TransactionType.SALE.ToString();
                            transaction.StatusId   = TransactionStatus.FAILED.ToString();
                            transaction.SubmitDate = DateTime.Now;

                            entities.Transactions.Add(transaction);
                            entities.SaveChanges();

                            long   transactionId = 0;
                            string message       = "";
                            string code          = string.Empty;

                            bool succesfull = CIM.CreateCustomerProfileTransactionAuthCapture(siteDT, invoiceDT, customerDT, creditCardDT, out transactionId, out message, out code);

                            if (succesfull)
                            {
                                invoice.Status     = InvoiceStatus.BILLED;
                                invoice.BilledDate = DateTime.Now;

                                transaction.Status = TransactionStatus.SUCCESFULL;
                                transaction.AuthorizeTransactionId = transactionId;

                                invoice.Customer.Referrer.BilledTotal++;
                                invoice.Customer.Referrer.RevenueAmount += invoice.Amount;

                                entities.SaveChanges();
                            }
                            else
                            {
                                invoice.FailCount   = invoice.FailCount.HasValue ? invoice.FailCount.Value + 1 : 1;
                                transaction.Message = message + code;
                                invoice.Status      = InvoiceStatus.BILLEDFAIL;
                                entities.SaveChanges();

                                if (invoice.FailCount >= maxFailCount && entities.NotificationProcess.Any())
                                {
                                    //To do some.
                                    //Send email for notification.

                                    string smtpAddress = CastleClub.BusinessLogic.Data.GlobalParameters.Smtp;
                                    int    portNumber  = 587;
                                    bool   enableSSL   = true;

                                    string emailFrom = CastleClub.BusinessLogic.Data.GlobalParameters.EmailAccount;
                                    string password  = CastleClub.BusinessLogic.Data.GlobalParameters.EmailPassword;
                                    string subject   = "Error an ocurred with invoice: " + invoiceDT.Id;
                                    string body      = "Error ocurred at " + maxFailCount + " time with invoice: " + invoiceDT.Id + ", customerId: " + invoice.CustomerId + ", and with credit card: " + invoice.Customer.CreditCards.FirstOrDefault().Id;

                                    using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
                                    {
                                        mail.From = new MailAddress(emailFrom);
                                        foreach (var emailTo in entities.NotificationProcess)
                                        {
                                            mail.To.Add(emailTo.To);
                                        }
                                        mail.Subject    = subject;
                                        mail.Body       = body;
                                        mail.IsBodyHtml = false;

                                        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                                        {
                                            smtp.Credentials = new NetworkCredential(emailFrom, password);
                                            smtp.EnableSsl   = enableSSL;
                                            smtp.Send(mail);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Utils.EventViewer.Writte("CastleClubAdmin", "PaymentTask - Process", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
                        }
                    }
                }
            }
        }