2012-07-20 89 views
2

我正在嘗試使用PayPal API執行一些付款操作,並且我將CertificateFile屬性設置爲我的證書文件的位置時出現致命異常。Paypal API在指向我的證書文件時摔倒了

相關的代碼如下:

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. 我在哪裏可以找到貝寶API中的IAPIProfile類的文檔,特別是文檔中的CertificateFile財產
  2. 如果我不應該把路給我證書文件在這個位置,我該怎麼辦?

只是爲了確認,MyTestCertificate.txt添加到我的解決方案和Copy to Output Directory設置爲Copy Always

例外全文如下:

{"Exception of type 'com.paypal.sdk.exceptions.FatalException' was thrown."}

堆棧跟蹤看起來是這樣的:

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的它記錄錯誤像這樣:

20 Jul 2012 12:39:11 FATAL [FatalException] com.paypal.sdk.exceptions.FatalException: Exception of type 'com.paypal.sdk.exceptions.FatalException' was thrown.

謝謝

+0

什麼是例外+跟蹤 – Woot4Moo 2012-07-20 11:15:15

+0

更新,出現異常的問題+追查 – JMK 2012-07-20 11:21:29

+0

是否有內部異常? – Rafal 2012-07-20 11:26:43

回答

0

事實證明aydiv是正確的,我不得不使用WinHttpCertCfg註冊證書。馬丁也是對的,因爲我使用的是簽名檔案,而不是證書檔案。

我必須做的第一件事就是使用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文件之前,我需要使用來自here的SOAP dll。這意味着我必須用Callerservices代替我的代碼中的NVPCallerServices以及其他一些內容。

我在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(); 
    } 
} 
1

您將不得不在Microsoft系統商店安裝您的證書。 SDK中的自述文件解釋瞭如何使用WinHttpCertCfg工具來做到這一點。

相關問題