Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.chargify</groupId>
<artifactId>chargify-sdk</artifactId>
<version>0.7.0</version>
<version>0.9.0</version>
<packaging>jar</packaging>

<name>Chargify SDK for Java</name>
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/chargify/Chargify.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.chargify;

import com.chargify.model.*;
import com.chargify.model.product.Product;
import com.chargify.model.product.ProductFamily;
import com.chargify.model.product.ProductPricePoint;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

public interface Chargify
{
Expand All @@ -21,6 +25,10 @@ public interface Chargify

Product findProductByApiHandle( String apiHandle );

Set<ProductPricePoint> findProductPricePointsByProductId( String productId );

Set<PricePoint> findComponentPricePoints( int componentId );

List<Product> findAllProducts();

List<Product> findProductsByProductFamilyId( String productFamilyId );
Expand All @@ -47,15 +55,21 @@ public interface Chargify

Subscription migrateSubscription( String subscriptionId, String productHandle );

Subscription migrateSubscription( String subscriptionId, String productHandle, String pricePointHandle );

Subscription reactivateSubscription( String subscriptionId );

Subscription reactivateSubscription( String subscriptionId, SubscriptionReactivationData reactivationData );

ComponentPricePointUpdate migrateSubscriptionComponentToPricePoint( String subscriptionId, int componentId,
String pricePointHandle );

List<ComponentPricePointUpdate> bulkUpdateSubscriptionComponentPricePoint( String subscriptionId, List<ComponentPricePointUpdate> items );

Subscription changeSubscriptionProduct( String subscriptionId, String productHandle, boolean delayed );

Subscription changeSubscriptionProduct( String subscriptionId, String productHandle, String pricePointHandle, boolean delayed );

RenewalPreview previewSubscriptionRenewal( String subscriptionId );

List<Metadata> createSubscriptionMetadata( String subscriptionId, Metadata... metadata );
Expand All @@ -66,17 +80,21 @@ ComponentPricePointUpdate migrateSubscriptionComponentToPricePoint( String subsc

Component createComponent( String productFamilyId, Component component );

Allocation createComponentAllocation( String subscriptionId, String componentId, Allocation allocation );
Allocation createComponentAllocation( String subscriptionId, int componentId, Allocation allocation );

AllocationPreview previewComponentAllocation( String subscriptionId, int componentId, int quantity );

List<Component> findComponentsByProductFamily( String productFamilyId );

Component findComponentByIdAndProductFamily( String componentId, String productFamilyId );
Component findComponentByIdAndProductFamily( int componentId, String productFamilyId );

ComponentWithPricePoints findComponentWithPricePointsByIdAndProductFamily( int componentId, String productFamilyId );

List<SubscriptionComponent> findSubscriptionComponents( String subscriptionId );

SubscriptionComponent findSubscriptionComponentById( String subscriptionId, String componentId );
SubscriptionComponent findSubscriptionComponentById( String subscriptionId, int componentId );

Usage reportSubscriptionComponentUsage( String subscriptionId, String componentId, Usage usage );
Usage reportSubscriptionComponentUsage( String subscriptionId, int componentId, Usage usage );

Customer createCustomer( Customer customer );

Expand Down
105 changes: 95 additions & 10 deletions src/main/java/com/chargify/ChargifyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.chargify.exceptions.ChargifyResponseErrorHandler;
import com.chargify.exceptions.ResourceNotFoundException;
import com.chargify.model.*;
import com.chargify.model.product.Product;
import com.chargify.model.product.ProductFamily;
import com.chargify.model.product.ProductPricePoint;
import com.chargify.model.wrappers.*;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.web.client.RestTemplateBuilder;
Expand All @@ -20,6 +23,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public final class ChargifyService implements Chargify
Expand Down Expand Up @@ -143,6 +147,36 @@ public Product findProductByApiHandle( String apiHandle )
}
}

@Override
public Set<ProductPricePoint> findProductPricePointsByProductId( String productId )
{
try
{
return httpClient.getForObject(
"/products/" + productId + "/price_points.json", ProductPricePointsWrapper.class )
.getPricePoints();
}
catch( ResourceNotFoundException e )
{
return null;
}
}

@Override
public Set<PricePoint> findComponentPricePoints( int componentId )
{
try
{
return httpClient.getForObject(
"/components/" + componentId + "/price_points.json", ComponentPricePointsWrapper.class )
.getPricePoints();
}
catch( ResourceNotFoundException e )
{
return null;
}
}

