我使用贝宝,但 return 在 asp.net mvc 4 中响应

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

您好,最近我在使用 Paypal Sanbox,它运行良好。 我的问题是,当通过使用或 cancal 完成付款时,我没有收到任何回复,很多文件都说使用 paypay ipn,但有必要吗? 第二个是当用户在我的控制器中付款然后 return 但是用户关闭浏览器选项卡然后不是 return

型号:

  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"];

    }

控制器 :

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="MyPaypalAc@gmail.com" />
<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" />

知道哪种 paypal 方法用于了解用户完成付款或 cancal 并重定向到我的网页 谢谢

建议使用 IPN 进行网站支付标准,因为不能保证所有买家在付款完成后都会被重定向到您的 return URL。

您可以在您的 PayPal 帐户中启用自动 Return(直接 link 到此部分 https://www.paypal.com/cgi-bin/customerprofileweb?cmd=_profile-website-payments ),但即使那样您也需要考虑:

  • 自动 Return 它不是即时的。将向买家显示一条消息几秒钟:

"We are redirecting you back to (Seller) website"

有些买家可能会关闭浏览器的 window/tab 而不是等待。

  • Auto Return 不适用于 credit/debit 卡直接付款,没有 PayPal 帐户。

这些买家将需要单击 "Return to (Seller)" 按钮才能重定向回您的网站,其中一些人可能会错过此 link。

启用 IPN 后,您将始终从 PayPal 异步接收包含交易详细信息的 POST,无论买家是否return访问过您的网站。

终于有很多示例展示了,我使用的是 IPN

收听 IPN 控制器

 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");
    }