|
@@ -3,6 +3,7 @@ package com.fdkankan.pay.util.paypal.sdk;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fdkankan.pay.common.ResultCode;
|
|
|
+import com.fdkankan.pay.entity.AliConfig;
|
|
|
import com.fdkankan.pay.entity.Order;
|
|
|
import com.fdkankan.pay.entity.PaypalConfig;
|
|
|
import com.fdkankan.pay.exception.BusinessException;
|
|
@@ -48,7 +49,8 @@ public class PaypalService {
|
|
|
if(paypalConfig == null){
|
|
|
throw new BusinessException(ResultCode.PAYPAL_CONFIG_ERROR);
|
|
|
}
|
|
|
- paypalConfig.setCallBackUrl( CacheUtil.mainUrl + paypalConfig.getCallBackUrl() +"/"+orderSn+"/"+param.getPayType());
|
|
|
+ paypalConfig.setSuccessUrl( CacheUtil.mainUrl + paypalConfig.getCallBackUrl() +"/"+orderSn+"/"+param.getPayType()+"/success");
|
|
|
+ paypalConfig.setCancelUrl( CacheUtil.mainUrl + paypalConfig.getCallBackUrl() +"/"+orderSn+"/"+param.getPayType()+"/cancel");
|
|
|
String body = subject;
|
|
|
//paypal支付
|
|
|
if(param.getPayType() == 5){
|
|
@@ -81,47 +83,39 @@ public class PaypalService {
|
|
|
throw new BusinessException(ResultCode.PAYPAL_ERROR);
|
|
|
}
|
|
|
|
|
|
- public Boolean callBack(HttpServletRequest request, HttpServletResponse response, Order order) {
|
|
|
+ public Boolean callBack(HttpServletRequest request, HttpServletResponse response, Order order,String result) {
|
|
|
log.info("paypal-callBack--order:{}",order);
|
|
|
Boolean payFlag = false;
|
|
|
String trade_no = null;
|
|
|
String openId = null;
|
|
|
+ if("cancel".equals(result)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
try {
|
|
|
- Enumeration en = request.getParameterNames();
|
|
|
- String str = "cmd=_notify-validate";
|
|
|
- while (en.hasMoreElements()) {
|
|
|
- String paramName = (String) en.nextElement();
|
|
|
- String paramValue = request.getParameter(paramName);
|
|
|
- try {
|
|
|
- str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, "utf-8");
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- StringWriter trace=new StringWriter();
|
|
|
- e.printStackTrace(new PrintWriter(trace));
|
|
|
- log.error(trace.toString());
|
|
|
- }
|
|
|
- //此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’
|
|
|
+ String paymentId = request.getParameter("paymentId");//商品名
|
|
|
+ String payerId = request.getParameter("PayerID");//购买数量
|
|
|
+ PaypalConfig paypalConfig = paypalConfigService.getByServeId(order.getServeId());
|
|
|
+ if(paypalConfig == null){
|
|
|
+ log.error("paypal-callBack--paypalConfig-notexist");
|
|
|
+ return false;
|
|
|
}
|
|
|
- final String itemName = request.getParameter("item_name");//商品名
|
|
|
- final String itemNumber = request.getParameter("item_number");//购买数量
|
|
|
- final String paymentDate = request.getParameter("payment_date");//交易时间
|
|
|
- final String receiverEmail = request.getParameter("receiver_email");//收款人email
|
|
|
- final String payerEmail = request.getParameter("payer_email");//付款人email
|
|
|
- final String paymentAmount = request.getParameter("mc_gross");//交易钱数
|
|
|
- final String paymentCurrency = request.getParameter("mc_currency");//货币种类
|
|
|
- final String paymentStatus = request.getParameter("payment_status");//交易状态
|
|
|
- final String txnId = request.getParameter("txn_id");//交易id
|
|
|
- String custom = request.getParameter("custom");//发送payment请求时候自定义的业务服务器订单号
|
|
|
- log.info("paypal回调参数:itemName:{},itemNumber:{},paymentDate:{},receiverEmail:{}," +
|
|
|
- "payerEmail:{},paymentAmount:{},paymentCurrency:{},paymentStatus:{},txnId:{},custom:{}",
|
|
|
- itemName,itemNumber,paymentDate,receiverEmail,payerEmail,paymentAmount,paymentCurrency,paymentStatus,txnId,custom);
|
|
|
-
|
|
|
- trade_no = txnId;
|
|
|
- openId = payerEmail;
|
|
|
- if ("Completed".equals(paymentStatus)) {
|
|
|
- //根据自己业务进行处理(修改订单状态,支付时间等等操作)
|
|
|
+
|
|
|
+ log.info("paypal回调参数:paymentId:{},payerId:{}", paymentId,payerId);
|
|
|
+ Payment payment = this.executePayment(paymentId, payerId,paypalConfig);
|
|
|
+ if (payment.getState().equals("approved")) {
|
|
|
log.info("paypal支付成功回调");
|
|
|
- //表示续费,有消费记录id
|
|
|
+ String custom = payment.getTransactions().get(0).getCustom();
|
|
|
+ String txnId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
|
|
|
+ String payerEmail = payment.getTransactions().get(0).getPayee().getEmail();
|
|
|
+ log.info("paypal-callBack:custom:{},txnid:{},payerEmail:{}",custom,txnId,payerEmail);
|
|
|
+
|
|
|
+ String orderSn = custom.split("_")[0];
|
|
|
+ if(custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")){
|
|
|
+ orderSn += "_" + custom.split("_")[2];
|
|
|
+ }
|
|
|
payFlag = true;
|
|
|
+ trade_no = txnId;
|
|
|
+ openId = payerEmail;
|
|
|
}
|
|
|
}catch (Exception e) {
|
|
|
log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
|
|
@@ -133,6 +127,58 @@ public class PaypalService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+// public Boolean callBack(HttpServletRequest request, HttpServletResponse response, Order order) {
|
|
|
+// log.info("paypal-callBack--order:{}",order);
|
|
|
+// Boolean payFlag = false;
|
|
|
+// String trade_no = null;
|
|
|
+// String openId = null;
|
|
|
+// try {
|
|
|
+// Enumeration en = request.getParameterNames();
|
|
|
+// String str = "cmd=_notify-validate";
|
|
|
+// while (en.hasMoreElements()) {
|
|
|
+// String paramName = (String) en.nextElement();
|
|
|
+// String paramValue = request.getParameter(paramName);
|
|
|
+// try {
|
|
|
+// str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, "utf-8");
|
|
|
+// } catch (UnsupportedEncodingException e) {
|
|
|
+// StringWriter trace=new StringWriter();
|
|
|
+// e.printStackTrace(new PrintWriter(trace));
|
|
|
+// log.error(trace.toString());
|
|
|
+// }
|
|
|
+// //此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’
|
|
|
+// }
|
|
|
+// final String itemName = request.getParameter("item_name");//商品名
|
|
|
+// final String itemNumber = request.getParameter("item_number");//购买数量
|
|
|
+// final String paymentDate = request.getParameter("payment_date");//交易时间
|
|
|
+// final String receiverEmail = request.getParameter("receiver_email");//收款人email
|
|
|
+// final String payerEmail = request.getParameter("payer_email");//付款人email
|
|
|
+// final String paymentAmount = request.getParameter("mc_gross");//交易钱数
|
|
|
+// final String paymentCurrency = request.getParameter("mc_currency");//货币种类
|
|
|
+// final String paymentStatus = request.getParameter("payment_status");//交易状态
|
|
|
+// final String txnId = request.getParameter("txn_id");//交易id
|
|
|
+// String custom = request.getParameter("custom");//发送payment请求时候自定义的业务服务器订单号
|
|
|
+// log.info("paypal回调参数:itemName:{},itemNumber:{},paymentDate:{},receiverEmail:{}," +
|
|
|
+// "payerEmail:{},paymentAmount:{},paymentCurrency:{},paymentStatus:{},txnId:{},custom:{}",
|
|
|
+// itemName,itemNumber,paymentDate,receiverEmail,payerEmail,paymentAmount,paymentCurrency,paymentStatus,txnId,custom);
|
|
|
+//
|
|
|
+// trade_no = txnId;
|
|
|
+// openId = payerEmail;
|
|
|
+// if ("Completed".equals(paymentStatus)) {
|
|
|
+// //根据自己业务进行处理(修改订单状态,支付时间等等操作)
|
|
|
+// log.info("paypal支付成功回调");
|
|
|
+// //表示续费,有消费记录id
|
|
|
+// payFlag = true;
|
|
|
+// }
|
|
|
+// }catch (Exception e) {
|
|
|
+// log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
|
|
|
+// return false;
|
|
|
+// }finally {
|
|
|
+// orderService.payResult(order,payFlag,trade_no,openId);
|
|
|
+// }
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 创建支付
|
|
@@ -225,8 +271,8 @@ public class PaypalService {
|
|
|
|
|
|
//回调地址
|
|
|
RedirectUrls redirectUrls = new RedirectUrls();
|
|
|
- redirectUrls.setReturnUrl(paypalConfig.getCallBackUrl());
|
|
|
- redirectUrls.setCancelUrl(paypalConfig.getCallBackUrl());
|
|
|
+ redirectUrls.setReturnUrl(paypalConfig.getSuccessUrl());
|
|
|
+ redirectUrls.setCancelUrl(paypalConfig.getCancelUrl());
|
|
|
payment.setRedirectUrls(redirectUrls);
|
|
|
|
|
|
return payment.create(apiContext);
|
|
@@ -286,9 +332,8 @@ public class PaypalService {
|
|
|
result.append(lineTxt);
|
|
|
}
|
|
|
reader.close();
|
|
|
- String accessTokey = JSONObject.parseObject(result.toString()).getString("access_token");
|
|
|
//log.info("getAccessToken:" + accessTokey);
|
|
|
- return accessTokey;
|
|
|
+ return JSONObject.parseObject(result.toString()).getString("access_token");
|
|
|
} catch (Exception err) {
|
|
|
err.printStackTrace();
|
|
|
} finally {
|