Search code examples
asp.net-mvcpaypalpayment-gatewaypaypal-ipnpaypal-sandbox

i use paypal but return responce in asp.net mvc 4


Hi Recently I Was use Paypal Sanbox and it work perfect . my Question is when payment done by use or cancal i am not getting any response many document say use paypay ipn but is it necessary? second is when user cancal payment then return in my controller but user close browser tab then not return

Model:

  public class PayPalModel
{

    public string cmd { get; set; }
    public string business { get; set; }
    public string no_shipping { get; set; }
    public string @return { get; set; }
    public string cancel_return { get; set; }
    public string notify_url { get; set; }
    public string currency_code { get; set; }
    public string item_name { get; set; }
    public string amount { get; set; }
    public string actionURL { get; set; }
    public PayPalModel(bool useSandbox)
    {
        this.cmd = "_xclick";
        this.business = ConfigurationManager.AppSettings["business"];
        this.cancel_return = ConfigurationManager.AppSettings["cancel_return"];
        this.@return = ConfigurationManager.AppSettings["return"];
        if (useSandbox)
        {
            this.actionURL = ConfigurationManager.AppSettings["test_url"];
        }
        else
        {
            this.actionURL = ConfigurationManager.AppSettings["Prod_url"];
        }

        this.notify_url = ConfigurationManager.AppSettings["notify_url"];

        this.currency_code = ConfigurationManager.AppSettings["currency_code"];

    }

Controller :

public ActionResult Index()
    {
        return View();
    }
    public ActionResult RedirectFromPaypal()
    {
        return View();
    }
    public ActionResult CancelFromPaypal()
    {
        return View();
    }
    public ActionResult NotifyFromPaypal()
    {
        return View();
    }
    //  [Authorize(Roles="Customers")]
   // [HttpPost]
    public ActionResult ValidateCommand(string product, string totalPrice)
    {
        bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);
        var paypal = new PayPalModel(useSandbox);
        paypal.item_name = product;
        paypal.amount = totalPrice;
        return View(paypal);
        // return View();
    }

WebConfig:

 <add key="business" value="[email protected]" />
<add key="IsSandbox" value="true" />
<add key="currency_code" value="USD" />
<add key="return" value="http://localhost/PayPal/RedirectFromPaypal" />
<add key="cancel_return" value="http://localhost/PayPal/CancelFromPaypal" />
<add key="notify_url" value="http://localhost/PayPal/NotifyFromPaypal" />

<add key="test_url" value="http://www.sandbox.paypal.com/cgi-bin/webscr" />
<add key="Prod_url" value="http://www.sandbox.paypal.com/cgi-bin/webscr" />

any idea which paypal method use to know payment done or cancal by user and redirect to my web page thanks you


Solution

  • finally many example show and i was use IPN

    Hear IPN Controller

     public ActionResult IPN()
        {
            SmartQueueContext context = new SmartQueueContext();
            //   var order = new Order(); // this is something I have defined in order to save the order in the database
    
            // Receive IPN request from PayPal and parse all the variables returned
            var formVals = new Dictionary<string, string>();
            formVals.Add("cmd", "_notify-validate"); //notify-synch_notify-validate
            //   formVals.Add("at", "this is a long token found in Buyers account"); // this has to be adjusted
            // formVals.Add("tx", Request["tx"]);
    
            // if you want to use the PayPal sandbox change this from false to true
            string response = GetPayPalResponse(formVals, false);
    
            if (response.Contains("VERIFIED"))
            {
                //string transactionID = GetPDTValue(response, "txn_id"); // txn_id //d
                // string sAmountPaid = GetPDTValue(response, "mc_gross"); // d
                //string deviceID = GetPDTValue(response, "custom"); // d
                //string payerEmail = GetPDTValue(response, "payer_email"); // d
                //string Item = GetPDTValue(response, "item_name");
    
                //validate the order
                string transactionID = Request["txn_id"];
                string sAmountPaid = Request["mc_gross"];
                string payerEmail = Request["payer_email"]; // d
                context.PayPalTransfer(SessionManager.ClientId, transactionID, payerEmail);
    
    
                return RedirectToAction("Summary", "PackageSetup");
    
            }
            else
            {
                return RedirectToAction("CancelFromPaypal", "PayPal");
            }
           // return RedirectToAction("Index", "PackageSetup");
        }