package com.san.paypal.controller;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.san.paypal.config.PaypalPaymentIntent;
import com.san.paypal.config.PaypalPaymentMethod;
import com.san.paypal.service.PaypalService;
import com.san.paypal.utils.URLUtil;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException;
/**
* @author xsansan
* @date 2018年9月12日
* Description:
*/
@Controller
@RequestMapping("/")
public class PaymentController {
public static final String PAYPAL_SUCCESS_URL = "pay/success";
public static final String PAYPAL_CANCEL_URL = "pay/cancel";
private Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private PaypalService paypalService;
@RequestMapping(method = RequestMethod.GET)
public String index(){
return "index";
}
@RequestMapping(method = RequestMethod.POST, value = "pay")
public String pay(HttpServletRequest request) {
String cancelUrl = URLUtil.getBaseURl(request) + "/" + PAYPAL_CANCEL_URL;
String successUrl = URLUtil.getBaseURl(request) + "/" + PAYPAL_SUCCESS_URL;
try {
// Create payment
Payment payment = paypalService.createPayment(
500.00,
"USD",
PaypalPaymentMethod.paypal,
PaypalPaymentIntent.sale,
"payment description",
cancelUrl,
successUrl);
System.out.println("createPayment=" + payment);
for(Links links : payment.getLinks()){
if(links.getRel().equalsIgnoreCase("approval_url")) {
return "redirect:" + links.getHref();
}
}
} catch (PayPalRESTException e) {
log.error(e.getMessage());
System.out.println(e.getDetails());
}
return "redirect:/";
}
@RequestMapping(method = RequestMethod.GET, value = PAYPAL_CANCEL_URL)
public String cancelPay() {
return "cancel";
}
@RequestMapping(method = RequestMethod.GET, value = PAYPAL_SUCCESS_URL)
public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) {
try {
System.out.println("paymentId=" + paymentId + ",payerId=" + payerId);
Payment payment = paypalService.executePayment(paymentId, payerId);
System.out.println("payment" + payment);
if(payment.getState().equals("approved")) {
return "success";
}
} catch (PayPalRESTException e) {
log.error(e.getMessage());
System.out.println(e.getDetails());
}
return "redirect:/";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
springboot-paypal.zip (42个子文件)
springboot-paypal
.gitignore 268B
.project 1KB
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 2KB
mvnw 6KB
target
test-classes
com
san
paypal
classes
META-INF
maven
com.san.paypal
springboot-demo
pom.xml 2KB
pom.properties 261B
MANIFEST.MF 346B
static
images
paypal.jpg 2KB
com
san
paypal
utils
URLUtil.class 1KB
testl
QueryPayment.class 2KB
PaypalStartApplication.class 720B
controller
PaymentController.class 4KB
config
PaypalPaymentMethod.class 1KB
Client.class 590B
PaypalPaymentIntent.class 1KB
service
PaypalService.class 4KB
templates
success.html 154B
cancel.html 157B
index.html 274B
application.properties 47B
.settings
org.eclipse.core.resources.prefs 155B
org.eclipse.jdt.core.prefs 243B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 97B
src
test
java
com
san
paypal
main
resources
static
images
paypal.jpg 2KB
templates
success.html 154B
cancel.html 157B
index.html 274B
application.properties 47B
java
com
san
paypal
utils
URLUtil.java 937B
testl
QueryPayment.java 1KB
PaypalStartApplication.java 316B
controller
PaymentController.java 3KB
config
PaypalPaymentIntent.java 179B
PaypalPaymentMethod.java 174B
Client.java 437B
service
PaypalService.java 3KB
mvnw.cmd 5KB
.springBeans 480B
.classpath 1KB
共 42 条
- 1
x_san3
- 粉丝: 22
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论4