private EventureOrder BuildOrder(JObject orderBundle) { //orderAmount: cart.getTotalPrice(), // orderHouseId: cart.houseId, // ownerId: cart.ownerId, // teamId: cart.teamId, // teamMemberId: cart.teamMemberId, // regs: cart.registrations var order = new EventureOrder { DateCreated = DateTime.Now, HouseId = (Int32)orderBundle["orderHouseId"], Amount = (Decimal)orderBundle["orderAmount"], Token = (string)orderBundle["stripeToken"], //is this safe?? OwnerId = (Int32)orderBundle["ownerId"], Status = "Init", Voided = false }; dynamic bundle = orderBundle; if (bundle.charges != null) //if no surcharges skip this { foreach (dynamic surchargeBundle in bundle.charges) { var surcharge = new Surcharge { Amount = surchargeBundle.amount, EventureListId = surchargeBundle.listId, ChargeType = surchargeBundle.chargeType, Description = surchargeBundle.desc, ParticipantId = (Int32)orderBundle["orderHouseId"], //surchargeBundle.partId, //EventureOrderId = order.Id, EventureOrder = order, DateCreated = DateTime.Now, CouponId = surchargeBundle.couponId }; string chargeType = surchargeBundle.chargeType ?? ""; switch (chargeType) { case "coupon": surcharge.SurchargeType = SurchargeType.Coupon; break; case "cartRule": surcharge.SurchargeType = SurchargeType.Discount; break; case "onlineFee": surcharge.SurchargeType = SurchargeType.OnlineFee; break; default: break; } //totalFees = 33; //totalFees + Convert.ToDecimal(surchargeBundle.amount); order.Surcharges.Add(surcharge); } } if (bundle.addons != null) //if no surcharges skip this { foreach (dynamic addonBundle in bundle.addons) { var surcharge = new Surcharge { Amount = addonBundle.amount, EventureListId = addonBundle.listId, ChargeType = addonBundle.chargeType, Description = addonBundle.addonName, ParticipantId = (Int32)orderBundle["orderHouseId"], EventureOrder = order, DateCreated = DateTime.Now, AddonId = addonBundle.addonId, Quantity = addonBundle.quantity, SurchargeType = SurchargeType.Addon }; //totalFees = 33; //totalFees + Convert.ToDecimal(surchargeBundle.amount); order.Surcharges.Add(surcharge); } } //db.Orders.Add(order); foreach (var regBundle in bundle.regs) { Registration registration = new Registration { EventureListId = regBundle.eventureListId, ParticipantId = regBundle.partId, ListAmount = regBundle.fee, Quantity = regBundle.quantity, //EventureOrderId = order.Id, GroupId = regBundle.groupId, EventureOrder = order, DateCreated = DateTime.Now, TotalAmount = Convert.ToDecimal(regBundle.fee), Type = "reg", RegStatus = RegStatus.Completed, Redeemed = true }; // order. var eventureListTypeId = regBundle.eventureListTypeId; order.Registrations.Add(registration); foreach (var answerBundle in regBundle.answers) { var answer = new Answer { AnswerText = answerBundle.answer, QuestionId = answerBundle.questionId, Registration = registration }; //registration.Answers.Add(answer); registration.Answers.Add(answer); } } return(order); }
public HttpResponseMessage Transfer(JObject saveBundle) { try { //int numOfRegs = 0; //decimal totalFees = 0; var transferId = (Int32)saveBundle["transferId"]; var transferNewListName = (string)saveBundle["transferNewListName"]; var participantId = (Int32)saveBundle["partId"]; var log = new EventureLog(); log.Message = "starting transfer: " + transferId; log.Caller = "transfer"; log.Status = "Info"; log.LogDate = System.DateTime.Now.ToLocalTime(); db.EventureLogs.Add(log); db.SaveChanges(); var transfer = db.EventureTransfers.Where(t => t.Id == transferId).Single(); var order = new EventureOrder { DateCreated = DateTime.Now, //HouseId = (Int32)saveBundle["houseId"], Amount = (Decimal)saveBundle["amount"], Token = (string)saveBundle["token"], //is this safe?? OwnerId = (Int32)saveBundle["ownerId"], Status = "init transfer", Voided = false }; db.Orders.Add(order); Owner owner = db.Owners.Where(o => o.Id == order.OwnerId).SingleOrDefault(); if (owner == null) { throw new Exception("Owner Setup is Not Configured Correctly"); } //validate //must have transferId, //i could process without partId just no email //calulate order.CardProcessorFeeInCents = Convert.ToInt32(Math.Round(Convert.ToInt32(order.Amount * 100) * owner.CardProcessorFeePercentPerCharge / 100, 0) + owner.CardProcessorFeeFlatPerChargeInCents); order.LocalFeeInCents = Convert.ToInt32(Math.Round(Convert.ToInt32(order.Amount * 100) * owner.LocalFeePercentOfCharge / 100, 0) + owner.LocalFeeFlatPerPerChargeInCents); order.LocalApplicationFee = order.LocalFeeInCents - order.CardProcessorFeeInCents; string custDesc = string.Empty; string partEmail = string.Empty; var part = db.Participants.Where(p => p.Id == participantId).FirstOrDefault(); if (part != null) { custDesc = "_transfer" + transferId; partEmail = part.Email; } else { //this should never happen throw exception? //NO house Id throw new Exception("There was an issue with submission, Not signed into account."); } // create customer var customerOptions = new StripeCustomerCreateOptions { Email = partEmail, Description = custDesc, TokenId = order.Token, }; var stripeCustomerService = new StripeCustomerService(owner.AccessToken); var customer = stripeCustomerService.Create(customerOptions); var stripeChargeService = new StripeChargeService(owner.AccessToken); //The token returned from the above method var stripeChargeOption = new StripeChargeCreateOptions() { AmountInCents = Convert.ToInt32(order.Amount * 100), Currency = "usd", CustomerId = customer.Id, Description = owner.Name, //this needs to be dynamic ApplicationFeeInCents = order.LocalApplicationFee }; var stripeCharge = stripeChargeService.Create(stripeChargeOption); if (string.IsNullOrEmpty(stripeCharge.FailureCode)) { // update reg var reg = db.Registrations.Where(r => r.Id == transfer.RegistrationId).Single(); reg.EventureListId = transfer.EventureListIdTo; // mjb 060914 reg.Name = transferNewListName; reg.Type = "xferup"; transfer.IsComplete = true; order.AuthorizationCode = stripeCharge.Id; //stripeCharge. order.CardNumber = stripeCharge.StripeCard.Last4; order.CardCvcCheck = stripeCharge.StripeCard.CvcCheck; order.CardExpires = stripeCharge.StripeCard.ExpirationMonth + "/" + stripeCharge.StripeCard.ExpirationYear; order.CardFingerprint = stripeCharge.StripeCard.Fingerprint; //order.CardId = stripeCharge.StripeCard.; order.CardName = stripeCharge.StripeCard.Name; order.CardOrigin = stripeCharge.StripeCard.Country; order.CardType = stripeCharge.StripeCard.Type; order.Voided = false; order.Status = "Complete"; db.SaveChanges(); var resp = Request.CreateResponse(HttpStatusCode.OK); //resp.Content = new StringContent(); resp.Content = new StringContent(transferId.ToString(), Encoding.UTF8, "text/plain"); //send transferId?? just for practice?? return(resp); //var resp = Request.CreateResponse(HttpStatusCode.OK); ////resp.Content = new StringContent(); //resp.Content = new StringContent(order.Id.ToString(), Encoding.UTF8, "text/plain"); //return resp; } else { order.Status = stripeCharge.FailureMessage; db.SaveChanges(); //should i save here? return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, stripeCharge)); } } catch (Exception ex) { //send quick email HttpResponseMessage result = new MailController().SendInfoMessage("*****@*****.**", "Error Handler_Payment_Post: " + ex.Message + "\n\n" + ex.InnerException); //regular log var logE = new EventureLog { Message = "Error Handler: " + ex.Message + "\n\n" + ex.InnerException, Caller = "Payment_Post", Status = "ERROR", LogDate = System.DateTime.Now.ToLocalTime() }; db.EventureLogs.Add(logE); db.SaveChanges(); var returnMessage = "There was error with your transaction, please try again."; if (ex.Source == "Stripe.net") { returnMessage = ex.Message; } if (Request != null) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, returnMessage)); } //return Request.CreateResponse(HttpStatusCode.InternalServerError); else { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); //return new HttpResponseMessage(HttpStatusCode.InternalServerError,); } } }