2015-07-22 125 views
0

這是我在貝寶捕捉支付寶

OAuthTokenCredential tokenCredential = new OAuthTokenCredential("<CLIENT_ID>", "<CLIENT_SECRET>"); 

var accessToken = tokenCredential.GetAccessToken(); 
Authorization authorization = Authorization.Get(accessToken, "5RA45624N3531924N"); 

Capture capture = new Capture(); 
Amount captureAmount = new Amount(); 
captureAmount.currency = "USD"; 
captureAmount.total = "1"; 
capture.amount = captureAmount; 
capture.is_final_capture = true; 

Capture responseCapture = authorization.Capture(accessToken, capture); 

用於捕獲支付代碼,但它說

參數類型「串」不分配給參數類型「PayPal.Api.APIContext '

在下面的行accessToken參數

Authorization authorization = Authorization.Get(accessToken, "5RA45624N3531924N"); 

我該如何解決這個問題?

回答

0

您在第一個參數中指定了令牌,但它不正確。

根據下面的定義:

public static Authorization Get(APIContext apiContext, string authorizationId) 

應指定APICONTEXT類型。

你可以嘗試像下面的代碼:

var apiContext = Configuration.GetAPIContext(); 
var authorization = Authorization.Get(apiContext, authorizationId); 
0

創建一個新類 'Configuration.cs'。

public static class Configuration 
{ 
    public readonly static string ClientId; 
    public readonly static string ClientSecret; 

    static Configuration() 
    { 
     var config = GetConfig(); 
     ClientId = config["clientId"]; 
     ClientSecret = config["clientSecret"]; 
    } 

    public static Dictionary<string,string> GetConfig() 
    { 
     return PayPal.Api.ConfigManager.Instance.GetProperties(); 
    } 

    private static string GetAccessToken() 
    { 
     var config = GetConfig(); 
     OAuthTokenCredential credential = new OAuthTokenCredential(ClientId, ClientSecret, config); 
     string accessToken = credential.GetAccessToken(); 
     return accessToken; 
    } 

    public static APIContext GetAPIContext() 
    { 
     string accessToken = GetAccessToken(); 
     APIContext apiContext = new APIContext(accessToken); 
     apiContext.Config = GetConfig(); 
     return apiContext; 
    } 

} 

GetAPIContext方法將返回APICONTEXT對象。將該對象作爲參數傳遞給Authorization.Get方法,而不是「accessToken」。

APIContext apiContext = Configuration.GetAPIContext(); 
Authorization authorization = Authorization.Get(apiContext, "6SY29185GS4409204");//Provide Payment Id returned after authorizing payment.