从PayPal SOAP API获取交易详情(希望也是自定义字段)

时间:2016-08-02 17:23:20

标签: paypal paypal-rest-sdk paypal-soap

我试图从PayPal的SOAP API获取交易详情,但我遇到了来自PayPal的错误。它返回错误:

The transaction id is not valid

我知道它是一个有效的交易ID,因为我能够使用PayPal NVP API获取交易详情,但可能我的SOAP请求格式不正确。我遵循了可以在Web上找到的PayPal SOAP API示例来构建SOAP XML,但它们很小。我使用mac的PAW程序向PayPal的API发送SOAP请求,但这个问题应该可以使用HTTP客户端重现。

我尝试使用PayPal的REST API但there's no method to get transaction details(例如姓名,电子邮件,付款金额,自定义字段)。我还尝试使用NVP (Name-Value-Pair) API,我确实收到了交易详情,但它没有给我所有存储的交易自定义字段。当我登录PayPal并查看单个交易时,我可以看到该交易的所有自定义字段,因此我知道它们已被存储。

SOAP API是我最后的希望。

这是我使用的SOAP信封请求:

 <?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="urn:ebay:api:PayPalAPI" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:ed="urn:ebay:apis:EnhancedDataTypes">
      <soapenv:Header>
           <ns:RequesterCredentials>
                <ebl:Credentials>
                     <ebl:Username>soap_api_username_here</ebl:Username>
                     <ebl:Password>soap_api_password_here</ebl:Password>
                     <ebl:Signature>soap_api_signature_here</ebl:Signature>
                </ebl:Credentials>
           </ns:RequesterCredentials>
      </soapenv:Header>
      <soapenv:Body>
           <ns:GetTransactionDetailsReq>
                <ns:GetTransactionDetailsRequest>
                     <ebl:Version>93.0</ebl:Version>
                     <ebl:TransactionID>8FX18476NR449891W</ebl:TransactionID>
                </ns:GetTransactionDetailsRequest>
           </ns:GetTransactionDetailsReq>
      </soapenv:Body>
 </soapenv:Envelope>

以下是我从PayPal SOAP API收到的回复:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ed="urn:ebay:apis:EnhancedDataTypes" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI">
      <SOAP-ENV:Header>
        <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"/>
        <RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
          <Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
            <Username xsi:type="xs:string"/>
            <Password xsi:type="xs:string"/>
            <Signature xsi:type="xs:string"/>
            <Subject xsi:type="xs:string"/>
          </Credentials>
        </RequesterCredentials>
      </SOAP-ENV:Header>
      <SOAP-ENV:Body id="_0">
        <GetTransactionDetailsResponse xmlns="urn:ebay:api:PayPalAPI">
          <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2016-08-02T16:43:02Z</Timestamp>
          <Ack xmlns="urn:ebay:apis:eBLBaseComponents">Failure</Ack>
          <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">a464c181339f4</CorrelationID>
          <Errors xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:ErrorType">
            <ShortMessage xsi:type="xs:string">Transaction refused because of an invalid argument. See additional error messages for details.</ShortMessage>
            <LongMessage xsi:type="xs:string">The transaction id is not valid</LongMessage>
            <ErrorCode xsi:type="xs:token">10004</ErrorCode>
            <SeverityCode xmlns="urn:ebay:apis:eBLBaseComponents">Error</SeverityCode>
          </Errors>
          <Version xmlns="urn:ebay:apis:eBLBaseComponents">93.0</Version>
          <Build xmlns="urn:ebay:apis:eBLBaseComponents">000000</Build>
          <PaymentTransactionDetails xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:PaymentTransactionType">
            <ReceiverInfo xsi:type="ebl:ReceiverInfoType"/>
            <PayerInfo xsi:type="ebl:PayerInfoType">
              <PayerStatus xsi:type="ebl:PayPalUserStatusCodeType">verified</PayerStatus>
              <PayerName xsi:type="ebl:PersonNameType"/>
              <Address xsi:type="ebl:AddressType">
                <AddressOwner xsi:type="ebl:AddressOwnerCodeType">PayPal</AddressOwner>
                <AddressStatus xsi:type="ebl:AddressStatusCodeType">None</AddressStatus>
              </Address>
            </PayerInfo>
            <PaymentInfo xsi:type="ebl:PaymentInfoType">
              <TransactionType xsi:type="ebl:PaymentTransactionCodeType">none</TransactionType>
              <PaymentType xsi:type="ebl:PaymentCodeType">none</PaymentType>
              <PaymentStatus xsi:type="ebl:PaymentStatusCodeType">None</PaymentStatus>
              <PendingReason xsi:type="ebl:PendingStatusCodeType">none</PendingReason>
              <ReasonCode xsi:type="ebl:ReversalReasonCodeType">none</ReasonCode>
            </PaymentInfo>
            <PaymentItemInfo xsi:type="ebl:PaymentItemInfoType">
              <Subscription xsi:type="ebl:SubscriptionInfoType"/>
              <Auction xsi:type="ebl:AuctionInfoType"/>
            </PaymentItemInfo>
          </PaymentTransactionDetails>
        </GetTransactionDetailsResponse>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我不确定我做错了什么,但也许有经验丰富的人可以比我更容易发现错误。我回答了另一个用户关于PayPal REST API的问题,并从中获取了交易详情,但据我所知,没有办法做到Get customer details after transaction。如果您已经找到了使用REST API实现此目的的方法,请告诉我因为我宁愿使用REST而不是SOAP。

