package com.paymill.services; import java.util.List; import com.paymill.utils.HttpClient; import com.paymill.utils.ParameterMap; import org.apache.commons.lang3.StringUtils; import com.paymill.context.PaymillContext; import com.paymill.models.Client; import com.paymill.models.Fee; import com.paymill.models.Payment; import com.paymill.models.PaymillList; import com.paymill.models.Preauthorization; import com.paymill.models.Transaction; /** * The {@link TransactionService} is used to list, create, edit and update PAYMILL {@link Transaction}s. * @author Vassil Nikolov * @since 3.0.0 */ public class TransactionService extends AbstractService { private final static String PATH = "/transactions"; private TransactionService( HttpClient httpClient ) { super( httpClient ); } /** * This function returns a {@link List} of PAYMILL {@link Transaction} objects. * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Transaction}s and their total count. */ public PaymillList<Transaction> list() { return this.list( null, null, null, null ); } /** * This function returns a {@link List} of PAYMILL {@link Transaction} 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 Transaction}s and their total count. */ public PaymillList<Transaction> list( Integer count, Integer offset ) { return this.list( null, null, count, offset ); } /** * This function returns a {@link List} of PAYMILL {@link Transaction} 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.Transaction.Filter} or <code>null</code> * @param order * {@link com.paymill.models.Transaction.Order} or <code>null</code> * @return {@link PaymillList} which contains a {@link List} of PAYMILL {@link Transaction}s and their total count. */ public PaymillList<Transaction> list( Transaction.Filter filter, Transaction.Order order ) { return this.list( filter, order, null, null ); } /** * This function returns a {@link List} of PAYMILL {@link Transaction} 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.Transaction.Filter} or <code>null</code> * @param order * {@link com.paymill.models.Transaction.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 Transaction}s and their total count. */ public PaymillList<Transaction> list( Transaction.Filter filter, Transaction.Order order, Integer count, Integer offset ) { return RestfulUtils.list( TransactionService.PATH, filter, order, count, offset, Transaction.class, super.httpClient ); } /** * This function refresh and returns the detailed information of the concrete requested {@link Transaction}. * @param transaction * A {@link Transaction} with Id. * @return Refreshed instance of the given {@link Transaction}. */ public Transaction get( Transaction transaction ) { return RestfulUtils.show( TransactionService.PATH, transaction, Transaction.class, super.httpClient ); } /** * This function refresh and returns the detailed information of the concrete requested {@link Transaction}. * @param transactionId * The Id of an existing {@link Transaction}. * @return Refreshed instance of the given {@link Transaction}. */ public Transaction get( String transactionId ) { return this.get( new Transaction( transactionId ) ); } /** * Executes a {@link Transaction} with token for the given amount in the given currency. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithToken( String token, Integer amount, String currency ) { return this.createWithTokenAndFee( token, amount, currency, null, null ); } /** * Executes a {@link Transaction} with token for the given amount in the given currency. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithToken( String token, Integer amount, String currency, String description ) { return this.createWithTokenAndFee( token, amount, currency, description, null ); } /** * Executes a {@link Transaction} with token for the given amount in the given currency. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param fee * A {@link Fee}. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithTokenAndFee( String token, Integer amount, String currency, Fee fee ) { return this.createWithTokenAndFee( token, amount, currency, null, fee ); } /** * Executes a {@link Transaction} with token for the given amount in the given currency. * @param token * Token generated by PAYMILL Bridge, which represents a credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @param fee * A {@link Fee}. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithTokenAndFee( String token, Integer amount, String currency, String description, Fee fee ) { ValidationUtils.validatesToken( token ); ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); ValidationUtils.validatesFee( fee ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "token", token ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "source", String.format( "%s-%s", PaymillContext.getProjectName(), PaymillContext.getProjectVersion() ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); if( fee != null && fee.getAmount() != null ) params.add( "fee_amount", String.valueOf( fee.getAmount() ) ); if( fee != null && StringUtils.isNotBlank( fee.getPayment() ) ) params.add( "fee_payment", fee.getPayment() ); return RestfulUtils.create( TransactionService.PATH, params, Transaction.class, super.httpClient ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param payment * A PAYMILL {@link Payment} representing credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPayment( Payment payment, Integer amount, String currency ) { return this.createWithPayment( payment, amount, currency, null ); } public Transaction createWithPayment( String paymentId, Integer amount, String currency ) { return this.createWithPayment( new Payment( paymentId ), amount, currency, null ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param payment * A PAYMILL {@link Payment} representing credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPayment( Payment payment, Integer amount, String currency, String description ) { ValidationUtils.validatesPayment( payment ); ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "payment", payment.getId() ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "source", String.format( "%s-%s", PaymillContext.getProjectName(), PaymillContext.getProjectVersion() ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); return RestfulUtils.create( TransactionService.PATH, params, Transaction.class, super.httpClient ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param paymentId * The Id of a PAYMILL {@link Payment} representing credit card or direct debit. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPayment( String paymentId, Integer amount, String currency, String description ) { return this.createWithPayment( new Payment( paymentId ), amount, currency, description ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param payment * A PAYMILL {@link Payment} representing credit card or direct debit. * @param client * The PAYMILL {@link Client} which have to be charged. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPaymentAndClient( Payment payment, Client client, Integer amount, String currency ) { return this.createWithPaymentAndClient( payment, client, amount, currency, null ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param paymentId * The Id of a PAYMILL {@link Payment} representing credit card or direct debit. * @param clientId * The Id of a PAYMILL {@link Client} which have to be charged. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPaymentAndClient( String paymentId, String clientId, Integer amount, String currency ) { return this.createWithPaymentAndClient( paymentId, clientId, amount, currency, null ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param payment * A PAYMILL {@link Payment} representing credit card or direct debit. * @param client * The PAYMILL {@link Client} which have to be charged. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPaymentAndClient( Payment payment, Client client, Integer amount, String currency, String description ) { ValidationUtils.validatesPayment( payment ); ValidationUtils.validatesClient( client ); ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "payment", payment.getId() ); params.add( "client", client.getId() ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "source", String.format( "%s-%s", PaymillContext.getProjectName(), PaymillContext.getProjectVersion() ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); return RestfulUtils.create( TransactionService.PATH, params, Transaction.class, super.httpClient ); } /** * Executes a {@link Transaction} with {@link Payment} for the given amount in the given currency. * @param paymentId * The Id of a PAYMILL {@link Payment} representing credit card or direct debit. * @param clientId * The Id of a PAYMILL {@link Client} which have to be charged. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPaymentAndClient( String paymentId, String clientId, Integer amount, String currency, String description ) { return this.createWithPaymentAndClient( new Payment( paymentId ), new Client( clientId ), amount, currency, description ); } /** * Executes a {@link Transaction} with {@link Preauthorization} for the given amount in the given currency. * @param preauthorization * A {@link Preauthorization}, which has reserved some money from the client’s credit card. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPreauthorization( Preauthorization preauthorization, Integer amount, String currency ) { return this.createWithPreauthorization( preauthorization.getId(), amount, currency, null ); } /** * Executes a {@link Transaction} with {@link Preauthorization} for the given amount in the given currency. * @param preauthorizationId * The Id of a {@link Preauthorization}, which has reserved some money from the client’s credit card. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPreauthorization( String preauthorizationId, Integer amount, String currency ) { return this.createWithPreauthorization( preauthorizationId, amount, currency, null ); } /** * Executes a {@link Transaction} with {@link Preauthorization} for the given amount in the given currency. * @param preauthorization * A {@link Preauthorization}, which has reserved some money from the client’s credit card. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPreauthorization( Preauthorization preauthorization, Integer amount, String currency, String description ) { return this.createWithPreauthorization( preauthorization.getId(), amount, currency, description ); } /** * Executes a {@link Transaction} with {@link Preauthorization} for the given amount in the given currency. * @param preauthorizationId * The Id of a {@link Preauthorization}, which has reserved some money from the client’s credit card. * @param amount * Amount (in cents) which will be charged. * @param currency * ISO 4217 formatted currency code. * @param description * A short description for the transaction. * @return {@link Transaction} object indicating whether a the call was successful or not. */ public Transaction createWithPreauthorization( String preauthorizationId, Integer amount, String currency, String description ) { ValidationUtils.validatesId( preauthorizationId ); ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); ParameterMap<String, String> params = new ParameterMap<String, String>(); params.add( "preauthorization", preauthorizationId ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "source", String.format( "%s-%s", PaymillContext.getProjectName(), PaymillContext.getProjectVersion() ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); return RestfulUtils.create( TransactionService.PATH, params, Transaction.class, super.httpClient ); } /** * This function updates the description of a {@link Transaction} and refresh its data. * @param transaction * A {@link Transaction} to be updated. */ public void update( Transaction transaction ) { RestfulUtils.update( TransactionService.PATH, transaction, Transaction.class, super.httpClient ); } }