嗨,最近我使用了Sanbox PayPal它工作得很好。我的问题是,当使用或 cancal 付款时,我没有得到任何回复,许多文件说使用 paypay ipn,但有必要吗?第二个是当用户Cancal付款然后返回我的控制器,但用户关闭浏览器选项卡然后不返回时
型:
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作为网站付款标准,因为无法保证所有买家在付款完成后都会被重定向到您的退货网址。
您可以在PayPal帐户中启用自动退货(直接链接到本节 https://www.paypal.com/cgi-bin/customerprofileweb?cmd=_profile-website-payments),但即便如此,您也需要考虑:
- 自动返回不是即时的。将向买家显示一条消息几秒钟:
"我们将您重定向回(卖家)网站"
一些买家可能会关闭浏览器的窗口/选项卡,而不是等待。
- 自动退货不适用于直接使用信用卡/借记卡进行的付款,没有PayPal帐户。
这些买家需要点击"返回(卖家)"按钮才能重定向回您的网站,其中一些买家可能会错过此链接。
启用 IPN 后,您将始终从包含交易详细信息的PayPal异步收到 POST,无论买家是否返回您的网站。
最后有很多例子显示,我正在使用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");
}