diff --git a/dist/chargebee-java-2.6.8.jar b/dist/chargebee-java-2.6.8.jar index 0c426e58..525b117b 100644 Binary files a/dist/chargebee-java-2.6.8.jar and b/dist/chargebee-java-2.6.8.jar differ diff --git a/src/main/java/com/chargebee/AsyncRequestInterceptor.java b/src/main/java/com/chargebee/AsyncRequestInterceptor.java new file mode 100644 index 00000000..c9820aef --- /dev/null +++ b/src/main/java/com/chargebee/AsyncRequestInterceptor.java @@ -0,0 +1,8 @@ +package com.chargebee; + +import com.chargebee.internal.AsyncResult; + +public interface AsyncRequestInterceptor { + + public AsyncResult handleRequest(AsyncRequestWrap requestWrap) throws Exception; +} diff --git a/src/main/java/com/chargebee/AsyncRequestWrap.java b/src/main/java/com/chargebee/AsyncRequestWrap.java new file mode 100644 index 00000000..e4f68202 --- /dev/null +++ b/src/main/java/com/chargebee/AsyncRequestWrap.java @@ -0,0 +1,21 @@ +package com.chargebee; + +import com.chargebee.internal.HttpUtil; +import com.chargebee.internal.ListRequest; +import com.chargebee.internal.RequestBase; + +public class AsyncRequestWrap { + public final Environment env; + public final T request; + public final boolean isListRequest; + public final HttpUtil.Method method; + public final String uri; + + public AsyncRequestWrap(Environment env, T request, HttpUtil.Method method, String uri) { + this.env = env; + this.request = request; + this.method = method; + this.isListRequest = request instanceof ListRequest; + this.uri = uri; + } +} diff --git a/src/main/java/com/chargebee/Environment.java b/src/main/java/com/chargebee/Environment.java index c04a1822..9b2ed24e 100644 --- a/src/main/java/com/chargebee/Environment.java +++ b/src/main/java/com/chargebee/Environment.java @@ -39,6 +39,8 @@ public class Environment { private static Environment defaultEnv; // singleton private RequestInterceptor reqInterceptor; + + private AsyncRequestInterceptor asyncReqInterceptor; public Environment(String siteName, String apiKey) { this(siteName, apiKey, null); @@ -57,14 +59,22 @@ public static void configure(String siteName, String apikey) { Environment.defaultEnv = new Environment(siteName, apikey); } - public static void reqInterceptor(RequestInterceptor reqInterceptor) { - defaultConfig().reqInterceptor = reqInterceptor; + public void reqInterceptor(RequestInterceptor reqInterceptor) { + this.reqInterceptor = reqInterceptor; + } + + public void asyncReqInterceptor(AsyncRequestInterceptor asyncReqInterceptor) { + this.asyncReqInterceptor = asyncReqInterceptor; } public RequestInterceptor reqInterceptor() { return reqInterceptor; } + public AsyncRequestInterceptor asyncReqInterceptor() { + return asyncReqInterceptor; + } + public static Environment defaultConfig() { if(defaultEnv == null) { throw new RuntimeException("The default environment has not been configured"); diff --git a/src/main/java/com/chargebee/internal/AsyncResult.java b/src/main/java/com/chargebee/internal/AsyncResult.java new file mode 100644 index 00000000..93509a82 --- /dev/null +++ b/src/main/java/com/chargebee/internal/AsyncResult.java @@ -0,0 +1,4 @@ +package com.chargebee.internal; + +public interface AsyncResult { +} diff --git a/src/main/java/com/chargebee/internal/ListRequest.java b/src/main/java/com/chargebee/internal/ListRequest.java index 3fae7057..b2e7d8c9 100644 --- a/src/main/java/com/chargebee/internal/ListRequest.java +++ b/src/main/java/com/chargebee/internal/ListRequest.java @@ -1,5 +1,6 @@ package com.chargebee.internal; +import com.chargebee.AsyncRequestWrap; import com.chargebee.RequestWrap; import com.chargebee.Environment; import com.chargebee.ListResult; @@ -59,6 +60,14 @@ public ListResult call() throws Exception { return (ListResult) (env.reqInterceptor() != null ? env.reqInterceptor().handleRequest(c) : c.call()); } + + public final AsyncResult asyncRequest(Environment env) throws IOException, Exception { + if(env.asyncReqInterceptor() == null) { + throw new RuntimeException("Async Request interceptor cannot be null"); + } + return env.asyncReqInterceptor().handleRequest(new AsyncRequestWrap(env, this, HttpUtil.Method.GET, this.uri)); + + } private static ListResult _request(Environment env, ListRequest req) throws IOException { if (env == null) { diff --git a/src/main/java/com/chargebee/internal/Request.java b/src/main/java/com/chargebee/internal/Request.java index 7e3f0729..f643dbff 100644 --- a/src/main/java/com/chargebee/internal/Request.java +++ b/src/main/java/com/chargebee/internal/Request.java @@ -33,6 +33,13 @@ public Result call() throws Exception { return (Result) (env.reqInterceptor() != null ? env.reqInterceptor().handleRequest(c) : c.call()); } + public final AsyncResult asyncRequest(Environment env) throws Exception { + if(env.asyncReqInterceptor() == null) { + throw new RuntimeException("Async Request interceptor cannot be null"); + } + return env.asyncReqInterceptor().handleRequest(new AsyncRequestWrap(env, this, this.httpMeth, this.uri)); + } + private static Result _request(Environment env, Request req) throws IOException { if (env == null) { throw new RuntimeException("Environment cannot be null"); @@ -52,5 +59,4 @@ private static Result _request(Environment env, Request req) throws IOExcepti public Params params() { return params; } - } diff --git a/src/main/java/com/chargebee/internal/RequestBase.java b/src/main/java/com/chargebee/internal/RequestBase.java index 55897eab..aa1f32ee 100644 --- a/src/main/java/com/chargebee/internal/RequestBase.java +++ b/src/main/java/com/chargebee/internal/RequestBase.java @@ -18,6 +18,10 @@ public Params params() { return params; } + public Map headers() { + return headers; + } + public U header(String headerName,String headerValue){ headers.put(headerName, headerValue); return (U)this; diff --git a/src/main/java/com/chargebee/models/Invoice.java b/src/main/java/com/chargebee/models/Invoice.java index fa4d768e..92c5087e 100644 --- a/src/main/java/com/chargebee/models/Invoice.java +++ b/src/main/java/com/chargebee/models/Invoice.java @@ -807,9 +807,9 @@ public static ChargeAddonRequest chargeAddon() throws IOException { return new ChargeAddonRequest(Method.POST, uri); } - public static Request stopDunning(String id) throws IOException { + public static StopDunningRequest stopDunning(String id) throws IOException { String uri = uri("invoices", nullCheck(id), "stop_dunning"); - return new Request(Method.POST, uri); + return new StopDunningRequest(Method.POST, uri); } public static ImportInvoiceRequest importInvoice() throws IOException { @@ -864,9 +864,9 @@ public static AddAddonChargeRequest addAddonCharge(String id) throws IOException return new AddAddonChargeRequest(Method.POST, uri); } - public static Request close(String id) throws IOException { + public static CloseRequest close(String id) throws IOException { String uri = uri("invoices", nullCheck(id), "close"); - return new Request(Method.POST, uri); + return new CloseRequest(Method.POST, uri); } public static CollectPaymentRequest collectPayment(String id) throws IOException { @@ -1217,6 +1217,24 @@ public Params params() { } } + public static class StopDunningRequest extends Request { + + private StopDunningRequest(Method httpMeth, String uri) { + super(httpMeth, uri); + } + + public StopDunningRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + + @Override + public Params params() { + return params; + } + } + public static class ImportInvoiceRequest extends Request { private ImportInvoiceRequest(Method httpMeth, String uri) { @@ -1642,6 +1660,12 @@ private ApplyPaymentsRequest(Method httpMeth, String uri) { super(httpMeth, uri); } + public ApplyPaymentsRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + public ApplyPaymentsRequest transactionId(int index, String transactionId) { params.addOpt("transactions[id][" + index + "]", transactionId); return this; @@ -1657,7 +1681,13 @@ public static class ApplyCreditsRequest extends Request { private ApplyCreditsRequest(Method httpMeth, String uri) { super(httpMeth, uri); } - + + public ApplyCreditsRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + public ApplyCreditsRequest creditNoteId(int index, String creditNoteId) { params.addOpt("credit_notes[id][" + index + "]", creditNoteId); return this; @@ -1838,6 +1868,10 @@ public AddChargeRequest avalaraServiceType(Integer avalaraServiceType) { return this; } + public AddChargeRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } public AddChargeRequest lineItemDateFrom(Timestamp lineItemDateFrom) { params.addOpt("line_item[date_from]", lineItemDateFrom); @@ -1879,6 +1913,12 @@ public AddAddonChargeRequest addonUnitPrice(Integer addonUnitPrice) { } + public AddAddonChargeRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + public AddAddonChargeRequest lineItemDateFrom(Timestamp lineItemDateFrom) { params.addOpt("line_item[date_from]", lineItemDateFrom); return this; @@ -1895,6 +1935,24 @@ public Params params() { } } + public static class CloseRequest extends Request { + + private CloseRequest(Method httpMeth, String uri) { + super(httpMeth, uri); + } + + public CloseRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + + @Override + public Params params() { + return params; + } + } + public static class CollectPaymentRequest extends Request { private CollectPaymentRequest(Method httpMeth, String uri) { @@ -1919,6 +1977,12 @@ public CollectPaymentRequest paymentSourceId(String paymentSourceId) { } + public CollectPaymentRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + @Override public Params params() { return params; @@ -2173,6 +2237,12 @@ public UpdateDetailsRequest poNumber(String poNumber) { } + public UpdateDetailsRequest comment(String comment) { + params.addOpt("comment", comment); + return this; + } + + public UpdateDetailsRequest billingAddressFirstName(String billingAddressFirstName) { params.addOpt("billing_address[first_name]", billingAddressFirstName); return this;