//------------------------------------------------------------------------------------------- public IGatewayResponse Bill(WeavverEntityContainer data, Sales_Orders order, Logistics_Addresses primaryAddress, Logistics_Addresses billingAddress) { string memo = "WEB PURCHASE"; // Add the credit to the ledger. Accounting_LedgerItems item = new Accounting_LedgerItems(); item.Id = Guid.NewGuid(); item.OrganizationId = OrganizationId; if (order.Orderee.HasValue) item.AccountId = order.Orderee.Value; else item.AccountId = order.Id; item.LedgerType = LedgerType.Receivable.ToString(); item.TransactionId = order.Id; item.PostAt = DateTime.UtcNow; item.Code = CodeType.Payment.ToString(); item.Memo = "Payment from Card " + Number.Substring(Number.Length - 4); item.Amount = Math.Abs(order.Total.Value); // order.BillingContactEmail // Submit to Authorize.Net var request = new AuthorizationRequest(Number, ExpirationMonth.ToString("D2") + ExpirationYear.ToString("D2"), order.Total.Value, memo, true); request.AddCustomer("", order.PrimaryContactNameFirst, order.PrimaryContactNameLast, primaryAddress.Line1, primaryAddress.State, primaryAddress.ZipCode); request.AddMerchantValue("OrderId", order.Id.ToString()); request.AddMerchantValue("CreatedBy", order.CreatedBy.ToString()); request.AddMerchantValue("LedgerItemId", item.Id.ToString()); request.AddShipping("", order.BillingContactNameFirst, order.BillingContactNameLast, billingAddress.Line1, billingAddress.State, billingAddress.ZipCode); var gate = new Gateway(ConfigurationManager.AppSettings["authorize.net_loginid"], ConfigurationManager.AppSettings["authorize.net_transactionkey"], (ConfigurationManager.AppSettings["authorize.net_testmode"] == "true")); var response = gate.Send(request, memo); item.ExternalId = response.TransactionID; if (!response.Approved) { //item.Voided = true; //item.VoidedBy = Guid.Empty; item.Memo += "\r\nPayment failed: Code " + response.ResponseCode + ", " + response.Message; } data.Accounting_LedgerItems.Add(item); return response; }
public ActionResult Authorize() { HttpContext ctx = System.Web.HttpContext.Current; Customer customer = new Customer(); Settings settings = ViewBag.settings; // Retrieve Customer from Sessions/Cookie customer.GetFromStorage(ctx); if (!customer.Cart.Validate()) { return RedirectToAction("Index", "Cart"); } if (customer.Cart.GetPaymentID() > 0) { UDF.ExpireCart(ctx, customer.ID); return RedirectToAction("Index", "Cart"); } customer.BindAddresses(); decimal amount = customer.Cart.getTotal(); string cardnum = Request.Form["cardnumber"]; string month = Request.Form["expiremonth"]; string year = Request.Form["expireyear"]; string cvv = Request.Form["cvv"]; string first = Request.Form["first"]; string last = Request.Form["last"]; //step 1 - create the request IGatewayRequest request = new AuthorizationRequest(cardnum, month + year, amount, "Transaction"); //These are optional calls to the API request.AddCardCode(cvv); //Customer info - this is used for Fraud Detection request.AddCustomer(customer.ID.ToString(), first, last, customer.Cart.Billing.street1 + ((customer.Cart.Billing.street2 != "") ? " " + customer.Cart.Billing.street2 : ""), customer.Cart.Billing.State1.abbr, customer.Cart.Billing.postal_code); //order number //request.AddInvoice("invoiceNumber"); //Custom values that will be returned with the response //request.AddMerchantValue("merchantValue", "value"); //Shipping Address request.AddShipping(customer.ID.ToString(), customer.Cart.Shipping.first, customer.Cart.Shipping.last, customer.Cart.Shipping.street1 + ((customer.Cart.Shipping.street2 != "") ? " " + customer.Cart.Shipping.street2 : ""), customer.Cart.Shipping.State1.abbr, customer.Cart.Shipping.postal_code); //step 2 - create the gateway, sending in your credentials and setting the Mode to Test (boolean flag) //which is true by default //this login and key are the shared dev account - you should get your own if you //want to do more testing bool testmode = false; if (settings.Get("AuthorizeNetTestMode").Trim() == "true") { testmode = true; } Gateway gate = new Gateway(settings.Get("AuthorizeNetLoginKey"), settings.Get("AuthorizeNetTransactionKey"), testmode); customer.Cart.SetStatus((int)OrderStatuses.PaymentPending); //step 3 - make some money IGatewayResponse response = gate.Send(request); if (response.Approved) { customer.Cart.AddPayment("credit card", response.AuthorizationCode, "Complete"); customer.Cart.SetStatus((int)OrderStatuses.PaymentComplete); customer.Cart.SendConfirmation(ctx); customer.Cart.SendInternalOrderEmail(ctx); int cartid = customer.Cart.ID; Cart new_cart = new Cart().Save(); new_cart.UpdateCart(ctx, customer.ID); DateTime cookexp = Request.Cookies["hdcart"].Expires; HttpCookie cook = new HttpCookie("hdcart", new_cart.ID.ToString()); cook.Expires = cookexp; Response.Cookies.Add(cook); customer.Cart = new_cart; customer.Cart.BindAddresses(); return RedirectToAction("Complete", new { id = cartid }); } else { customer.Cart.SetStatus((int)OrderStatuses.PaymentDeclined); return RedirectToAction("Index", new { message = response.Message }); } }
public ActionResult Authorize(int id = 0) { Customer c = new Customer { ID = id }; c.Get(); Cart currentCart = c.Carts.Where(x => x.payment_id == 0).First<Cart>(); Settings settings = ViewBag.settings; decimal amount = currentCart.getTotal(); string cardnum = Request.Form["cardnumber"]; string month = Request.Form["expiremonth"]; string year = Request.Form["expireyear"]; string cvv = Request.Form["cvv"]; string first = Request.Form["first"]; string last = Request.Form["last"]; //step 1 - create the request IGatewayRequest request = new AuthorizationRequest(cardnum, month + year, amount, "Transaction"); //These are optional calls to the API request.AddCardCode(cvv); //Customer info - this is used for Fraud Detection request.AddCustomer(c.ID.ToString(), first, last, currentCart.Billing.street1 + ((currentCart.Billing.street2 != "") ? " " + currentCart.Billing.street2 : ""), currentCart.Billing.State1.abbr, currentCart.Billing.postal_code); //order number //request.AddInvoice("invoiceNumber"); //Custom values that will be returned with the response //request.AddMerchantValue("merchantValue", "value"); //Shipping Address request.AddShipping(c.ID.ToString(), currentCart.Shipping.first, currentCart.Shipping.last, currentCart.Shipping.street1 + ((currentCart.Shipping.street2 != "") ? " " + currentCart.Shipping.street2 : ""), currentCart.Shipping.State1.abbr, currentCart.Shipping.postal_code); //step 2 - create the gateway, sending in your credentials and setting the Mode to Test (boolean flag) //which is true by default //this login and key are the shared dev account - you should get your own if you //want to do more testing bool testmode = false; if (settings.Get("AuthorizeNetTestMode").Trim() == "true") { testmode = true; } Gateway gate = new Gateway(settings.Get("AuthorizeNetLoginKey"), settings.Get("AuthorizeNetTransactionKey"), testmode); //step 3 - make some money IGatewayResponse response = gate.Send(request); if (response.Approved) { currentCart.AddPayment("credit card", response.AuthorizationCode,"Complete"); currentCart.SendConfirmation(); int cartid = currentCart.ID; return RedirectToAction("Step6", new { id = cartid }); } else { TempData["message"] = response.Message; return RedirectToAction("Step5", new { id = c.ID }); } }