public void WebHook() { try { var paymentAlertStatus = 0; var postdata = new StreamReader(HttpContext.Request.InputStream).ReadToEnd(); var data = JObject.Parse(postdata); var signature = Request.Headers["Stripe-Signature"]; var eventid = data["id"].ToString(); var custid = data["data"].First().First["customer"].ToString(); var msg = ""; var emailboday = ""; var eventdata = StripeHelper.GetStripeEvent(eventid); if (eventdata != null && (eventdata.Type == "invoice.payment_succeeded" || eventdata.Type == "invoice.payment_failed")) { String coupon_id = null; var list = data["data"].First().First["lines"]; var Amount = ((double)list["data"].First["amount"]) / 100; var Amount_off = ((double)data["data"].First().First["amount_due"]) / 100; var Discount = data["data"].First().First["discount"]; var start = (double)list["data"].First["period"]["start"]; var end = (double)list["data"].First["period"]["end"]; var startperiod = MppUtility.UnixTimeStampToDateTime(start); var endperiod = MppUtility.UnixTimeStampToDateTime(end); var discount = Math.Round(Amount - Amount_off, 2, MidpointRounding.AwayFromZero); var customerdata = StripeHelper.GetStripeCustomer(custid); var email = customerdata.Email; var last4 = ""; var carddetails = StripeHelper.GetCard(custid); foreach (var card in carddetails) { last4 = card.Last4; } if (Discount.Count() > 0) { coupon_id = Discount["coupon"]["id"].ToString(); } switch (eventdata.Type) { case "invoice.payment_succeeded": msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 1); emailboday = "Payment Success. A payment of $" + Amount_off + " was made using your card on file ending with <b> " + last4 + " </b>"; paymentAlertStatus = 1; break; case "invoice.payment_failed": msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 0); emailboday = "Payment failed.Payment $" + Amount_off + " failed from your card ending with <b> " + last4 + " </b> "; paymentAlertStatus = 2; break; } if (msg == "" && Amount_off != 0) { var respo = EmailAlert.PaymentAlertMail(email, emailboday); if (respo == "success") { sellerData.UpdatePaymentAlert(paymentAlertStatus, custid); } } } } catch (Exception ex) { String msg = ex.Message; } }
public HttpStatusCode WebHook() { try { var paymentAlertStatus = 0; string secret = MppUtility.ReadConfig("MS_WebHookReceiverSecret_Custom"); var postdata = new StreamReader(HttpContext.Request.InputStream).ReadToEnd(); var emailboday = ""; var signature = Request.Headers["Stripe-Signature"]; var stripeEvent = StripeEventUtility.ConstructEvent(postdata, signature, secret); var eventid = stripeEvent.Id; var eventdata = StripeHelper.GetStripeEvent(eventid); var data = JObject.Parse(postdata); var custid = data["data"].First().First["customer"].ToString(); var msg = ""; if (eventdata != null && (eventdata.Type == "invoice.payment_succeeded" || eventdata.Type == "invoice.payment_failed")) { String coupon_id = null; var list = data["data"].First().First["lines"]; var Amount = ((double)list["data"].First["amount"]) / 100; var Amount_off = ((double)data["data"].First().First["amount_due"]) / 100; var Discount = data["data"].First().First["discount"]; var start = (double)list["data"].First["period"]["start"]; var end = (double)list["data"].First["period"]["end"]; var startperiod = MppUtility.UnixTimeStampToDateTime(start); var endperiod = MppUtility.UnixTimeStampToDateTime(end); var discount = Math.Round(Amount - Amount_off, 2, MidpointRounding.AwayFromZero); var customerdata = StripeHelper.GetStripeCustomer(custid); var email = customerdata.Email; var last4 = ""; var carddetails = StripeHelper.GetCard(custid); foreach (var card in carddetails) { last4 = card.Last4; } if (Discount.Count() > 0) { coupon_id = Discount["coupon"]["id"].ToString(); } switch (eventdata.Type) { case "invoice.payment_succeeded": msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 1); emailboday = "A payment of $" + Amount_off + " was made using your card on file ending with <b> " + last4 + " </b>"; paymentAlertStatus = 1; break; case "invoice.payment_failed": msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 0); upData.UpdatePlanData(custid, 0); StripeHelper.UnSubscribe(custid); emailboday = @"A payment $" + Amount_off + " failed from your card ending with <b> " + last4 + " </b>" + "Please check your card details or add new card."; paymentAlertStatus = 2; break; } if (msg == "" && Amount_off != 0) { var respo = EmailAlert.PaymentAlertMail(email, emailboday); if (respo == "success") { sellerData.UpdatePaymentAlert(paymentAlertStatus, custid); } } } else if (stripeEvent.Type == "charge.failed") { var Amount = ((double)data["data"].First().First["amount"]) / 100; var failure_message = data["data"].First().First["failure_message"].ToString(); // msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount, 0, 0.00, startperiod, endperiod, 0); if (Amount > 1)//Card verification charge { upData.UpdatePlanData(custid, 0); StripeHelper.UnSubscribe(custid); } var last4 = ""; var carddetails = StripeHelper.GetCard(custid); var customerdata = StripeHelper.GetStripeCustomer(custid); var email = customerdata.Email; foreach (var card in carddetails) { last4 = card.Last4; } emailboday = @"A payment $" + Amount + " failed from your card ending with <b> " + last4 + " </b>" + "Please check your card details or add new card.</b>Possible reason:" + failure_message; var respo = EmailAlert.PaymentAlertMail(email, emailboday); if (respo == "success" && Amount > 1) { sellerData.UpdatePaymentAlert(2, custid); } } } catch (Exception ex) { String msg = ex.Message; LogFile.WriteLog(msg); } return(HttpStatusCode.OK); }
//Billing public ActionResult CreateStripeCustomer(string token) { var email = Session["Email"].ToString(); var subscription = new StripeSubscription(); var customerDetails = new StripeCustomer(); var userInfo = new UpdateUserInfo(); string msg = ""; var cardStatus = 0; DataTable userData; StripeCharge charge = null; try { if (token != null) { var ds = new AccountData().ValidateEmailAndGetUserinfo(email); userData = ds.Tables[0]; var custID = Convert.ToString(userData.Rows[0]["stp_custId"]); if (userData.Rows.Count > 0 && !string.IsNullOrWhiteSpace(custID)) //update { var customerUpdateOptions = new StripeCustomerUpdateOptions { Email = email, Description = Session["FirstName"] + " " + Session["LastName"], SourceToken = token }; //updated customer customerDetails = StripeHelper.UpdateCustomer(custID, customerUpdateOptions); //add a test charge for $1 var makeTestCharge = new StripeChargeCreateOptions { CustomerId = customerDetails.Id, Amount = 100, Currency = "usd", Description = "Card verification charge", SourceTokenOrExistingSourceId = customerDetails.DefaultSourceId }; charge = StripeHelper.MakePayment(makeTestCharge); if (charge != null) { var refund = StripeHelper.RefundCharge(charge.Id); cardStatus = 1; userInfo.CustId = customerDetails.Id; userInfo.CardId = customerDetails.DefaultSourceId; userInfo.UserId = Convert.ToInt32(Session["UserID"]); AccountData.UpdateStripeCardData(userInfo); if ((SessionData.PlanID == 1 && SessionData.TrialEndOn < DateTime.Now) || SessionData.PlanStatus == 0) //When trail was expired || Unsubscribed and adding a card since payments are not succesfull { msg = StripeServices.SubscribePlan(SessionData.StripeCustId, userInfo.CardId); } if (String.IsNullOrWhiteSpace(msg)) { SessionData.PlanStatus = 1; //Default subscription SessionData.StripeCustId = customerDetails.Id; SessionData.StripeCardId = customerDetails.DefaultSourceId; } } else { StripeHelper.DeleteCard(customerDetails.DefaultSourceId, custID); cardStatus = 0; } } customerDetails = StripeHelper.GetStripeCustomer(customerDetails.Id); } var respo = new { Status = cardStatus, Message = msg, CustomerDetails = customerDetails }; return(Json(respo, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { LogFile.WriteLog("CreateCustomer - " + ex.Message.ToString()); return(Json(null, JsonRequestBehavior.AllowGet)); } }
//[ValidateAntiForgeryToken] public ActionResult Login(LoginViewModel model, String returnUrl) { String msg = ""; Int32 UserID = 0; DataSet ds; DataTable dt, dtbl; DataRow dr = null, dr1 = null; if (ModelState.IsValid) { String email = model.Email.Trim(); ds = userregistrationData.ValidateEmailAndGetUserinfo(email); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { dt = ds.Tables[0]; dr = dt.Rows[0]; UserID = Convert.ToInt32(dr["MppUserID"]); msg = userregistrationData.CheckUserLogin(UserID, model.Password); if (msg == "") { dtbl = ds.Tables[1]; dr1 = dtbl.Rows[0]; String CustmId, CardId; SessionData.UserID = UserID; String FirstName = Convert.ToString(dr["FirstName"]); SessionData.LastName = Convert.ToString(dr["LastName"]); String PlanDate = Convert.ToString(dr1["PlanDate"]); DateTime?Today, NextPlanDate = DateTime.MaxValue; Today = DateTime.Now; DateTime StartDate = Convert.ToDateTime(dr["StartDate"]); if (!String.IsNullOrWhiteSpace(PlanDate)) { NextPlanDate = Convert.ToDateTime(PlanDate); } SessionData.StartDate = StartDate.ToString("yyyy/MM/dd hh:mm:ss tt"); SessionData.PlanStatus = Convert.ToInt32(dr["PlanStatus"]); SessionData.PlanID = Convert.ToInt32(dr["PlanID"]); SessionData.FormulaAccess = Convert.ToInt16(dr["IsSetFormula"]); SessionData.TrialEndOn = Convert.ToDateTime(dr["TrailEndDate"]); SessionData.Email = email; SessionData.IsAgreementAccept = Convert.ToInt16(dr["IsAgreementConfirm"]); SessionData.ProfileAccess = Convert.ToInt16(dr["ProfileAccess"]); SessionData.AlertCount = Convert.ToInt32(dr1["total_alerts"]); CustmId = Convert.ToString(dr["stp_custId"]); CardId = Convert.ToString(dr["stp_cardId"]); if (!String.IsNullOrWhiteSpace(CustmId)) { FormsAuthentication.SetAuthCookie(FirstName, false); var custm = StripeHelper.GetStripeCustomer(CustmId); if (!String.IsNullOrWhiteSpace(custm.DefaultSourceId) && String.IsNullOrWhiteSpace(CardId)) { StripeHelper.DeleteCard(custm.DefaultSourceId, CustmId); } SessionData.StripeCardId = CardId; SessionData.StripeCustId = CustmId; if (((SessionData.PlanID == 1 && SessionData.TrialEndOn < Today && !String.IsNullOrWhiteSpace(CardId)) || NextPlanDate < Today) && SessionData.PlanStatus == 1) // When plan was expired { if (String.IsNullOrWhiteSpace(CardId)) { return(RedirectToAction("Plan", "Settings")); } else { msg = StripeServices.SubscribePlan(CustmId, CardId); } if (!String.IsNullOrWhiteSpace(msg)) { TempData["IsValid"] = msg; return(RedirectToAction("Login", "UserAccount")); } } if (SessionData.PlanID != 1) { if (SessionData.PlanStatus == 0) // handles unsubscribed status after trial period { return(RedirectToAction("Plan", "Settings")); } else if (String.IsNullOrWhiteSpace(CardId) && SessionData.PlanStatus == 1) // When CardId was not found and Plan was not trail { return(RedirectToAction("Cards", "Settings")); } } } else { return(View("Error")); } if (Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } else { return(RedirectToAction("Dashboard", "Main")); } } } else { msg = "Invalid login attempt!"; } TempData["IsValid"] = msg; return(RedirectToAction("Login", "UserAccount")); } return(View(model)); }