cancel
Showing results for 
Search instead for 
Did you mean: 

paypal not returning to mo application after payment

Highlighted
New Community Member

paypal not returning to mo application after payment

Hi,

In my current Java/Spring project, I am using the paypal-java-sdk to try implement a payment procedure to the application. I managed to make this work a long time ago, in an old project, but right now I am struggling with a issue for a while now. The application flow goes normally after the click on the "checkout" button on my view, going to the page to select the payment method. But either if I finalize or cancel the payment, instead of returning to my application, the paypal website is redirect to my account page. Below is the code I have right now. Anyone have any idea of what I am missing here?

 

in my controller:

 

  @RequestMapping(value = "/checkout", method=RequestMethod.GET)
  public String checkout(@RequestParam("usuario_id") Integer usuario_id, @RequestParam(value="payerId", required=false) String payerId, @RequestParam(value="guid", required=false) String guid) throws com.paypal.base.rest.PayPalRESTException {
    return "redirect:"+this.serv.checkout(usuario_id, payerId, guid);
  }

 

in my service class:

 

  public String checkout(Integer usuario_id, String payerId, String guid) throws com.paypal.base.rest.PayPalRESTException {
    Usuario usuario = this.dao.findBy("id", usuario_id);

    String clientId = paypalDao.get().getClientId();
    String clientSecret = paypalDao.get().getClientSecret();
    APIContext apiContext = new APIContext(clientId, clientSecret, "sandbox");

    String redirectURL = "";
    if(payerId != null) {
      if(guid != null) {
        Payment payment = new Payment();
        payment.setId(map.get(guid));
        PaymentExecution paymentExecution = new PaymentExecution();
        paymentExecution.setPayerId(payerId);
        payment.execute(apiContext, paymentExecution);

        Pedido pedido = new Pedido();
        pedido.setProdutos(new HashSet<Produto>());
        Cesta cesta = usuario.getCesta();
        if(cesta.getProdutos() != null) {
          for(Produto produto : cesta.getProdutos())
            pedido.getProdutos().add(produto);
          cesta.getProdutos().clear();
          cestaDao.update(cesta);
        }
        pedido.setDataCompra(new java.util.Date());
        pedidoDao.insert(pedido);
        if(usuario.getPedidos() == null) {
          usuario.setPedidos(new HashSet<Pedido>());
          usuario.getPedidos().add(pedido);
          this.dao.update(usuario);
        } else {
          usuario.getPedidos().add(pedido);
          this.dao.update(usuario);
        }
        redirectURL =  "/order/"+pedido.getId().toString();
      }
    } else {
      Payment createdPayment = createPayment(usuario, apiContext);
      Iterator<Links> links = createdPayment.getLinks().iterator();
      while (links.hasNext()) {
        Links link = links.next();
        if (link.getRel().equalsIgnoreCase("approval_url"))
          redirectURL = link.getHref();
      }
      map.put(guid, createdPayment.getId());
    }
    return redirectURL;
  }

  public Payment createPayment(Usuario usuario, APIContext apiContext) throws com.paypal.base.rest.PayPalRESTException  {
    Amount amount = new Amount();
    amount.setCurrency("BRL");
    amount.setTotal(this.cart_total(usuario.getId()).toString());

    String desc = "Lista de produtos comprados\n";
    for(Produto produto : usuario.getCesta().getProdutos())
      desc = desc + "* " + produto.getNome() + "\n";

    Transaction transaction = new Transaction();
    transaction.setAmount(amount);
    transaction.setDescription(desc);

    java.util.List<Transaction> transactions = new java.util.ArrayList<Transaction>();
    transactions.add(transaction);

    Payer payer = new Payer();
    payer.setPaymentMethod("paypal");

    Payment payment = new Payment();
    payment.setIntent("sale");
    payment.setPayer(payer);
    payment.setTransactions(transactions);

    RedirectUrls redirectUrls = new RedirectUrls();
    redirectUrls.setCancelUrl(request.getContextPath() + "/cancel");
    redirectUrls.setReturnUrl(request.getContextPath() + "/checkout?usuario_id="+usuario.getId()+"?guid="+UUID.randomUUID().toString());
    payment.setRedirectUrls(redirectUrls);

    return payment.create(apiContext);
  }