Guides
...
Usage Metering
Backfill Meters and Customers
Java sample to backfill customers in Stripe and Amberflo from Postgres
2min
The following JAVA sample shows how to create customers from a Postgres database.
The sample performs the following tasks:
- Query customer Postgres customer table
- For each row,
- Check if customer does not already exist in Amberflo
- Create the customer in Stripe
- Create the customer in Amberflo and set the Stripe ID from previous step in the traits
- Assign the customer to a default rate plan.
Maven dependencies (check for latest versions):
pom.xml
1<dependencies>
2 <dependency>
3 <groupId>io.amberflo</groupId>
4 <artifactId>metering-java-client</artifactId>
5 <version>1.3.6</version>
6 </dependency>
7 <dependency>
8 <groupId>org.postgresql</groupId>
9 <artifactId>postgresql</artifactId>
10 <version>42.3.1</version>
11 </dependency>
12 <dependency>
13 <groupId>com.stripe</groupId>
14 <artifactId>stripe-java</artifactId>
15 <version>20.52.0</version>
16 </dependency>
17</dependencies>
Sample Java Code:
Java
1import com.amberflo.metering.customer_details.clients.CustomerDetailsClient;
2import com.amberflo.metering.customer_details.clients.CustomerProductPlanClient;
3import com.amberflo.metering.customer_details.model.CustomerDetails;
4import com.amberflo.metering.customer_details.model.CustomerProductPlan;
5import com.stripe.Stripe;
6import com.stripe.model.Customer;
7import com.stripe.param.CustomerCreateParams;
8
9import java.sql.Connection;
10import java.sql.DriverManager;
11import java.sql.ResultSet;
12import java.sql.Statement;
13import java.util.HashMap;
14import java.util.Map;
15
16public class CustomerPostgresBackfillProvider {
17
18 //TODO: set the database connection, queries, Amberflo, and Stripe info
19 final static String DB_HOST = "sample-host.us-west-2.rds.amazonaws.com";
20 final static String DB_NAME = "sampleDb";
21 final static String DB_PASS = "sample-pass";
22 final static String DB_USER = "sample-user";
23 final static String SCHEMA = "sampleSchema";
24 final static String COUNT_QUERY = "SELECT count(*) AS total FROM customers;";
25 final static String SELECT_QUERY = "SELECT id, customerName, customerAlternateId, email FROM customers;";
26 final static String AMBERFLO_PRODUCT_PLAN_ID = "default-product-plan-id";
27 final static String AMBERFLO_API_KEY = "amberflo-api-key";
28 final static String STRIPE_API_KEY = "sk-api-key";
29 //END TODO
30
31 final static CustomerProductPlanClient customerProductPlanClient = new CustomerProductPlanClient(AMBERFLO_API_KEY);
32 final static CustomerDetailsClient customerDetailsClient = new CustomerDetailsClient(AMBERFLO_API_KEY);
33 static int counter = 0;
34 static int existing = 0;
35
36 public static void main(final String[] args) throws Exception {
37 Statement stmt = null;
38 Connection con = null;
39 ResultSet rs = null;
40 try {
41 final String url = String.format("jdbc:postgresql://%s:5432/%s?currentSchema=%s", DB_HOST, DB_NAME, SCHEMA);
42 con = DriverManager.getConnection(url, DB_USER, DB_PASS);
43 stmt = con.createStatement();
44 //query to get count of customers
45 rs = stmt.executeQuery(COUNT_QUERY);
46 rs.next();
47 int total = rs.getInt("total");
48 System.out.println("Total customers = " + total);
49
50 //Read all customer rows from database table
51 rs = stmt.executeQuery(SELECT_QUERY);
52 while (rs.next()) {
53 int id = rs.getInt("id");
54 final String customerName = rs.getString("customerName");
55 final String customerAlternateId = rs.getString("customerAlternateId");
56 final String email = rs.getString("email");
57 System.out.println(String.format("ID=%s, customerName=%s, customerAlternateId=%s, email=%s",
58 id, customerName, customerAlternateId, email));
59 createCustomerInStripeAndAmberflo(Integer.toString(id), email, customerName, customerAlternateId);
60 }
61 System.out.println("DONE!!! Total customers migrated = " + counter);
62 System.out.println("Customers that already existed = " + existing);
63 } catch (Exception ex) {
64 ex.printStackTrace();
65 } finally {
66 if (rs != null) rs.close();
67 if (stmt != null) stmt.close();
68 if (con != null) con.close();
69 }
70 }
71
72 private static void createCustomerInStripeAndAmberflo(final String customerId,
73 final String customerEmail,
74 final String customerName,
75 final String customerAlternateId
76 ) {
77 try {
78 Stripe.apiKey = STRIPE_API_KEY;
79 final Map<String, String> metadata = new HashMap();
80 metadata.put("customerId", customerId);
81 metadata.put("customerAlternateId", customerAlternateId);
82
83 final CustomerCreateParams params = CustomerCreateParams
84 .builder()
85 .setEmail(customerEmail)
86 .setMetadata(metadata)
87 .build();
88
89
90 //check if customer does not already exist in Amberflo
91 final CustomerDetails customerDetails1 = customerDetailsClient.get(customerId);
92 if (customerDetails1 != null
93 && customerId.equals(customerDetails1.getCustomerId())
94 && customerDetails1.getTraits().containsKey("stripeId")) {
95 System.out.println(String.format("Amberflo: Customer with id %s already exists in Amberflo and in Stripe with id %s, so skipping ...",
96 customerId, customerDetails1.getTraits().get("stripeId")));
97 existing++;
98 return;
99 }
100
101 //create customer in Stripe
102 final Customer stripeCustomer = Customer.create(params);
103 System.out.println(String.format("Amberflo: Customer with id %s created in Stripe with id %s",
104 customerId, stripeCustomer.getId()));
105
106 final Map<String, String> traits = new HashMap();
107 traits.put("customerAlternateId", customerAlternateId);
108 traits.put("stripeId", stripeCustomer.getId());
109 final CustomerDetails customerDetails = new CustomerDetails(customerId, customerName, customerEmail, traits);
110
111 //create customer in Amberflo
112 customerDetailsClient.addOrUpdate(customerDetails);
113 System.out.println(String.format("Amberflo: Customer with id %s created in Amberflo",
114 customerId));
115
116 //Assign customer to a default product plan
117 final String productPlanId = AMBERFLO_PRODUCT_PLAN_ID;
118 final CustomerProductPlan customerProductPlan = new CustomerProductPlan(productPlanId, customerId);
119 customerProductPlanClient.addOrUpdate(customerProductPlan);
120 System.out.println(String.format("Amberflo: Customer with id %s assigned to product plan %s in Amberflo",
121 customerId, productPlanId));
122 counter++;
123 } catch (Exception ex) {
124 System.out.println(String.format("Amberflo customer creation failed: %s", ex.getMessage()));
125 ex.printStackTrace();
126 }
127 }
128}
Updated 31 Jul 2024
Did this page help you?