---
title: Synchronization and Mapping
slug: docs/synching-customers-with-amberflo-and-stripe
createdAt: 2024-07-19T08:23:35.817Z
---

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:

1. Creating the customer in Stripe and capturing the resulting Stripe customer ID.
2. Creating the customer in Amberflo, and storing the Stripe customer ID in the `traits` field.
3. Assigning the customer to a default rate plan in Amberflo.

Maven dependencies (check for latest versions):

```xml
<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.

```java
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();
        }
    }
}
```

