2

我正在尝试使用 Paypal API 捕获一些付款,并且在将 CertificateFile 属性设置为证书文件位置的代码行上出现致命异常。

相关代码如下:

using com.paypal.sdk.profiles;
using com.paypal.sdk.services;
using com.paypal.sdk.util;

IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
profile.CertificateFile = @"~\MyTestCertificate.txt";

深入了解异常详细信息并没有给我更多信息,它或多或少只是确认确实抛出了致命异常。

像这样省略波浪号和反斜杠会引发相同的错误:

profile.CertificateFile = @"MyTestCertificate.txt";

我想也许我需要文件的内容,而不是位置,所以我尝试了以下但得到了同样的错误:

profile.CertificateFile = new StreamReader(@"MyTestCertificate.txt").ReadToEnd().ToString();

似乎无论您将 CertificateFile 属性设置为什么,都会出现致命异常。

几个问题:

  1. 我在哪里可以找到有关 Paypal API 中 IAPIProfile 类的文档,特别是该CertificateFile属性的文档
  2. 如果我不应该将证书文件的路径放在这个位置,我应该怎么做?

只是为了确认,MyTestCertificate.txt已添加到我的解决方案中并Copy to Output Directory设置为Copy Always.

异常文本如下:

{“'com.paypal.sdk.exceptions.FatalException' 类型的异常被抛出。”}

StackTrace 看起来像这样:

at com.paypal.sdk.profiles.SignatureAPIProfile.set_CertificateFile(String value)
at MyProject_Payment_Processing.Paypal.DoCaptureCode(String authorization_id, String amount) in C:\Users\JMK\documents\visual studio 2010\Projects\MyProject Payment Processing\MyProject Payment Processing\Paypal.cs:line 16
at MyProject_Payment_Processing.Program.Main(String[] args) in C:\Users\JMK\documents\visual studio 2010\Projects\MyProject Payment Processing\MyProject Payment Processing\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Paypal API 使用 Log4Net 记录错误,如下所示:

2012 年 7 月 20 日 12:39:11 FATAL [FatalException] com.paypal.sdk.exceptions.FatalException:抛出了“com.paypal.sdk.exceptions.FatalException”类型的异常。

谢谢

4

2 回答 2

1

您必须在 Microsoft 系统商店中安装您的证书。SDK 中的自述文件解释了如何使用 WinHttpCertCfg 工具执行此操作。

于 2012-07-25T11:14:11.293 回答
0

事实证明aydiv是对的,我不得不用WinHttpCertCfg注册证书。Martin 也是正确的,因为我使用的是签名配置文件,而不是证书配置文件。

我要做的第一件事是使用 OpenSSL 加密我的证书,使用以下命令。我必须在这里输入一个密码,我稍后会使用它。这创建了一个名为的加密证书文件paypal_cert.p12

openssl pkcs12 -export -in MyTestCertificate.txt -inkey MyTestCertificate.txt -out paypal_cert.p12

然后我安装WinHttpCertCfg并使用以下命令注册我的证书,替换PFXPassword我之前输入的密码:

winhttpcertcfg -i PFXFile -c LOCAL_MACHINE\My -a JMK -p PFXPassword

另外,我之前使用的是 NVP DLL 文件,而我需要使用 SOAP dll 从这里。这意味着我必须NVPCallerServices在我的代码中替换为Callerservices,以及其他一些东西。

我在 C# .Net 中执行 Paypal DoCapture 的最终代码如下,希望这将有助于将来遇到此问题的人!

class Paypal
{
    public string DoCaptureCode(string authorization_id, string amount)
    {
        CallerServices caller = new CallerServices();

        IAPIProfile profile = ProfileFactory.createSSLAPIProfile();

        profile.APIUsername = "JMK";
        profile.APIPassword = "FooBar";
        profile.CertificateFile = @"~\MyTestCertificate.txt";
        profile.Environment = "sandbox";
        caller.APIProfile = profile;

        DoCaptureRequestType pp_request = new DoCaptureRequestType();
        pp_request.Version = "51.0";

        pp_request.AuthorizationID = authorization_id;
        pp_request.Amount = new BasicAmountType();
        pp_request.Amount.Value = amount;
        pp_request.Amount.currencyID = CurrencyCodeType.GBP;
        pp_request.CompleteType = CompleteCodeType.Complete;

        DoCaptureResponseType pp_response = new DoCaptureResponseType();
        pp_response = (DoCaptureResponseType)caller.Call("DoCapture", pp_request);
        return pp_response.Ack.ToString();
    }
}
于 2012-07-30T11:23:11.160 回答