Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ MailerLite Java SDK
- [Create a webhook](#update-a-webhook)
- [Update a webhook](#update-a-webhook)
- [Delete a webhook](#delete-a-webhook)
- [E-commerce](#e-commerce)
- [Shops](#shops)
- [Products and variants](#products-and-variants)
- [Categories](#categories)
- [Customers, orders and carts](#customers-orders-and-carts)
- [Imports](#imports)
- [Revenue reports and settings](#revenue-reports-and-settings)

# Installation

Expand Down Expand Up @@ -1235,6 +1242,107 @@ class Example {
}
```

## E-commerce

Shop-level resources hang off `ecommerce()`, and everything inside a shop takes the shop ID first. Each resource follows the same pattern as the rest of the SDK: `retriever()` for reads, `builder()` for creates and updates, and `delete()`. Every call throws `MailerLiteException`.

```java
MailerLite ml = new MailerLite();
ml.setToken("Your API Token");
Ecommerce ecommerce = ml.ecommerce();
```

### Shops

```java
ShopsList shops = ecommerce.shops().retriever().limit(25).get();
SingleShop shop = ecommerce.shops().retriever().getSingle("shopId");
ecommerce.shops().builder().name("My shop").url("https://shop.example.com").currency("EUR").create();
ecommerce.shops().builder().name("New name").update("shopId");
ecommerce.shops().delete("shopId");
```

### Products and variants

```java
ProductsList products = ecommerce.products("shopId").retriever().enabledFilter(true).sort("-created_at").get();
SingleProduct product = ecommerce.products("shopId").retriever().getSingle("productId");
ecommerce.products("shopId").builder().name("Red T-shirt").price(19.99).categoryId("categoryId").create();
ecommerce.products("shopId").builder().price(24.99).update("productId");
ecommerce.products("shopId").delete("productId");
ecommerce.products("shopId").changeStatus("productId", false);
ecommerce.products("shopId").assignCategories("productId", new String[] {"categoryId"}, true);

ProductVariantsList variants = ecommerce.products("shopId").variants("productId").retriever().get();
ecommerce.products("shopId").variants("productId").builder().name("Extra large").attribute("size", "XL").update("variantId");
ecommerce.products("shopId").variants("productId").delete("variantId");
```

### Categories

```java
CategoriesList categories = ecommerce.categories("shopId").retriever().get();
ecommerce.categories("shopId").builder().name("Hats").create();
ecommerce.categories("shopId").builder().name("Caps").update("categoryId");
ecommerce.categories("shopId").delete("categoryId");
ecommerce.categories("shopId").retriever().getProducts("categoryId");
ecommerce.categories("shopId").assignProduct("categoryId", "productId");
ecommerce.categories("shopId").unassignProduct("categoryId", "productId");
```

### Customers, orders and carts

```java
CustomersList customers = ecommerce.customers("shopId").retriever().emailFilter("customer@example.com").get();
ecommerce.customers("shopId").builder().email("customer@example.com").createSubscriber(true).create();
ecommerce.customers("shopId").builder().name("Jane").update("customerId");
ecommerce.customers("shopId").delete("customerId");

OrdersList orders = ecommerce.orders("shopId").retriever().attributedFilter(true).emailIdFilter("emailId").get();

CustomerBuilderBody customer = new CustomerBuilderBody();
customer.email = "customer@example.com";
CartItemBuilderBody item = new CartItemBuilderBody();
item.productId = "productId";
item.quantity = 1;
ecommerce.orders("shopId").builder().customer(customer).item(item).status("complete").create();
ecommerce.orders("shopId").builder().status("complete").update("orderId");
ecommerce.orders("shopId").delete("orderId");

CartsList carts = ecommerce.carts("shopId").retriever().get();
ecommerce.carts("shopId").builder().checkoutUrl("https://shop.example.com/cart").update("cartId");
ecommerce.carts("shopId").items("cartId").retriever().get();
ecommerce.carts("shopId").items("cartId").builder().quantity(2).update("itemId");
ecommerce.carts("shopId").items("cartId").delete("itemId");
```

### Imports

```java
ProductBuilderBody product = new ProductBuilderBody();
product.name = "Red T-shirt";
ecommerce.imports("shopId").products(List.of(product));
```

`categories()`, `customers()` and `orders()` work the same way with their builder bodies.

### Revenue reports and settings

```java
SingleEcommerceReport shopStats = ecommerce.shops().ecommerceReport("shopId").startDate("2026-10-01").endDate("2026-10-31").stats();

SingleEcommerceReport campaignStats = ml.campaigns().ecommerceReport("campaignId").shopId("shopId").stats();
OrderActivityList campaignOrders = ml.campaigns().ecommerceReport("campaignId").shopId("shopId").orders();

SingleEcommerceReport subscriberStats = ml.subscribers().ecommerceReport("subscriberId").shopId("shopId").stats();

SingleAutomationEcommerceStats automationStats = ml.automations().reports("automationId").monthlyEcommerceTotals();
OrderActivityList automationOrders = ml.automations().reports("automationId").orderActivity().timeFrame("all_time").get();
SingleAutomationEcommerceStats emailStats = ml.automations().emails("automationId").ecommerceOverviewStats("emailId");

SingleEcommerceSettings settings = ecommerce.getSettings();
```

# Testing

Change the properties in the `TestHelper` class of the `com.mailerlite.tests` package to correspond to your account details, then simply run
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/mailerlite/sdk/MailerLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.mailerlite.sdk.campaigns.Campaigns;
import com.mailerlite.sdk.fields.Fields;
import com.mailerlite.sdk.forms.Forms;
import com.mailerlite.sdk.ecommerce.Ecommerce;
import com.mailerlite.sdk.groups.Groups;
import com.mailerlite.sdk.segments.Segments;
import com.mailerlite.sdk.susbcribers.Subscribers;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class MailerLite
/** The forms. */
protected Forms forms;

protected Ecommerce ecommerce;

/**
* Campaigns.
*
Expand Down Expand Up @@ -173,6 +176,16 @@ public Forms forms()
}


public Ecommerce ecommerce()
{
if (ecommerce == null) {
ecommerce = new Ecommerce(this);
}

return ecommerce;
}


/**
* Sets the MailerLite API token.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/mailerlite/sdk/campaigns/Campaigns.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mailerlite.sdk.MailerLite;
import com.mailerlite.sdk.MailerLiteApi;
import com.mailerlite.sdk.MailerLiteResponse;
import com.mailerlite.sdk.ecommerce.EcommerceReportRetriever;
import com.mailerlite.sdk.exceptions.MailerLiteException;

/**
Expand Down Expand Up @@ -88,4 +89,15 @@ public MailerLiteResponse delete(String campaignId) throws MailerLiteException
return response;
}

/**
* E-commerce revenue report of a campaign.
*
* @param campaignId the campaign id
* @return the ecommerce report retriever
*/
public EcommerceReportRetriever ecommerceReport(String campaignId)
{
String endpoint = "/campaigns/".concat(campaignId);
return new EcommerceReportRetriever(apiObjectReference, endpoint.concat("/reports/ecommerce"), endpoint.concat("/reports/ecommerce/orders"));
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/mailerlite/sdk/ecommerce/CartBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*************************************************
* MailerLite Java SDK
* https://github.com/mailerlite/mailerlite-java
*
* @author MailerLite <support@mailerlite.com>
* https://mailerlite.com
**************************************************/
package com.mailerlite.sdk.ecommerce;

import com.mailerlite.sdk.MailerLite;
import com.mailerlite.sdk.exceptions.MailerLiteException;

/**
* The Class CartBuilder.
*/
public class CartBuilder {

private MailerLite apiObjectReference;

private String shopId;

private CartBuilderBody builderBody = new CartBuilderBody();

public CartBuilder(MailerLite apiRef, String shopId)
{
apiObjectReference = apiRef;
this.shopId = shopId;
}

public CartBuilderBody body()
{
return builderBody;
}

public CartBuilder checkoutUrl(String checkoutUrl)
{
builderBody.checkoutUrl = checkoutUrl;
return this;
}

public CartBuilder cartTotal(Double cartTotal)
{
builderBody.cartTotal = cartTotal;
return this;
}

public CartBuilder customerId(String customerId)
{
builderBody.customerId = customerId;
return this;
}

public CartBuilder resourceId(String resourceId)
{
builderBody.resourceId = resourceId;
return this;
}

public SingleCart update(String cartId) throws MailerLiteException
{
return EcommerceApi.create(apiObjectReference).putRequest(EcommerceApi.shopPath(shopId, "/carts/").concat(cartId), EcommerceApi.jsonWithout(builderBody, "items"), SingleCart.class);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/mailerlite/sdk/ecommerce/CartBuilderBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*************************************************
* MailerLite Java SDK
* https://github.com/mailerlite/mailerlite-java
*
* @author MailerLite <support@mailerlite.com>
* https://mailerlite.com
**************************************************/
package com.mailerlite.sdk.ecommerce;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;

/**
* The Class CartBuilderBody.
*/
public class CartBuilderBody {

@SerializedName("checkout_url")
public String checkoutUrl;

@SerializedName("cart_total")
public Double cartTotal;

@SerializedName("ecommerce_customer_id")
public String customerId;

@SerializedName("resource_id")
public String resourceId;

@SerializedName("items")
public ArrayList<CartItemBuilderBody> items;
}
89 changes: 89 additions & 0 deletions src/main/java/com/mailerlite/sdk/ecommerce/CartItemBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*************************************************
* MailerLite Java SDK
* https://github.com/mailerlite/mailerlite-java
*
* @author MailerLite <support@mailerlite.com>
* https://mailerlite.com
**************************************************/
package com.mailerlite.sdk.ecommerce;

import com.mailerlite.sdk.MailerLite;
import com.mailerlite.sdk.exceptions.MailerLiteException;

/**
* The Class CartItemBuilder.
*/
public class CartItemBuilder {

private MailerLite apiObjectReference;

private String shopId;

private String cartId;

private CartItemBuilderBody builderBody = new CartItemBuilderBody();

public CartItemBuilder(MailerLite apiRef, String shopId, String cartId)
{
apiObjectReference = apiRef;
this.shopId = shopId;
this.cartId = cartId;
}

public CartItemBuilderBody body()
{
return builderBody;
}

public CartItemBuilder productId(String productId)
{
builderBody.productId = productId;
return this;
}

public CartItemBuilder productResourceId(String productResourceId)
{
builderBody.productResourceId = productResourceId;
return this;
}

public CartItemBuilder productVariantId(String productVariantId)
{
builderBody.productVariantId = productVariantId;
return this;
}

public CartItemBuilder variant(String variant)
{
builderBody.variant = variant;
return this;
}

public CartItemBuilder quantity(Integer quantity)
{
builderBody.quantity = quantity;
return this;
}

public CartItemBuilder price(Double price)
{
builderBody.price = price;
return this;
}

public CartItemBuilder resourceId(String resourceId)
{
builderBody.resourceId = resourceId;
return this;
}

public SingleCartItem create() throws MailerLiteException
{
return EcommerceApi.create(apiObjectReference).postRequest(EcommerceApi.shopPath(shopId, "/carts/").concat(cartId).concat("/items"), EcommerceApi.json(builderBody), SingleCartItem.class);
}

public SingleCartItem update(String itemId) throws MailerLiteException
{
return EcommerceApi.create(apiObjectReference).putRequest(EcommerceApi.shopPath(shopId, "/carts/").concat(cartId).concat("/items").concat("/").concat(itemId), EcommerceApi.json(builderBody), SingleCartItem.class);
}
}
Loading