1

大家下午好!

我使用 PayPal REST API java sdk,我想为我的应用程序的不同环境提供不同的配置。这是我尝试这样做的方式:

private static boolean IS_PRODUCTION = false;

private static String PAYPAL_ACCESS_TOKEN;

private static void initPayPal() {
    InputStream is = null;
    try {
        is = ApplicationConfig.class.getResourceAsStream(
                IS_PRODUCTION? "/my_paypal_sdk_config.properties" : "/my_paypal_sdk_config_test.properties");
        PayPalResource.initConfig(is);
        String clientID = ConfigManager.getInstance().getConfigurationMap().get("clientID");
        String clientSecret = ConfigManager.getInstance().getConfigurationMap().get("clientSecret");
        PAYPAL_ACCESS_TOKEN = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

在尝试获取我拥有的 clientID 时

java.io.IOException: Resource 'sdk_config.properties' could not be found

奇怪的行为 - 我以为我刚刚将 sdk 配置为使用我自己的属性文件。

请建议我如何正确设置这些设置!

4

3 回答 3

4

所以这是我找到的解决方案:

  1. 在默认位置创建一个空的 sdk_config.properties 文件
  2. 加载您自己的属性:

    private static void initPayPal() {
        InputStream is = null;
        try {
            is = ApplicationConfig.class.getResourceAsStream(
        IS_PRODUCTION ? "/my_paypal_sdk_config.properties" : "/my_paypal_sdk_config_test.properties");
            Properties props = new Properties();
            props.load(is);
            PayPalResource.initConfig(props);
            ConfigManager.getInstance().load(props);
            String clientID = ConfigManager.getInstance().getConfigurationMap().get("clientID");
            String clientSecret = ConfigManager.getInstance().getConfigurationMap().get("clientSecret");
            PAYPAL_ACCESS_TOKEN = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    
于 2013-08-21T15:15:50.377 回答
1

我们在集成步骤上对 PayPal Java SDK 做了一些很好的改进。我们不再需要 sdk_config.properties 文件,因为它们不能很好地工作,特别是对于多配置设置。

现在,您只需使用 、 和 来创建一个APIContext实例。从那里为任何 API 操作传递该上下文对象。clientIdclientSecretmode

下面是代码在不同配置下的样子:

APIContext defaultContext = new APIContext(clientId1, clientSecret1, "sandbox");
APIContext sandboxContext = new APIContext(clientId2, clientSecret2, "sandbox");
APIContext someOtherContext = new APIContext(clientId3, clientSecret3, "live");
APIContext liveContext = new APIContext(clientId, clientSecret, "live");

// Now pass any of the above context in these calls, and it would use those configurations.
Payment payment = new Payment();
// Fill in all the details.
payment.create(defaultContext);
// Replace that defaultContext with any of those other contexts.

这是解释该 wiki 页面:https ://github.com/paypal/PayPal-Java-SDK/wiki/Making-First-Call

于 2016-07-13T23:43:42.793 回答
0

我对 SDK 0.11 版本有同样的错误。我使用自己的属性文件,但代码仍在寻找“sdk_config.properties”。我将它放入我的 CLASSPATH 的根目录中,但仍然出现相同的错误。然后我提出了明显而可怕的解决方案:将空的“sdk_config.properties”放入“rest-api-sdk-0.11.0.jar”库中。这个街头魔术解决了我的问题。

于 2014-10-31T12:36:02.467 回答