使用 WSDL (SOAP) 将 PayPal 集成到 C#/.NET 解决方案中
环境: Visual Studio 2010 专业版 .NET框架4 C#
使用以下 WSDL 添加了服务参考:https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl
问题1:当像这样简单编译时,从Reference.cs文件中得到一堆错误。看起来像命名空间错误。它提到它在我的项目的命名空间中找不到服务引用命名空间。因此,我进入了 Reference.cs 文件,无论在哪里出现此错误,我都在方法名称之前删除了项目的命名空间,现在它可以编译了。
终于可以访问所有类了。 使用所需属性创建并填充 DoDirectPaymentReq 和 CustomSecurityHeader 对象。 创建了 PayPalAPIAAInterfaceClient 类的实例,其中包含方法 DoDirectPayment,该方法接受 CustomSecurityHeader 和 DoDirectPaymentReq 类型的参数。看起来像这样:
using (var client = new **PayPalAPIAAInterfaceClient**())
{
var credentials = new CustomSecurityHeaderType
{
Credentials = new UserIdPasswordType
{
Username = "[email protected]",
Password = "xxxxxxx",
Signature = "jksadfuhasfweakjhasf"
}
};
_doDirectPaymentResponseType = client.DoDirectPayment(ref credentials, _doDirectPaymentReq);
}
问题 2 :为包含上述代码的方法编写 TestMethod 后,出现如下错误:
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
因此,到目前为止我还无法使用 PayPal 进行成功的交易通过在 C# 中使用 WSDL 实现 SOAP 协议。
我的印象是这很简单。只需添加服务引用并利用在 WSDL 代理中创建的类及其属性和方法。
我哪里出错了?
我使用了错误的 WSDL 吗?我想先在沙盒中进行测试,然后再上线。
如果我对 WSDL 的理解是正确的,那么类 PayPalAPIAAInterfaceClient 不知道其端点,我不知道是否应该手动设置,因为它已经存在于 WSDL 定义中最后(检查一下)。我认为类本身应该知道要调用哪个端点,具体取决于我是使用签名还是证书来填充 CustomSecurityHeaderType。
但是 PayPalAPIAAInterfaceClient 类如何知道我是否正在尝试调用沙箱(测试)还是实时交易?
PayPal 曾经有两种不同的 WSDL 用于 Sandbox 和 Live。您可以在这里找到它们: ->https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_soap_PayPalSOAPAPIArchitecture
在与他们的支持人员交谈后,我被要求使用以下内容适用于沙盒和 Live 的 WSDL: ->https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl
但是我如何告诉 PayPalAPIAAInterfaceClient 类何时应该执行实时或沙盒测试。以及使用哪个端点取决于我的 SOAP 和签名方法。这里提到了 PayPal 的端点:
https://cms.paypal .com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_endpoints
帮助!
Environment :
Visual Studio 2010 Professional
.NET Framework 4
C#
Added Service Reference using the following WSDL : https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl
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.
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 = "[email protected]",
Password = "xxxxxxx",
Signature = "jksadfuhasfweakjhasf"
}
};
_doDirectPaymentResponseType = client.DoDirectPayment(ref credentials, _doDirectPaymentReq);
}
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
Therefore, so far I have not been able to make a successful transaction using PayPal SOAP protocol via using WSDL in C#.
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.
Where am I going wrong ?
Am I using the wrong WSDL ? I'd like to test against Sandbox first and then go Live.
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.
But how does the PayPalAPIAAInterfaceClient class know whether I am trying to call into the Sandbox (testing) or it is a live transaction ?
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
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
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
HELP !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些问题,但解决起来应该很痛苦。首先,当我将服务引用添加到您在帖子顶部链接的 WSDL 时,我对您描述的命名空间没有任何问题。可能是您自己的命名空间/引用与自动生成的术语存在某种冲突,或者您在添加引用过程中选择了一些奇怪的选项?删除并重新添加可能会解决问题,或者我想您可以忽略它,因为您已经解决了它。 (但是,编辑自动生成的代码有点麻烦,因此您最终应该计划修复。)
要解决
InvalidOperationException
,您可能只需要指定 Visual Studio 已自动添加到您的 app.config 文件中。您的配置文件中应该有类似这样的内容:您可以将所需端点的名称传递给代理类的构造函数。还有其他选项可以解决此问题,但只需指定端点就很容易和干净。 (注意:如果您的配置文件中没有此部分,则在添加服务引用阶段会出现问题。我再次建议重置您的项目并重新添加引用。)
最后,当您使用代理类时,您不想使用
using
块,尽管它是IDisposable
。基本上,WCF 中存在一个设计错误。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.)
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: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.)
Finally, you don't want to use a
using
block when you make use of the proxy class in spite of it beingIDisposable
. Basically, there's a design bug in WCF.我也遇到了同样的问题,因为我正在做单元测试。
您必须将
application.config
文件复制到测试项目,否则它将找不到 WCF 配置。I had the same problem, because I was doing unit testing.
You have to copy the
application.config
file to the test project, otherwise it won't find the WCF config.