Nailgun Nailgun - 4 months ago 24
Java Question

PayPal REST API java sdk - custom config file

Good afternoon all!

I use PayPal REST API java sdk and I want to have different configurations for different environments of my application. Here is how I'm trying to do so:

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


and while trying to get the clientID I have

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


Strange behavior - I thought I've just configured the sdk to use my own properties file.

Please advice how could I set up those settings properly!

Answer

So here is the solution I found:

  1. Create an empty sdk_config.properties file in default location
  2. Load your own properties:

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