@Override
public List<Product> findAllProducts()
{
Expand Down Expand Up @@ -288,6 +322,18 @@ public Subscription migrateSubscription( String subscriptionId, String productHa
.getSubscription();
}

@Override
public Subscription migrateSubscription( String subscriptionId, String productHandle, String pricePointHandle )
{
final Migration migration = new Migration();
migration.setProductHandle( productHandle );
migration.setPricePointHandle( pricePointHandle );

return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/migrations.json",
new MigrationWrapper( migration ), SubscriptionWrapper.class )
.getSubscription();
}

@Override
public Subscription reactivateSubscription( String subscriptionId )
{
Expand Down Expand Up @@ -316,20 +362,43 @@ public ComponentPricePointUpdate migrateSubscriptionComponentToPricePoint( Strin
{
return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/price_points.json",
new ComponentPricePointUpdatesWrapper(
new ComponentPricePointUpdate( componentId, pricePointHandle ) ),
List.of( new ComponentPricePointUpdate( componentId, pricePointHandle ) ) ),
ComponentPricePointUpdatesWrapper.class )
.getPricePointUpdates().get( 0 );
}

@Override
public List<ComponentPricePointUpdate> bulkUpdateSubscriptionComponentPricePoint( String subscriptionId, List<ComponentPricePointUpdate> items )
{
return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/price_points.json",
new ComponentPricePointUpdatesWrapper( items ),
ComponentPricePointUpdatesWrapper.class )
.getPricePointUpdates()[ 0 ];
.getPricePointUpdates();
}

@Override
public Subscription changeSubscriptionProduct( String subscriptionId, String productHandle, boolean delayed )
{
final Subscription subscription = new Subscription();
subscription.setProductHandle( productHandle );
subscription.setProductChangeDelayed( delayed );
final SubscriptionProductUpdate productUpdate = new SubscriptionProductUpdate();
productUpdate.setProductHandle( productHandle );
productUpdate.setChangeDelayed( delayed );

return httpClient.exchange( "/subscriptions/" + subscriptionId + ".json", HttpMethod.PUT,
new HttpEntity<>( new SubscriptionWrapper( subscription ) ), SubscriptionWrapper.class )
new HttpEntity<>( new SubscriptionProductUpdateWrapper( productUpdate ) ), SubscriptionWrapper.class )
.getBody()
.getSubscription();
}

@Override
public Subscription changeSubscriptionProduct( String subscriptionId, String productHandle, String pricePointHandle, boolean delayed )
{
final SubscriptionProductUpdate productUpdate = new SubscriptionProductUpdate();
productUpdate.setProductHandle( productHandle );
productUpdate.setChangeDelayed( delayed );
productUpdate.setPricePointHandle( pricePointHandle );

return httpClient.exchange( "/subscriptions/" + subscriptionId + ".json", HttpMethod.PUT,
new HttpEntity<>( new SubscriptionProductUpdateWrapper( productUpdate ) ), SubscriptionWrapper.class )
.getBody()
.getSubscription();
}
Expand Down Expand Up @@ -404,14 +473,23 @@ public Component createComponent( String productFamilyId, Component component )
}

@Override
public Allocation createComponentAllocation( String subscriptionId, String componentId, Allocation allocation )
public Allocation createComponentAllocation( String subscriptionId, int componentId, Allocation allocation )
{
return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/components/" + componentId +
"/allocations.json",
new AllocationWrapper( allocation ), AllocationWrapper.class )
.getAllocation();
}

@Override
public AllocationPreview previewComponentAllocation( String subscriptionId, int componentId, int quantity )
{
return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/allocations/preview.json",
Map.of( "allocations", List.of( new AllocationPreview.ComponentAllocationDTO( componentId, quantity ) ) ),
AllocationPreviewWrapper.class )
.getAllocationPreview();
}

@Override
public List<Component> findComponentsByProductFamily( String productFamilyId )
{
Expand All @@ -422,7 +500,7 @@ public List<Component> findComponentsByProductFamily( String productFamilyId )
}

