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"); } } }
protected override void PostProcessCheckout(CheckoutResult result) { // 2Checkout workaround for Direct Return = Yes Response.Clear(); // write refresh html if (!result.Succeed) { Response.Write( "<html><head><META http-equiv=\"refresh\" content=\"0;" + "URL=" + RedirectUrl + CheckoutBasePage.OrderFailedUri + "\"></head></html>" ); } else { Response.Write( "<html><head><META http-equiv=\"refresh\" content=\"0;" + "URL=" + RedirectUrl + CheckoutBasePage.OrderCompleteUri + "\"></head></html>" ); } }
/// <summary> /// Performs checkout operation /// </summary> /// <param name="spaceId">Space.</param> /// <param name="gatewayId">Gateway.</param> /// <param name="invoiceId">Invoice.</param> /// <param name="details">Array of parameters.</param> /// <returns>Checkout result object.</returns> public static CheckoutResult CheckOut(string contractId, int invoiceId, string methodName, CheckoutDetails details) { CheckoutResult result = new CheckoutResult(); try { Contract contractInfo = ContractSystem.ContractController.GetContract(contractId); // impersonate ContractSystem.ContractController.ImpersonateAsContractReseller(contractInfo); // TRACE ES.TaskManager.StartTask(TASK_SOURCE, CHECKOUT_TASK, methodName); ES.TaskManager.Write("Start accepting payment for invoice"); ES.TaskManager.WriteParameter("ContractID", contractId); ES.TaskManager.WriteParameter("InvoiceID", invoiceId); // get user details ContractAccount account = ContractSystem.ContractController.GetContractAccountSettings(contractId); // try to load plugin type and throw an exception if type not found IPaymentGatewayProvider provider = (IPaymentGatewayProvider)SystemPluginController.GetContractPaymentMethod( contractInfo, methodName); // add invoice details Invoice invoice = InvoiceController.GetCustomerInvoiceInternally(invoiceId); // append information for the provider details[CheckoutKeys.ContractNumber] = contractId; details[CheckoutKeys.Amount] = invoice.Total.ToString("0.00"); details[CheckoutKeys.InvoiceNumber] = invoice.InvoiceNumber; details[CheckoutKeys.Currency] = invoice.Currency; ES.TaskManager.Write("Submitting payment transaction"); // call checkout routine TransactionResult pgResult = provider.SubmitPaymentTransaction(details); // log provider response SystemPluginController.LogContractPayment(contractInfo, methodName, pgResult.RawResponse); // ERROR if (!pgResult.Succeed) { result.Succeed = false; result.StatusCode = pgResult.StatusCode; // ES.TaskManager.WriteError("Transaction failed"); ES.TaskManager.WriteParameter("StatusCode", result.StatusCode); ES.TaskManager.WriteParameter("RawResponse", pgResult.RawResponse); // EXIT return result; } // OK ES.TaskManager.Write("Transaction is OK"); // check whether the transaction already exists CustomerPayment tran = StorehouseController.LookupForTransaction(pgResult.TransactionId); // lookup for pending transaction if (tran == null) { // add payment record result.PaymentId = StorehouseController.AddCustomerPayment(contractId, invoice.InvoiceId, pgResult.TransactionId, invoice.Total, invoice.Currency, methodName, pgResult.TransactionStatus); // ERROR if (result.PaymentId < 1) { result.Succeed = false; result.StatusCode = result.PaymentId.ToString(); // ES.TaskManager.WriteError("Could not add customer payment record to the db"); ES.TaskManager.WriteParameter("ResultCode", result.StatusCode); // EXIT return result; } } // if transaction is already submitted just update it's status if (tran != null) StorehouseController.UpdateTransactionStatus(tran.PaymentId, pgResult.TransactionStatus); // OK result.Succeed = true; // ensure user requests to persist his payment details for credit card if (details.Persistent && methodName == PaymentMethod.CREDIT_CARD) StorehouseController.SetPaymentProfile(contractId, details); } catch (Exception ex) { result.Succeed = false; result.StatusCode = GENERAL_FAILURE; // ES.TaskManager.WriteError(ex); } finally { ES.TaskManager.CompleteTask(); } // EXIT return result; }
/// <summary> /// Performs checkout operation /// </summary> /// <param name="spaceId">Space.</param> /// <param name="gatewayId">Gateway.</param> /// <param name="invoiceId">Invoice.</param> /// <param name="details">Array of parameters.</param> /// <returns>Checkout result object.</returns> public static CheckoutResult CheckOut(string contractId, int invoiceId, string methodName, CheckoutDetails details) { CheckoutResult result = new CheckoutResult(); try { Contract contractInfo = ContractSystem.ContractController.GetContract(contractId); // impersonate ContractSystem.ContractController.ImpersonateAsContractReseller(contractInfo); // TRACE ES.TaskManager.StartTask(TASK_SOURCE, CHECKOUT_TASK, methodName); ES.TaskManager.Write("Start accepting payment for invoice"); ES.TaskManager.WriteParameter("ContractID", contractId); ES.TaskManager.WriteParameter("InvoiceID", invoiceId); // get user details ContractAccount account = ContractSystem.ContractController.GetContractAccountSettings(contractId); // try to load plugin type and throw an exception if type not found IPaymentGatewayProvider provider = (IPaymentGatewayProvider)SystemPluginController.GetContractPaymentMethod( contractInfo, methodName); // add invoice details Invoice invoice = InvoiceController.GetCustomerInvoiceInternally(invoiceId); // append information for the provider details[CheckoutKeys.ContractNumber] = contractId; details[CheckoutKeys.Amount] = invoice.Total.ToString("0.00"); details[CheckoutKeys.InvoiceNumber] = invoice.InvoiceNumber; details[CheckoutKeys.Currency] = invoice.Currency; ES.TaskManager.Write("Submitting payment transaction"); // call checkout routine TransactionResult pgResult = provider.SubmitPaymentTransaction(details); // log provider response SystemPluginController.LogContractPayment(contractInfo, methodName, pgResult.RawResponse); // ERROR if (!pgResult.Succeed) { result.Succeed = false; result.StatusCode = pgResult.StatusCode; // ES.TaskManager.WriteError("Transaction failed"); ES.TaskManager.WriteParameter("StatusCode", result.StatusCode); ES.TaskManager.WriteParameter("RawResponse", pgResult.RawResponse); // EXIT return(result); } // OK ES.TaskManager.Write("Transaction is OK"); // check whether the transaction already exists CustomerPayment tran = StorehouseController.LookupForTransaction(pgResult.TransactionId); // lookup for pending transaction if (tran == null) { // add payment record result.PaymentId = StorehouseController.AddCustomerPayment(contractId, invoice.InvoiceId, pgResult.TransactionId, invoice.Total, invoice.Currency, methodName, pgResult.TransactionStatus); // ERROR if (result.PaymentId < 1) { result.Succeed = false; result.StatusCode = result.PaymentId.ToString(); // ES.TaskManager.WriteError("Could not add customer payment record to the db"); ES.TaskManager.WriteParameter("ResultCode", result.StatusCode); // EXIT return(result); } } // if transaction is already submitted just update it's status if (tran != null) { StorehouseController.UpdateTransactionStatus(tran.PaymentId, pgResult.TransactionStatus); } // OK result.Succeed = true; // ensure user requests to persist his payment details for credit card if (details.Persistent && methodName == PaymentMethod.CREDIT_CARD) { StorehouseController.SetPaymentProfile(contractId, details); } } catch (Exception ex) { result.Succeed = false; result.StatusCode = GENERAL_FAILURE; // ES.TaskManager.WriteError(ex); } finally { ES.TaskManager.CompleteTask(); } // EXIT return(result); }
protected virtual void PostProcessCheckout(CheckoutResult result) { // check order payment result status if (result.Succeed) // go to the order success page Response.Redirect(RedirectUrl + OrderCompleteUri); else // go to order failed page Response.Redirect(RedirectUrl + OrderFailedUri); }
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"); }