Skip to content

Commit

Permalink
Merge pull request #306 from swlodarski-sumoheavy/10.1.x
Browse files Browse the repository at this point in the history
SP-1027 Add X-BitPay-Platform-Info header
  • Loading branch information
bobbrodie authored Sep 4, 2024
2 parents 5997f2c + befd951 commit 4755e50
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 5 deletions.
171 changes: 171 additions & 0 deletions src/main/java/com/bitpay/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ public Client(PosToken token) throws BitPayGenericException {
this(token, Environment.PROD);
}

/**
* Constructor for POS facade.
*
* @param token POS token
* @param platformInfo Platform info
* @throws BitPayGenericException BitPayGenericException class
*/
public Client(PosToken token, String platformInfo) throws BitPayGenericException {
this(token, Environment.PROD, platformInfo);
}

/**
* Constructor for POS facade.
*
Expand All @@ -109,6 +120,35 @@ public Client(
this.guidGenerator = new GuidGenerator();
}

/**
* Constructor for POS facade.
*
* @param token POS token
* @param environment Environment
* @param platformInfo Platform info
* @throws BitPayGenericException BitPayGenericException class
*/
public Client(
PosToken token,
Environment environment,
String platformInfo
) throws BitPayGenericException {
if (Objects.isNull(token) || Objects.isNull(environment)) {
BitPayExceptionProvider.throwMissingParameterException();
}

this.tokenContainer = new TokenContainer();
this.tokenContainer.addPos(token.value());
this.bitPayClient = new BitPayClient(
getHttpClient(null, null),
new HttpRequestFactory(),
getBaseUrl(environment),
null,
platformInfo
);
this.guidGenerator = new GuidGenerator();
}

/**
* Constructor for use if the keys and SIN are managed by this library.
*
Expand Down Expand Up @@ -138,6 +178,38 @@ public Client(
this.guidGenerator = new GuidGenerator();
}

/**
* Constructor for use if the keys and SIN are managed by this library.
*
* @param environment Target environment. Options: Env.Test / Env.Prod
* @param privateKey The full path to the securely located private key or the HEX key value.
* @param tokenContainer Object containing the available tokens.
* @param proxyDetails HttpHost Optional Proxy setting (set to NULL to ignore)
* @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore)
* @param platformInfo Platform Info
* @throws BitPayGenericException BitPayGenericException class
*/
public Client(
Environment environment,
PrivateKey privateKey,
TokenContainer tokenContainer,
HttpHost proxyDetails,
CredentialsProvider proxyCredentials,
String platformInfo
) throws BitPayGenericException {
ECKey ecKey = getEcKey(privateKey);
this.tokenContainer = tokenContainer;
this.deriveIdentity(ecKey);
this.bitPayClient = new BitPayClient(
getHttpClient(proxyDetails, proxyCredentials),
new HttpRequestFactory(),
getBaseUrl(environment),
ecKey,
platformInfo
);
this.guidGenerator = new GuidGenerator();
}

/**
* Constructor for use if the keys and SIN are managed by this library.
*
Expand Down Expand Up @@ -168,6 +240,40 @@ public Client(
this.guidGenerator = new GuidGenerator();
}


/**
* Constructor for use if the keys and SIN are managed by this library.
*
* @param configFilePath The path to the configuration file.
* @param proxy HttpHost Optional Proxy setting (set to NULL to ignore)
* @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore)
* @param platformInfo Platform Info
* @throws BitPayGenericException BitPayGenericException class
*/
public Client(
ConfigFilePath configFilePath,
HttpHost proxy,
CredentialsProvider proxyCredentials,
String platformInfo
) throws BitPayGenericException {
Config config = this.buildConfigFromFile(configFilePath);
this.tokenContainer = new TokenContainer(config);
ECKey ecKey = this.getEcKey(config);
if (Objects.isNull(ecKey)) {
BitPayExceptionProvider.throwValidationException("Missing ECKey");
}

this.deriveIdentity(ecKey);
this.bitPayClient = new BitPayClient(
getHttpClient(proxy, proxyCredentials),
new HttpRequestFactory(),
getBaseUrl(config.getEnvironment()),
ecKey,
platformInfo
);
this.guidGenerator = new GuidGenerator();
}

/**
* Constructor for all injected classes.
*
Expand Down Expand Up @@ -199,6 +305,18 @@ public static Client createPosClient(PosToken token) throws BitPayGenericExcepti
return new Client(token);
}

/**
* Create pos (light) client.
*
* @param token the token
* @param platformInfo the platform info
* @return the client
* @throws BitPayGenericException BitPayGenericException class
*/
public static Client createPosClient(PosToken token, String platformInfo) throws BitPayGenericException {
return new Client(token, platformInfo);
}

/**
* Create pos (light) client.
*
Expand All @@ -214,6 +332,23 @@ public static Client createPosClient(
return new Client(token, environment);
}

/**
* Create pos (light) client.
*
* @param token the token
* @param environment environment
* @param platformInfo the platform info
* @return the client
* @throws BitPayGenericException BitPayGenericException class
*/
public static Client createPosClient(
PosToken token,
Environment environment,
String platformInfo
) throws BitPayGenericException {
return new Client(token, environment, platformInfo);
}

/**
* Create standard client.
*
Expand All @@ -233,6 +368,27 @@ public static Client createClientByPrivateKey(
return new Client(env, privateKey, tokenContainer, null, null);
}

/**
* Create standard client.
*
* @param privateKey the private key
* @param tokenContainer the token container
* @param environment environment
* @param platformInfo the platform info
* @return Client Client
* @throws BitPayGenericException BitPayGenericException class
*/
public static Client createClientByPrivateKey(
PrivateKey privateKey,
TokenContainer tokenContainer,
Environment environment,
String platformInfo
) throws BitPayGenericException {
Environment env = Objects.isNull(environment) ? Environment.PROD : environment;

return new Client(env, privateKey, tokenContainer, null, null, platformInfo);
}

