diff --git a/pom.xml b/pom.xml index e868558..6db4a75 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.paymill paymill-java jar - 4.0.2-SNAPSHOT + 5.0.0-SNAPSHOT paymill-java http://www.paymill.com diff --git a/src/main/java/com/paymill/context/PaymillContext.java b/src/main/java/com/paymill/context/PaymillContext.java index c0db117..81e4369 100644 --- a/src/main/java/com/paymill/context/PaymillContext.java +++ b/src/main/java/com/paymill/context/PaymillContext.java @@ -6,9 +6,10 @@ import java.util.Date; import java.util.Properties; +import com.paymill.utils.HttpClient; +import com.paymill.utils.JerseyClient; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.DateConverter; -import org.apache.commons.lang3.StringUtils; import com.fasterxml.jackson.databind.ObjectMapper; import com.paymill.models.Deserializer; @@ -20,8 +21,6 @@ import com.paymill.services.SubscriptionService; import com.paymill.services.TransactionService; import com.paymill.services.WebhookService; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; /** * PaymillContecxt loads the context of PAYMILL for a single account, by providing a merchants private key
@@ -40,12 +39,12 @@ * @author Vassil Nikolov * @since 3.0.0 */ -public final class PaymillContext { +public class PaymillContext { public final static ObjectMapper PARSER = new ObjectMapper(); private final static Properties PROPERTIES = new Properties(); - private Client httpClient; + private final HttpClient httpClient; private ClientService clientService; private OfferService offerService; @@ -78,14 +77,20 @@ public PaymillContext( final String apiKey ) { * of infinity is declared. */ public PaymillContext( final String apiKey, Integer timeout ) { + this( new JerseyClient( apiKey, timeout ) ); + } + + /** + * Creates a PAYMILL context with the given HttpClient implementation. + * @param client + * Http client implementation. + */ + public PaymillContext( final HttpClient client ) { ConvertUtils.register( new DateConverter( null ), Date.class ); InputStream input = null; try { - this.httpClient = new Client(); - this.httpClient.setReadTimeout( timeout ); - this.httpClient.setConnectTimeout( timeout ); - this.httpClient.addFilter( new HTTPBasicAuthFilter( apiKey, StringUtils.EMPTY ) ); + this.httpClient = client; this.clientService = this.getPrivateConstructor( ClientService.class ).newInstance( this.httpClient ); this.offerService = this.getPrivateConstructor( OfferService.class ).newInstance( this.httpClient ); @@ -153,7 +158,7 @@ public WebhookService getWebhookService() { } private Constructor getPrivateConstructor( final Class clazz ) throws Exception { - Constructor declaredConstructor = clazz.getDeclaredConstructor( Client.class ); + Constructor declaredConstructor = clazz.getDeclaredConstructor( HttpClient.class ); declaredConstructor.setAccessible( true ); return declaredConstructor; } diff --git a/src/main/java/com/paymill/models/Offer.java b/src/main/java/com/paymill/models/Offer.java index bc79ac7..4d0b371 100755 --- a/src/main/java/com/paymill/models/Offer.java +++ b/src/main/java/com/paymill/models/Offer.java @@ -113,8 +113,8 @@ public Interval.Period getInterval() { return interval; } - public void setInterval( String interval ) { - this.interval = new Interval.Period( interval ); + public void setInterval(Interval.Period interval) { + this.interval = interval; } public Offer.SubscriptionCount getSubscriptionCount() { diff --git a/src/main/java/com/paymill/models/Subscription.java b/src/main/java/com/paymill/models/Subscription.java index 275f3d3..60d5927 100755 --- a/src/main/java/com/paymill/models/Subscription.java +++ b/src/main/java/com/paymill/models/Subscription.java @@ -493,7 +493,7 @@ public Subscription.Order byCreatedAt() { public enum Status { - ACTIVE("active"), INACTIVE("inactive"); + ACTIVE("active"), INACTIVE("inactive"), FAILED("failed"), EXPIRED("expired"); private String value; diff --git a/src/main/java/com/paymill/services/AbstractService.java b/src/main/java/com/paymill/services/AbstractService.java index b1a3c07..dfd1742 100644 --- a/src/main/java/com/paymill/services/AbstractService.java +++ b/src/main/java/com/paymill/services/AbstractService.java @@ -1,12 +1,12 @@ package com.paymill.services; -import com.sun.jersey.api.client.Client; +import com.paymill.utils.HttpClient; class AbstractService { - protected Client httpClient; + protected HttpClient httpClient; - protected AbstractService( Client httpClient ) { + protected AbstractService( HttpClient httpClient ) { this.httpClient = httpClient; } diff --git a/src/main/java/com/paymill/services/ClientService.java b/src/main/java/com/paymill/services/ClientService.java index e1cab4b..82d2a84 100644 --- a/src/main/java/com/paymill/services/ClientService.java +++ b/src/main/java/com/paymill/services/ClientService.java @@ -2,22 +2,21 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; import org.apache.commons.lang3.StringUtils; import com.paymill.models.Client; import com.paymill.models.PaymillList; -import com.sun.jersey.core.util.MultivaluedMapImpl; /** * The {@link ClientService} is used to list, create, edit, delete and update PAYMILL {@link Client}s. * @author Vassil Nikolov * @since 3.0.0 */ -public final class ClientService extends AbstractService { +public class ClientService extends AbstractService { - private ClientService( com.sun.jersey.api.client.Client httpClient ) { + private ClientService( HttpClient httpClient ) { super( httpClient ); } @@ -131,7 +130,7 @@ public Client createWithDescription( String description ) { * @return {@link Client} object, which represents a PAYMILL client. */ public Client createWithEmailAndDescription( String email, String description ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); if( StringUtils.isNotBlank( email ) ) params.add( "email", email ); if( StringUtils.isNotBlank( description ) ) diff --git a/src/main/java/com/paymill/services/OfferService.java b/src/main/java/com/paymill/services/OfferService.java index 2887f11..712661b 100644 --- a/src/main/java/com/paymill/services/OfferService.java +++ b/src/main/java/com/paymill/services/OfferService.java @@ -2,13 +2,12 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - import com.paymill.models.Client; import com.paymill.models.Interval; import com.paymill.models.Offer; import com.paymill.models.PaymillList; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link OfferService} is used to list, create, edit, delete and update PAYMILL {@link Offer}s. @@ -17,7 +16,7 @@ */ public class OfferService extends AbstractService { - private OfferService( com.sun.jersey.api.client.Client httpClient ) { + private OfferService( HttpClient httpClient ) { super( httpClient ); } @@ -131,7 +130,7 @@ public Offer create( Integer amount, String currency, Interval.Period interval, ValidationUtils.validatesName( name ); ValidationUtils.validatesTrialPeriodDays( trialPeriodDays ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "interval", interval.toString() ); @@ -194,7 +193,7 @@ public Offer create( Integer amount, String currency, String interval, String na * @return the updated offer. */ public Offer update( Offer offer, boolean updateSubscriptions ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "update_subscriptions", String.valueOf( updateSubscriptions ) ); return RestfulUtils.update( OfferService.PATH, offer, params, true, Offer.class, super.httpClient ); } @@ -207,7 +206,7 @@ public Offer update( Offer offer, boolean updateSubscriptions ) { * if true, the plan and all subscriptions associated with it will be deleted. If false, only the plan will be deleted. */ public void delete( Offer offer, boolean removeWithSubscriptions ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "remove_with_subscriptions", String.valueOf( removeWithSubscriptions ) ); RestfulUtils.delete( OfferService.PATH, offer, params, Offer.class, super.httpClient ); } diff --git a/src/main/java/com/paymill/services/PaymentService.java b/src/main/java/com/paymill/services/PaymentService.java index fbf7c7c..460509e 100644 --- a/src/main/java/com/paymill/services/PaymentService.java +++ b/src/main/java/com/paymill/services/PaymentService.java @@ -2,12 +2,11 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - import com.paymill.models.Client; import com.paymill.models.Payment; import com.paymill.models.PaymillList; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link PaymentService} is used to list, create, edit, delete and update PAYMILL {@link Payment}s. @@ -18,7 +17,7 @@ public class PaymentService extends AbstractService { private final static String PATH = "/payments"; - private PaymentService( com.sun.jersey.api.client.Client httpClient ) { + private PaymentService( HttpClient httpClient ) { super( httpClient ); } @@ -102,7 +101,7 @@ public Payment get( String paymentId ) { public Payment createWithToken( String token ) { ValidationUtils.validatesToken( token ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); return RestfulUtils.create( PaymentService.PATH, params, Payment.class, super.httpClient ); @@ -136,7 +135,7 @@ public Payment createWithTokenAndClient( String token, String clientId ) { ValidationUtils.validatesToken( token ); ValidationUtils.validatesId( clientId ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "client", clientId ); diff --git a/src/main/java/com/paymill/services/PreauthorizationService.java b/src/main/java/com/paymill/services/PreauthorizationService.java index 6e940d9..86d4833 100644 --- a/src/main/java/com/paymill/services/PreauthorizationService.java +++ b/src/main/java/com/paymill/services/PreauthorizationService.java @@ -2,8 +2,8 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; import org.apache.commons.lang3.StringUtils; import com.paymill.context.PaymillContext; @@ -11,7 +11,6 @@ import com.paymill.models.PaymillList; import com.paymill.models.Preauthorization; import com.paymill.models.Transaction; -import com.sun.jersey.core.util.MultivaluedMapImpl; /** * The {@link PreauthorizationService} is used to list, create and delete PAYMILL {@link Preauthorization}s. @@ -22,7 +21,7 @@ public class PreauthorizationService extends AbstractService { private final static String PATH = "/preauthorizations"; - private PreauthorizationService( final com.sun.jersey.api.client.Client httpClient ) { + private PreauthorizationService( final HttpClient httpClient ) { super( httpClient ); } @@ -130,7 +129,7 @@ public Preauthorization createWithToken( final String token, final Integer amoun ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "amount", String.valueOf( amount ) ); @@ -176,7 +175,7 @@ public Preauthorization createWithPayment( final Payment payment, final Integer ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "amount", String.valueOf( amount ) ); diff --git a/src/main/java/com/paymill/services/RefundService.java b/src/main/java/com/paymill/services/RefundService.java index 71adf7f..82630da 100644 --- a/src/main/java/com/paymill/services/RefundService.java +++ b/src/main/java/com/paymill/services/RefundService.java @@ -1,15 +1,13 @@ package com.paymill.services; -import java.util.List; - -import javax.ws.rs.core.MultivaluedMap; - -import org.apache.commons.lang3.StringUtils; - import com.paymill.models.PaymillList; import com.paymill.models.Refund; import com.paymill.models.Transaction; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; /** * The {@link RefundService} is used to list and create PAYMILL {@link Refund}s. @@ -20,7 +18,7 @@ public class RefundService extends AbstractService { private final static String PATH = "/refunds"; - private RefundService( com.sun.jersey.api.client.Client httpClient ) { + private RefundService( HttpClient httpClient ) { super( httpClient ); } @@ -96,7 +94,7 @@ public Refund get( String refundId ) { } /** - * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn’t refunded at + * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn't refunded at * all. The inserted amount will be refunded to the credit card / direct debit of the original {@link Transaction}. There will * be some fees for the merchant for every refund.
*
@@ -117,7 +115,7 @@ public Refund refundTransaction( Transaction transaction, Integer amount ) { } /** - * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn’t refunded at + * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn't refunded at * all. The inserted amount will be refunded to the credit card / direct debit of the original {@link Transaction}. There will * be some fees for the merchant for every refund.
*
@@ -138,7 +136,7 @@ public Refund refundTransaction( String transactionId, Integer amount ) { } /** - * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn’t refunded at + * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn't refunded at * all. The inserted amount will be refunded to the credit card / direct debit of the original {@link Transaction}. There will * be some fees for the merchant for every refund.
*
@@ -161,7 +159,7 @@ public Refund refundTransaction( String transactionId, Integer amount, String de } /** - * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn’t refunded at + * This function refunds a {@link Transaction} that has been created previously and was refunded in parts or wasn't refunded at * all. The inserted amount will be refunded to the credit card / direct debit of the original {@link Transaction}. There will * be some fees for the merchant for every refund.
*
@@ -182,7 +180,7 @@ public Refund refundTransaction( String transactionId, Integer amount, String de public Refund refundTransaction( Transaction transaction, Integer amount, String description ) { ValidationUtils.validatesAmount( amount ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); diff --git a/src/main/java/com/paymill/services/RestfulUtils.java b/src/main/java/com/paymill/services/RestfulUtils.java index cc38769..a65e8db 100644 --- a/src/main/java/com/paymill/services/RestfulUtils.java +++ b/src/main/java/com/paymill/services/RestfulUtils.java @@ -5,8 +5,8 @@ import java.util.ArrayList; import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; @@ -17,17 +17,13 @@ import com.paymill.models.PaymillList; import com.paymill.models.SnakeCase; import com.paymill.models.Updateable; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; final class RestfulUtils { private final static String ENDPOINT = "https://api.paymill.com/v2.1"; - static PaymillList list( String path, Object filter, Object order, Integer count, Integer offset, Class clazz, Client httpClient ) { - MultivaluedMap params = RestfulUtils.prepareFilterParameters( filter ); + static PaymillList list( String path, Object filter, Object order, Integer count, Integer offset, Class clazz, HttpClient httpClient ) { + ParameterMap params = RestfulUtils.prepareFilterParameters( filter ); String param = RestfulUtils.prepareOrderParameter( order ); if( StringUtils.isNotBlank( param ) && !StringUtils.startsWith( param, "_" ) ) { params.add( "order", param ); @@ -38,44 +34,44 @@ static PaymillList list( String path, Object filter, Object order, Intege if( offset != null && offset >= 0 ) { params.add( "offset", String.valueOf( offset ) ); } - return RestfulUtils.deserializeList( RestfulUtils.get( path, params, httpClient ), clazz ); + return RestfulUtils.deserializeList( httpClient.get( ENDPOINT + path, params ), clazz ); } - static T show( String path, T target, Class clazz, Client httpClient ) { + static T show( String path, T target, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.get( path + "/" + id, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.get( ENDPOINT + path + "/" + id ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T create( String path, MultivaluedMap params, Class clazz, Client httpClient ) { - return RestfulUtils.deserializeObject( RestfulUtils.post( path, params, httpClient ), clazz ); + static T create( String path, ParameterMap params, Class clazz, HttpClient httpClient ) { + return RestfulUtils.deserializeObject( httpClient.post( ENDPOINT + path, params ), clazz ); } - static T update( String path, T target, Class clazz, Client httpClient ) { - MultivaluedMap params = RestfulUtils.prepareEditableParameters( target ); + static T update( String path, T target, Class clazz, HttpClient httpClient ) { + ParameterMap params = RestfulUtils.prepareEditableParameters( target ); String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.put( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.put( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T update( String path, T target, MultivaluedMap params, boolean includeTargetUpdateables, Class clazz, Client httpClient ) { + static T update( String path, T target, ParameterMap params, boolean includeTargetUpdateables, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); if( includeTargetUpdateables ) { params.putAll( RestfulUtils.prepareEditableParameters( target ) ); } - T source = RestfulUtils.deserializeObject( RestfulUtils.put( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.put( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T delete( String path, T target, MultivaluedMap params, Class clazz, Client httpClient ) { + static T delete( String path, T target, ParameterMap params, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.delete( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.delete( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T delete( String path, T target, Class clazz, Client httpClient ) { + static T delete( String path, T target, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.delete( path + "/" + id, null, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.delete( ENDPOINT + path + "/" + id, null ), clazz ); return RestfulUtils.refreshInstance( source, target ); } @@ -141,8 +137,9 @@ private static PaymillList deserializeList( String content, Class claz return null; } - private static MultivaluedMap prepareEditableParameters( Object instance ) { - MultivaluedMap params = new MultivaluedMapImpl(); + private static ParameterMap prepareEditableParameters( Object instance ) { + ParameterMap params = new ParameterMap(); + for( Field field : instance.getClass().getDeclaredFields() ) { Updateable updateable = field.getAnnotation( Updateable.class ); if( updateable != null ) { @@ -176,38 +173,9 @@ private static MultivaluedMap prepareEditableParameters( Object return params; } - private static String get( String path, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.get( ClientResponse.class ); - return response.getEntity( String.class ); - } - - private static String get( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ).queryParams( params ); - ClientResponse response = webResource.get( ClientResponse.class ); - return response.getEntity( String.class ); - } - - private static String post( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.post( ClientResponse.class, params ); - return response.getEntity( String.class ); - } - - private static String put( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.put( ClientResponse.class, params ); - return response.getEntity( String.class ); - } - - private static String delete( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.delete( ClientResponse.class, params ); - return response.getEntity( String.class ); - } + private static ParameterMap prepareFilterParameters( Object instance ) { + ParameterMap params = new ParameterMap(); - private static MultivaluedMap prepareFilterParameters( Object instance ) { - MultivaluedMap params = new MultivaluedMapImpl(); if( instance == null ) return params; try { diff --git a/src/main/java/com/paymill/services/SubscriptionService.java b/src/main/java/com/paymill/services/SubscriptionService.java index cb37a75..5d49c9f 100644 --- a/src/main/java/com/paymill/services/SubscriptionService.java +++ b/src/main/java/com/paymill/services/SubscriptionService.java @@ -3,8 +3,6 @@ import java.util.Date; import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - import com.paymill.models.Client; import com.paymill.models.Interval; import com.paymill.models.Offer; @@ -12,7 +10,8 @@ import com.paymill.models.PaymillList; import com.paymill.models.Subscription; import com.paymill.models.Subscription.Creator; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link SubscriptionService} is used to list, create, edit, delete and update PAYMILL {@link Subscription}s. @@ -23,7 +22,7 @@ public class SubscriptionService extends AbstractService { private final static String PATH = "/subscriptions"; - private SubscriptionService( com.sun.jersey.api.client.Client httpClient ) { + private SubscriptionService( HttpClient httpClient ) { super( httpClient ); } @@ -152,7 +151,7 @@ public Subscription create( Payment payment, Client client, Offer offer, Integer throw new IllegalArgumentException( "Either an offer or amount, currency and interval must be set, when creating a subscription" ); } - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); ValidationUtils.validatesPayment( payment ); params.add( "payment", payment.getId() ); if( client != null ) { @@ -229,7 +228,7 @@ public Subscription create( String paymentId, String clientId, String offerId, I * @return the updated subscription */ public Subscription pause( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "pause", String.valueOf( true ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -260,7 +259,7 @@ public Subscription pause( String subscriptionId ) { * @return the updated subscription */ public Subscription unpause( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "pause", String.valueOf( false ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -371,7 +370,7 @@ public Subscription changeAmountTemporary( String subscriptionId, Integer amount } private Subscription changeAmount( Subscription subscription, Integer amount, Integer type, String currency, Interval.PeriodWithChargeDay interval ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); params.add( "amount_change_type", String.valueOf( type ) ); if( currency != null ) { @@ -489,7 +488,7 @@ public Subscription changeOfferKeepCaptureDateNoRefund( String subscription, Off private Subscription changeOffer( Subscription subscription, Offer offer, Integer type ) { ValidationUtils.validatesOffer( offer ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "offer", offer.getId() ); params.add( "offer_change_type", String.valueOf( type ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -502,7 +501,7 @@ private Subscription changeOffer( Subscription subscription, Offer offer, Intege * @return the updated subscription. */ public Subscription endTrial( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "trial_end", String.valueOf( false ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -526,7 +525,7 @@ public Subscription endTrial( String subscription ) { * @return the updated subscription. */ public Subscription endTrialAt( Subscription subscription, Date date ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "trial_end", String.valueOf( date.getTime() / 1000 ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -552,7 +551,7 @@ public Subscription endTrialAt( String subscription, Date date ) { * @return the updated subscription. */ public Subscription limitValidity( Subscription subscription, Interval.Period newValidity ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); ValidationUtils.validatesIntervalPeriod( newValidity ); params.add( "period_of_validity", newValidity.toString() ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -601,7 +600,7 @@ public Subscription limitValidity( String subscription, String newValidity ) { * @return the updated subscription. */ public Subscription unlimitValidity( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "period_of_validity", "remove" ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -662,7 +661,7 @@ public Subscription cancel( String subscriptionId ) { } private Subscription delete( Subscription subscription, boolean remove ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "remove", String.valueOf( remove ) ); return RestfulUtils.delete( SubscriptionService.PATH, subscription, params, Subscription.class, super.httpClient ); } @@ -698,36 +697,4 @@ private Subscription delete( Subscription subscription, boolean remove ) { public Subscription update( Subscription subscription ) { return RestfulUtils.update( SubscriptionService.PATH, subscription, Subscription.class, super.httpClient ); } - - /** - * Updates a subscription.Following fields will be updated:
- *

- *

    - *
  • interval (note, that nextCaptureAt will not change.) - *
  • currency - *
  • name - *
      - *

      - * To update further properties of a subscription use following methods:
      - *

      - *

        - *
      • {@link SubscriptionService#cancel(Subscription)} to cancel - *
      • {@link SubscriptionService#changeAmount(Subscription, Integer)} to change the amount - *
      • {@link SubscriptionService#changeOfferChangeCaptureDateAndRefund(Subscription, Offer)} to change the offer. - *
      • {@link SubscriptionService#changeOfferKeepCaptureDateAndRefund(Subscription, Offer)} to change the offer. - *
      • {@link SubscriptionService#changeOfferKeepCaptureDateNoRefund(Subscription, Offer)} to change the offer. - *
      • {@link SubscriptionService#endTrial(Subscription)} to end the trial - *
      • {@link SubscriptionService#limitValidity(Subscription, com.paymill.models.Interval.Period} to change the validity. - *
      • {@link SubscriptionService#pause(Subscription)} to pause - *
      • {@link SubscriptionService#unlimitValidity(Subscription)} to change the validity. - *
      • {@link SubscriptionService#unpause(Subscription)} to unpause. - *
          - *

          - * @param subscriptionId - * Id of the {@link Subscription} to be updated. - * @return - */ - public Subscription update( String subscriptionId ) { - return RestfulUtils.update( SubscriptionService.PATH, new Subscription( subscriptionId ), Subscription.class, super.httpClient ); - } } diff --git a/src/main/java/com/paymill/services/TransactionService.java b/src/main/java/com/paymill/services/TransactionService.java index eb70af3..e08f7ef 100644 --- a/src/main/java/com/paymill/services/TransactionService.java +++ b/src/main/java/com/paymill/services/TransactionService.java @@ -2,8 +2,8 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; import org.apache.commons.lang3.StringUtils; import com.paymill.context.PaymillContext; @@ -13,7 +13,6 @@ import com.paymill.models.PaymillList; import com.paymill.models.Preauthorization; import com.paymill.models.Transaction; -import com.sun.jersey.core.util.MultivaluedMapImpl; /** * The {@link TransactionService} is used to list, create, edit and update PAYMILL {@link Transaction}s. @@ -24,7 +23,7 @@ public class TransactionService extends AbstractService { private final static String PATH = "/transactions"; - private TransactionService( com.sun.jersey.api.client.Client httpClient ) { + private TransactionService( HttpClient httpClient ) { super( httpClient ); } @@ -165,7 +164,7 @@ public Transaction createWithTokenAndFee( String token, Integer amount, String c ValidationUtils.validatesCurrency( currency ); ValidationUtils.validatesFee( fee ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); @@ -216,7 +215,7 @@ public Transaction createWithPayment( Payment payment, Integer amount, String cu ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); @@ -296,7 +295,7 @@ public Transaction createWithPaymentAndClient( Payment payment, Client client, I ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "client", client.getId() ); params.add( "amount", String.valueOf( amount ) ); @@ -388,7 +387,7 @@ public Transaction createWithPreauthorization( String preauthorizationId, Intege ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "preauthorization", preauthorizationId ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); diff --git a/src/main/java/com/paymill/services/WebhookService.java b/src/main/java/com/paymill/services/WebhookService.java index 5cea454..d027325 100644 --- a/src/main/java/com/paymill/services/WebhookService.java +++ b/src/main/java/com/paymill/services/WebhookService.java @@ -2,13 +2,11 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - import com.paymill.models.PaymillList; import com.paymill.models.Webhook; import com.paymill.models.Webhook.EventType; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link WebhookService} is used to list, create, edit and update PAYMILL {@link Webhook}s. @@ -19,7 +17,7 @@ public class WebhookService extends AbstractService { private final static String PATH = "/webhooks"; - private WebhookService( Client httpClient ) { + private WebhookService( HttpClient httpClient ) { super( httpClient ); } @@ -103,7 +101,7 @@ public Webhook get( String webhookId ) { * @return A {@link Webhook} */ public Webhook createUrlWebhook( String url, Webhook.EventType[] eventTypes ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "url", url ); for( Webhook.EventType eventType : eventTypes ) @@ -121,7 +119,7 @@ public Webhook createUrlWebhook( String url, Webhook.EventType[] eventTypes ) { * @return A {@link Webhook} */ public Webhook createEmailWebhook( String email, Webhook.EventType[] eventTypes ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "email", email ); for( Webhook.EventType eventType : eventTypes ) diff --git a/src/main/java/com/paymill/utils/HttpClient.java b/src/main/java/com/paymill/utils/HttpClient.java new file mode 100644 index 0000000..7077606 --- /dev/null +++ b/src/main/java/com/paymill/utils/HttpClient.java @@ -0,0 +1,15 @@ +package com.paymill.utils; + +public interface HttpClient { + + public String get( String path ); + + public String get( String path, ParameterMap params ); + + public String post( String path, ParameterMap params ); + + public String put( String path, ParameterMap params ); + + public String delete( String path, ParameterMap params ); + +} diff --git a/src/main/java/com/paymill/utils/JerseyClient.java b/src/main/java/com/paymill/utils/JerseyClient.java new file mode 100644 index 0000000..1973f7a --- /dev/null +++ b/src/main/java/com/paymill/utils/JerseyClient.java @@ -0,0 +1,72 @@ +package com.paymill.utils; + +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.commons.lang3.StringUtils; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public final class JerseyClient implements HttpClient { + private final Client httpClient; + + public JerseyClient( final String apiKey ) { + this( apiKey, null ); + } + + public JerseyClient( final String apiKey, final Integer timeout ) { + this.httpClient = new Client(); + if( timeout != null ) { + this.httpClient.setReadTimeout( timeout ); + this.httpClient.setConnectTimeout( timeout ); + } + this.httpClient.addFilter( new HTTPBasicAuthFilter( apiKey, StringUtils.EMPTY ) ); + } + + @Override + public String get( final String path ) { + WebResource webResource = this.httpClient.resource( path ); + ClientResponse response = webResource.get( ClientResponse.class ); + return response.getEntity( String.class ); + } + + @Override + public String get( final String path, final ParameterMap params ) { + WebResource webResource = this.httpClient.resource( path ).queryParams( JerseyClient.convertMap( params ) ); + ClientResponse response = webResource.get( ClientResponse.class ); + return response.getEntity( String.class ); + } + + @Override + public String post( final String path, final ParameterMap params ) { + WebResource webResource = this.httpClient.resource( path ); + ClientResponse response = webResource.post( ClientResponse.class, JerseyClient.convertMap( params ) ); + return response.getEntity( String.class ); + } + + @Override + public String put( final String path, final ParameterMap params ) { + WebResource webResource = this.httpClient.resource( path ); + ClientResponse response = webResource.put( ClientResponse.class, JerseyClient.convertMap( params ) ); + return response.getEntity( String.class ); + } + + @Override + public String delete( final String path, final ParameterMap params ) { + WebResource webResource = this.httpClient.resource( path ); + ClientResponse response = webResource.delete( ClientResponse.class, JerseyClient.convertMap( params ) ); + return response.getEntity( String.class ); + } + + private static MultivaluedMap convertMap( final ParameterMap map ) { + if( map == null ) { + return null; + } + MultivaluedMap params = new MultivaluedMapImpl(); + params.putAll( map ); + return params; + } +} diff --git a/src/main/java/com/paymill/utils/ParameterMap.java b/src/main/java/com/paymill/utils/ParameterMap.java new file mode 100644 index 0000000..e760776 --- /dev/null +++ b/src/main/java/com/paymill/utils/ParameterMap.java @@ -0,0 +1,92 @@ +package com.paymill.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public final class ParameterMap implements Map> { + private final HashMap> map; + + public ParameterMap() { + this.map = new HashMap>(); + } + + public void add( final K key, final V value ) { + List values = this.map.get( key ); + + if( values == null ) { + values = new ArrayList(); + } + + values.add( value ); + + this.map.put( key, values ); + } + + public V getFirst( final K key ) { + return this.get( key ) != null ? this.get( key ).get( 0 ) : null; + } + + @Override + public int size() { + return this.map.size(); + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public boolean containsKey( final Object o ) { + return this.map.containsKey( o ); + } + + @Override + public boolean containsValue( final Object o ) { + return this.map.containsValue( o ); + } + + @Override + public List get( final Object o ) { + return this.map.get( o ); + } + + @Override + public List put( final K k, final List vs ) { + return this.map.put( k, vs ); + } + + @Override + public List remove( final Object o ) { + return this.map.remove( o ); + } + + @Override + public void putAll( final Map> map ) { + this.map.putAll( map ); + } + + @Override + public void clear() { + this.map.clear(); + } + + @Override + public Set keySet() { + return this.map.keySet(); + } + + @Override + public Collection> values() { + return this.map.values(); + } + + @Override + public Set>> entrySet() { + return this.map.entrySet(); + } +} diff --git a/src/test/java/com/paymill/services/OfferServiceTest.java b/src/test/java/com/paymill/services/OfferServiceTest.java index 23e3f8e..bee791d 100644 --- a/src/test/java/com/paymill/services/OfferServiceTest.java +++ b/src/test/java/com/paymill/services/OfferServiceTest.java @@ -110,7 +110,7 @@ public void testUpdateOfferAndSubscritions() { Assert.assertEquals( subscription.getOffer().getId(), offer.getId() ); Assert.assertEquals( subscription.getInterval().getInterval(), (Integer) 1 ); Assert.assertEquals( subscription.getInterval().getUnit(), Interval.Unit.MONTH ); - offer.setInterval( "2 WEEK" ); + offer.setInterval( new Interval.Period( "2 WEEK" ) ); this.offerService.update( offer, true ); Assert.assertEquals( offer.getId(), offer.getId() ); Assert.assertEquals( offer.getInterval().getInterval(), (Integer) 2 );