0

我无法理解我在哪里又犯了错误。我正在运行此代码:

<form id="frm" action="https://www.sandbox.paypal.com/cgi-bin/webscr">

<input id="cmd" name="cmd" type="hidden" value="_xclick" />
<input id="business" name="business" type="hidden"   value="anilcs_1361585097_biz@gmail.com" />
<input id="no_shipping" name="no_shipping" type="hidden" value="" />
 <input id="return" name="return" type="hidden" value="http://localhost:1552/Home/IPN"/>
<input id="cancel_return" name="cancel_return" type="hidden" value="http://localhost:1552/SportsStore/CancelFromPaypal" />
 <input id="notify_url" name="notify_url" type="hidden" value="http://localhost:1552/SportsStore/NotifyFromPaypal" />
 <input id="currency_code" name="currency_code" type="hidden" value="USD" />
 <input id="item_Name" name="item_Name" type="hidden" value="test" />
 <input id="amount" name="amount" type="hidden" value="10" />
 <input type="submit" value="PayPal">
</form>

它工作正常并获得验证状态。

当我运行此代码时:

<form id="frm" action="https://www.sandbox.paypal.com/cgi-bin/webscr">

@*<input id="cmd" name="cmd" type="hidden" value="_xclick" />*@
<input id="cmd" name="cmd" type="hidden" value="_cart"/>
<input type="hidden" name="upload" value="1"/>
<input id="business" name="business" type="hidden" value="anilcs_1361585097_biz@gmail.com" />
@*<input id="no_shipping" name="no_shipping" type="hidden" value="" />*@
<input id="return" name="return" type="hidden" value="http://localhost:1552/Home/IPN" />
<input id="cancel_return" name="cancel_return" type="hidden" value="http://localhost:1552/SportsStore/CancelFromPaypal" />
<input id="notify_url" name="notify_url" type="hidden" value="http://localhost:1552/SportsStore/NotifyFromPaypal" />
<input id="currency_code" name="currency_code" type="hidden" value="USD" />

<input type="hidden" name="item_name_1" value="Football T-Shirt"/>
<input type="hidden" name="quantity_1" value="4"/>
<input type="hidden" name="amount_1" value="1.00"/>
<input type="hidden" name="on0_1" value="Color"/>
<input type="hidden" name="os0_1" value="Red"/>
<input type="hidden" name="on1_1" value="Size"/>
<input type="hidden" name="os1_1" value="Small"/>

<input type="hidden" name="item_name_2" value="Notebook"/>
<input type="hidden" name="quantity_2" value="2"/>
<input type="hidden" name="amount_2" value="2.00"/>
<input type="hidden" name="on0_2" value="Number of Pages"/>
<input type="hidden" name="os0_2" value="200"/>
<input type="hidden" name="on1_2" value="Type"/>
<input type="hidden" name="os1_2" value="3 Ring"/>
<input type="submit" value="PayPal">
</form>

我从

http://www.arunrana.net/2012/01/paypal-integration-in-mvc3-and-razor.html

这是我在 C# 中的代码

public ActionResult IPN()
    {

        try
        {
            var formVals = new Dictionary<string, string>();
            formVals.Add("cmd", "_notify-validate");


            string response = GetPayPalResponse(formVals, true);

            if (response == "VERIFIED")
            {

            }

        }
        catch (Exception ex)
        {
        }

        return View();
    }
    [HttpPost]
    string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox)
    {

        string paypalUrl = useSandbox
                               ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
                               : "https://www.paypal.com/cgi-bin/webscr";


        var req = (HttpWebRequest)WebRequest.Create(paypalUrl);

        // Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

       // byte[] param = Request.BinaryRead(Request.ContentLength);
       // byte[] param = Request.BinaryRead(HttpContext.Request.ContentLength);
        byte[] param = Request.BinaryRead(HttpContext.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);

        var sb = new StringBuilder();
        sb.Append(strRequest);

        foreach (string key in formVals.Keys)
        {
            sb.AppendFormat("&{0}={1}", key, formVals[key]);
        }
        strRequest += sb.ToString();
        req.ContentLength = strRequest.Length;

        //for proxy
        //WebProxy proxy = new WebProxy(new Uri("http://urlort#");
        //req.Proxy = proxy;
        //Send the request to PayPal and get the response
        string response = "";
        using (var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII))
        {
            streamOut.Write(strRequest);
            streamOut.Close();
            using (var streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                response = streamIn.ReadToEnd();
            }
        }

        return response;
    }
}
4

0 回答 0