Paypal Express Checkout 集成的问题 (WEBREQUEST)

Posted

技术标签:

【中文标题】Paypal Express Checkout 集成的问题 (WEBREQUEST)【英文标题】:Problems With Paypal Express Checkout Integration (WEBREQUEST) 【发布时间】:2015-09-10 00:47:29 【问题描述】:

所以我一直在努力从 PayPal 文档中找出问题所在,并且总是觉得我的 Webrequest 有问题。

所以我将所有代码剥离回基本代码,并简单地通过 HTTP 提交请求,而 PLUS 方面是我现在从 PayPal 沙箱服务器收到响应,其中 ACK=SuccessTOKEN=Valid-token-value-here 还返回了一些其他变量也可以使用 CORRELATIONID 和 TIMESTAMP。

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

因此,如果有人可以发布他们的 WebRequest 方法,那就太好了。

这是我用于网络请求的代码,我确定它是错误的,但无法确定哪里出错了。

此外,当我在调试期间在本地主机上运行代码时,一切正常,调用成功完成并收到了一个令牌。

当我实时运行它时,我在错误异常中收到错误编号 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

【问题讨论】:

空白页的 HTTP 响应代码是什么? 我已经更新了我的问题,请参阅上面的错误信息。 【参考方案1】:

好的,现在很明显,您没有收到来自服务器的任何响应,因为您的服务器根本无法连接到 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 请求,看看是否成功。

【讨论】:

你能告诉我如何打开另一个公共网站的 HTTP 请求吗?谢谢 您可能想查看this 并确保捕获异常以找到原因。 嗨,BoffinbraiN,感谢您帮助我。由于时间紧迫,我一直没有找出问题所在,我外包了贝宝集成并支付了费用以解决问题。鉴于您是唯一愿意提供帮助的人,因此我将您的答案标记为正确答案并奖励您 50 点声望。谢谢你的帮助。 @ZafKhan 祝你好运!我希望外包的代码质量很好,这样你就有可能从中学到一些技巧。

以上是关于Paypal Express Checkout 集成的问题 (WEBREQUEST)的主要内容,如果未能解决你的问题,请参考以下文章

php 更改YITH PayPal Express Checkout for WooCommerce的Set Express Checkout Request参数

PayPal Express Checkout 通行证 transactionID

如何访问 Paypal Express Checkout 的响应?

如何为 PayPal Express Checkout 配置 IPN?

paypal express checkout - 400 错误请求

Paypal Express Checkout / React - 发送产品信息