我正在寻找一种在代码中设置PayPal SOAP API端点的方法,而不是在web.config或app.config中指定它.我需要从特定于环境的配置中读取和使用端点,该配置不是web.config/app.config.
这可能吗?我已经在他们的github repo上阅读了SDK的一些代码,但这似乎不可能,但我希望我错过了一些东西.
我正在使用PayPal Merchant SDK for .Net,ver 2.1.96.0.
我需要能够指定.config文件之外的所有重要设置.我很懒,所以我就是这样做的:
paypalConfig = new Dictionary<string, string>();
paypalConfig.Add("apiUsername", "xxxxx");
paypalConfig.Add("apiPassword", "xxxxx");
paypalConfig.Add("apiSignature", "xxxxx");
==>paypalConfig.Add("mode","live|sandbox");
Run Code Online (Sandbox Code Playgroud)
然后你需要通过再次指定凭证来调用你的方法(没有调查为什么这是必要的):
var service = new PayPalAPIInterfaceServiceService(paypalConfig);
var doCaptureResponse = service.DoCapture(amount, new SignatureCredential(paypalConfig["apiUsername"], paypalConfig["apiPassword"], paypalConfig["apiSignature"]));
Run Code Online (Sandbox Code Playgroud)
这是完全可行的,但是您必须通过将绑定创建为对象来对值进行硬编码。这是我为自己的项目制定的:
protected static PayPalAPIInterface GetService()
{
return new PayPalAPIInterfaceClient(new BasicHttpBinding()
{
SendTimeout = new TimeSpan(0, 5, 0),
MaxReceivedMessageSize = 200000,
Security = new BasicHttpSecurity()
{
Mode = BasicHttpSecurityMode.Transport,
Transport = new HttpTransportSecurity()
{
ClientCredentialType = HttpClientCredentialType.None,
ProxyCredentialType = HttpProxyCredentialType.None,
},
Message = new BasicHttpMessageSecurity()
{
ClientCredentialType = BasicHttpMessageCredentialType.Certificate,
}
}
},
new EndpointAddress(@"https://api-3t.paypal.com/2.0/")
).ChannelFactory.CreateChannel();
}
Run Code Online (Sandbox Code Playgroud)
您可以设置更多参数 - 理论上,.config文件中的所有内容都可以在此处重现。不过,这对我有用,所以我没有再继续下去。
还值得注意的是,这使您能够将 PayPal 调用放入库中,而不必将绑定复制到包含该库的项目的配置文件中,这就是我首先开发它的原因。
编辑:这是基本定义PayPalAPIInterfaceClient- 不保证它实际上足以使用。
public partial class PayPalAPIInterfaceClient : System.ServiceModel.ClientBase<PayPalAPIInterfaceServiceService>
{
public PayPalAPIInterfaceClient(System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress)
: base(binding, remoteAddress) { }
}
Run Code Online (Sandbox Code Playgroud)
您还可以修改之前的代码以使用返回类型PayPalAPIInterfaceServiceService。