Synchronization and Mapping

Syncing customers in Amberflo and Stripe is during the creation of customers in your system.

The sample code provides the following functionality:

  1. Create the customer in Stripe to obtain the Stripe ID.
  2. Create the customer in Amberflo and set the Stripe ID from the previous step in the traits
  3. 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();
        }
    }
}