使用 WSDL (SOAP) 在 C#/.NET 解决方案中集成 PayPal [英] Integrating PayPal in C#/.NET Solution using WSDL (SOAP)

查看:16
本文介绍了使用 WSDL (SOAP) 在 C#/.NET 解决方案中集成 PayPal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Visual Studio 2010 专业版.NET 框架 4C#

使用以下 WSDL 添加服务参考:https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

Added Service Reference using the following WSDL : https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

问题 1:当像这样简单地编译时,从 Reference.cs 文件中得到一堆错误.看起来像命名空间错误.它提到它在我的项目命名空间中找不到服务引用命名空间.因此,我进入了 Reference.cs 文件,无论在何处出现此错误,我都会在方法名称之前删除项目的命名空间,现在它可以编译了.

Problem 1 : When compiled simply like this, get a bunch of errors from the Reference.cs file. Looks like namespace errors. It mentions that it cannot find the Service Reference Namespace in my project's Namespace. Therefore, I went into the Reference.cs file and whereever I got this error, I removed the project's namespace before the method names, and now it compiles.

终于可以访问所有课程了.创建并填充具有所需属性的 DoDirectPaymentReqCustomSecurityHeader 对象.创建了 PayPalAPIAAInterfaceClient 类的实例,其中包含方法 DoDirectPayment,该方法接受类型为 CustomSecurityHeader 和 DoDirectPaymentReq 的参数.看起来像这样:

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

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);
}

问题2:为包含上述代码的方法编写TestMethod后,出现如下错误:

Problem 2 : After writing a TestMethod for the method which contains the above code, I get the 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

因此,到目前为止,我还无法通过在 C# 中使用 WSDL 使用 PayPal SOAP 协议进行成功的交易.

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

我的印象是这很简单.只需添加服务引用并利用在代理中从 WSDL 创建的属性和方法的类.

I was under the impression that this is very simple. Simply Add Service Reference and utilize the Classes with their properties and methods created in the proxy from WSDL.

我哪里出错了?

我是否使用了错误的 WSDL?我想先对 Sandbox 进行测试,然后再上线.

Am I using the wrong WSDL ? I'd like to test against Sandbox first and then go Live.

如果我对 WSDL 是正确的,看起来类 PayPalAPIAAInterfaceClient 不知道它的端点,我不知道我是否应该手动设置,因为它已经存在最后是 WSDL 定义(检查一下).我认为类本身应该知道要调用哪个端点,具体取决于我是使用签名还是证书来填充 CustomSecurityHeaderType.

If I am right with the WSDL, looks like the class PayPalAPIAAInterfaceClient doesn't know its endpoint, which I don't know if I am suppose to set manually or not since its already there in the WSDL definition at the end (check it out). I think the class itself should know which endpoint to call depending on whether I am using Signature or Certificate to populate CustomSecurityHeaderType.

但是 PayPalAPIAAInterfaceClient 类如何知道我是尝试调用沙箱(测试)还是实时交易?

But how does the PayPalAPIAAInterfaceClient class know whether I am trying to call into the Sandbox (testing) or it is a live transaction ?

PayPal 过去有两种不同的 WSDL,分别用于 Sandbox 和 Live.它们可以在这里找到:->https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_soap_PayPalSOAPAPIArchitecture

PayPal used to have two different WSDLs for Sandbox and for Live. They can be found here : ->https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_soap_PayPalSOAPAPIArchitecture

在与他们的支持人员交谈后,我被要求对 Sandbox 和 Live 使用以下 WSDL:->https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

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

但是我如何告诉 PayPalAPIAAInterfaceClient 类何时执行实时或沙盒测试.以及使用哪个端点取决于我的 SOAP 和签名方法.这里提到了 PayPal 的端点:

But how do I tell the PayPalAPIAAInterfaceClient class when it is suppose to perform Live or Sandbox tests. And also to which end point to use depending on my method of SOAP and Signature. The endpoints from PayPal are mentioned here :

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

帮助!

推荐答案

您在这里遇到了一些问题,但解决这些问题应该不会太痛苦.首先,当我将服务引用添加到您在帖子顶部链接的 WSDL 时,我对您描述的名称空间没有任何问题.可能是您自己的命名空间/引用与自动生成的术语有某种冲突,或者您在添加引用过程中选择了一些奇怪的选项?删除并重新添加可能会解决问题,或者我想您可以忽略它,因为您已经解决了它.(不过,编辑自动生成的代码有点麻烦,因此您最终应该计划修复.)

You have a few problems here, but none should be too painful to resolve. First of all, when I add a Service Reference to the WSDL you link at the top of your post I don't have any of the problems with namespaces that you describe. It could be that your own namespaces/references are conflicting somehow with the auto-generated terms, or perhaps that you selected some strange option during the add reference process? A delete-and-re-add might solve the problem, or I guess you can just ignore it since you've already worked around it. (It is kind of a hassle to edit auto-generated code, however, so you should plan on a fix eventually.)

要解决 InvalidOperationException,您可能只需要指定 Visual Studio 自动添加到 app.config 文件的端点之一.您的配置文件中应该有这样的内容:

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

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

您可以将您想要的端点的名称传递给代理类的构造函数.有其他选项来解决这个问题,但只指定一个端点很容易和干净.(注意:如果您的配置文件中没有此部分,则在 添加服务引用 阶段会出现问题.我再次建议您重置您的项目并重新添加引用.)

You can pass the name of the endpoint you want to the constructor of the proxy class. There are other options to solve this problem, but just specifying an endpoint is easy and clean. (Note: if you don't have this section in your config file, then something went wrong during the Add Service Reference phase. Again I would just suggest resetting your project and re-adding the reference.)

最后,当您使用代理类时,您不想使用 using 块,尽管它是 IDisposable.基本上,WCF 中存在设计错误.

Finally, you don't want to use a using block when you make use of the proxy class in spite of it being IDisposable. Basically, there's a design bug in WCF.

这篇关于使用 WSDL (SOAP) 在 C#/.NET 解决方案中集成 PayPal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