package br.com.aspotato.pagarme.services;

import br.com.aspotato.pagarme.exceptions.InvalidFormatException;
import br.com.aspotato.pagarme.exceptions.SubmitException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;
import br.com.aspotato.pagarme.models.Transaction;
import br.com.aspotato.pagarme.utils.PagarMeUtil;
import com.mashape.unirest.http.exceptions.UnirestException;
import java.text.ParseException;
import java.util.ArrayList;
import org.json.JSONException;

public class TransactionService extends BasicService {
	
    private final String RESOURCE = "transactions"; 

    public ArrayList<Transaction> findAll() throws Exception {

            ArrayList<Transaction> transactions = new ArrayList<Transaction>();
            HttpResponse<JsonNode> jsonResponse = Unirest.get(instance.getBaseUrl() + RESOURCE)
                            .header("accept", "application/json")
                            .queryString("api_key", instance.getApi_key())
                            .asJson();
            for (int i = 0; i < jsonResponse.getBody().getArray().length(); i++) {
                    JSONObject resultObject = jsonResponse.getBody().getArray().getJSONObject(i);
                    transactions.add((Transaction) PagarMeUtil.convertJsonToObject(Transaction.class, resultObject));
            }

            return transactions;
    }
    
    /**
     * Execute a transaction
     * @param request Transaction data to be performed
     * @return Transaction object returned from Pagar.me
     */
    public Transaction executeTransaction(Transaction request) throws   IllegalAccessException
                                                                        , UnirestException
                                                                        , InvalidFormatException
                                                                        , SubmitException
                                                                        , InstantiationException
                                                                        , JSONException
                                                                        , ParseException      {
        
        JSONObject obj = this.postToRemoteResource(RESOURCE, request);
        return (Transaction) PagarMeUtil.convertJsonToObject(Transaction.class, obj);
        
    }

    /**
     * Find a transaction
     * @param id Transaction ID
     * @return Transaction Data
     * @throws UnirestException
     * @throws IllegalAccessException
     * @throws InvalidFormatException
     * @throws SubmitException
     * @throws InstantiationException
     * @throws JSONException
     * @throws ParseException 
     */
    public Transaction findbyId(String id) throws   UnirestException
                                                    , IllegalAccessException
                                                    , InvalidFormatException
                                                    , SubmitException
                                                    , InstantiationException
                                                    , JSONException
                                                    , ParseException  {

        Transaction transaction = (Transaction) this.getDataFromRemoteResouceWithId(Transaction.class, RESOURCE, id);

        return transaction;
    }
	
}

@@ -157,24 +157,75 @@
 tException;%0A
+import br.com.aspotato.pagarme.models.BankAccount;%0A
 import com.m
@@ -3512,13 +3512,1336 @@
 n;%0A    %7D
+%0A%0A    /**%0A     * Estornar a transaction%0A     * @param idTransaction identifica%C3%A7%C3%A3o da transa%C3%A7%C3%A3o%0A     * @param bankAccount necessario para estorno de boleto%0A     * @return%0A     * @throws IllegalAccessException%0A     * @throws UnirestException%0A     * @throws InvalidFormatException%0A     * @throws SubmitException%0A     * @throws InstantiationException%0A     * @throws JSONException%0A     * @throws ParseException%0A     */%0A    public Transaction refundTransaction(int idTransaction, BankAccount bankAccount) throws IllegalAccessException%0A                                                                    , UnirestException%0A                                                                    , InvalidFormatException%0A                                                                    , SubmitException%0A                                                                    , InstantiationException%0A                                                                    , JSONException%0A                                                                    , ParseException      %7B%0A        String refundEndpoint = RESOURCE + String.format(%22/%25d/refund%22, idTransaction);%0A        JSONObject obj = this.postToRemoteResource(refundEndpoint, bankAccount);%0A        return (Transaction) PagarMeUtil.convertJsonToObject(Transaction.class, obj);%0A    %7D
 %0A%09%0A%7D%0A

Add metodo para fazer estorno da transacao