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) \<dependencies> \<dependency> \<groupid>io amberflo\</groupid> \<artifactid>metering java client\</artifactid> \<version>1 3 6\</version> \</dependency> \<dependency> \<groupid>com stripe\</groupid> \<artifactid>stripe java\</artifactid> \<version>20 52 0\</version> \</dependency> \</dependencies> java code with helper class to integrate with amberflo and stripe integration for customer creation, call amberflocustomerhelper createcustomerinstripeandamberflo() method from your customer transaction import com amberflo metering customer details clients customerdetailsclient; import com amberflo metering customer details clients customerproductplanclient; import com amberflo metering customer details model customerdetails; import com amberflo metering customer details model customerproductplan; import com stripe stripe; import com stripe model customer; import com stripe param customercreateparams; import java util hashmap; import java util map; public class amberflocustomerhelper { //todo set the amberflo and stripe info final static string amberflo product plan id = "default product plan id"; final static string amberflo api key = "amberflo api key"; final static string stripe api key = "sk api key"; //end todo final static customerproductplanclient customerproductplanclient = new customerproductplanclient(amberflo api key); final static customerdetailsclient customerdetailsclient = new customerdetailsclient(amberflo api key); / @param customerid @param customeremail @param customername @param customeralternateidname this is optional in case your system maintains an alternate id @param customeralternateidvalue this is optional / public static void createcustomerinstripeandamberflo(final string customerid, final string customeremail, final string customername, final string customeralternateidname, final string customeralternateidvalue ) { try { stripe apikey = stripe api key; //stripe does not allow custom customerid and generates one //to link your customer record to stripe, you will need to pass it as metadata final map\<string, string> metadata = new hashmap(); metadata put("customerid", customerid); if(customeralternateidname != null || !customeralternateidname isempty()) { metadata put(customeralternateidname, customeralternateidvalue); } final customercreateparams params = customercreateparams builder() setemail(customeremail) setmetadata(metadata) build(); //create customer in stripe final customer stripecustomer = customer create(params); system out println(string format("amberflo customer with id %s created in stripe with id %s", customerid, stripecustomer getid())); final map\<string, string> traits = new hashmap(); if(customeralternateidname != null && !customeralternateidname isempty()) { traits put(customeralternateidname, customeralternateidvalue); } traits put("stripeid", stripecustomer getid()); final customerdetails customerdetails = new customerdetails(customerid, customername, customeremail, traits); //create customer in amberflo use the stripe id to link customers in amberflo and stripe //this will be used for payment automation of amberflo invoices in stripe customerdetailsclient addorupdate(customerdetails); system out println(string format("amberflo customer with id %s created in amberflo", customerid)); //assign customer to a default product plan final string productplanid = amberflo product plan id; final customerproductplan customerproductplan = new customerproductplan(productplanid, customerid); customerproductplanclient addorupdate(customerproductplan); system out println(string format("amberflo customer with id %s assigned to product plan %s in amberflo", customerid, productplanid)); } catch (exception ex) { system out println(string format("amberflo customer creation failed %s", ex getmessage())); ex printstacktrace(); } } }