感谢。

2 个答案:

答案 0 :(得分:1)

错误消息是红鲱鱼。 PayPal无法找到任何TransactionId,将空的TransactionId报告为无效。

TransactionId元素属于urn:ebay:api:PayPalAPI命名空间。您的XML将其附加到urn:ebay:apis:eBLBaseComponents命名空间。您需要做的就是将ebl:TransactionId更改为ns:TransactionId

 <?xml version="1.0" encoding="UTF-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="urn:ebay:api:PayPalAPI" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:ed="urn:ebay:apis:EnhancedDataTypes">
  <soapenv:Header>
       <ns:RequesterCredentials>
            <ebl:Credentials>
                 <ebl:Username>soap_api_username_here</ebl:Username>
                 <ebl:Password>soap_api_password_here</ebl:Password>
                 <ebl:Signature>soap_api_signature_here</ebl:Signature>
            </ebl:Credentials>
       </ns:RequesterCredentials>
  </soapenv:Header>
  <soapenv:Body>
      <ns:GetTransactionDetailsReq>
           <ns:GetTransactionDetailsRequest>
                <ebl:Version>204.0</ebl:Version>
                <ns:TransactionID>8FX18476NR449891W</ns:TransactionID>
           </ns:GetTransactionDetailsRequest>
      </ns:GetTransactionDetailsReq>
 </soapenv:Body>

选中example at the PayPal KB

答案 1 :(得分:0)

我会把它作为评论,但它充满了goobly gook。以牺牲方式为代价,我可能会有所帮助。 所以我的vb.net代码向paypal发送一个表单,其中包括&#34; notify_url&#34;。

            ' determining the URL to work with depending on whether sandbox or a real PayPal account should be used
            If strRealOrSand = "Sand" Then
                URL = "https://www.sandbox.paypal.com/cgi-bin/webscr"
                business = "x@xxx.com"  ' AppSettings("BusinessEmail")  (sandbox account business email)
            ElseIf strRealOrSand = "Real" Then
                URL = "https://www.paypal.com/cgi-bin/webscr"
                business = "xxx@xxx.com"    ' AppSettings("BusinessEmail")  (real pay pal account account business email)
            End If

