PayPal SOAP API reponses require manual parsing

瘦欲@ 提交于 2020-01-05 02:53:39

问题


I am currently working on a web project that includes implementing PayPal's Express Checkout as payment service. The project is in C#, I am using Visual Studio 2010 and the SOAP version of the PayPal API. The version of the API I am using is 91.0 (tried switching this to other versions but the problem remains) and I am working in the sandbox to do the development.

In the SOAP API developer guide I found the WSDL to import the service in my project as web reference. I can do every step of the Express Checkout payment flow. Making requests is a walk in the park, using the responses from the service however are not.

For some reason the data fields in the response are not filled in automatically as I would expect. All useful fields are null. Because of this I manually have to parse the XML-string in the response to get the data I need instead of just accessing the right objects. Needless to say, I do not want to work this way.

A quick example from the last step of the payment flow (DoExpressCheckout):

PayPalAPIAASoapBinding apiaa = new PayPalAPIAASoapBinding();
apiaa.RequesterCredentials = ... ;

DoExpressCheckoutPaymentReq decreq = new DoExpressCheckoutPaymentReq();
decreq.DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType();
/* Filling in all the fields required */

DoExpressCheckoutPaymentResponseType decres = apiaa.DoExpressCheckoutPayment(decreq);

The decres object normally has a field DoExpressCheckoutPaymentResponseDetails that contains all details I need. According to the manual and what I expect, I should be able to, for example, read the token returned in the reponse like this:

string token = decres.DoExpressCheckoutPaymentResponseDetails.Token;

Instead, the DoExpressCheckoutPaymentResponseDetails field is null and I have get the token like this:

string token = decres.Any.ChildNodes[0].InnerText;

I do not get any error codes from the PayPal service.

Even more strange is when I use the TransactionSearch function to find multiple transactions. Say the functions returns 50 transactions. 49 transactions are filled in the correct object where one would expect to find them after reading the API documentation. The first transaction however is not and has to be parsed manually from the Any field in the response.

Did anyone ever encoutered the same or similar problem?

Fyi: I am used to programming web applications PHP. On specific demand I am doing this in C#. Is this standard C# or Visual Studio behaviour (doubt it) or am I doing/configuring an aspect of the SOAP service the wrong way in Visual Studio?


回答1:


SOLVED: The solution is to STOP USING THE LEGACY "WEB REFERENCE", use the newer standard Service Reference instead.

Most of my code worked just fine with the new Service Reference, I just had to setup the Web.config and change the way I create an instance of the service.

return new PayPalAPIAAInterfaceClient("PayPalAPIAA"); took the place of PayPalAPIAASoapBinding.

Original Post Follows.......

I second this question, it's not a matter of invalid credentials, failed transaction, or mismatched API version. I'm doing everything right, the response DOES contain a Token, but it is going into the anyField variable rather than where it belongs.

This bug reared it's ugly head after I upgraded from VS 2008 .Net 3.5 to VS 2010 .Net 4. I suspect that the bug is not on the PayPal side, but rather a bug in .Net 4 XML Parsing.

The Token should be going into the Token property here...

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:ebay:api:PayPalAPI")]
public partial class SetExpressCheckoutResponseType : AbstractResponseType {

    private string tokenField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public string Token {
        get {
            return this.tokenField;
        }
        set {
            this.tokenField = value;
        }
    }
}

Instead it ends up here....

[System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement Any {
            get {
                return this.anyField;
            }
            set {
                this.anyField = value;
            }
        }

I may be able to fix this inside the Reference.cs file, but my fix would be overwritten as soon as I update the web service reference.

Here is someone else with the same problem, but their solution didn't work for me. https://www.x.com/developers/paypal/forums/soap/paypal-api-aa-and-net-wcf-undeserialized-fields

Help!!




回答2:


Hard to say exactly. Typically, those fields are null when the transaction failed, and present when the transaction succeeded.

Are you checking decres.Ack after calling DoExpressCheckoutPayment? If decres.Ack is AckCodeType.Success or AckCodeType.SuccessWithWarning, then the transaction succeeded and you should expect DoExpressCheckoutPaymentResponseDetails to be populated. Otherwise, the transaction failed and the fields will be null.



来源:https://stackoverflow.com/questions/12106242/paypal-soap-api-reponses-require-manual-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!