CUSTOMER BILLING
Customer Management
Synchronization and Mapping
2 min
to ensure customers are properly synchronized between amberflo and stripe, the sync process should take place at the time of customer creation in your system the integration flow involves creating the customer in stripe and capturing the resulting stripe customer id creating the customer in amberflo, and storing the stripe customer id in the traits field assigning the customer to a default rate plan in amberflo 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> to automate this, use the provided java helper method amberflocustomerhelper createcustomerinstripeandamberflo() call this method from your customer creation transaction logic to handle the integration seamlessly 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(); } } }
