public ActionResult PaymentReceive(string token, string payerID) { try { string currency = "CAD"; string query; //string payerID = payerID; // First thing's first. We'll need to get the token and payer ID returned from the previous call: //|| string.IsNullOrEmpty(payerID) if (string.IsNullOrEmpty(token)) { ViewBag.ErrorMessage = "Something went wrong, payment didn't go through the gateway."; return(View()); } //BufferRegister model = new BufferRegister(); BufferRegister dbModel; //= DBHelper.DBMembership.GetModelByTransactionToken(token); Helpers.DB xObj = new Helpers.DB(); dbModel = xObj.GetBufferRegisterByPayToken(token, new AppDbContext()); string nameProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Title); string descProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Description); decimal tax = 13 / 100; string sTax = Helpers.Tools.PriceFormat(tax); string Amount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price); string totalAmount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price * (1 + tax)); string sTaxAmt = Helpers.Tools.PriceFormat(tax * dbModel.BufferMembership.Price); // 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; string NVP = string.Empty; NVP += "USER="******"&PWD=" + Your_Test_Account_Password; NVP += "&SIGNATURE=" + Your_Test_Account_Signature; NVP += "&METHOD=CreateRecurringPaymentsProfile"; NVP += "&VERSION=86"; //product NVP += "&DESC=" + nameProduct; //#Profile description - same value as a billing agreement description //NVP += "&NAME=" + nameProduct; //#Profile description - same value as a billing agreement description //payment NVP += "&TOKEN=" + token; NVP += "&TAXAMT=" + sTaxAmt; //NVP += "&INITAMT=" + Amount; NVP += "&BILLINGPERIOD=Year"; // #Period of time between billings NVP += "&BILLINGFREQUENCY=1"; // #Frequency of charges NVP += "&AMT=" + Amount; // #The amount the buyer will pay in a payment period NVP += "&MAXFAILEDPAYMENTS=3"; // #Maximum failed payments before suspension of the profile NVP += "&NOSHIPPING=2"; // #Maximum failed payments before suspension of the profile NVP += "&CURRENCYCODE=" + currency; // Products involved in the transaction NVP += "&PAYMENTREQUEST_0_AMT=" + totalAmount; // Total payment for the transaction NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + Amount; // Purchased product price NVP += "&PAYMENTREQUEST_0_TAXAMT=" + sTaxAmt; // Tax amount NVP += "&PAYMENTREQUEST_0_QTY=1"; //NVP += "&L_PAYMENTREQUEST_0_NAME0=" + nameProduct; // Product name //NVP += "&L_PAYMENTREQUEST_0_DESC0=" + descProduct; // Product description //NVP += "&L_PAYMENTREQUEST_0_AMT0=" + totalAmount; // Product price //NVP += "&L_PAYMENTREQUEST_0_QTY0=1"; // Product quantity //Payer Information Fields NVP += "&EMAIL=" + dbModel.BufferContact.Email; NVP += "&SHIPTONAME=" + dbModel.BufferContact.FirstName + " " + dbModel.BufferContact.LastName; NVP += "&SUBSCRIBERNAME=" + dbModel.BufferContact.FirstName + " " + dbModel.BufferContact.LastName; NVP += "&PROFILEREFERENCE=INVOICENUMBERORSTUFF"; NVP += "&PROFILESTARTDATE=" + DateTime.UtcNow.ToString(); // #Billing date start, in UTC/GMT format NVP += "&FIRSTNAME=" + dbModel.BufferContact.FirstName; NVP += "&LASTNAME=" + dbModel.BufferContact.LastName; NVP += "&STREET=1 Anything Street"; NVP += "&ADDRESS1=1 Anything Street"; NVP += "&CITY=" + dbModel.BufferContact.City; //NVP += "&STATE=ON"; //NVP += "&ZIP=L4Z3U2"; NVP += "&COUNTRYCODE=CA"; NVP += "&BUSINESS=City of Hamilton"; NVP += "&AUTOBILLAMT=AddToNextBilling"; // 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 Profile = response.Substring(response.IndexOf("PROFILEID"), response.IndexOf("&", response.IndexOf("PROFILEID")) - response.IndexOf("PROFILEID")); string TransactNumber = Profile.Split('=')[1]; ViewBag.PaymentStatus += TransactNumber; //model = DBHelper.DBMembership.CheckTransactionTokenReturn(token); if (dbModel.isBufferValid()) { PayTransaction modelTrans = new PayTransaction(); modelTrans.PayerID = payerID; modelTrans.TransactionNumber = TransactNumber; dbModel.BufferTransaction = modelTrans; using (AppDbContext context = new AppDbContext()) { //validate transaction, update database query = "update BufferRegister set PayerID=@PayerID, TransactionNumber=@TransactionNumber where Email=@Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", dbModel.BufferContact.Email), new SqlParameter("@TransactionNumber", dbModel.BufferTransaction.TransactionNumber), new SqlParameter("@PayerID", dbModel.BufferTransaction.PayerID)); //buffer transfer, update database query = "BufferTransfer @Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", dbModel.BufferContact.Email)); } //create application username Helpers.MembUsers.CreateMember(dbModel, new AuthDbContext()); } } else { //model = (MembershipContactInfoModel)Session["Payer"]; throw new Exception("Something went wrong, payment didn't go through the gateway."); } return(View(dbModel)); } catch (Exception ex) { ModelState.AddModelError("CustomError", ex.Message); return(View("Error")); } }
public ActionResult PaymentItemReceive(string token, string payerID) { try { string query; // 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()); } //BufferRegister model = new BufferRegister(); BufferRegister dbModel; //= DBHelper.DBMembership.GetModelByTransactionToken(token); Helpers.DB xObj = new Helpers.DB(); dbModel = xObj.GetBufferRegisterByPayToken(token, new AppDbContext()); // using (AppDbContext context = new AppDbContext()) // { // query = "SELECT BufferRegister.*, Membership.Title, Membership.[Description], Membership.Price from BufferRegister " + // " LEFT JOIN Membership on Membership.MembershipID = BufferRegister.MembershipID " + // " WHERE BufferRegister.TransactionToken = @TransactionToken "; // dbModel = context.Database.SqlQuery<BufferRegister>(query, // new SqlParameter("@TransactionToken", token) // ).First(); //} string nameProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Title); string descProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Description); decimal tax = 13 / 100; string sTax = Helpers.Tools.PriceFormat(tax); string Amount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price); string totalAmount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price * (1 + tax)); // 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=" + totalAmount; NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + Amount; NVP += "&PAYMENTREQUEST_0_SHIPPINGAMT=0"; NVP += "&PAYMENTREQUEST_0_HANDLINGAMT=0"; NVP += "&PAYMENTREQUEST_0_TAXAMT=" + sTax; // 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 (dbModel.isBufferValid()) { PayTransaction modelTrans = new PayTransaction(); modelTrans.PayerID = payerID; modelTrans.TransactionNumber = TransactNumber; dbModel.BufferTransaction = modelTrans; using (AppDbContext context = new AppDbContext()) { //validate transaction, update database query = "update BufferRegister set PayerID=@PayerID, TransactionNumber=@TransactionNumber where Email=@Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", dbModel.BufferContact.Email), new SqlParameter("@TransactionNumber", dbModel.BufferTransaction.TransactionNumber), new SqlParameter("@PayerID", dbModel.BufferTransaction.PayerID)); //buffer transfer, update database query = "BufferTransfer @Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", dbModel.BufferContact.Email)); } //create application username Helpers.MembUsers.CreateMember(dbModel, new AuthDbContext()); } } else { //model = (MembershipContactInfoModel)Session["Payer"]; throw new Exception("Something went wrong, payment didn't go through the gateway."); } return(View(dbModel)); } catch (Exception ex) { ModelState.AddModelError("CustomError", ex.Message); return(View("Error")); } }
/// <summary> /// Submit Stripe Payment from payment page /// </summary> /// <param name="model"></param> /// <returns></returns> public ActionResult PaymentReceive(BufferRegister model) { try { string token = model.BufferTransaction.TransactionToken; //save return token update model in database string query = "update BufferRegister set TransactionToken = @TransactionToken where Email = @Email"; using (AppDbContext context = new AppDbContext()) { context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", model.BufferContact.Email), new SqlParameter("@TransactionToken", token)); //refresh model from DB Helpers.DB objDB = new Helpers.DB(); model = objDB.GetBufferRegisterByPayToken(token, context); } string currency = "cad"; int amt = int.Parse(Helpers.Tools.PriceFormat(model.BufferMembership.Price * 100)); decimal tax = 13 / 100; //customer subscription var chargeCustomer = new StripeCustomerCreateOptions { Email = model.BufferContact.Email, Description = model.BufferMembership.Title, PlanId = model.BufferMembership.Title, SourceToken = model.BufferTransaction.TransactionToken, TaxPercent = tax, Metadata = new Dictionary <string, string>() { { "Name", model.BufferContact.FirstName + " " + model.BufferContact.LastName } } }; var custServ = new StripeCustomerService(); var stripeCustomer = custServ.Create(chargeCustomer); if (stripeCustomer != null) { model.BufferTransaction.TransactionNumber = stripeCustomer.Id; model.BufferTransaction.PayerID = stripeCustomer.DefaultSourceId; using (AppDbContext context = new AppDbContext()) { //validate transaction, update database query = "update BufferRegister set PayerID = @PayerID, TransactionNumber=@TransactionNumber where Email = @Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", model.BufferContact.Email), new SqlParameter("@TransactionNumber", model.BufferTransaction.TransactionNumber), new SqlParameter("@PayerID", model.BufferTransaction.PayerID)); //buffer transfer, update database query = "BufferTransfer @Email"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@Email", model.BufferContact.Email)); } //create application username Helpers.MembUsers.CreateMember(model, new AuthDbContext()); } ////send to Stripe //var charge = new StripeChargeCreateOptions //{ // Amount = amt, // Currency = currency, // ReceiptEmail = model.BufferContact.Email, // Description = model.BufferMembership.Title, // SourceTokenOrExistingSourceId = model.BufferTransaction.TransactionToken, //}; //var chargeService = new StripeChargeService(); //var stripeCharge = chargeService.Create(charge); //if (stripeCharge.Paid) //{ // model.BufferTransaction.TransactionNumber = stripeCharge.Id; // model.BufferTransaction.PayerID = stripeCharge.Source.Id; // using (AppDbContext context = new AppDbContext()) // { // //validate transaction, update database // query = "update BufferRegister set PayerID = @PayerID, TransactionNumber=@TransactionNumber where Email = @Email"; // context.Database.ExecuteSqlCommand(query, // new SqlParameter("@Email", model.BufferContact.Email), // new SqlParameter("@TransactionNumber", model.BufferTransaction.TransactionNumber), // new SqlParameter("@PayerID", model.BufferTransaction.PayerID)); // //buffer transfer, update database // query = "BufferTransfer @Email"; // context.Database.ExecuteSqlCommand(query, // new SqlParameter("@Email", model.BufferContact.Email)); // } // //create application username // Helpers.MembUsers.CreateMember(model, new AuthDbContext()); //} return(View(model)); } catch (Exception ex) { Console.Write(ex.Message); ModelState.AddModelError("CustomError", ex.Message); return(View("Error")); } }