Paypal Express Checkout集成的问题(WEBREQUEST)

Zed*_*ddy 5 vb.net asp.net paypal paypal-sandbox

因此,我一直在努力从PayPal文档中删除头部或尾部,并且总觉得我的Webrequest有些不对劲.

所以我剥夺所有的代码回到基本,简单地提交通过HTTP与加方的要求是,我现在得到回应从那里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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bof*_*ain 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
Run Code Online (Sandbox Code Playgroud)

显然这是因为我使用空白用户名和密码进行测试。

因此,您的服务器设置存在问题,导致您无法建立外部连接,无论是在 IIS 级别还是由于您的防火墙配置。

如果没有亲自出现在您的计算机上,我们无法做太多事情来追踪阻止它的原因,但您可以尝试打开对其他公共网站(例如 Google.com)的 HTTP 请求,看看是否成功。