com.braintreegateway.SubscriptionGateway Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.braintree-java
Show all versions of org.apache.servicemix.bundles.braintree-java
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
The newest version!
package com.braintreegateway;
import com.braintreegateway.exceptions.NotFoundException;
import com.braintreegateway.util.Http;
import com.braintreegateway.util.NodeWrapper;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* Provides methods to interact with {@link Subscription Subscriptions}.
* Including create, find, update, cancel, etc.
* This class does not need to be instantiated directly.
* Instead, use {@link BraintreeGateway#subscription()} to get an instance of this class:
*
*
* BraintreeGateway gateway = new BraintreeGateway(...);
* gateway.subscription().create(...)
*
*
* For more detailed information on {@link Subscription Subscriptions}, see https://developer.paypal.com/braintree/docs/reference/response/subscription/java
*/
public class SubscriptionGateway {
private Http http;
private Configuration configuration;
public SubscriptionGateway(Http http, Configuration configuration) {
this.http = http;
this.configuration = configuration;
}
/**
* Cancels the {@link Subscription} with the given id.
* @param id of the {@link Subscription} to cancel.
* @return a {@link Result}.
*/
public Result cancel(String id) {
NodeWrapper node = http.put(configuration.getMerchantPath() + "/subscriptions/" + id + "/cancel");
return new Result(node, Subscription.class);
}
/**
* Creates a {@link Subscription}.
* @param request the request.
* @return a {@link Result}.
*/
public Result create(SubscriptionRequest request) {
NodeWrapper node = http.post(configuration.getMerchantPath() + "/subscriptions", request);
return new Result(node, Subscription.class);
}
public Result delete(String customerId, String id) {
http.delete(configuration.getMerchantPath() + "/subscriptions/" + id);
return new Result();
}
/**
* Finds a {@link Subscription} by id.
* @param id the id of the {@link Subscription}.
* @return the {@link Subscription} or raises a {@link com.braintreegateway.exceptions.NotFoundException}.
*/
public Subscription find(String id) {
if (id == null || id.trim().equals("")) {
throw new NotFoundException();
}
return new Subscription(http.get(configuration.getMerchantPath() + "/subscriptions/" + id));
}
/**
* Updates a {@link Subscription}.
* @param id the id of the {@link Subscription}.
* @param request the request.
* @return a {@link Result}.
*/
public Result update(String id, SubscriptionRequest request) {
NodeWrapper node = http.put(configuration.getMerchantPath() + "/subscriptions/" + id, request);
return new Result(node, Subscription.class);
}
/**
* Search for a {@link Subscription}.
* @param searchRequest the {@link SubscriptionSearchRequest}.
* @return a {@link Result}.
*/
public ResourceCollection search(SubscriptionSearchRequest searchRequest) {
NodeWrapper node = http.post(configuration.getMerchantPath() + "/subscriptions/advanced_search_ids", searchRequest);
return new ResourceCollection(new SubscriptionPager(this, searchRequest), node);
}
List fetchSubscriptions(SubscriptionSearchRequest search, List ids) {
search.ids().in(ids);
NodeWrapper response = http.post(configuration.getMerchantPath() + "/subscriptions/advanced_search", search);
List items = new ArrayList();
for (NodeWrapper node : response.findAll("subscription")) {
items.add(new Subscription(node));
}
return items;
}
private Result retryCharge(SubscriptionTransactionRequest txnRequest) {
NodeWrapper response = http.post(configuration.getMerchantPath() + "/transactions", txnRequest);
return new Result(response, Transaction.class);
}
public Result retryCharge(String subscriptionId) {
return retryCharge(new SubscriptionTransactionRequest()
.subscriptionId(subscriptionId));
}
public Result retryCharge(String subscriptionId, BigDecimal amount) {
return retryCharge(new SubscriptionTransactionRequest()
.subscriptionId(subscriptionId)
.amount(amount));
}
public Result retryCharge(String subscriptionId, Boolean submitForSettlement) {
return retryCharge(new SubscriptionTransactionRequest()
.subscriptionId(subscriptionId)
.options()
.submitForSettlement(submitForSettlement).done());
}
public Result retryCharge(String subscriptionId, BigDecimal amount, Boolean submitForSettlement) {
return retryCharge(new SubscriptionTransactionRequest()
.subscriptionId(subscriptionId)
.amount(amount)
.options()
.submitForSettlement(submitForSettlement).done());
}
}