捕获支付Paypal

时间:2015-07-22 07:31:42

标签: paypal

这是我在Paypal中用于捕获付款的代码

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);

但它说

  

参数类型&#39;字符串&#39;不能分配给参数类型&#39; PayPal.Api.APIContext&#39;

在以下行accessToken参数

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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您在第一个参数中指定了令牌,它不正确。

根据以下定义:

public static Authorization Get(APIContext apiContext, string authorizationId)

您应该指定APIContext类型。

您可以尝试以下代码:

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

答案 1 :(得分:0)

创建一个新类&#39; Configuration.cs&#39;。

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方法,而不是&#34; accessToken&#34;。

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