notify_url = "http://www.xxxxx.com/accounts/done.aspx"  



    <form id="payForm" method="post" action="<%Response.Write (URL)%>" >
        <input type="hidden" name="cmd" value="<%Response.Write (cmd)%>" />
        <input type="hidden" name="business" value="<%Response.Write (business)%>" />
        <input type="hidden" name="item_name" value="<%Response.Write (item_name)%>" />
        <input type="hidden" name="amount" value="<%Response.Write (amount)%>" />
        <input type="hidden" name="no_shipping" value="<%Response.Write (no_shipping)%>" />
        <input type="hidden" name="return" value="<%Response.Write (return_url)%>" />
        <input type="hidden" name="rm" value="<%Response.Write (rm)%>" />
        <input type="hidden" name="notify_url" value="<%Response.Write (notify_url)%>" />
        <input type="hidden" name="cancel_return" value="<%Response.Write (cancel_url)%>" />
        <input type="hidden" name="currency_code" value="<%Response.Write (currency_code)%>" />
        <input type="hidden" name="custom" value="<%Response.Write (row_id)%>" />
    </form>

    <script type="text/javascript">
        document.forms["payForm"].submit ();
    </script>

</body>

然后在我的&#34; notify_url&#34;页面加载它获取PayPal在事务完成后发回的内容。您会注意到有大量的调试电子邮件被发回给我。这是我的行为。

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim vReceived As String = ""
        Dim vFormValues As String = ""
        Dim vResponder As String = ""
        Dim strE As String = ""
        Dim intAccountID As Integer = 0
        Dim intCreditsPurchased As Integer = 0

        Dim vWebRequest As System.Net.HttpWebRequest
        Dim vSSend As Boolean = False
        'vWebRequest = CType(System.Net.WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), System.Net.HttpWebRequest)
        vWebRequest = CType(System.Net.WebRequest.Create("https://www.paypal.com/cgi-bin/webscr"), System.Net.HttpWebRequest)



        vWebRequest.Method = "POST"
        vWebRequest.ServicePoint.Expect100Continue = False
        vWebRequest.ContentType = "application/x-www-form-urlencoded"



        Try
            vFormValues = Encoding.ASCII.GetString(Request.BinaryRead(Request.ContentLength))
            vReceived = "cmd=_notify-validate&" & vFormValues
        Catch ex As Exception
            sSend_Mail_From_done_aspx("Requested Form.ToString", ex.ToString)
        End Try

        vWebRequest.ContentLength = vReceived.Length
        Dim vStreamOut As System.IO.StreamWriter = New System.IO.StreamWriter(vWebRequest.GetRequestStream(), Encoding.ASCII)
        vStreamOut.Write(vReceived)
        vStreamOut.Close()
        Dim vStreamIn As New System.IO.StreamReader(vWebRequest.GetResponse().GetResponseStream())
        vResponder = vStreamIn.ReadToEnd()
        vStreamIn.Close()



        Dim vFieldName As String
        Dim vFieldValue As String = ""
        Dim vFields As New Collection

        For Each vFieldName In Request.Form
            'strE = strE & Request.Form.Item(vFieldName) & " "
            strE &= vFieldName & ": " & Request.Form.Item(vFieldName) & vbCrLf
            'vFieldValue = Request.Form.Item(vFieldName)
            'vFields.Add(Decode(vFieldValue), Decode(vFieldName).ToLower)
        Next

        sSend_Mail_From_done_aspx("BEFORE VERIFIED CHECK, WHAT vResponder CONTAINS", strE)


        Dim transactionID As String = ""
        Dim dblAmount As Double
        Dim intRowID As Integer
        Dim dblPayPalFee As Double

        Dim strMemo As String = ""
        Dim strPayerEmail As String = ""
        Dim strPaymentDate As String = ""

        If Trim(vResponder).ToUpper = "VERIFIED" Then

            For Each vFieldName In Request.Form
                strE &= vFieldName & ": " & Request.Form.Item(vFieldName) & vbCrLf

            Next

            sSend_Mail_From_done_aspx("VERIFIED CAME THROUGH", strE)

            Try
                transactionID = Request.Form.Item("txn_id").ToString()
                dblAmount = CType(Request.Form.Item("payment_gross"), Double)
                intRowID = CType(Request.Form.Item("custom"), Integer)
                dblPayPalFee = CType(Request.Form.Item("payment_fee"), Double)
                If Len(Request.Form.Item("memo") & "") <> 0 Then
                    strMemo = Request.Form.Item("memo").ToString.Replace("'", "")
                End If
                strPayerEmail = Request.Form.Item("payer_email").ToString
                strPaymentDate = Request.Form.Item("payment_date").ToString


            Catch ex As Exception
                sSend_Mail_From_done_aspx("Request.QueryString)", ex.ToString)
            End Try


            Try

                '==============================
                'SAVE TRANSACTION INFO HERE
                '==============================
                Dim sb As New StringBuilder
                sb.Append("UPDATE dbo.tblTransactions ")
                sb.Append("SET pp_txn_id='" & transactionID & "'")
                sb.Append(", pp_payment_gross=" & dblAmount)
                sb.Append(", pp_mc_fee=" & dblPayPalFee)
                sb.Append(", memo='" & strMemo & "'")
                sb.Append(", pp_payer_email='" & strPayerEmail & "'")
                sb.Append(", pp_payment_date='" & strPaymentDate & "'")
                sb.Append(" WHERE RowID =" & intRowID)

                'SEND SQL TO DEVELOPER
                sSend_Mail_From_done_aspx("INSERT statement BEFORE action", sb.ToString)

                Dim conn As New SqlConnection(f1.fUseThisConnection(Server.MachineName))
                Dim cmd As New SqlCommand(sb.ToString, conn)

                cmd.Connection.Open()
                cmd.ExecuteNonQuery()

                '=========================================================
                'UPDATE ACCOUNT INFO HERE (ADD CREDITS TO CURRENT AMOUNT)
                '=========================================================
                Dim dr As System.Data.SqlClient.SqlDataReader


                sb.Length = 0
                'GET ACCOUNT ID, AND CreditsPurchased
                sb.Append("SELECT AccountID, CreditsPurchased FROM dbo.tblTransactions ")
                sb.Append(" WHERE RowID =" & intRowID)

                cmd.CommandText = sb.ToString
                dr = cmd.ExecuteReader()

                If dr.Read() Then

                    intAccountID = dr("AccountID")
                    intCreditsPurchased = dr("CreditsPurchased")


                End If

                dr.Close()


                sb.Length = 0

                'AT 1.10 A CREDIT, WE DIVIDE THE AMOUNT PAYPAY CHARGES BY THIS TO GET CREDITS PURCHASED

                sb.Append("UPDATE dbo.tblAL SET Credits = Credits + " & intCreditsPurchased)
                sb.Append(" WHERE AccountID =" & intAccountID)

                cmd.CommandText = sb.ToString
                cmd.ExecuteNonQuery()

                'SEND ACCOUNT UPDATE AMOUNT TO DEVELOPER
                sSend_Mail_From_done_aspx(intCreditsPurchased & " CREDITS ADDED TO ", sb.ToString)

                cmd.Dispose()
                conn.Close()
                conn = Nothing

                MySession.Credits = intCreditsPurchased


            Catch ex As Exception
                sSend_Mail_From_done_aspx("tblTransactions NOT UPDATED with Transaction Info", ex.ToString())
            End Try

        Else
            sSend_Mail_From_done_aspx(Trim(vResponder).ToUpper, "TRANSACTION NOT VERIFIED")

        End If
    End Sub

我认为请求/回复如下:

提交表单

URL = "https://www.paypal.com/cgi-bin/webscr"

发回&#34; notify_url&#34;已提交

Dim vWebRequest As System.Net.HttpWebRequest
            Dim vSSend As Boolean = False
            'vWebRequest = CType(System.Net.WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), System.Net.HttpWebRequest)
            vWebRequest = CType(System.Net.WebRequest.Create("https://www.paypal.com/cgi-bin/webscr"), System.Net.HttpWebRequest)

肯定会有一个&#34; PayPalFiddle&#34;哈哈