在我当前的java / spring项目中,我正在使用paypal-java-sdk尝试为我的应用程序实现支付系统。很久以前,我设法使用丢失的旧项目来完成此工作,其代码类似于现在的代码,但是现在,当我运行该应用程序并尝试使用Paypal SDK进行付款时,重定向到付款方式选择页面,然后,如果我确认或取消付款,我将转到我的帐户页面,而不是返回到应用程序。
我有此代码:
在我的控制器中,此方法从视图接收呼叫:
@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);
}
在我的服务类中,这2种方法使用Paypal处理付款:
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);
//saves the order data in the database and redirect to the order page
}
} 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);
}
任何人都可以给我提示我在这里缺少什么吗?