Web config
==============
<!-- Start Paypal pages-->
<add key="PayPalUrl" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
<add key="PaypalEmailId" value="saurabh_kashyap-facilitator@seologistics.com"></add>
<add key="PaypalSuccessUrl" value="/Paypal/PayoutSuccess"></add>
<add key="PaypalErrorUrl" value="/Paypal/PayoutError"></add>
<add key="PaypalNotifyUrl" value="/Paypal/PayoutNotify"></add>
<add key="EnvironmentType" value="Dev"></add>
<!-- End Paypal Pages-->
Controler
==================
namespace EMPBenefits.Controllers
{
public class PayPalController : Controller
{
#region Payout amount to EMP by admin...
#region Declare all object
/// <summary>
/// Declare object of outside class
/// </summary>
GenericUnitOfWork _Unitofwork = new GenericUnitOfWork();
EncryptDecrypt objEncDec = new EncryptDecrypt();
Sendmail objsendmail = new Sendmail();
#endregion
public ActionResult PaymentWithPaypal(_Employerreg objpaymentdetails)
{
TempData["userpaymentinfo"] = objpaymentdetails;
return PayoutEMP(objpaymentdetails);
}
public ActionResult PayoutEMP(_Employerreg objdata)
{
// _Employerreg objdata = (_Employerreg)Session["Paymentdetails"];
int EMPId = Convert.ToInt32(objdata.employeerid);
decimal EMPEarning = Convert.ToDecimal(objdata.TotalPrice);
string Employeername = Convert.ToString(objdata.firstName);
string EMPPaypalEmail = ConfigurationManager.AppSettings["PaypalEmailId"];
string redirect = ConfigurationManager.AppSettings["PayPalUrl"] + "?cmd=_xclick&business=" + EMPPaypalEmail;
redirect += "&item_name=" + Employeername;
redirect += "&quantity=" + 1;
string totalPaymentAmount = Convert.ToString(EMPEarning);
redirect += "&amount=" + totalPaymentAmount;
redirect += "&ItemTotal=" + totalPaymentAmount;
redirect += "&tax_cart=" + "1";
redirect += "&rm=" + "2";
redirect += "¤cy_code=USD";
redirect += "&custom=" + EMPPaypalEmail + "|" + EMPId + "|" + totalPaymentAmount;
redirect += "¬ify_url=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalNotifyUrl"];
redirect += "&return=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalSuccessUrl"];
redirect += "&cancel_return=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalErrorUrl"];
TempData["requesturl"] = redirect;
return Redirect(redirect);
}
public ActionResult PayoutSuccess()
{
string strSandbox = ConfigurationManager.AppSettings["PayPalUrl"];
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = System.Text.Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
//enquiry status and update database
if (strResponse == "VERIFIED")
{
if (TempData["CurrentTransactionId"] == null)
{
if (System.Configuration.ConfigurationManager.AppSettings["EnvironmentType"] == "Dev")
{
System.Collections.Specialized.NameValueCollection these_argies = System.Web.HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
string txn_id = these_argies["txn_id"];
string payer_id = these_argies["payer_id"];
string payment_status = these_argies["payment_status"];
string payment_type = these_argies["payment_type"];
string payment_fee = these_argies["payment_fee"];
string receiver_id = these_argies["receiver_id"];
string handling_amount = these_argies["handling_amount"];
string payment_gross = these_argies["payment_gross"];
string custom = these_argies["custom"];
int providerId = 0;
decimal providerAmount = 0;
var customList = custom.Split('|');
if (customList.Count() == 3)
{
providerId = Convert.ToInt32(customList[1]);
Decimal.TryParse(customList[2], NumberStyles.Any, CultureInfo.InvariantCulture, out providerAmount);
}
_Employerreg objuserinfo = (_Employerreg)TempData["userpaymentinfo"];
tblPayment trns = new tblPayment();
trns.status = payment_status;
trns.amount =Convert.ToDecimal(payment_gross);
trns.transaction_date = DateTime.UtcNow;
trns.transactionNumber = txn_id;
trns.employerId = objuserinfo.employeerid;
trns.paymentOptionId = 1;
trns.request = Convert.ToString(TempData["requesturl"]);
_Unitofwork.GetRepositoryInstance<tblPayment>().Add(trns);
_Unitofwork.SaveChanges();
int id=Convert.ToInt32(objuserinfo.employeerid);
tblEmployer objemployee = _Unitofwork.GetRepositoryInstance<tblEmployer>().GetFirstOrDefaultByParameter(i => i.employerId == id);
objemployee.paymentOptionId = 1;
objemployee.lastUpdatedDate = DateTime.Now;
objemployee.lastUpdatedByUserId = id;
objemployee.numberOfEmployees = objuserinfo.numberofemp;
objemployee.subscriptionStartDate = DateTime.UtcNow;
objemployee.userName= GenerateRandom.RandomUserName(Convert.ToInt32(ConfigurationManager.AppSettings["UserNameLength"]));
objemployee.password = objEncDec.Encrypt(GenerateRandom.RandomPassword(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"])));
objemployee.isPaid = true;
_Unitofwork.GetRepositoryInstance<tblEmployer>().Update(objemployee);
_Unitofwork.SaveChanges();
}
}
}
else if (strResponse == "INVALID")
{
PayoutError();
}
return RedirectToAction("success", "Home");
}
public ActionResult PayoutError()
{
TempData["PaypalResponseAfterPayout"] = "There were some issues dring the payment and it is not completed. Please try again";
return RedirectToAction("index", "Home");
}
public ActionResult PayoutNotify()
{
string strSandbox = ConfigurationManager.AppSettings["PayPalUrl"];
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = System.Text.Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
if (System.Configuration.ConfigurationManager.AppSettings["EnvironmentType"] == "Live")
{
System.Collections.Specialized.NameValueCollection these_argies = System.Web.HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
string txn_id = these_argies["txn_id"];
string payer_id = these_argies["payer_id"];
string payment_status = these_argies["payment_status"];
string payment_type = these_argies["payment_type"];
string payment_fee = these_argies["payment_fee"];
string receiver_id = these_argies["receiver_id"];
string handling_amount = these_argies["handling_amount"];
string payment_gross = these_argies["payment_gross"];
string custom = these_argies["custom"];
int providerId = 0;
decimal providerAmount = 0;
var customList = custom.Split('|');
if (customList.Count() == 3)
{
providerId = Convert.ToInt32(customList[1]);
Decimal.TryParse(customList[2], NumberStyles.Any, CultureInfo.InvariantCulture, out providerAmount);
}
//Tbl_Transactions trns = new Tbl_Transactions();
//trns.AppointmentId = 0;
//trns.AdminPromo = 0;
//trns.AmountPaid = providerAmount; // Payout Amount
//trns.BookingFee = 0;
//trns.PayerMemberId = Session["AdminMember"] != null ? Convert.ToInt32(Session["AdminMember"]) : 0; // Admin Id
//trns.ProviderAmount = -providerAmount;
//trns.ProviderFee = 0;
//trns.ProviderPromo = 0;
//trns.RecieverMemberId = providerId; //Provider Id
//trns.TransactionStatusId = 1;
//trns.VatPercent = 0;
//trns.OfferDiscount = _GlobalDiscount;
//trns.SeekerAmount = 0;
//trns.TransactionTypeId = 3; // Admin pays to provider: monthly earning is paid by admin to provider
//trns.CreatedOn = DateTime.UtcNow;
//trns.ModifiedOn = DateTime.UtcNow;
//trns.PayerPayPalId = payer_id;
//trns.PaypalToken = txn_id;
//trns.PaypalPaymentId = txn_id;
//trns.ModifiedOn = DateTime.UtcNow;
//trns.TransactionStatusId = 2;
//_unitOfWork.GetRepositoryInstance<Tbl_Transactions>().Add(trns);
//_unitOfWork.SaveChanges();
}
return null;
}
#endregion
}
}
==============
<!-- Start Paypal pages-->
<add key="PayPalUrl" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
<add key="PaypalEmailId" value="saurabh_kashyap-facilitator@seologistics.com"></add>
<add key="PaypalSuccessUrl" value="/Paypal/PayoutSuccess"></add>
<add key="PaypalErrorUrl" value="/Paypal/PayoutError"></add>
<add key="PaypalNotifyUrl" value="/Paypal/PayoutNotify"></add>
<add key="EnvironmentType" value="Dev"></add>
<!-- End Paypal Pages-->
Controler
==================
namespace EMPBenefits.Controllers
{
public class PayPalController : Controller
{
#region Payout amount to EMP by admin...
#region Declare all object
/// <summary>
/// Declare object of outside class
/// </summary>
GenericUnitOfWork _Unitofwork = new GenericUnitOfWork();
EncryptDecrypt objEncDec = new EncryptDecrypt();
Sendmail objsendmail = new Sendmail();
#endregion
public ActionResult PaymentWithPaypal(_Employerreg objpaymentdetails)
{
TempData["userpaymentinfo"] = objpaymentdetails;
return PayoutEMP(objpaymentdetails);
}
public ActionResult PayoutEMP(_Employerreg objdata)
{
// _Employerreg objdata = (_Employerreg)Session["Paymentdetails"];
int EMPId = Convert.ToInt32(objdata.employeerid);
decimal EMPEarning = Convert.ToDecimal(objdata.TotalPrice);
string Employeername = Convert.ToString(objdata.firstName);
string EMPPaypalEmail = ConfigurationManager.AppSettings["PaypalEmailId"];
string redirect = ConfigurationManager.AppSettings["PayPalUrl"] + "?cmd=_xclick&business=" + EMPPaypalEmail;
redirect += "&item_name=" + Employeername;
redirect += "&quantity=" + 1;
string totalPaymentAmount = Convert.ToString(EMPEarning);
redirect += "&amount=" + totalPaymentAmount;
redirect += "&ItemTotal=" + totalPaymentAmount;
redirect += "&tax_cart=" + "1";
redirect += "&rm=" + "2";
redirect += "¤cy_code=USD";
redirect += "&custom=" + EMPPaypalEmail + "|" + EMPId + "|" + totalPaymentAmount;
redirect += "¬ify_url=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalNotifyUrl"];
redirect += "&return=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalSuccessUrl"];
redirect += "&cancel_return=" + ConfigurationManager.AppSettings["PageURL"] + ConfigurationManager.AppSettings["PaypalErrorUrl"];
TempData["requesturl"] = redirect;
return Redirect(redirect);
}
public ActionResult PayoutSuccess()
{
string strSandbox = ConfigurationManager.AppSettings["PayPalUrl"];
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = System.Text.Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
//enquiry status and update database
if (strResponse == "VERIFIED")
{
if (TempData["CurrentTransactionId"] == null)
{
if (System.Configuration.ConfigurationManager.AppSettings["EnvironmentType"] == "Dev")
{
System.Collections.Specialized.NameValueCollection these_argies = System.Web.HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
string txn_id = these_argies["txn_id"];
string payer_id = these_argies["payer_id"];
string payment_status = these_argies["payment_status"];
string payment_type = these_argies["payment_type"];
string payment_fee = these_argies["payment_fee"];
string receiver_id = these_argies["receiver_id"];
string handling_amount = these_argies["handling_amount"];
string payment_gross = these_argies["payment_gross"];
string custom = these_argies["custom"];
int providerId = 0;
decimal providerAmount = 0;
var customList = custom.Split('|');
if (customList.Count() == 3)
{
providerId = Convert.ToInt32(customList[1]);
Decimal.TryParse(customList[2], NumberStyles.Any, CultureInfo.InvariantCulture, out providerAmount);
}
_Employerreg objuserinfo = (_Employerreg)TempData["userpaymentinfo"];
tblPayment trns = new tblPayment();
trns.status = payment_status;
trns.amount =Convert.ToDecimal(payment_gross);
trns.transaction_date = DateTime.UtcNow;
trns.transactionNumber = txn_id;
trns.employerId = objuserinfo.employeerid;
trns.paymentOptionId = 1;
trns.request = Convert.ToString(TempData["requesturl"]);
_Unitofwork.GetRepositoryInstance<tblPayment>().Add(trns);
_Unitofwork.SaveChanges();
int id=Convert.ToInt32(objuserinfo.employeerid);
tblEmployer objemployee = _Unitofwork.GetRepositoryInstance<tblEmployer>().GetFirstOrDefaultByParameter(i => i.employerId == id);
objemployee.paymentOptionId = 1;
objemployee.lastUpdatedDate = DateTime.Now;
objemployee.lastUpdatedByUserId = id;
objemployee.numberOfEmployees = objuserinfo.numberofemp;
objemployee.subscriptionStartDate = DateTime.UtcNow;
objemployee.userName= GenerateRandom.RandomUserName(Convert.ToInt32(ConfigurationManager.AppSettings["UserNameLength"]));
objemployee.password = objEncDec.Encrypt(GenerateRandom.RandomPassword(Convert.ToInt32(ConfigurationManager.AppSettings["PasswordLength"])));
objemployee.isPaid = true;
_Unitofwork.GetRepositoryInstance<tblEmployer>().Update(objemployee);
_Unitofwork.SaveChanges();
}
}
}
else if (strResponse == "INVALID")
{
PayoutError();
}
return RedirectToAction("success", "Home");
}
public ActionResult PayoutError()
{
TempData["PaypalResponseAfterPayout"] = "There were some issues dring the payment and it is not completed. Please try again";
return RedirectToAction("index", "Home");
}
public ActionResult PayoutNotify()
{
string strSandbox = ConfigurationManager.AppSettings["PayPalUrl"];
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = System.Text.Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
if (System.Configuration.ConfigurationManager.AppSettings["EnvironmentType"] == "Live")
{
System.Collections.Specialized.NameValueCollection these_argies = System.Web.HttpUtility.ParseQueryString(strResponse_copy);
string user_email = these_argies["payer_email"];
string pay_stat = these_argies["payment_status"];
string txn_id = these_argies["txn_id"];
string payer_id = these_argies["payer_id"];
string payment_status = these_argies["payment_status"];
string payment_type = these_argies["payment_type"];
string payment_fee = these_argies["payment_fee"];
string receiver_id = these_argies["receiver_id"];
string handling_amount = these_argies["handling_amount"];
string payment_gross = these_argies["payment_gross"];
string custom = these_argies["custom"];
int providerId = 0;
decimal providerAmount = 0;
var customList = custom.Split('|');
if (customList.Count() == 3)
{
providerId = Convert.ToInt32(customList[1]);
Decimal.TryParse(customList[2], NumberStyles.Any, CultureInfo.InvariantCulture, out providerAmount);
}
//Tbl_Transactions trns = new Tbl_Transactions();
//trns.AppointmentId = 0;
//trns.AdminPromo = 0;
//trns.AmountPaid = providerAmount; // Payout Amount
//trns.BookingFee = 0;
//trns.PayerMemberId = Session["AdminMember"] != null ? Convert.ToInt32(Session["AdminMember"]) : 0; // Admin Id
//trns.ProviderAmount = -providerAmount;
//trns.ProviderFee = 0;
//trns.ProviderPromo = 0;
//trns.RecieverMemberId = providerId; //Provider Id
//trns.TransactionStatusId = 1;
//trns.VatPercent = 0;
//trns.OfferDiscount = _GlobalDiscount;
//trns.SeekerAmount = 0;
//trns.TransactionTypeId = 3; // Admin pays to provider: monthly earning is paid by admin to provider
//trns.CreatedOn = DateTime.UtcNow;
//trns.ModifiedOn = DateTime.UtcNow;
//trns.PayerPayPalId = payer_id;
//trns.PaypalToken = txn_id;
//trns.PaypalPaymentId = txn_id;
//trns.ModifiedOn = DateTime.UtcNow;
//trns.TransactionStatusId = 2;
//_unitOfWork.GetRepositoryInstance<Tbl_Transactions>().Add(trns);
//_unitOfWork.SaveChanges();
}
return null;
}
#endregion
}
}
No comments:
Post a Comment