asp.net - asp.net 应用程序中的 paypal ipn 集成

标签 asp.net paypal c#-3.0

我正在尝试将 paypal ipn 集成到我的应用程序中,但我收到类似“System.dll 中出现类型为‘System.Net.WebException’的异常”的错误,但未在 streamwriter 的用户代码中处理。我已经尝试过下面的代码。请提出建议。

using System;
using System.IO;
using System.Text;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class ipn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //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(strLive);

        //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;

        //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")
        {
            //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

        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation

        }
        else
        {
            //log response/ipn data for manual investigation
        }

    }
}

最佳答案

string Identity_Token = "zkiMk_t - XXXXXagbIDrzPAChXuWIYVS66TXXXXXXXXXXXXXXXXXXXXXXXXXX";//From you paypal account

        string txToken = Request.QueryString["tx"];

        string query = string.Format("cmd=_notify-synch&tx={0}&at={1}", txToken, Identity_Token);

        string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);
        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 += query;
        req.ContentLength = strRequest.Length;


        //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 response was SUCCESS, parse response string and output details
        if (strResponse.StartsWith("SUCCESS"))
        {


            Label1.Text = "OK";
        }
        else
        {
            Label1.Text = "NO";
        }
    }
}

关于asp.net - asp.net 应用程序中的 paypal ipn 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37253785/

相关文章:

c#-4.0 - 如何在 C# 中捕获未处理的异常?

asp.net - 从 Office Docs/PDFS 转换为 HTML

asp.net - IIS Express 为每个请求加载和卸载模块

paypal - 如何使用计费协议(protocol)收取首付款?

node.js - nodejs Paypal pdt 返回 302

c#-3.0 - 将日期转换为 "dd-MMM-yyyy"格式 c#

c# - 将图像/文件从 Angular 7 上传到 .net core

javascript - asp.net AjaxControlToolkit CalendarExtender : setting displayed month programmatically from client side

java - 使用 Paypal REST API 时需要 API 证书

dynamic - 使用 .NET 3.5 API 访问 Facebook C# SDK 结果对象?