public ActionResult PaymentSave(MembershipContactInfoModel model) { //if (model.GatewayName != "PayPal") { // //ModelState.AddModelError(string.Empty, "Wrong gateway channel, please contact your Administrator."); // return View("Error"); //} ViewBag.ErrorMessage = ""; if (!model.isContactValid()) { return(RedirectToAction("Index", "Membership")); } //check if model email exists AuthDbContext context = new AuthDbContext(); var usrManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (usrManager.FindByEmail(model.Email) != null) { //already a member ViewBag.ErrorMessage = "Invalid information, this account already exists, please return and correct the information."; } if (!model.isContactValid()) { ViewBag.ErrorMessage = "Invalid information, please return and fill out all the required information."; return(View(model)); } DBHelper.DBMembership.BufferRegisterContact(ref model); return(View(model)); }
public ActionResult ContactInfo(MembershipContactInfoModel model) { ViewBag.MembershipId = model.MembershipId; ViewBag.MembershipTitle = DBHelper.DBMembership.getMembershipById(model.MembershipId).Title; ViewBag.Price = model.Price; return(View(model)); }
/// <summary> /// last call when receiving the payment confirmation /// when payment returns from the gateway /// if session saved before sending the payment is the same with the payment token then transaction ok /// go extract information from the database for that payment token /// </summary> /// <param name="TransactionToken"></param> /// <returns></returns> public static MembershipContactInfoModel CheckTransactionTokenReturn(string TransactionToken) { MembershipContactInfoModel model = new MembershipContactInfoModel(); //if (Session["TransactionToken"] !=null && Session["TransactionToken"].ToString().Equals(TransactionToken)) //{ // model = GetModelByTransactionToken(TransactionToken); //} model = GetModelByTransactionToken(TransactionToken); return(model); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> public static void BufferRegisterContact(ref MembershipContactInfoModel model) { using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "BufferRegisterSave"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@email", model.Email); command.Parameters.AddWithValue("@firstname", model.FirstName); command.Parameters.AddWithValue("@lastname", model.LastName); command.Parameters.AddWithValue("@city", model.City); command.Parameters.AddWithValue("@MembershipId", model.MembershipId); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { model.MembershipId = int.Parse(reader["MembershipId"].ToString()); model.MembershipTitle = reader["MembershipTitle"].ToString(); model.MembershipDescription = reader["MembershipDescription"].ToString(); model.RoleName = reader["RoleName"].ToString(); var dblPrice = reader["Price"].ToString(); double dblMPrice = 0; double.TryParse(dblPrice, out dblMPrice); model.Price = dblMPrice; model.Email = reader["Email"].ToString(); model.FirstName = reader["FirstName"].ToString(); model.LastName = reader["LastName"].ToString(); model.City = reader["City"].ToString(); model.TokenID = reader["TokenID"].ToString(); } if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } //return model.TokenID; } } }
public static MembershipContactInfoModel GetModelByTransactionToken(string TransactionToken) { MembershipContactInfoModel model = new MembershipContactInfoModel(); using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "select b.*, m.Title MembershipTitle, m.[Description] MembershipDescription, m.RoleName, m.Price from BufferRegister b left join Membership m on m.MembershipID = b.MembershipId where b.TransactionToken = @TransactionToken"; command.Parameters.AddWithValue("@TransactionToken", TransactionToken); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { model.TokenID = reader["TokenID"].ToString(); model.Email = reader["Email"].ToString(); model.FirstName = reader["FirstName"].ToString(); model.LastName = reader["LastName"].ToString(); model.City = reader["City"].ToString(); model.TransactionToken = TransactionToken; model.MembershipId = int.Parse(reader["MembershipId"].ToString()); model.MembershipTitle = reader["MembershipTitle"].ToString(); model.MembershipDescription = reader["MembershipDescription"].ToString(); model.RoleName = reader["RoleName"].ToString(); var dblPrice = reader["Price"].ToString(); double dblMPrice = 0; double.TryParse(dblPrice, out dblMPrice); model.Price = dblMPrice; } if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } } } return(model); }
public static void UpdateTransactionToken(MembershipContactInfoModel model) { using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "update BufferRegister set TransactionToken = @TransactionToken where Email = @Email"; command.Parameters.AddWithValue("@Email", model.Email); command.Parameters.AddWithValue("@TransactionToken", model.TransactionToken); command.ExecuteNonQuery(); if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } } } }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> public static bool CreateMember(MembershipContactInfoModel model) { AuthDbContext context = new AuthDbContext(); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); //Here we create a Admin super user who will maintain the website var user = new ApplicationUser(); user.Email = model.Email; user.UserName = user.Email; string userPWD = "xyz"; var chkUser = UserManager.Create(user, userPWD); //if (!roleManager.RoleExists(model.RoleName)) //Add default User to Role Admin if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, model.RoleName); } return(true); }
/// <summary> /// /// </summary> /// <param name="model"></param> public static void BufferTransfer(MembershipContactInfoModel model) { using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "BufferTransfer"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@Email", model.Email); command.ExecuteNonQuery(); if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } } } }
public ActionResult ContactInfo(int?mid) { if (mid == null) { return(RedirectToAction("Index")); } ViewBag.MembershipId = mid.Value; MembershipModel model = DBHelper.DBMembership.getMembershipById(mid.Value); ViewBag.MembershipTitle = model.Title; ViewBag.Price = model.Price; Gateway modelGateway = DBHelper.DBGateway.getDefaultGateway(); MembershipContactInfoModel memModel = new MembershipContactInfoModel(); memModel.MembershipId = mid.Value; memModel.MembershipTitle = model.Title; memModel.Price = model.Price; memModel.GatewayId = modelGateway.GatewayId; memModel.GatewayName = modelGateway.GatewayName; return(View(memModel)); }
/// <summary> /// Submit Stripe Payment from payment page /// </summary> /// <param name="model"></param> /// <returns></returns> public ActionResult PaymentReceive(MembershipContactInfoModel model) { try { //save token update model DBHelper.DBMembership.BufferRegisterContact(ref model); DBHelper.DBMembership.UpdateTransactionToken(model); string currency = "cad"; int amt = int.Parse(DBHelper.DBMembership.PriceFormat(model.Price * 100)); var charge = new StripeChargeCreateOptions { Amount = amt, Currency = currency, ReceiptEmail = model.Email, Description = model.MembershipTitle, SourceTokenOrExistingSourceId = model.TransactionToken, }; var chargeService = new StripeChargeService(); var stripeCharge = chargeService.Create(charge); ViewBag.ChargeID = stripeCharge.Id; model.TransactionNumber = stripeCharge.Id; model.PayerID = stripeCharge.Source.Id; if (stripeCharge.Paid) { DBHelper.DBMembership.ValidateTransaction(model); DBHelper.DBMembership.BufferTransfer(model); } return(View()); } catch (Exception ex) { Console.Write(ex.Message); return(View("Error")); } }
/// <summary> /// get form submit from ContactInfo /// Save information to Buffer /// </summary> /// <param name="model"></param> /// <returns></returns> public ActionResult PaymentSave(MembershipContactInfoModel model) { if (model.GatewayName != "Stripe") { ModelState.AddModelError("CustomError", "Error: Wrong gateway channel"); return(View("Error")); } try { ViewBag.ErrorMessage = ""; if (!model.isContactValid()) { return(RedirectToAction("Index", "Membership")); } //check if model email exists AuthDbContext context = new AuthDbContext(); var usrManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (usrManager.FindByEmail(model.Email) != null) { //already a member ModelState.AddModelError("CustomError", "Error: Invalid information, this account already exists, please return and correct the information."); return(View("Error")); } //save contact information into Buffer DBHelper.DBMembership.BufferRegisterContact(ref model); return(View(model)); } catch (Exception ex) { ModelState.AddModelError("CustomError", "Error: " + ex.Message); return(View("Error")); } }
public ActionResult PaymentSend(MembershipContactInfoModel model) { //get the information as saved from DB for security purposes // example: send the exact amount as indicated in the database, do not use model (model can be changed in browser inspect source etc) MembershipContactInfoModel dbModel = DBHelper.DBMembership.GetModelByEmail(model.Email); //string returnURL = "https://localhost:44354/PayPal/PaymentReceive"; //string cancelURL = "https://localhost:44354/PayPal/PaymentCancel"; string returnURL = "https://huronet.com/PayPal/PaymentReceive"; string cancelURL = "https://huronet.com/PayPal/PaymentCancel"; string Your_Test_Merchant_Account_User = "******"; string Your_Test_Account_Password = "******"; string Your_Test_Account_Signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31Am.wVsyYEVgjySzw5m3Tj9q9KCk5"; string nameProduct = HttpUtility.UrlEncode(dbModel.MembershipTitle); string descProduct = HttpUtility.UrlEncode(dbModel.MembershipDescription); double tax = 13 / 100; // This are the URLs the PayPal process uses. The endpoint URL is created using the NVP string generated below while the redirect url is where the page the user will navigate to when leaving PayPal plus the PayerID and the token the API returns when the request is made. string NVP = string.Empty; // API call method: add the desired checkout method. As I've mentioned above, we're using the express checkout. NVP += "METHOD=SetExpressCheckout"; NVP += "&VERSION=123"; // Credentials identifying you as the merchant NVP += "&USER="******"&PWD=" + Your_Test_Account_Password; NVP += "&SIGNATURE=" + Your_Test_Account_Signature; // Redirect from PayPal portal NVP += "&RETURNURL=" + returnURL; // Return URL from the PayPal portal for completed payment NVP += "&CANCELURL=" + cancelURL; // Return URL from the PayPal portal for a cancelled purchase // Payment request information NVP += "&PAYMENTREQUEST_0_PAYMENTACTION=Sale"; // Type of transaction NVP += "&PAYMENTREQUEST_0_AMT=" + DBHelper.DBMembership.PriceFormat(dbModel.Price * (1 + tax));; // Total payment for the transaction NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + DBHelper.DBMembership.PriceFormat(dbModel.Price);; // Purchased product price NVP += "&PAYMENTREQUEST_0_SHIPPINGAMT=0"; // Shipping amount NVP += "&PAYMENTREQUEST_0_HANDLINGAMT=0"; // Handling charges NVP += "&PAYMENTREQUEST_0_TAXAMT=" + DBHelper.DBMembership.PriceFormat(dbModel.Price * tax);; // Tax amount // Products involved in the transaction NVP += "&L_PAYMENTREQUEST_0_NAME0=" + nameProduct; // Product name NVP += "&L_PAYMENTREQUEST_0_DESC0=" + descProduct; // Product description NVP += "&L_PAYMENTREQUEST_0_AMT0=" + DBHelper.DBMembership.PriceFormat(dbModel.Price); // Product price NVP += "&L_PAYMENTREQUEST_0_QTY0=1"; // Product quantity System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; // Make the API call to the PayPal Service HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint); request.Method = "POST"; request.ContentLength = NVP.Length; string sResponse = string.Empty; using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) { sw.Write(NVP); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader sr = new StreamReader(response.GetResponseStream())) { sResponse = sr.ReadToEnd(); } // Receive the token for the operation and redirect the user to the generated URL for the PayPal portal string token = string.Empty; string[] splitResponse = sResponse.Split('&'); if (splitResponse.Length > 0) { foreach (string responseField in splitResponse) { if (responseField.Contains("TOKEN")) { token = responseField.Substring(6); break; } } if (!string.IsNullOrEmpty(token)) { //token receiving - payment sent ok model.TransactionToken = HttpUtility.UrlDecode(token); DBHelper.DBMembership.UpdateTransactionToken(model); //seesion is lost after redirect ??! //Session["TransactionToken"] = token; //Session["Payer"] = model; redirectUrl = string.Format(redirectUrl, token); //redirect to paypal website Response.Redirect(redirectUrl); } } // If we get here, something went wrong; //lblError.Visible = true; // Simple error handling :) return(View()); }
public ActionResult PaymentReceive(string token, string payerID) { ViewBag.ErrorMessage = ""; // First thing's first. We'll need to get the token and payer ID returned from the previous call: if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(payerID)) { ViewBag.ErrorMessage = "Something went wrong, payment didn't go through the gateway."; return(View()); } string Your_Test_Merchant_Account_User = "******"; string Your_Test_Account_Password = "******"; string Your_Test_Account_Signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31Am.wVsyYEVgjySzw5m3Tj9q9KCk5"; MembershipContactInfoModel model = new MembershipContactInfoModel(); MembershipContactInfoModel dbModel = DBHelper.DBMembership.GetModelByTransactionToken(token); string nameProduct = HttpUtility.UrlEncode(dbModel.MembershipTitle); string descProduct = HttpUtility.UrlEncode(dbModel.MembershipDescription); double tax = 13 / 100; try { // Than we add the tokens to string type variables as we'll need to rebuild the NVP string // Rebuilding the NVP string for the request; I've hardcoded the payment values again as this sample app does not have a database behind it. string NVP = string.Empty; NVP += "METHOD=DoExpressCheckoutPayment"; NVP += "&VERSION=123"; NVP += "&USER="******"&PWD=" + Your_Test_Account_Password; NVP += "&SIGNATURE=" + Your_Test_Account_Signature; NVP += "&TOKEN=" + token; NVP += "&PAYERID=" + payerID; NVP += "&PAYMENTREQUEST_0_PAYMENTACTION=Sale"; NVP += "&PAYMENTREQUEST_0_AMT=" + DBHelper.DBMembership.PriceFormat(dbModel.Price * (1 + tax)); NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + DBHelper.DBMembership.PriceFormat(dbModel.Price); NVP += "&PAYMENTREQUEST_0_SHIPPINGAMT=0"; NVP += "&PAYMENTREQUEST_0_HANDLINGAMT=0"; NVP += "&PAYMENTREQUEST_0_TAXAMT=" + DBHelper.DBMembership.PriceFormat(tax); // Making the API call string response = APICall(NVP); // Interpreting the response from PayPal; As a simple UI for checking the transaction, I'm displaying the transaction ID in the page on success so to make things easier when I'm checking the transaction log in PayPal's web UI. if (response.Contains("Success")) { string transactionId = response.Substring(response.IndexOf("PAYMENTINFO_0_TRANSACTIONID"), response.IndexOf("&", response.IndexOf("PAYMENTINFO_0_TRANSACTIONID")) - response.IndexOf("PAYMENTINFO_0_TRANSACTIONID")); string TransactNumber = transactionId.Split('=')[1]; ViewBag.PaymentStatus += transactionId; model = DBHelper.DBMembership.CheckTransactionTokenReturn(token); if (model.isContactValid()) { model.PayerID = payerID; model.TransactionNumber = TransactNumber; //update transaction number and payerid to buffer DBHelper.DBMembership.ValidateTransaction(model); //create username DBHelper.DBMembership.CreateMember(model); //transfer Buffer into Contacts and the rest of tables DBHelper.DBMembership.BufferTransfer(model); } } else { //model = (MembershipContactInfoModel)Session["Payer"]; ViewBag.ErrorMessage = "Something went wrong, payment didn't go through the gateway."; } } catch (Exception ex) { //model = (MembershipContactInfoModel)Session["Payer"]; ViewBag.ErrorMessage = ex.Message; } return(View(model)); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> public ActionResult MembershipPay(MembershipContactInfoModel model) { return(View()); }
public ActionResult PaymentSend(MembershipContactInfoModel model) { // return(View()); }