这里写目录标题
- 环境准备
- 1.注册paypal账户
- 2.登录开发者中心
- 3.创建沙箱账户
- 4.在沙箱模式创建一个app,平台默认创建好了一个
- 4.1填写信息
- 4.2绑定某个沙箱账号
- 4.3Client ID及Secret
- 支付代码
- 1.pom
- 2.实例化配置
- 3.支付订单
- 3.1用sandbox的personal的账号登录,并支付
- 4.支付成功&失败
- 5.退款
环境准备
1.注册paypal账户
PayPal官方
2.登录开发者中心
PayPal开发者中心
3.创建沙箱账户
Business为收款账号,Personal为付款账号,平台默认创建好了两个账号
这里显示的 账户和密码,支付的时候用
4.在沙箱模式创建一个app,平台默认创建好了一个
4.1填写信息
类型:
Merchant-仅为自己收款
Platform-作为平台收款,可分账给卖家
4.2绑定某个沙箱账号
4.3Client ID及Secret
进入app详细页面,查看Client ID及Secret(后续需填写到代码中)
支付代码
1.pom
<dependency>
<groupId>com.github.javen205</groupId>
<artifactId>IJPay-All</artifactId>
<version>2.9.3</version>
</dependency>
2.实例化配置
public PayPalApiConfig getConfig() {
PayPalApiConfig config = new PayPalApiConfig();
config.setClientId(payPalBean.getClientId());
config.setClientSecret(payPalBean.getSecret());
config.setSandBox(payPalBean.getSandBox());
config.setDomain(payPalBean.getDomain());
PayPalApiConfigKit.setThreadLocalApiConfig(config);
return config;
}
3.支付订单
private final static String RETURN_URL = "/center/pay/order/successUrl";
private final static String CANCEL_URL = "/center/pay/order/cancelUrl";
/**
* @return void
* @params []
* @Author
* @Date
* @Description: 支付订单
*/
public void index() {
String id = get("id");
if (StrKit.isBlank(id)) {
//re("订单id未提供");
return;
}
BusOrder target = BusOrder.dao.template("order.find", Kv.by("id", id)).findFirst();
if (target == null) {
//re("订单不存在");
return;
}
String pay_method = target.getPayMethod();
try {
PayPalApiConfig config = getConfig();
//参数请求参数文档 https://developer.paypal.com/docs/api/orders/v2/#orders_create
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("intent", "CAPTURE");//不改
ArrayList<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> amount = new HashMap<>();
amount.put("currency_code", "USD");//美元
double actualPrice = NumberUtil.mul(target.getActualPrice() + "", "1").doubleValue();
amount.put("value", actualPrice);//实际付款
Map<String, Object> itemMap = new HashMap<>();
itemMap.put("amount", amount);
list.add(itemMap);
dataMap.put("purchase_units", list);
Map<String, String> applicationContext = new HashMap<>();
applicationContext.put("cancel_url", config.getDomain().concat(CANCEL_URL) + "?id=" + target.getId() + "&user=" + getHeader("userCode"));//失败跳转地址
applicationContext.put("return_url", config.getDomain().concat(RETURN_URL));//成功后跳转地址
applicationContext.put("brand_name", "test");//品牌名称
//applicationContext.put("logo_url", "");//付款方式徽标的 URL
dataMap.put("application_context", applicationContext);
String data = JSONUtil.toJsonStr(dataMap);
log.info(data);
IJPayHttpResponse resData = PayPalApi.createOrder(config, data);
log.info(resData.toString());
if (resData.getStatus() == 201) {
String resultStr = resData.getBody();
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(resultStr);
//System.out.println(jsonObject);
target.setOrderThirdId(jsonObject.getStr("id"));
target.update();
JSONArray links = jsonObject.getJSONArray("links");
for (int i = 0; i < links.size(); i++) {
JSONObject item = links.getJSONObject(i);
String rel = item.getStr("rel");
String href = item.getStr("href");
if ("approve".equalsIgnoreCase(rel)) {
// redirect(href);
rsPara("支付地址获取成功", Kv.by("href", href));//支付地址 提交给前端
return;
}
}
}
re("支付失败");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
3.1用sandbox的personal的账号登录,并支付
4.支付成功&失败
//支付成功
public void successUrl() {
getPageStore("orderResult");
String token = get("token");
String payerId = get("PayerID");
BusOrder target = BusOrder.dao.template("order.find", Kv.by("order_third_id", token)).findFirst();
//确认付款
log.info("token:" + token);
log.info("payerId:" + payerId);
PayPalApiConfig config = getConfig();
IJPayHttpResponse response = PayPalApi.captureOrder(config, token, "");
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
if (response.getStatus() == 201 && jsonObject.getString("status").equals("COMPLETED")) {
target.setPayTime(DateUtil.now());
target.setCallbackJson(response.getBody());
target.setState("2");
target.update();
rs("支付成功,跳转页面");
redirect(config.getDomain() + "/center/order/getOrders");
return;
}
setAttr("status", response.getStatus());
render("/order/result.html");
}
//未支付 返回
public void cancelUrl() {
//String readData = HttpKit.readData(getRequest());
/* System.out.println(readData);
rs(readData);*/
// PayPalApiConfig config = getConfig();
String userCode = get("user");
BusUser log_user = Redis.use().get(RedisHeader.USER_LOGIN_CODE + userCode);
if (log_user == null) {
//re("");
render("/");
return;
}
String id = get("id");
BusOrder target = BusOrder.dao.template("order.find", Kv.by("id", id)).findFirst();
/*String token = get("token");
BusOrder target = BusOrder.dao.template("order.find", Kv.by("order_third_id", token)).findFirst();*/
setAttr("orderObj", target);
getPageStore("orderDetail");
render("/order/order_detail.html");
}
5.退款
/**
* @return void
* @params []
* @Author
* @Date
* @Description: 退款订单
*/
public void refund() {
com.alibaba.fastjson.JSONObject obj = getAttr("obj");
String id = obj.getString("id");
String refund_note = obj.getString("refund_note");
BusOrder target = BusOrder.dao.template("order.find", Kv.by("id", id)).findFirst();
if (target == null) {
re("订单不存在");
return;
}
try {
com.alibaba.fastjson.JSONObject callbackJson = com.alibaba.fastjson.JSONObject.parseObject(target.getCallbackJson());//支付的时候给的json
String captureId = callbackJson.getJSONArray("purchase_units").getJSONObject(0).getJSONObject("payments")
.getJSONArray("captures").getJSONObject(0).getString("id");
PayPalApiConfig config = getConfig();
Map<String, Object> map = new HashMap<>();
String invoice_id = PayKit.generateStr();
map.put("invoice_id", invoice_id);//退款编号
map.put("note_to_payer", refund_note);//退款原因
Map<String, String> amount = new HashMap<>();
double v = NumberUtil.mul(target.getActualPrice() + "", "1").doubleValue();
amount.put("value", v + "");
amount.put("currency_code", "USD");
map.put("amount", amount);
String data = JSONUtil.toJsonStr(map);
log.info("refund data:" + data);
IJPayHttpResponse response = PayPalApi.refund(config, captureId, data);
log.info(response.toString());
if (response.getStatus() == 201) {
target.setRefundTime(DateUtil.now());
target.setState("5");
target.setRefundNote(refund_note);
target.setRefundInvoice(invoice_id);
target.update();
rs("Refund success");
//redirect(config.getDomain() + "/center/order/getOrders");
return;
} else {
re("Refund failure ");
}
return;
} catch (Exception e) {
e.printStackTrace();
}
}