/**
* Create standard client.
*
Expand All @@ -244,6 +400,21 @@ public static Client createClientByConfigFilePath(ConfigFilePath configFilePath)
return new Client(configFilePath, null, null);
}

/**
* Create standard client.
*
* @param configFilePath the config file path
* @param platformInfo the platform info
* @return the client
* @throws BitPayGenericException BitPayGenericException class
*/
public static Client createClientByConfigFilePath(
ConfigFilePath configFilePath,
String platformInfo
) throws BitPayGenericException {
return new Client(configFilePath, null, null, platformInfo);
}


/**
* Authorize this client with the server using the specified pairing code (Server Initiated Pairing).
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/com/bitpay/sdk/client/BitPayClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class BitPayClient {
private final HttpRequestFactory httpRequestFactory;
private final String baseUrl;
private final ECKey ecKey;
private String platformInfo = null;

/**
* Instantiates a new Bit pay client.
Expand All @@ -57,6 +58,29 @@ public BitPayClient(
this.ecKey = ecKey;
}

/**
* Instantiates a new Bit pay client.
*
* @param httpClient the http client
* @param httpRequestFactory the http request factory
* @param baseUrl the base url
* @param ecKey the ECKey
* @param platformInfo the Platform Info
*/
public BitPayClient(
final HttpClient httpClient,
final HttpRequestFactory httpRequestFactory,
final String baseUrl,
final ECKey ecKey,
final String platformInfo
) {
this.httpClient = httpClient;
this.baseUrl = baseUrl;
this.httpRequestFactory = httpRequestFactory;
this.ecKey = ecKey;
this.platformInfo = platformInfo;
}

/**
* Send GET request.
*
Expand Down Expand Up @@ -241,12 +265,9 @@ public HttpResponse update(

httpPut.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8)));

this.addSignatureRequiredHeaders(httpPut, endpoint + json);
httpPut.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
httpPut.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
this.addDefaultHeaders(httpPut);
httpPut.addHeader("Content-Type", "application/json");
httpPut.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpPut.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
this.addSignatureRequiredHeaders(httpPut, endpoint + json);

LoggerProvider.getLogger().logRequest(HttpPut.METHOD_NAME, endpoint, httpPut.toString());

Expand All @@ -266,6 +287,9 @@ private void addDefaultHeaders(AbstractHttpMessage httpMessage) {
httpMessage.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpMessage.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
httpMessage.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
if (this.platformInfo != null && !this.platformInfo.isEmpty()) {
httpMessage.addHeader("x-bitPay-platform-info", this.platformInfo);
}
}

private void addSignatureRequiredHeaders(AbstractHttpMessage httpMessage, String uri)
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/bitpay/sdk/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public void it_should_provide_pos_client() throws BitPayGenericException {
Assertions.assertEquals(posToken, bitpay.getAccessToken(Facade.POS));
}

@Test
public void it_should_provide_pos_client_with_platform_info_header() throws BitPayGenericException {
// given
String posToken = "posToken";

// when
Client bitpay = Client.createPosClient(new PosToken(posToken), "MyPlatform_v1.0.0");

// then
Assertions.assertEquals(posToken, bitpay.getAccessToken(Facade.POS));
}

@Test
public void it_should_provide_client_by_key() throws BitPayGenericException {
// given
Expand All @@ -109,6 +121,22 @@ public void it_should_provide_client_by_key() throws BitPayGenericException {
Assertions.assertEquals(merchantToken, bitpay.getAccessToken(Facade.MERCHANT));
}

@Test
public void it_should_provide_client_by_key_with_platform_info_header() throws BitPayGenericException {
// given
String privateKey =
"3082013102010104208ae30afbc7e93cb10cb983f70863e546b53f0b2c6158b1a71b576fd09790cff3a081e33081e0020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f3044042000000000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000000704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a124032200035d6a7e38d7c08b8a626e2390d0360a72a58bd1c5e1348e0eb810d4bbab3d3adf";
String merchantToken = "merchantToken";
TokenContainer tokens = new TokenContainer();
tokens.addMerchant(merchantToken);

// when
Client bitpay = Client.createClientByPrivateKey(new PrivateKey(privateKey), tokens, Environment.TEST, "MyPlatform_v1.0.0");

// then
Assertions.assertEquals(merchantToken, bitpay.getAccessToken(Facade.MERCHANT));
}

@Test
public void it_should_provide_client_by_config() throws BitPayGenericException {
// given
Expand All @@ -122,6 +150,19 @@ public void it_should_provide_client_by_config() throws BitPayGenericException {
Assertions.assertEquals("payoutToken", bitpay.getAccessToken(Facade.PAYOUT));
}

@Test
public void it_should_provide_client_by_config_with_platform_info_header() throws BitPayGenericException {
// given
String path = System.getProperty("user.dir") + "/src/test/java/com/bitpay/sdk/BitPay.config.json";

// when
Client bitpay = Client.createClientByConfigFilePath(new ConfigFilePath(path), "MyPlatform_v1.0.0");

// then
Assertions.assertEquals("merchantToken", bitpay.getAccessToken(Facade.MERCHANT));
Assertions.assertEquals("payoutToken", bitpay.getAccessToken(Facade.PAYOUT));
}

@Test
public void it_should_throws_BitPayApiException_for_invalid_privateKey() {
BitPayGenericException exception = Assertions.assertThrows(BitPayGenericException.class, () -> {
Expand Down
Loading

0 comments on commit 4755e50

Please sign in to comment.