Paypal integrated with Java/Spring application never return after payment selection screen

klebermo
Contributor
Contributor

I am trying integrate the paypal java sdk with my java/spring project, and so far I got this code:

 

**controller**

 

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

 

**service**

 

public String checkout_paypal(Integer usuario_id, String payerId, String guid) throws PayPalRESTException {
Usuario usuario = this.dao.findBy("id", usuario_id);
Payment createdPayment = null;

org.loja.settings.paypal.Paypal paypal = (org.loja.settings.paypal.Paypal) paypalDao.get();
String appId = paypal.getClientId();
String appKey = paypal.getClientSecret();
APIContext apiContext = new APIContext(appId, appKey, "sandbox");

if (payerId != null) {
Payment payment = new Payment();
if (guid != null) {
payment.setId(map.get(guid));

PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.setPayerId(payerId);

try {
createdPayment = payment.execute(apiContext, paymentExecution);
} catch (PayPalRESTException e) {
//
}
}
return "/order/"+create_order(usuario, "paypal").toString();
} else {
Details details = new Details();
details.setSubtotal(String.valueOf(cart_total(usuario.getId())));

Amount amount = new Amount();
amount.setCurrency("BRL");
amount.setTotal(String.valueOf(cart_total(usuario.getId())));
amount.setDetails(details);

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

ItemList itemList = new ItemList();
List<Item> items = new ArrayList<Item>();
for(Produto p : usuario.getCesta().getProdutos()) {
Item item = new Item();
item.setName(p.getNome()).setQuantity("1").setCurrency("BRL").setPrice(String.valueOf(p.getPreco()));
items.add(item);
}
itemList.setItems(items);
transaction.setItemList(itemList);

List<Transaction> transactions = new 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();
guid = UUID.randomUUID().toString().replaceAll("-", "");
redirectUrls.setCancelUrl("/usuario/cart");
redirectUrls.setReturnUrl("/usuario/checkout_paypal?usuario_id="+usuario.getId().toString()+"?payerId="+payerId);
payment.setRedirectUrls(redirectUrls);

createdPayment = payment.create(apiContext);
Iterator<Links> links = createdPayment.getLinks().iterator();
String url = null;
while (links.hasNext()) {
Links link = links.next();
if (link.getRel().equalsIgnoreCase("approval_url")) {
url = link.getHref();
}
}
map.put(guid, createdPayment.getId());
return url+"?guid="+guid;
}
}

 

But when I run the application and try making a checkout with paypal, I am redirect to a screen to choose payment method; after that selection, when I click to continue the process, I go to the start page of my account (where is show balance and transations history). and never return to my application, where should go to the order page. When I click to cancel the transaction and return to the website, this works as expected.

 

 

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.