问题
I am trying to DoCapture some payments using the Paypal API and I am getting a Fatal Exception on the line of code where I set the CertificateFile property to the location of my certificate file.
The relevant code is below:
using com.paypal.sdk.profiles;
using com.paypal.sdk.services;
using com.paypal.sdk.util;
IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
profile.CertificateFile = @"~\MyTestCertificate.txt";
Drilling down into the Exceptions details doesn't give me much more information, it more or less just confirms that a Fatal Exception has indeed been thrown.
Leaving out the tilde and backslash like so throws the same error:
profile.CertificateFile = @"MyTestCertificate.txt";
I thought that maybe I needed the contents of the file, instead of the location so I tried the following but got the same error:
profile.CertificateFile = new StreamReader(@"MyTestCertificate.txt").ReadToEnd().ToString();
It seems that whatever you set the CertificateFile property to, you get a fatal exception.
A couple of questions:
- Where can I find documentation on the IAPIProfile class in the Paypal API, in particular documentation for the
CertificateFileproperty - If I am not supposed to put the path to my certificate file in this location, what am I supposed to do?
Just to confirm, MyTestCertificate.txt is added to my solution and Copy to Output Directory is set to Copy Always.
The exception text is as follows:
{"Exception of type 'com.paypal.sdk.exceptions.FatalException' was thrown."}
The StackTrace looks like this:
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()
The Paypal API uses Log4Net which logs the error as so:
20 Jul 2012 12:39:11 FATAL [FatalException] com.paypal.sdk.exceptions.FatalException: Exception of type 'com.paypal.sdk.exceptions.FatalException' was thrown.
Thanks
回答1:
You will have to install your certificate in the Microsoft system store. The Readme in the SDK explains how you can do that using the WinHttpCertCfg tool.
回答2:
It turns out aydiv was right, I had to use WinHttpCertCfg to register the certificate. Martin was also right in that I was using the signature profile, as opposed to the certificate profile.
The first thing I had to do was use OpenSSL to encrypt my certificate, using the following command. I had to enter a password here, which I used later. This created an encrypted certificate file named paypal_cert.p12:
openssl pkcs12 -export -in MyTestCertificate.txt -inkey MyTestCertificate.txt -out paypal_cert.p12
I then installed WinHttpCertCfg and used the following command to register my certificate, substituting PFXPassword for the password I entered earlier:
winhttpcertcfg -i PFXFile -c LOCAL_MACHINE\My -a JMK -p PFXPassword
Also, I was using the NVP DLL files before, I instead need to use the SOAP dll's from here. This meant that I had to replace NVPCallerServices in my code with Callerservices, along with a few other things.
My final code for performing a Paypal DoCapture in C# .Net is below, hopefully this will help somebody who comes across this problem in the future!
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();
}
}
来源:https://stackoverflow.com/questions/11578192/paypal-api-falls-over-when-pointing-to-my-certificate-file