Пример #1
0
        public void RunPaymentQueue()
        {
            int resellerId = SecurityContext.User.UserId;
            // 1. load unpaid invoices
            List <Invoice> invoices = InvoiceController.GetUnpaidInvoices(resellerId);

            // TRACE
            TaskManager.Write("Running payment queue");
            TaskManager.WriteParameter("Items found", invoices.Count);
            // 2. load payment profile for each customer
            foreach (Invoice invoice in invoices)
            {
                try
                {
                    // load payment profile
                    CheckoutDetails details = StorehouseController.GetPaymentProfileInternally(invoice.ContractId);
                    //
                    if (details != null)
                    {
                        // TRACE
                        TaskManager.Write("Trying to submit payment");
                        TaskManager.WriteParameter("InvoiceID", invoice.InvoiceId);
                        // 3. submit payment for each invoice if profile exists
                        CheckoutResult result = PaymentGatewayController.CheckOut(invoice.ContractId,
                                                                                  invoice.InvoiceId, PaymentMethod.CREDIT_CARD, details);
                        // ERROR
                        if (!result.Succeed)
                        {
                            TaskManager.WriteError("Payment failed");
                            TaskManager.WriteParameter("Result code", result.StatusCode);
                            continue;
                        }
                        // OK
                        TaskManager.Write("Payment OK");
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Payment failed");
                }
            }
        }
 public static CheckoutResult CompleteCheckout(string contractId, int invoiceId,
                                               string methodName, CheckoutDetails details)
 {
     //
     return(PaymentGatewayController.CheckOut(contractId, invoiceId, methodName, details));
 }
        public void PickupReceivedTransactions()
        {
            TaskManager.Write("Start looking for transactions submitted");
            //
            List <HandlerResponse> transactions = ServiceHandlerController.GetServiceHandlersResponsesByReseller(SecurityContext.User.UserId);

            //
            if (transactions.Count > 0)
            {
                XmlDocument xmldoc      = new XmlDocument();
                XmlElement  root        = xmldoc.CreateElement("Result");
                XmlElement  succeedNode = xmldoc.CreateElement("Succeed");
                XmlElement  failedNode  = xmldoc.CreateElement("Failed");
                root.AppendChild(succeedNode);
                root.AppendChild(failedNode);
                //
                List <HandlerResponse> succeedItems = new List <HandlerResponse>();
                List <HandlerResponse> failedItems  = new List <HandlerResponse>();
                //
                TaskManager.Write("Found {0} transactions pending", transactions.Count.ToString());
                foreach (HandlerResponse transaction in transactions)
                {
                    XmlElement responseNode = xmldoc.CreateElement("Response");
                    responseNode.SetAttribute("ID", Convert.ToString(transaction.ResponseId));
                    //
                    try
                    {
                        CheckoutDetails details = new CheckoutDetails();
                        //
                        string[] dataPairs = transaction.TextResponse.Split('&');
                        foreach (string dataPair in dataPairs)
                        {
                            string[] data = dataPair.Split('=');
                            if (data.Length >= 2)
                            {
                                details[data[0]] = data[1];
                            }
                        }
                        //
                        CheckoutResult result = PaymentGatewayController.CheckOut(transaction.ContractId, transaction.InvoiceId,
                                                                                  transaction.MethodName, details);
                        //
                        if (result.Succeed)
                        {
                            succeedNode.AppendChild(responseNode);
                            succeedItems.Add(transaction);
                        }
                        else
                        {
                            responseNode.SetAttribute("Error", result.StatusCode);
                            failedNode.AppendChild(responseNode);
                            //
                            transaction.ErrorMessage = result.StatusCode;
                            failedItems.Add(transaction);
                        }
                    }
                    catch (Exception ex)
                    {
                        //
                        if (!failedItems.Contains(transaction))
                        {
                            responseNode.SetAttribute("Error", ex.StackTrace);
                            failedNode.AppendChild(responseNode);
                            //
                            transaction.ErrorMessage = ex.StackTrace;
                            failedItems.Add(transaction);
                        }
                        //
                        TaskManager.WriteError(ex);
                    }
                }
                // peform transactions update
                ServiceHandlerController.UpdateServiceHandlersResponses(SecurityContext.User.UserId, root.InnerXml);
            }
            else
            {
                TaskManager.Write("No transactions found");
            }
            TaskManager.Write("End looking for transactions submitted");
        }