Guides
Customer Management
Synchronization and Mapping
2min
Syncing customers in Amberflo and Stripe is during the creation of customers in your system.
The sample code provides the following functionality:
- Create the customer in Stripe to obtain the Stripe ID.
- Create the customer in Amberflo and set the Stripe ID from the previous step in the traits
- Assign the customer to a default rate plan.
Maven dependencies (check for latest versions):
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>com.stripe</groupId>
9 <artifactId>stripe-java</artifactId>
10 <version>20.52.0</version>
11 </dependency>
12</dependencies>
Java code with helper class to integrate with Amberflo and Stripe integration for customer creation, call AmberfloCustomerHelper.createCustomerInStripeAndAmberflo() method from your customer transaction.
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.util.HashMap;
10import java.util.Map;
11
12public class AmberfloCustomerHelper {
13 //TODO: set the Amberflo and Stripe info
14 final static String AMBERFLO_PRODUCT_PLAN_ID = "default-product-plan-id";
15 final static String AMBERFLO_API_KEY = "amberflo-api-key";
16 final static String STRIPE_API_KEY = "sk-api-key";
17 //END TODO
18
19 final static CustomerProductPlanClient customerProductPlanClient = new CustomerProductPlanClient(AMBERFLO_API_KEY);
20 final static CustomerDetailsClient customerDetailsClient = new CustomerDetailsClient(AMBERFLO_API_KEY);
21
22 /***
23 *
24 * @param customerId
25 * @param customerEmail
26 * @param customerName
27 * @param customerAlternateIdName This is optional in case your system maintains an alternate ID.
28 * @param customerAlternateIdValue This is optional.
29 */
30 public static void createCustomerInStripeAndAmberflo(final String customerId,
31 final String customerEmail,
32 final String customerName,
33 final String customerAlternateIdName,
34 final String customerAlternateIdValue
35 ) {
36 try {
37 Stripe.apiKey = STRIPE_API_KEY;
38 //Stripe does not allow custom customerId and generates one.
39 //To link your customer record to Stripe, you will need to pass it as metadata.
40 final Map<String, String> metadata = new HashMap();
41 metadata.put("customerId", customerId);
42 if(customerAlternateIdName != null || !customerAlternateIdName.isEmpty()) {
43 metadata.put(customerAlternateIdName, customerAlternateIdValue);
44 }
45
46 final CustomerCreateParams params = CustomerCreateParams
47 .builder()
48 .setEmail(customerEmail)
49 .setMetadata(metadata)
50 .build();
51
52 //Create customer in Stripe
53 final Customer stripeCustomer = Customer.create(params);
54 System.out.println(String.format("Amberflo: Customer with id %s created in Stripe with id %s",
55 customerId, stripeCustomer.getId()));
56
57 final Map<String, String> traits = new HashMap();
58 if(customerAlternateIdName != null && !customerAlternateIdName.isEmpty()) {
59 traits.put(customerAlternateIdName, customerAlternateIdValue);
60 }
61 traits.put("stripeId", stripeCustomer.getId());
62 final CustomerDetails customerDetails = new CustomerDetails(customerId, customerName, customerEmail, traits);
63
64 //Create customer in Amberflo. Use the Stripe ID to link customers in Amberflo and Stripe.
65 //This will be used for payment automation of Amberflo invoices in Stripe.
66 customerDetailsClient.addOrUpdate(customerDetails);
67 System.out.println(String.format("Amberflo: Customer with id %s created in Amberflo",
68 customerId));
69
70 //Assign customer to a default product plan
71 final String productPlanId = AMBERFLO_PRODUCT_PLAN_ID;
72 final CustomerProductPlan customerProductPlan = new CustomerProductPlan(productPlanId, customerId);
73 customerProductPlanClient.addOrUpdate(customerProductPlan);
74 System.out.println(String.format("Amberflo: Customer with id %s assigned to product plan %s in Amberflo",
75 customerId, productPlanId));
76 } catch (Exception ex) {
77 System.out.println(String.format("Amberflo customer creation failed: %s", ex.getMessage()));
78 ex.printStackTrace();
79 }
80 }
81}
Updated 31 Jul 2024
Did this page help you?