package com.paymill.services; import java.util.List; import com.paymill.models.Client; import com.paymill.models.Payment; import com.paymill.models.PaymillList; 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. * @author Vassil Nikolov * @since 3.0.0 */ public class PaymentService extends AbstractService { private final static String PATH = "/payments"; private PaymentService( HttpClient httpClient ) { super( httpClient ); } /** * This function returns a {@link List} of PAYMILL {@link Payment} objects. * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Payment}s and their total count. */ public PaymillList<Payment> list() { return this.list( null, null, null, null ); } /** * This function returns a {@link List} of PAYMILL {@link Payment} objects, overriding the default count and offset. * @param count * Max {@link Integer} of returned objects in the {@link PaymillList} * @param offset * {@link Integer} to start from. * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Payment}s and their total count. */ public PaymillList<Payment> list( Integer count, Integer offset ) { return this.list( null, null, count, offset ); } /** * This function returns a {@link List} of PAYMILL {@link Payment} objects. In which order this list is returned depends on the * optional parameters. If <code>null</code> is given, no filter or order will be applied. * @param filter * {@link com.paymill.models.Payment.Filter} or <code>null</code> * @param order * {@link com.paymill.models.Payment.Order} or <code>null</code> * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Payment}s and their total count. */ public PaymillList<Payment> list( Payment.Filter filter, Payment.Order order ) { return this.list( filter, order, null, null ); } /** * This function returns a {@link List} of PAYMILL {@link Payment} objects. In which order this list is returned depends on the * optional parameters. If <code>null</code> is given, no filter or order will be applied, overriding the default count and * offset. * @param filter * {@link com.paymill.models.Payment.Filter} or <code>null</code> * @param order * {@link com.paymill.models.Payment.Order} or <code>null</code> * @param count * Max {@link Integer} of returned objects in the {@link PaymillList} * @param offset * {@link Integer} to start from. * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Payment}s and their total count. */ public PaymillList<Payment> list( Payment.Filter filter, Payment.Order order, Integer count, Integer offset ) { return RestfulUtils.list( PaymentService.PATH, filter, order, count, offset, Payment.class, super.httpClient ); } /** * Returns and refresh data of a specific {@link Payment}. * @param payment * A {@link Payment} with Id. * @return Refreshed instance of the given {@link Payment}. */ public Payment get( Payment payment ) { return RestfulUtils.show( PaymentService.PATH, payment, Payment.class, super.httpClient ); } /** * Returns and refresh data of a specific {@link Payment}. * @param paymentId * A {@link Payment} with Id. * @return Refreshed instance of the given {@link Payment}. */ public Payment get( String paymentId ) { return this.get( new Payment( paymentId ) ); } /** * Creates a credit card or direct debit {@link Payment} from a given token. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @return New PAYMILL {@link Payment}. */ public Payment createWithToken( String token ) { ValidationUtils.validatesToken( token ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "token", token ); return RestfulUtils.create( PaymentService.PATH, params, Payment.class, super.httpClient ); } /** * Creates a credit card or direct debit {@link Payment} from a given token. With the provided {@link Client}, the * {@link Payment} will be created and subsequently be added to the given {@link Client}. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param client * A {@link Client}, which is already stored in PAYMILL. * @return New PAYMILL {@link Payment}. */ public Payment createWithTokenAndClient( String token, Client client ) { if( client == null ) throw new IllegalArgumentException( "Client can not be null" ); return this.createWithTokenAndClient( token, client.getId() ); } /** * Creates a credit card or direct debit {@link Payment} from a given token. With the provided {@link Client}, the * {@link Payment} will be created and subsequently be added to the given {@link Client}. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param clientId * The Id of a {@link Client}, which is already stored in PAYMILL. * @return New PAYMILL {@link Payment}. */ public Payment createWithTokenAndClient( String token, String clientId ) { ValidationUtils.validatesToken( token ); ValidationUtils.validatesId( clientId ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "token", token ); params.add( "client", clientId ); return RestfulUtils.create( PaymentService.PATH, params, Payment.class, super.httpClient ); } /** * Deletes the specified {@link Payment}. * @param payment * {@link Payment} to be deleted. */ public void delete( Payment payment ) { RestfulUtils.delete( PaymentService.PATH, payment, Payment.class, super.httpClient ); } /** * Deletes the specified {@link Payment}. * @param paymentId * The Id of the {@link Payment} to be deleted. */ public void delete( String paymentId ) { this.delete( new Payment( paymentId ) ); } }