Paypal Express Checkout集成的问题(WEBREQUEST)

时间:2015-06-24 01:13:36

标签: asp.net vb.net paypal-sandbox paypal

因此,我一直在努力使用PayPal文档制作头或尾,并且总觉得我的Webrequest出现了一些不正确的事情。

所以我将所有代码都删除回基本,只需通过HTTP提交请求,PLUS方面就是我现在从PayPal沙箱服务器获得回复,其中ACK=SuccessTOKEN=Valid-token-value-here有还返回了一些其他变量,例如CORRELATIONID和TIMESTAMP。

因此我尝试了一些webrequest示例,我只是得到一个空白屏幕,而不是被重定向到Paypal,以便(沙盒)客户完成付款。

因此,如果任何人都可以发布他们的WebRequest方法,那就太棒了。

这是我用于webrequest的代码,我确定它错了但无法确定它出错的地方。

此外,当我在调试期间在localhost上运行代码时,一切正常,并且通过SUCCESS完成调用并收到TOKEN。

当我实时运行时,我在错误异常中收到错误编号5,并在状态描述中收到文件“远程主机无法连接”。

这是更新的代码

Function MakeWebRequest(ByVal pUseSandbox As Boolean, ByVal pRequestMethod As String, ByVal pReturnUrl As String, ByVal pCancelUrl As String, ByRef pRtnStatus As String, ByRef pRtnStatusId As HttpStatusCode, ByRef pRtnResponseString As String) As Boolean
'
Dim _sxHost As String = Nothing
Dim _sxEndpoint As String = Nothing
Dim _sxNameValCol As System.Collections.Specialized.NameValueCollection = Nothing
Dim _sxResponseCol As System.Collections.Specialized.NameValueCollection = Nothing
Dim _sxCounta As Integer = Nothing
Dim _sxParamsString As String = Nothing
'
'-> Init
_sxParamsString = ""
MakeWebRequest = False
_sxNameValCol = New System.Collections.Specialized.NameValueCollection()
_sxResponseCol = New System.Collections.Specialized.NameValueCollection()
If pUseSandbox Then
  _sxHost = "http://www.sandbox.paypal.com"
  _sxEndpoint = "https://api-3t.sandbox.paypal.com/nvp"
Else
  _sxHost = "http://www.paypal.com"
  _sxEndpoint = "https://api-3t.paypal.com/nvp"
