Skip to content

Commit bac0670

Browse files
author
Antonio Buedo
authored
#v3.0 - Client setup script added (#48)
1 parent c2b3e15 commit bac0670

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

java-bitpay-client.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<content url="file://$MODULE_DIR$">
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
88
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/setup/java" isTestSource="false" />
910
<excludeFolder url="file://$MODULE_DIR$/doc" />
1011
<excludeFolder url="file://$MODULE_DIR$/target" />
1112
</content>

pom.xml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,42 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>com.bitpay</groupId>
8-
<artifactId>com.bitpay</artifactId>
8+
<artifactId>bitpay_client</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

12+
<name>BitPay</name>
13+
<description>Full implementation of the BitPay Payment Gateway. This library implements BitPay's Cryptographically Secure RESTful API.</description>
14+
<url>https://bitpay.com/</url>
15+
16+
<licenses>
17+
<license>
18+
<name>MIT License</name>
19+
<url>https://github.com/bitpay/java-bitpay-client/blob/master/LICENSE</url>
20+
<distribution>github</distribution>
21+
</license>
22+
</licenses>
23+
24+
<scm>
25+
<url>https://github.com/bitpay/java-bitpay-client</url>
26+
<connection>scm:git:git://github.com/bitpay/java-bitpay-client.git</connection>
27+
<developerConnection>scm:git:[email protected]:bitpay/java-bitpay-client.git</developerConnection>
28+
</scm>
29+
30+
<developers>
31+
<developer>
32+
<email>[email protected]</email>
33+
<name>Antonio Buedo</name>
34+
<url>https://github.com/anbugal</url>
35+
<id>anbugal</id>
36+
</developer>
37+
</developers>
38+
1239
<properties>
1340
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1441
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15-
<maven.compiler.source>1.7</maven.compiler.source>
16-
<maven.compiler.target>1.7</maven.compiler.target>
42+
<maven.compiler.source>12</maven.compiler.source>
43+
<maven.compiler.target>12</maven.compiler.target>
1744
</properties>
1845

1946
<profiles>

src/main/java/com/bitpay/Client.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,6 @@ private void clearAccessTokenCache() {
10921092
private void cacheToken(String key, String token) throws BitPayException {
10931093
// we add the token to the runtime dictionary
10941094
if (tokenExist(key)) {
1095-
_tokenCache.replace(key, token);
1096-
} else {
10971095
_tokenCache.put(key, token);
10981096
}
10991097

src/setup/java/BitPaySetup.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import com.bitpay.Client;
2+
import com.bitpay.Env;
3+
import com.bitpay.model.Facade;
4+
import com.bitpay.util.KeyUtils;
5+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.ObjectWriter;
9+
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import org.bitcoinj.core.ECKey;
11+
12+
import java.io.File;
13+
import java.nio.file.Paths;
14+
import java.util.HashMap;
15+
import java.util.Scanner;
16+
import java.util.concurrent.atomic.AtomicReference;
17+
18+
class BitPaySetup {
19+
public static void main(String[] args) {
20+
Scanner scanner = new Scanner(System.in);
21+
String opt, env, envUrl, privateKeyPath, pairingCodeMerchant, pairingCodePayroll;
22+
Client bitpay;
23+
24+
do {
25+
System.out.println("Select target environment:");
26+
System.out.println("Press T for testing or P for production:");
27+
opt = scanner.next();
28+
} while (opt.toLowerCase().equals("t") && opt.toLowerCase().equals("p"));
29+
30+
if (opt.toLowerCase().equals("t")) {
31+
env = Env.Test;
32+
envUrl = Env.TestUrl;
33+
} else {
34+
env = Env.Prod;
35+
envUrl = Env.ProdUrl;
36+
}
37+
38+
privateKeyPath = Paths.get(".").toAbsolutePath().normalize().toString() + "/output/bitpay_private_" + env.toLowerCase() + ".key";
39+
40+
System.out.println("Generating private key... ");
41+
try {
42+
File directory = new File(Paths.get(".").toAbsolutePath().normalize().toString() + "/output");
43+
if (!directory.exists()) {
44+
directory.mkdir();
45+
}
46+
47+
if (!KeyUtils.privateKeyExists(privateKeyPath)) {
48+
ECKey _ecKey = KeyUtils.createEcKey();
49+
KeyUtils.saveEcKey(_ecKey);
50+
KeyUtils.createEcKey();
51+
System.out.println("Private key generated successfully with public key:");
52+
System.out.println(_ecKey.getPublicKeyAsHex());
53+
} else {
54+
KeyUtils.loadEcKey();
55+
}
56+
} catch (Exception e) {
57+
System.out.println(e.getMessage());
58+
System.exit(0);
59+
}
60+
61+
System.out.println("Generating config file... ");
62+
try {
63+
ObjectMapper mapper = new ObjectMapper();
64+
65+
bitpay = new Client(env, privateKeyPath, new Env.Tokens());
66+
pairingCodeMerchant = bitpay.requestClientAuthorization(Facade.Merchant);
67+
pairingCodePayroll = bitpay.requestClientAuthorization(Facade.Payroll);
68+
69+
HashMap<String, String> tokens = new HashMap<>();
70+
tokens.put("merchant", bitpay.getAccessToken(Facade.Merchant));
71+
tokens.put("payroll", bitpay.getAccessToken(Facade.Payroll));
72+
AtomicReference<JsonNode> ApiTokens = new AtomicReference<>(mapper.valueToTree(tokens));
73+
74+
ObjectNode envConfig = mapper.createObjectNode();
75+
envConfig.put("PrivateKeyPath", privateKeyPath).put("ApiTokens", ApiTokens.get());
76+
77+
ObjectNode envTarget = mapper.createObjectNode();
78+
envTarget.put(env, envConfig);
79+
80+
ObjectNode bitPayConfiguration = mapper.createObjectNode();
81+
bitPayConfiguration.put("Environment", env).put("EnvConfig", envTarget);
82+
83+
ObjectNode configurationFile = mapper.createObjectNode();
84+
configurationFile.put("BitPayConfiguration", bitPayConfiguration);
85+
86+
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(configurationFile);
87+
88+
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
89+
writer.writeValue(new File(Paths.get(".").toAbsolutePath().normalize().toString() + "/output/BitPay.config.json"), configurationFile);
90+
91+
System.out.println("In location:");
92+
System.out.println(Paths.get(".").toAbsolutePath().normalize().toString() + "/output/BitPay.config.json");
93+
System.out.println("With the following information:");
94+
System.out.println(jsonString);
95+
System.out.println();
96+
System.out.println("To complete your setup, Go to " + Env.TestUrl + "dashboard/merchant/api-tokens and pair this client with your merchant account using the pairing codes:");
97+
System.out.println();
98+
System.out.println(pairingCodeMerchant + " for the Merchant facade.");
99+
System.out.println(pairingCodePayroll + " for the Payroll facade ONLY if you have requested access for this role.");
100+
} catch (Exception e) {
101+
System.out.println(e.getMessage());
102+
System.exit(0);
103+
}
104+
105+
System.out.println("The private key and the config file is been generated in a directory called output in the root of this package.");
106+
System.out.println("Make sure you store this key in a secure location and update the PrivateKeyPath in the generated config file.");
107+
}
108+
}

0 commit comments

Comments
 (0)