支付接口接入
String url=profileHttpUrl+"/aliPay/notifyApp";
Orders orders = ordersJpaRepository.getOne(ordersId);
String outTradeNo=orders.getOrderNum();
//构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
//设置网关地址
certAlipayRequest . setServerUrl ( "https://openapi.alipay.com/gateway.do" );
//设置应用Id
certAlipayRequest . setAppId ( commonRepository.findOne(63).getValue() );
//设置应用私钥
certAlipayRequest . setPrivateKey ( commonRepository.findOne(65).getValue() );
//设置请求格式,固定值json
certAlipayRequest . setFormat ( "json" );
//设置字符集
certAlipayRequest . setCharset ( "UTF-8" );
//设置签名类型
certAlipayRequest . setSignType ( "RSA2" );
//设置应用公钥证书路径
certAlipayRequest . setCertPath ( classPath+app_cert_path );
//设置支付宝公钥证书路径
certAlipayRequest . setAlipayPublicCertPath ( classPath+alipay_cert_path);
//设置支付宝根证书路径
certAlipayRequest . setRootCertPath ( classPath+alipay_root_cert_path );
//构造client
AlipayClient alipayClient = new DefaultAlipayClient ( certAlipayRequest);
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest ();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel ();
model . setBody ( Config.BASE_NAME );
model . setSubject ( Config.BASE_NAME );
model . setOutTradeNo ( outTradeNo );
model . setTimeoutExpress ( "30m" );
model . setTotalAmount ( orders.getPayMoney()+"");
model . setProductCode ( "QUICK_MSECURITY_PAY" );
request . setBizModel ( model );
request . setNotifyUrl ( url );
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient . sdkExecute ( request );
if(response.isSuccess()){
System.out.println();
return ResultUtil.success(response.getBody());
} else {
log.info("调用失败");
ResultUtil.error(-100,"获取订单失败!");
}
System . out . println ( response . getBody ()); //就是orderString 可以直接给客户端请求,无需再做处理。
} catch ( AlipayApiException e ) {
e . printStackTrace ();
}
支付之后回调函数
public void notifyApp(HttpServletRequest request, HttpServletResponse response){
//获取支付宝POST过来反馈信息
Map<String,String> params = new HashMap<String,String>();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
try {
log.info("回调成功!!!");
boolean flag = AlipaySignature.rsaCertCheckV1(params, classPath+alipay_cert_path,
AliPayConstants.CHARSET,"RSA2");
log.info(flag+"回调验证信息");
if(flag){
String outTradeNo = params.get("out_trade_no");
//支付成功,处理业务
Orders orders = ordersService.findByOrderNum(outTradeNo);
orders.setPayWay(2); //支付方式为2支付宝
orders.setStatus(2); //已付款
orders.setPayTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); //设置支付时间
// if(orders.getPinkId()>0){
// GoodsPink storePink = goodsPinkRepository.findOrderById(orders.getOrderNum(),orders.getPinkId());
// if(storePink !=null){
// storePink.setIsRefund(0);
// goodsPinkJpaRepository.saveAndFlush(storePink);
// }
// }
// orders.setCanUse(orders.getPayMoney());
ordersService.updateBody(orders);
//订单分销
// ordersService.fenXiao(orders.getId());
}
} catch (AlipayApiException e) {
e.printStackTrace();
log.info("回调验证失败!!!");
}
}
订单退款
public boolean refund(Long ordersId) {
try {
Orders orders = ordersJpaRepository.getOne(ordersId);
// AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", commonRepository.findOne(63).getValue(),
// commonRepository.findOne(65).getValue(), "json", AliPayConstants.CHARSET, commonRepository.findOne(64).getValue(), "RSA2");
CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
//设置网关地址
certAlipayRequest . setServerUrl ( "https://openapi.alipay.com/gateway.do" );
//设置应用Id
certAlipayRequest . setAppId ( commonRepository.findOne(63).getValue() );
//设置应用私钥
certAlipayRequest . setPrivateKey ( commonRepository.findOne(65).getValue() );
//设置请求格式,固定值json
certAlipayRequest . setFormat ( "json" );
//设置字符集
certAlipayRequest . setCharset ( "UTF-8" );
//设置签名类型
certAlipayRequest . setSignType ( "RSA2" );
//设置应用公钥证书路径
certAlipayRequest . setCertPath ( classPath+app_cert_path );
//设置支付宝公钥证书路径
certAlipayRequest . setAlipayPublicCertPath ( classPath+alipay_cert_path);
//设置支付宝根证书路径
certAlipayRequest . setRootCertPath ( classPath+alipay_root_cert_path );
//构造client
AlipayClient alipayClient = new DefaultAlipayClient ( certAlipayRequest);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
model.setOutTradeNo(orders.getOrderNum()); //订单号
model.setRefundAmount(orders.getPayMoney()+""); //退款金额
request.setBizModel(model);
AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);
if(response.isSuccess()){
return true;
} else {
return false;
}
}catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}