End If
'-> Create Request
Try
  '-> Key/Value Collection Params
  _sxNameValCol.Add("METHOD", "SetExpressCheckout")
  _sxNameValCol.Add("USER", _UserName)
  _sxNameValCol.Add("PWD", _Password)
  _sxNameValCol.Add("SIGNATURE", _Signature)
  _sxNameValCol.Add("PAYMENTREQUEST_0_AMT", Format(_Basket.BasketTotalIncDelivery / 100, "0.00"))
  _sxNameValCol.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale")
  _sxNameValCol.Add("PAYMENTREQUEST_0_CURRENCYCODE", "GBP")
  _sxNameValCol.Add("RETURNURL", pReturnUrl)
  _sxNameValCol.Add("CANCELURL", pCancelUrl)
  _sxNameValCol.Add("REQCONFIRMSHIPPING", "0")
  _sxNameValCol.Add("NOSHIPPING", "2")
  _sxNameValCol.Add("LOCALECODE", "EN")
  _sxNameValCol.Add("BUTTONSOURCE", "PP-ECWizard")
  _sxNameValCol.Add("VERSION", "93.0")
  '-> UrlEncode
  For _sxCounta = 0 To _sxNameValCol.Count - 1
    If _sxCounta = 0 Then
      _sxParamsString = _sxParamsString & _sxNameValCol.Keys(_sxCounta) & "=" & HttpUtility.UrlEncode(_sxNameValCol(_sxCounta))
    Else
      _sxParamsString = _sxParamsString & "&" & _sxNameValCol.Keys(_sxCounta) & "=" & HttpUtility.UrlEncode(_sxNameValCol(_sxCounta))
    End If
  Next
  '-> Credentials (not used)
  '_sxRequest.Credentials = CredentialCache.DefaultCredentials
  Try
    Dim _sxRequest As WebRequest = DirectCast(System.Net.WebRequest.Create(_sxEndpoint), System.Net.HttpWebRequest)
    '-> Convert request to byte-array
    Dim _sxByteArray As Byte() = Encoding.UTF8.GetBytes(_sxParamsString)
    _sxRequest.Method = "POST"                                                      'Our method is post, otherwise the buffer (_sxParamsString) would be useless
    _sxRequest.ContentType = "application/x-www-form-urlencoded"                    'We use form contentType, for the postvars
    _sxRequest.ContentLength = _sxByteArray.Length                                  'The length of the buffer (postvars) is used as contentlength
    Dim _sxPostDataStream As System.IO.Stream = _sxRequest.GetRequestStream()       'We open a stream for writing the postvars
    _sxPostDataStream.Write(_sxByteArray, 0, _sxByteArray.Length)                   'Now we write, and afterwards, we close
    _sxPostDataStream.Close()                                                       'Closing is always important!
    '-> Create Response
    Dim _sxResponse As HttpWebResponse = DirectCast(_sxRequest.GetResponse(), HttpWebResponse)
    '-> Get Response Status
    pRtnStatus = _sxResponse.StatusDescription
    pRtnStatusId = _sxResponse.StatusCode
    '-> Reponse Stream
    Dim _sxResponseStream As Stream = _sxResponse.GetResponseStream()               'Open a stream to the response
    '-> Response Stream Reader
    Dim _sxStreamReader As New StreamReader(_sxResponseStream)                      'Open as reader for the stream 
    pRtnResponseString = _sxStreamReader.ReadToEnd()                                'Read the response string
    MakeWebRequest = True
    '-> Tidy up
    _sxStreamReader.Close()
    _sxResponseStream.Close()
    _sxResponse.Close()
    _sxByteArray = Nothing
    _sxPostDataStream = Nothing
    _sxRequest = Nothing
    _sxResponse = Nothing
    _sxResponseStream = Nothing
    _sxStreamReader = Nothing
  Catch ex As Exception
    pRtnStatusId = Err.Number
    pRtnStatus = "response(" & ex.Message & ")"
    Decode(pRtnResponseString, _sxResponseCol)
    pRtnResponseString = Stringify(_sxResponseCol)
  End Try
Catch ex As Exception
  pRtnStatusId = Err.Number
  pRtnStatus = "request(" & ex.Message & ")"
  Decode(pRtnResponseString, _sxResponseCol)
  pRtnResponseString = Stringify(_sxResponseCol)
End Try
'-> Tidy Up
_sxHost = Nothing
_sxEndpoint = Nothing
_sxNameValCol = Nothing
_sxResponseCol = Nothing
_sxCounta = Nothing
_sxParamsString = Nothing
'
End Function

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,现在很清楚,您没有收到服务器的任何回复,因为您的服务器根本无法连接到PayPal的服务器。因此,您没有服务器响应和消息Unable to connect to the remote server。当我测试时,我得到了以下正文的HTTP 200响应:

TIMESTAMP=2015-07-07T09:07:39Z&CORRELATIONID=7f4d2313c9696&ACK=Failure&VERSION=93.0&BUILD=17276661&L_ERRORCODE0=10002&L_SHORTMESSAGE0=Authentication/Authorization Failed&L_LONGMESSAGE0=You do not have permissions to make this API call&L_SEVERITYCODE0=Error

显然那是因为我测试了一个空白的用户名和密码。

因此,您的服务器设置出现问题,导致您无法在IIS级别或防火墙配置进行外部连接。

如果没有物理存在于您的计算机上,我们可以做很多事情来追踪阻止它的内容,但您可以尝试向其他公共网站(如Google.com)打开HTTP请求,看看这些是否成功。