@Override
public Component findComponentByIdAndProductFamily( String componentId, String productFamilyId )
public Component findComponentByIdAndProductFamily( int componentId, String productFamilyId )
{
try
{
Expand All @@ -437,6 +515,13 @@ public Component findComponentByIdAndProductFamily( String componentId, String p
}
}

@Override
public ComponentWithPricePoints findComponentWithPricePointsByIdAndProductFamily( int componentId, String productFamilyId )
{
return new ComponentWithPricePoints( findComponentByIdAndProductFamily( componentId, productFamilyId ),
findComponentPricePoints( componentId ) );
}

@Override
public List<SubscriptionComponent> findSubscriptionComponents( String subscriptionId )
{
Expand All @@ -447,7 +532,7 @@ public List<SubscriptionComponent> findSubscriptionComponents( String subscripti
}

@Override
public SubscriptionComponent findSubscriptionComponentById( String subscriptionId, String componentId )
public SubscriptionComponent findSubscriptionComponentById( String subscriptionId, int componentId )
{
try
{
Expand All @@ -463,7 +548,7 @@ public SubscriptionComponent findSubscriptionComponentById( String subscriptionI
}

@Override
public Usage reportSubscriptionComponentUsage( String subscriptionId, String componentId, Usage usage )
public Usage reportSubscriptionComponentUsage( String subscriptionId, int componentId, Usage usage )
{
return httpClient.postForObject( "/subscriptions/" + subscriptionId + "/components/" + componentId +
"/usages.json",
Expand Down
113 changes: 113 additions & 0 deletions src/main/java/com/chargify/model/AllocationPreview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.chargify.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor
@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public class AllocationPreview
{
@JsonProperty( "start_date" )
private LocalDateTime startDate;
@JsonProperty( "end_date" )
private LocalDateTime endDate;
@JsonProperty( "period_type" )
private String periodType;
@JsonProperty( "total_in_cents" )
private Integer totalInCents;
@JsonProperty( "total_discount_in_cents" )
private Integer totalDiscountInCents;
@JsonProperty( "total_tax_in_cents" )
private Integer totalTaxInCents;
@JsonProperty( "subtotal_in_cents" )
private Integer subtotalInCents;
@JsonProperty( "existing_balance_in_cents" )
private Integer existingBalanceInCents;
@JsonProperty( "direction" )
private String direction;
@JsonProperty( "proration_scheme" )
private String prorationScheme;
@JsonProperty( "line_items" )
private List<LineItem> lineItems;
@JsonProperty( "allocations" )
private List<Allocation> allocations;


@NoArgsConstructor
@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public static class LineItem
{
@JsonProperty( "transaction_type" )
private String transactionType;
private ComponentKind kind;
@JsonProperty( "amount_in_cents" )
private Integer amountInCents;
private String memo;
@JsonProperty( "discount_amount_in_cents" )
private Integer discountAmountInCents;
@JsonProperty( "taxable_amount_in_cents" )
private Integer taxable_amount_in_cents;
@JsonProperty( "component_id" )
private Integer componentId;
@JsonProperty( "component_handle" )
private String componentHandle;
@JsonProperty( "accrue_charge" )
private Boolean accrueCharge;
@JsonProperty( "upgrade_charge" )
private String upgradeCharge;
}

@NoArgsConstructor
@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public static class Allocation
{
@JsonProperty( "allocation_id" )
private String allocationId;
@JsonProperty( "component_id" )
private Integer componentId;
@JsonProperty( "subscription_id" )
private Integer subscriptionId;
private Integer quantity;
@JsonProperty( "previous_quantity" )
private Integer previousQuantity;
private String memo;
private LocalDateTime timestamp;
@JsonProperty( "proration_upgrade_scheme" )
private String prorationUpgradeScheme;
@JsonProperty( "proration_downgrade_scheme" )
private String prorationDowngradeScheme;
@JsonProperty( "price_point_id" )
private Integer pricePointId;
@JsonProperty( "previous_price_point_id" )
private Integer previousPricePointId;
@JsonProperty( "component_handle" )
private String componentHandle;
@JsonProperty( "accrue_charge" )
private Boolean accrueCharge;
@JsonProperty( "upgrade_charge" )
private String upgradeCharge;
@JsonProperty( "downgrade_credit" )
private String downgradeCredit;
@JsonProperty( "created_at" )
private LocalDateTime createdAt;
}

@AllArgsConstructor
@Getter
public static class ComponentAllocationDTO
{
@JsonProperty( "component_id" )
private final int componentId;
private final int quantity;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/chargify/model/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@JsonInclude( JsonInclude.Include.NON_NULL )
public class Component implements Serializable
{
private String id;
private Integer id;

private String name;

Expand Down
Loading