@@ -1,5 +1,6 @@
package net.lab1024.smartadmin.module.system.alipay ;
import com.alibaba.fastjson.JSONObject ;
import com.alipay.api.AlipayApiException ;
import com.alipay.api.AlipayClient ;
import com.alipay.api.DefaultAlipayClient ;
@@ -10,10 +11,21 @@ import com.alipay.api.response.AlipayTradePrecreateResponse;
import io.swagger.annotations.Api ;
import lombok.extern.slf4j.Slf4j ;
import net.lab1024.smartadmin.common.anno.OperateLog ;
import net.lab1024.smartadmin.common.heartbeat.StringUtil ;
import net.lab1024.smartadmin.constant.SwaggerTagConst ;
import net.lab1024.smartadmin.module.system.alipay.alipayModel.AliPayEntity ;
import net.lab1024.smartadmin.module.system.alipay.conf.AlipayConfig ;
import net.lab1024.smartadmin.module.system.qrcode.QRCodeUtil ;
import net.lab1024.smartadmin.module.system.royalcanin.MemberAccountService ;
import net.lab1024.smartadmin.module.system.royalcanin.good.model.OrderEntity ;
import net.lab1024.smartadmin.module.system.royalcanin.good.model.ProductMasterQueryEntity ;
import net.lab1024.smartadmin.module.system.royalcanin.good.service.OrderService ;
import net.lab1024.smartadmin.module.system.royalcanin.memberAccount.MemberAccountChangeEntity ;
import net.lab1024.smartadmin.module.system.royalcanin.notify.model.NotifyEntity ;
import net.lab1024.smartadmin.module.system.royalcanin.notify.service.NotifyService ;
import net.lab1024.smartadmin.util.MapRemoveNullUtil ;
import net.lab1024.smartadmin.util.SmartHttpUtil ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.* ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
@@ -23,7 +35,7 @@ import java.util.Iterator;
import java.util.Map ;
import java.util.UUID ;
@Api ( tags = { SwaggerTagConst . Admin . MANAGER_MALL_PAY_ API } )
@Api ( tags = { SwaggerTagConst . Admin . MANAGER_MALL_API } )
@OperateLog
@Slf4j
@RestController
@@ -32,6 +44,17 @@ public class AlipayController {
public static String notify_url = " " ;
@Autowired
private OrderService orderService ;
String url = " https://miniapp-test.royalcanin.com.cn/rcmini/h5/ " ;
@Autowired
private MemberAccountService memberAccountService ;
@Autowired
private NotifyService notifyService ;
/**
* @Description: 前往支付宝第三方网关进行支付
*/
@@ -65,47 +88,88 @@ public class AlipayController {
}
//
// @RequestMapping("royalcanin/updateOrderAliPay")
// public String returnUrl(HttpServletRequest request) throws Exception {
// Map<String,String> params = new HashMap<String,String>();
// Map<String,String[]> requestParams = request.getParameterMap();
// for (Iterator<String> 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);
// }
// System.out.println("params:"+params);
// boolean signVerified = AlipaySignature.rsaCheckV1(params,AlipayConfig.alipay_public_key,AlipayConfig.charset,AlipayConfig.sign_type); //调用SDK验证签名
// System.out.println(signVerified);
// //——请在这里编写您的程序(以下代码仅作参考)——
// if(signVerified) {
// // 更新订单状态
// //商户订单号
// String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
// //付款金额
// String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
//
//
// //支付宝交易号
// String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
// OrderEntity orderEntity = orderService.findByOrderId(out_trade_no);
// String[] products = orderEntity.getProductCode().split(",");
// ProductMasterQueryEntity productMasterQueryEntity = new ProductMasterQueryEntity();
// double basePoint = 0;
// //自动获取会员积分
// for (String product : products) {
// productMasterQueryEntity.setProductCode(product);
// Map<String, String> paramMap = MapRemoveNullUtil.setConditionMap(productMasterQueryEntity);
// MapRemoveNullUtil.removeNullEntry(paramMap);
// paramMap.put("secret", "H5@2021");
// paramMap.put("channelId", "15");
// String sb = SmartHttpUtil.sendPostForm(url + "productMaster/query", paramMap, null);
// JSONObject jsonObject = JSONObject.parseObject(sb);
// com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString("data"));
// JSONObject jsonObjectData = (JSONObject) jsonArray.get(0);
// basePoint = basePoint + Double.parseDouble(jsonObjectData.getString("basePoint"));
// }
// MemberAccountChangeEntity memberAccountChangeEntity = new MemberAccountChangeEntity();
// memberAccountChangeEntity.setChangeTypeId("1");
// memberAccountChangeEntity.setChangeValue(StringUtil.toString(basePoint));
// memberAccountChangeEntity.setMemberId(orderEntity.getMemberId());
// memberAccountService.memberAccountChange(memberAccountChangeEntity);
// NotifyEntity notifyEntity = new NotifyEntity();
// notifyEntity.setAppId(AlipayConfig.app_id);
// notifyEntity.setMchId(request.getParameter("seller_id"));
// notifyEntity.setSign(request.getParameter("sign"));
// notifyEntity.setResultCode(request.getParameter("code"));
// notifyEntity.setOpenid(request.getParameter("merchant_order_no"));
// notifyEntity.setOutTradeNo(out_trade_no);
// notifyEntity.setTotalFee(total_amount);
// notifyEntity.setTradeType(request.getParameter("sub_code"));
// notifyEntity.setFeeType("CNY");
// notifyEntity.setNonceStr(trade_no);
// notifyService.insertNotify(notifyEntity);
//
//
//
//
//
//
// String trade_status= new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
// System.out.println("trade_status: "+trade_status);
// return "trade_no:"+trade_no+"<br/>out_trade_no:"+out_trade_no+"<br/>total_amount:"+total_amount;
// }else {
// return "验签失败";
// }
// }
@RequestMapping ( " royalcanin/updateOrderAliPay " )
public String returnUrl ( HttpServletRequest request ) throws UnsupportedEncodingException , AlipayApiException {
Map < String , String > params = new HashMap < String , String > ( ) ;
Map < String , String [ ] > requestParams = request . getParameterMap ( ) ;
for ( Iterator < String > 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 ) ;
}
System . out . println ( " params: " + params ) ;
boolean signVerified = AlipaySignature . rsaCheckV1 ( params , AlipayConfig . alipay_public_key , AlipayConfig . charset , AlipayConfig . sign_type ) ; //调用SDK验证签名
System . out . println ( signVerified ) ;
//——请在这里编写您的程序(以下代码仅作参考)——
if ( signVerified ) {
//商户订单号
String out_trade_no = new String ( request . getParameter ( " out_trade_no " ) . getBytes ( " ISO-8859-1 " ) , " UTF-8 " ) ;
//支付宝交易号
String trade_no = new String ( request . getParameter ( " trade_no " ) . getBytes ( " ISO-8859-1 " ) , " UTF-8 " ) ;
//付款金额
String total_amount = new String ( request . getParameter ( " total_amount " ) . getBytes ( " ISO-8859-1 " ) , " UTF-8 " ) ;
String trade_status = new String ( request . getParameter ( " trade_status " ) . getBytes ( " ISO-8859-1 " ) , " UTF-8 " ) ;
System . out . println ( " trade_status: " + trade_status ) ;
return " trade_no: " + trade_no + " <br/>out_trade_no: " + out_trade_no + " <br/>total_amount: " + total_amount ;
} else {
return " 验签失败 " ;
}
}
@RequestMapping ( value = " /alipayNotifyNotice " )
@ResponseBody
public String alipayNotifyNotice ( HttpServletRequest request , HttpServletRequest response ) throws Exception {
@@ -151,6 +215,7 @@ public class AlipayController {
String total_amount = new String ( request . getParameter ( " total_amount " ) . getBytes ( " ISO-8859-1 " ) , " UTF-8 " ) ;
if ( trade_status . equals ( " TRADE_FINISHED " ) ) {
return " TRADE_FINISHED " ;
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理, 根据订单号( out_trade_no) 在商户网站的订单系统中查到该笔订单的详细, 并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
@@ -167,23 +232,48 @@ public class AlipayController {
// 修改叮当状态,改为 支付成功,已付款; 同时新增支付流水
// ordersService.updateOrderStatus(out_trade_no, trade_no, total_amount);
//
// //这里不用 查 只是为了 看日志 查的方法应该卸载 同步回调 页面 中
// Orders order = ordersService.getOrderById(out_trade_no);
// Product product = productService.getProductById(order.getProductId());
//
// LOGGER.info("********************** 支付成功(支付宝异步通知)查询 只是为了 看日志 **********************");
// LOGGER.info("* 订单号: {}", out_trade_no);
// LOGGER.info("* 支付宝交易号: {}", trade_no);
// LOGGER.info("* 实付金额: {}", total_amount);
// LOGGER.info("* 购买产品: {}", product.getName());
// LOGGER.info("***************************************************************");
OrderEntity orderEntity = orderService . findByOrderId ( out_trade_no ) ;
String [ ] products = orderEntity . getProductCode ( ) . split ( " , " ) ;
ProductMasterQueryEntity productMasterQueryEntity = new ProductMasterQueryEntity ( ) ;
double basePoint = 0 ;
//自动获取会员积分
for ( String product : products ) {
productMasterQueryEntity . setProductCode ( product ) ;
Map < String , String > paramMap = MapRemoveNullUtil . setConditionMap ( productMasterQueryEntity ) ;
MapRemoveNullUtil . removeNullEntry ( paramMap ) ;
paramMap . put ( " secret " , " H5@2021 " ) ;
paramMap . put ( " channelId " , " 15 " ) ;
String sb = SmartHttpUtil . sendPostForm ( url + " productMaster/query " , paramMap , null ) ;
JSONObject jsonObject = JSONObject . parseObject ( sb ) ;
com . alibaba . fastjson . JSONArray jsonArray = JSONObject . parseArray ( jsonObject . getString ( " data " ) ) ;
JSONObject jsonObjectData = ( JSONObject ) jsonArray . get ( 0 ) ;
basePoint = basePoint + Double . parseDouble ( jsonObjectData . getString ( " basePoint " ) ) ;
}
MemberAccountChangeEntity memberAccountChangeEntity = new MemberAccountChangeEntity ( ) ;
memberAccountChangeEntity . setChangeTypeId ( " 1 " ) ;
memberAccountChangeEntity . setChangeValue ( StringUtil . toString ( basePoint ) ) ;
memberAccountChangeEntity . setMemberId ( orderEntity . getMemberId ( ) ) ;
memberAccountService . memberAccountChange ( memberAccountChangeEntity ) ;
NotifyEntity notifyEntity = new NotifyEntity ( ) ;
notifyEntity . setAppId ( AlipayConfig . app_id ) ;
notifyEntity . setMchId ( request . getParameter ( " seller_id " ) ) ;
notifyEntity . setSign ( request . getParameter ( " sign " ) ) ;
notifyEntity . setResultCode ( request . getParameter ( " code " ) ) ;
notifyEntity . setOpenid ( request . getParameter ( " merchant_order_no " ) ) ;
notifyEntity . setOutTradeNo ( out_trade_no ) ;
notifyEntity . setTotalFee ( total_amount ) ;
notifyEntity . setTradeType ( request . getParameter ( " sub_code " ) ) ;
notifyEntity . setFeeType ( " CNY " ) ;
notifyEntity . setNonceStr ( trade_no ) ;
notifyService . insertNotify ( notifyEntity ) ;
} else {
return " 验签失败 " ;
}
// LOGGER.info("支付成功...");
} else { //验证失败
// LOGGER.info("支付, 验签失败...");
return " 支付, 验签失败 " ;
}
return " success " ;