简体   繁体   English

获得Paypal沙箱的回应

[英]Get response from paypal sandbox

I am trying to integrate the paypal sandbox with my asp.net code. 我正在尝试将贝宝沙箱与我的asp.net代码集成在一起。 The requirement is that i have to take the user to the paypal site , the user will login with his credential and pay a designated amount. 要求是我必须将用户带到Paypal网站,用户将使用其凭据登录并支付指定的金额。

Now i am able to take the user to the paypal site and get the transaction done sucessfully as per my code below. 现在,我可以将用户带到Paypal网站,并按照下面的代码成功完成交易。 My next step is to get the response from the paypal. 我的下一步是从贝宝那里得到答复。 In the response i require the status and also i will send an uniqueid while redirecting to the paypal site which i want in the response too so that my application can identify from which user the response has arrived. 在响应中,我需要状态,并且在重定向到我也想要在响应中的贝宝站点时,我还将发送唯一标识,以便我的应用程序可以标识响应来自哪个用户。

private void PayJNP()
    {
        try
        {
            string redirectUrl = "";
            redirectUrl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["paypalemail"].ToString();
            redirectUrl += "&first_name=ATPTrader";
            redirectUrl += "&item_name=JNP";
            redirectUrl += "&amount=100";
            redirectUrl += "&return=" + ConfigurationManager.AppSettings["SuccessURL"].ToString();
            redirectUrl += "&cancel_return=" + ConfigurationManager.AppSettings["FailedURL"].ToString();
            Response.Redirect(redirectUrl);
        }
        catch (Exception Ex)
        {

        }
    }

Please help me to capture the response. 请帮助我捕获响应。

TIA TIA


I have started implementing the IPN but i am getting some issues 我已经开始实施IPN,但是遇到了一些问题

I have configured IPN in the paypal site 我已经在贝宝网站中配置了IPN

From one from thru some hidden controls i am sending data to the paypal sandbox 从一些隐藏的控件中,我正在将数据发送到贝宝沙箱

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="form1"
        name="form1">
        <input type="hidden" name="cmd" value="_xclick"/>
        <input type="hidden" name="business" value="shantanusenin@gmail.com"/><!--Paypal or sandbox Merchant account -->
        <input type="hidden" name="item_name" value="JNP"/>
        <input type="hidden" name="item_number" value="1"/>
        <input type="hidden" name="amount" value="100"/>
        <input type="hidden" name="return" value="http://alltradepartners.com/test/paypal.aspx"/><!--this page will be your redirection page -->
        <input type="hidden" name="cancel_return" value="http://alltradepartners.com/test/partnerregistration.aspx"/>
        <input type="hidden" name="currency_code" value="USD"/>
        <input type="hidden" name="notify_url" value="http://alltradepartners.com/test/paypal.aspx"/><!--this should be your domain web page where you going to receive all your transaction variables -->
    </form>

now i have a page which collects the response 现在我有一个收集响应的页面

//Post back to either sandbox or live
        string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        // string strLive = "https://www.paypal.com/cgi-bin/webscr";
        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(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        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 (strResponse == "VERIFIED")
        {

            Response.Write("VERIFIED");
            //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine(strResponse);
            //txWriter.Close();

            //check the payment_status is Completed
            //check that txn_id has not been previously processed
            //check that receiver_email is your Primary PayPal email
            //check that payment_amount/payment_currency are correct
            //process payment





            NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse);
            string user_email = these_argies["payer_email"];
            string pay_stat = these_argies["payment_status"];
            //.
            //.  more args as needed look at the list from paypal IPN doc
            //.


            if (pay_stat.Equals("Completed"))
            {
                Response.Write("Completed");
                //Send_download_link("yours_truly@mycompany.com", user_email, "Your order", "Thanks for your order this the downnload link ... blah blah blah");
            }       

        }
        else if (strResponse == "INVALID")
        {
            Response.Write("INVALID");
            //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine(strResponse);
            ////log for manual investigation
            //txWriter.Close();
        }
        else
        {
            Response.Write("SUCCESS");
            //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine("Invalid");
            ////log response/ipn data for manual investigation
            //txWriter.Close();
        }

Now i am able to send the request and getting the response as VERIFIED, but i am not getting the tranactionID. 现在,我能够发送请求并获得已验证的响应,但是我没有获得tranactionID。 Please suggest where i am missing 请提出我想念的地方

TIA TIA

I'd recommend you use Instant Payment Notification (IPN). 建议您使用即时付款通知(IPN)。 With IPN you'll get a post back after every transaction to your set notification URL that tells you whether or not a payment has completed. 使用IPN,每次交易后,您都会在设置的通知URL上回发帖子,该URL告诉您付款是否完成。

You'll define the notification URL using the notify_url variable. 您将使用notify_url变量定义通知URL。

Here is a link to our IPN code samples: https://github.com/paypal/ipn-code-samples 这是我们IPN代码示例的链接: https : //github.com/paypal/ipn-code-samples

If you have a specific question about the service I can help you out further. 如果您对服务有特定疑问,我可以为您提供进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM