PayPal integration in C # /. NET solution using WSDL (SOAP)

Environment: Visual Studio 2010 Professional .NET Framework 4 C #

Added a service link using the following WSDL: https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

Problem 1 : when compiling just like that, get a bunch of errors from the Reference.cs file. Looks like namespace errors. It mentions that he cannot find the reference namespace in the project namespace. So I went into the Reference.cs file, and if I got this error, I deleted the project namespace before the method name and now it compiles.

Finally, access all classes. Created and populated DoDirectPaymentReq and CustomSecurityHeader objects with the required properties. Created an instance of the PayPalAPIAAInterfaceClient class, which contains the DoDirectPayment method, which takes arguments of the type CustomSecurityHeader and DoDirectPaymentReq. Looks like that:

using (var client = new **PayPalAPIAAInterfaceClient**()) { var credentials = new CustomSecurityHeaderType { Credentials = new UserIdPasswordType { Username = " xxxxxxxx@xxxxxx.com ", Password = "xxxxxxx", Signature = "jksadfuhasfweakjhasf" } }; _doDirectPaymentResponseType = client.DoDirectPayment(ref credentials, _doDirectPaymentReq); } 

Problem 2 : after writing TestMethod for a method that contains the above code, I get an error as follows:

 System.InvalidOperationException: Could not find default endpoint element that references contract 'Paypal.PayPalAPIAAInterface' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration) at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() at System.ServiceModel.ClientBase`1..ctor() at PaymentEngine.Paypal.PayPalAPIAAInterfaceClient..ctor() in Reference.cs: line 30063 

Therefore, so far I have not been able to complete a successful transaction using the PayPal SOAP protocol using WSDL in C #.

I got the impression that it is very simple. Just add a link to the service and use the classes with their properties and methods created in the proxy from WSDL.

Where am I going wrong?

Am I using the wrong WSDL? First I would like to test Sandbox, and then go to Live.

If I'm right with WSDL, it looks like the PayPalAPIAAInterfaceClient class does not know the endpoint , which I do not know if I intend to install manually or not, since its already there definition of WSDL at the end (check it). I think the class itself needs to know which endpoint to call, depending on whether I use a signature or certificate to populate CustomSecurityHeaderType.

But how does the PayPalAPIAAInterfaceClient class know if I'm trying to call the Sandbox (testing) or is it a direct transaction?

PayPal used two different WSDLs for the sandbox and for the Live. You can find them here: -> https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_soap_PayPalSOAPAPIArchitecture

After talking with their support, I was asked to use the following WSDL for Sandbox and Live: -> https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

But how can I tell the PayPalAPIAAInterfaceClient class when it should run Live or Sandbox tests. As well as which endpoint to use depending on my SOAP and Signature method. PayPal endpoints are mentioned here:

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_endpoints

HELP!

+6
source share
2 answers

You have several problems here, but none of them should be too painful to solve. First of all, when I add the service link to the WSDL, you are linking at the top of the post, I have no problem with the namespaces that you are describing. Maybe your own namespaces / links somehow conflict with auto-generated terms, or maybe you chose some strange parameter while adding the link process? Removing and re-adding may solve the problem, or I think you can just ignore it since you have already worked on it. (However, it’s just a hassle to edit the automatically generated code, so you should plan a fix in the end.)

To resolve an InvalidOperationException , you probably just need to specify one of the endpoints that Visual Studio automatically added to the app.config file. Your config file should have something like this:

 <system.serviceModel> <client> <endpoint name="PayPalAPI" ... /> <endpoint name="PayPalAPIAA" ... /> </client> </system.serviceModel> 

You can pass the name of the endpoint that you want for the proxy constructor. There are other options to solve this problem, but just specifying the endpoint is easy and clean. (Note: if you do not have this section in your configuration file, something else went wrong while adding the service link, and I just suggest resetting your project and re-adding the link.)

Finally, you do not want to use the using block when using the proxy class, despite the fact that it is IDisposable . Basically, there is a design error in WCF .

+6
source

I had the same problem because I was doing unit testing.

You need to copy the application.config file to the test project, otherwise it will not find the WCF configuration.

+1
source

Source: https://habr.com/ru/post/905340/


All Articles