【问题标题】:How do invoke the Paypal IPN using the REST API c# .net如何使用 REST API c# .net 调用 Paypal IPN
【发布时间】:2023-03-07 14:18:01
【问题描述】:

我在调用 PayPal IPN 时遇到问题。我不知道要给出哪个 URL 或我打算给出哪个 URL。我在互联网上到处寻找帮助,但似乎没有任何可用的东西,因此我来这里。

首先,我有 PaymentWithPaypal 操作

public ActionResult PaymentWithPaypal(int? id, Page page)
    {  
        //getting the apiContext as earlier
        APIContext apiContext = Models.Configuration.GetAPIContext();

        try
        {        
            string payerId = Request.Params["PayerID"];

            if (string.IsNullOrEmpty(payerId))
            {

                string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/ControllerName/PaymentWithPayPal?";


                var guid = Guid.NewGuid().ToString();

                //CreatePayment function gives us the payment approval url

                //on which payer is redirected for paypal acccount payment
                var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);

                //get links returned from paypal in response to Create function call

                var links = createdPayment.links.GetEnumerator();

                string paypalRedirectUrl = null;

                while (links.MoveNext())
                {
                    Links lnk = links.Current;

                    if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                    {
                        //saving the payapalredirect URL to which user will be redirected for payment
                        paypalRedirectUrl = lnk.href;
                    }
                }

                // saving the paymentID in the key guid
                Session.Add(guid, createdPayment.id);

                return Redirect(paypalRedirectUrl);
            }
            else
            {

                // This section is executed when we have received all the payments parameters

                // from the previous call to the function Create

                // Executing a payment

                var guid = Request.Params["guid"];

                var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                if (executedPayment.state.ToLower() != "approved")
                {
                    return View("FailureView");
                }

            }
        }
        catch (Exception ex)
        {
            Logger.Log("Error" + ex.Message);
            return View("FailureView");
        }
        return View("SuccessView");
    }

这是 IPN 的代码。

[HttpPost]
    public HttpStatusCodeResult Receive()
    {
        //Store the IPN received from PayPal
        LogRequest(Request);

        //Fire and forget verification task
        Task.Run(() => VerifyTask(Request));

        //Reply back a 200 code
        return new HttpStatusCodeResult(HttpStatusCode.OK);

    }

    private void VerifyTask(HttpRequestBase ipnRequest)
    {
        var verificationResponse = string.Empty;

        try
        {
            var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");

            //Set values for the verification request
            verificationRequest.Method = "POST";
            verificationRequest.ContentType = "application/x-www-form-urlencoded";
            var param = Request.BinaryRead(ipnRequest.ContentLength);
            var strRequest = Encoding.ASCII.GetString(param);

            //Add cmd=_notify-validate to the payload
            strRequest = "cmd=_notify-validate&" + strRequest;
            verificationRequest.ContentLength = strRequest.Length;

            //Attach payload to the verification request
            var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();

            //Send the request to PayPal and get the response
            var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();

        }
        catch (Exception exception)
        {
            Logger.Log("Error" + exception.Message);
            //Capture exception for manual investigation
        }

        ProcessVerificationResponse(verificationResponse);

    }


    private void LogRequest(HttpRequestBase request)
    {
        // Persist the request values into a database or temporary data store
    }

    private void ProcessVerificationResponse(string verificationResponse)
    {
        if (verificationResponse.Equals("VERIFIED"))
        {
            Logger.Log("Verified");
            // check that Payment_status=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
        }
        else if (verificationResponse.Equals("INVALID"))
        {
            Logger.Log(verificationResponse);
        }
        else
        {
            //Log error
        }
    }

现在来澄清一下。我对 IPN 的理解是,当客户购买商品时,SELLER 会收到一封电子邮件,告诉他们他们已售出产品,然后您可以从中访问 transactionId 等。

所以在我看来,我有一个带有按钮的表单,看起来像这样。

@Html.ActionLink("Buy Now", "PaymentWithPaypal", new { Id = Model.Id, @class = "" })

这是将客户带到贝宝的原因,然后他们可以在那里购买,但这是我被卡住的地方,因为我不确定如何调用 IPN 或者它是否需要自己的视图。

此时此刻,任何澄清都会有很大帮助。

【问题讨论】:

  • SHOUTING有什么特殊原因吗?
  • AFAIK IPN 不会向卖家或客户发送任何电子邮件,它只是一种获取已完成付款的提要的方法,然后您可以使用 IPN 中的信息在您的网站上更新您的订单状态消息。
  • @Anaheim 那么这是否意味着卖家和客户在购买时会收到一封电子邮件?
  • 双方(卖家和买家)都应该收到的电子邮件,但这与 IPN 无关。

标签: c# .net paypal-ipn paypal-rest-sdk


【解决方案1】:

一种方法是将其置于 PayPal 帐户设置下。单击“应用程序”后,您会在其下方看到重定向 url 选项。只需在此处添加即可。 Paypal .net sdk 没有传递notify_url 的选项。所有其他模式都有。因为,paypal.net sdk 接受return_url,这通常与您的代码中提到的操作方法相同。

检查这个: https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNSetup/#

如果你想实现实时事件,你现在需要使用 webhook。以下文档: https://github.com/paypal/PayPal-NET-SDK/wiki/Webhook-Event-Validation

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 2014-07-19
    • 2014-08-05
    • 2013-09-26
    • 2013-09-04
    • 2020-07-14
    • 2012-03-26
    相关资源
    最近更新 更多