Estou tendo implementar em java a comunicação com os gateways de pagamento PagSeguro e Pagamento Digital utilizando apache commons httpclient e não está fucionando.
Segue os códigos
Pagamento Digital
package br.com.jlartes.pagamentoDigital;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import br.com.jlartes.util.mail.CalcuteFreight;
import br.com.jlartes.util.mail.Cep;
import br.com.jlartes.vo.DemandVO;
public class PagamentoDigital {
public void send(DemandVO demand) {
String url = "https://www.pagamentodigital.com.br/checkout/pay/";
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
CalcuteFreight cf = new CalcuteFreight();
String cepOrigem = Cep.cepHome;
String codService = new String();
// Configure the form parameters
// Loja
method.addParameter("email_loja", "joelmoraes@hotmail.com");
method.addParameter("tipo_integracao", "PAD");
// Produto
for (int i = 0; i < demand.getItens().size(); i++) {
method
.addParameter("produto_codigo_" + (i + 1), String
.valueOf(demand.getItens().get(i).getProduct()
.getId()));
method.addParameter("produto_descricao_" + (i + 1), demand
.getItens().get(i).getProduct().getName());
method.addParameter("produto_qtde_" + (i + 1), String
.valueOf(demand.getItens().get(i).getQuantity()));
method.addParameter("produto_valor_" + (i + 1), String
.valueOf(demand.getItens().get(i).getProduct()
.getPrice()));
}
if (demand.getShipping().getName().equals("PAC")) {
codService = "41106";
method.addParameter("tipo_frete", "encomenda");
}
if (demand.getShipping().getName().equals("SEDEX")) {
codService = "40010";
method.addParameter("tipo_frete", "sedex");
}
double frete = cf.calculaFrete(cepOrigem, demand.getAddress()
.getCep(), codService, demand.getTotalDemand());
method.addParameter("frete", String.valueOf(frete));
// Cliente
method.addParameter("nome", demand.getUser().getName());
method.addParameter("cep", demand.getAddress().getCep());
method.addParameter("endereco", demand.getAddress().getAddress()
+ ", " + demand.getAddress().getNumber());
method.addParameter("complemento", demand.getAddress()
.getComplement());
method
.addParameter("bairro", demand.getAddress()
.getNeighborhood());
method.addParameter("cidade", demand.getAddress().getCity());
method.addParameter("estado", demand.getAddress().getState());
method.addParameter("telefone", demand.getUser().getPhone());
method.addParameter("email", demand.getUser().getEmail());
// Retorno
// method.addParameter("url_retorno", "");
// method.addParameter("redirect", "true");
// Execute the POST method
int statusCode = client.executeMethod(method);
if (statusCode != -1) {
String contents = method.getResponseBodyAsString();
System.out.println(contents);
method.releaseConnection();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
PagSeguro
package br.com.jlartes.pagseguro;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import br.com.jlartes.util.mail.CalcuteFreight;
import br.com.jlartes.util.mail.Cep;
import br.com.jlartes.vo.DemandVO;
public class PagSeguro {
public void send(DemandVO demand) {
String url = "https://pagseguro.uol.com.br/checkout/checkout.jhtml";
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
CalcuteFreight cf = new CalcuteFreight();
String cepOrigem = Cep.cepHome;
String codService = new String();
// Loja
method.addParameter("email_cobranca", "joelmoraes@hotmail.com");
method.addParameter("tipo", "CP");
method.addParameter("moeda", "BRL");
// Produto
for (int i = 0; i < demand.getItens().size(); i++) {
method.addParameter("item_id_" + (i + 1), String.valueOf(demand
.getItens().get(i).getProduct().getId()));
method.addParameter("item_descr_" + (i + 1), demand.getItens()
.get(i).getProduct().getName());
method.addParameter("item_quant_" + (i + 1), String
.valueOf(demand.getItens().get(i).getQuantity()));
method.addParameter("item_valor_" + (i + 1), String
.valueOf(demand.getItens().get(i).getProduct()
.getPrice() * 100));
}
if (demand.getShipping().getName().equals("PAC")) {
codService = "41106";
method.addParameter("tipo_frete", "EN");
}
if (demand.getShipping().getName().equals("SEDEX")) {
codService = "40010";
method.addParameter("tipo_frete", "SD");
}
double frete = cf.calculaFrete(cepOrigem, demand.getAddress()
.getCep(), codService, demand.getTotalDemand());
method.addParameter("item_frete_1", String.valueOf(frete * 100));
// Cliente
method.addParameter("cliente_nome", demand.getUser().getName());
method.addParameter("cliente_cep", demand.getAddress().getCep());
method
.addParameter("cliente_end", demand.getAddress()
.getAddress());
method.addParameter("cliente_num", demand.getAddress().getNumber());
method.addParameter("cliente_compl", demand.getAddress()
.getComplement());
method.addParameter("cliente_bairro", demand.getAddress()
.getNeighborhood());
method
.addParameter("cliente_cidade", demand.getAddress()
.getCity());
method.addParameter("cliente_uf", demand.getAddress().getState());
method.addParameter("cliente_pais", "BRA");
method.addParameter("cliente_ddd", demand.getUser().getPhone()
.substring(1, 3));
method.addParameter("cliente_tel", demand.getUser().getPhone()
.substring(3));
method.addParameter("cliente_email", demand.getUser().getEmail());
// Retorno
// Execute the POST method
int statusCode = client.executeMethod(method);
if (statusCode != -1) {
String contents = method.getResponseBodyAsString();
System.out.println(contents);
method.releaseConnection();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Alguém poderia me ajudar.
Obrigado.