|
|
@@ -0,0 +1,284 @@
|
|
|
+package com.uas.cloud.mall.payment.service.impl;
|
|
|
+
|
|
|
+import com.uas.cloud.commons.domain.PageInfo;
|
|
|
+import com.uas.cloud.commons.persistence.criteria.PredicateUtils;
|
|
|
+import com.uas.cloud.commons.util.FastjsonUtils;
|
|
|
+import com.uas.cloud.commons.util.OkHttpUtils;
|
|
|
+import com.uas.cloud.mall.payment.domain.Payment;
|
|
|
+import com.uas.cloud.mall.payment.repository.PaymentRepository;
|
|
|
+import com.uas.cloud.mall.payment.service.PaymentService;
|
|
|
+import com.uas.cloud.mall.payment.util.ApipaySubmit;
|
|
|
+import com.uas.cloud.mall.payment.util.DirectPayConfig;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.commons.collections.map.HashedMap;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class PaymentServiceImpl implements PaymentService{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaymentRepository paymentRepository;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,String> paymentSign(String orderid,String ensurePrice, String subject){
|
|
|
+
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+
|
|
|
+ // 获取当前时间
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String timestamp = sdf.format(new Date());
|
|
|
+
|
|
|
+ map.put("out_trade_no", orderid);
|
|
|
+ map.put("total_amount", ensurePrice);
|
|
|
+ map.put("subject", subject);
|
|
|
+ map.put("timestamp", timestamp);
|
|
|
+ map.put("method", DirectPayConfig.METHOD);
|
|
|
+ map.put("charset", DirectPayConfig.DEFAULT_CHARSET);
|
|
|
+ map.put("sign_type", DirectPayConfig.SIGN_TYPE);
|
|
|
+ map.put("version", DirectPayConfig.VERSION);
|
|
|
+ map.put("business_code", DirectPayConfig.BUSINESS_CODE);
|
|
|
+ map.put("timeout_express", DirectPayConfig.TIMEOUT_EXPRESS);
|
|
|
+ map.put("notify_url", DirectPayConfig.NOTIFY_URL);
|
|
|
+ map.put("return_url", DirectPayConfig.RETURN_URL);
|
|
|
+ map.put("partner_id", DirectPayConfig.PARTNER_ID);
|
|
|
+ map.put("seller_id", DirectPayConfig.SELLER_ID);
|
|
|
+ map.put("seller_name", DirectPayConfig.SELLER_NAME);
|
|
|
+
|
|
|
+ // 生成签名后的参数
|
|
|
+ Map<String, String> sPara = ApipaySubmit.buildRequestPara(map);
|
|
|
+
|
|
|
+ return sPara;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String ysepayNotify(HttpServletRequest request) throws ParseException {
|
|
|
+
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ 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] + ",";
|
|
|
+ }
|
|
|
+ System.out.println("name:" + name + " value:" + valueStr);
|
|
|
+ params.put(name, valueStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证 params 签名
|
|
|
+ boolean paramsResult = ApipaySubmit.verifySign(params);
|
|
|
+ String trade_status = request.getParameter("trade_status");
|
|
|
+
|
|
|
+ if(paramsResult){
|
|
|
+ System.out.println("异步通知验证签名成功");
|
|
|
+ if(trade_status.equals("TRADE_SUCCESS")){
|
|
|
+ // 保存付款成功交易信息
|
|
|
+ SimpleDateFormat sdf_nt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ SimpleDateFormat sdf_ad = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String yseid = "pay";
|
|
|
+ /* String yseid = EncodingRulesConstant.YSEPAY.replace("_TIMESTAP_NUMBER", createNumberService.getTimeNumber("trade$ysepay", 8));*/
|
|
|
+ String orderid = params.get("out_trade_no");
|
|
|
+ String tradeNo = params.get("trade_no");
|
|
|
+ String totalAmount = params.get("total_amount");
|
|
|
+ Date paymentAccountDate = sdf_ad.parse(params.get("account_date"));
|
|
|
+ Date notifyTime = sdf_nt.parse(params.get("notify_time"));
|
|
|
+ String notifyType = params.get("notify_type");
|
|
|
+ String signType = params.get("sign_type");
|
|
|
+ String paymentSign = params.get("sign");
|
|
|
+ Payment payment = new Payment(yseid, orderid, "已付款", tradeNo, totalAmount, paymentAccountDate, notifyTime, notifyType, signType, paymentSign);
|
|
|
+ save(payment);
|
|
|
+ return "success";
|
|
|
+ }else {
|
|
|
+ return "fail";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "fail";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void save(Payment payment){
|
|
|
+ paymentRepository.save(payment);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, String> refundSign(String orderid, String reid, String refundMoney, String refundReason){
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
+ params.put("out_trade_no", orderid);
|
|
|
+ params.put("out_request_no", reid);
|
|
|
+ params.put("refund_amount", refundMoney);
|
|
|
+ params.put("refund_reason", refundReason);
|
|
|
+ String biz_content = FastjsonUtils.toJson(params);
|
|
|
+
|
|
|
+ // 生成当前时间
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String timestamp = sdf.format(new Date());
|
|
|
+
|
|
|
+ // 待签名参数
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ map.put("biz_content", biz_content);
|
|
|
+ map.put("timestamp", timestamp);
|
|
|
+ map.put("method", "ysepay.online.trade.refund");
|
|
|
+ map.put("charset", DirectPayConfig.DEFAULT_CHARSET);
|
|
|
+ map.put("sign_type", DirectPayConfig.SIGN_TYPE);
|
|
|
+ map.put("version", DirectPayConfig.VERSION);
|
|
|
+ map.put("partner_id", DirectPayConfig.PARTNER_ID);
|
|
|
+
|
|
|
+ // 生成签名后的参数
|
|
|
+ Map<String, String> sPara = ApipaySubmit.buildRequestPara(map);
|
|
|
+
|
|
|
+ return sPara;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String refund(String orderid, String reid, String refundMoney, String refundReason) throws ParseException, IOException {
|
|
|
+
|
|
|
+ Map<String, String> sPara = new HashMap<String, String>();
|
|
|
+ sPara = refundSign(orderid, reid, refundMoney, refundReason);
|
|
|
+
|
|
|
+ String url = "https://openapi.ysepay.com/gateway.do";
|
|
|
+ Response response = OkHttpUtils.sendPost(url, sPara);
|
|
|
+
|
|
|
+ // 银盛官方同步返回信息
|
|
|
+ String reponseText = response.body().string();
|
|
|
+ String refundInfo = FastjsonUtils.parseObject(reponseText).getString("ysepay_online_trade_refund_response");
|
|
|
+
|
|
|
+ // 获取验证签名加密字符串原文
|
|
|
+ String subStr = reponseText.substring(1, reponseText.length() - 1);
|
|
|
+ int firstIndex = subStr.indexOf('{');
|
|
|
+ int lastIndex = subStr.lastIndexOf('}');
|
|
|
+ String responseBody = subStr.substring(firstIndex, lastIndex + 1);
|
|
|
+ String charset = DirectPayConfig.DEFAULT_CHARSET;
|
|
|
+ String sign = FastjsonUtils.parseObject(reponseText).getString("sign");
|
|
|
+
|
|
|
+ // 验证签名以及msg是否为成功
|
|
|
+ boolean paramsResult = ApipaySubmit.verifySign(sign, responseBody, charset);
|
|
|
+ String msg = FastjsonUtils.parseObject(refundInfo).getString("msg");
|
|
|
+
|
|
|
+ if(paramsResult){
|
|
|
+ System.out.println("同步签名验证成功");
|
|
|
+ if(msg.equals("Success")){
|
|
|
+ // 保存退款信息
|
|
|
+ SimpleDateFormat sdf_rad = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String yseid = "refund";
|
|
|
+ /* String yseid = EncodingRulesConstant.YSEPAY.replace("_TIMESTAP_NUMBER", createNumberService.getTimeNumber("trade$ysepay", 8));*/
|
|
|
+ String tradeNo = FastjsonUtils.parseObject(refundInfo).getString("trade_no");
|
|
|
+ String totalAmount = paymentRepository.findByOrderid(orderid).getTotalAmount();
|
|
|
+ Date paymentAccountDate = paymentRepository.findByOrderid(orderid).getPaymentAccountDate();
|
|
|
+ String refundAmount = FastjsonUtils.parseObject(refundInfo).getString("refund_amount");
|
|
|
+ Date refundAccountDate = sdf_rad.parse(FastjsonUtils.parseObject(refundInfo).getString("account_date"));
|
|
|
+ String refundSign = FastjsonUtils.parseObject(reponseText).getString("sign");
|
|
|
+ Payment payment = new Payment(yseid, orderid, reid, "已退款", tradeNo, totalAmount, paymentAccountDate, refundAmount, refundAccountDate, refundSign);
|
|
|
+ save(payment);
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "fail";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,String> accountSign(){
|
|
|
+
|
|
|
+ // 业务参数转为JSON格式
|
|
|
+ Map<String, String> params = new HashMap<String, String>();
|
|
|
+ params.put("user_code", DirectPayConfig.SELLER_ID);
|
|
|
+ params.put("user_name", DirectPayConfig.SELLER_NAME);
|
|
|
+ params.put("account_no", "0000400025744626");
|
|
|
+ String biz_content = FastjsonUtils.toJson(params);
|
|
|
+
|
|
|
+ // 生成当前时间
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String timestamp = sdf.format(new Date());
|
|
|
+
|
|
|
+ // 待签名参数
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ map.put("biz_content", biz_content);
|
|
|
+ map.put("timestamp", timestamp);
|
|
|
+ map.put("method", "ysepay.online.user.account.get");
|
|
|
+ map.put("charset", DirectPayConfig.DEFAULT_CHARSET);
|
|
|
+ map.put("sign_type", DirectPayConfig.SIGN_TYPE);
|
|
|
+ map.put("version", DirectPayConfig.VERSION);
|
|
|
+ map.put("partner_id", DirectPayConfig.PARTNER_ID);
|
|
|
+
|
|
|
+ // 生成查询银盛账户余额请求参数map(加上签名)
|
|
|
+ Map<String, String> sPara = ApipaySubmit.buildRequestPara(map);
|
|
|
+
|
|
|
+ return sPara;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,String> account() throws IOException {
|
|
|
+
|
|
|
+ Map<String, String> sPara = new HashMap<String, String>();
|
|
|
+ sPara = accountSign();
|
|
|
+
|
|
|
+ String url = "https://openapi.ysepay.com/gateway.do";
|
|
|
+ Response response = OkHttpUtils.sendPost(url, sPara);
|
|
|
+
|
|
|
+ String reponseText = response.body().string();
|
|
|
+ String accountInfo = FastjsonUtils.parseObject(reponseText).getString("ysepay_online_user_account_get_response");
|
|
|
+
|
|
|
+ // 获取验证签名加密字符串原文
|
|
|
+ String subStr = reponseText.substring(1, reponseText.length() - 1);
|
|
|
+ int firstIndex = subStr.indexOf('[');
|
|
|
+ int lastIndex = subStr.lastIndexOf(']');
|
|
|
+ String responseBody = subStr.substring(firstIndex, lastIndex + 1);
|
|
|
+ String charset = DirectPayConfig.DEFAULT_CHARSET;
|
|
|
+ String sign = FastjsonUtils.parseObject(reponseText).getString("sign");
|
|
|
+
|
|
|
+ // 验证签名以及msg是否为成功
|
|
|
+ boolean paramsResult = ApipaySubmit.verifySign(sign, responseBody, charset);
|
|
|
+ String info = FastjsonUtils.fromJsonArray(accountInfo).getString(0);
|
|
|
+ String msg = FastjsonUtils.parseObject(info).getString("msg");
|
|
|
+
|
|
|
+ if(paramsResult){
|
|
|
+ System.out.println("同步签名验证成功");
|
|
|
+ if(msg.equals("Success")){
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("accountInfo", accountInfo);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<Payment> queryPayment(final PageInfo pageInfo, String keyword, Long fromDate, Long toDate){
|
|
|
+ if (StringUtils.hasText(keyword)) {
|
|
|
+ // 输入订单号,按订单号查询
|
|
|
+ pageInfo.filter("orderid", keyword);
|
|
|
+ }
|
|
|
+ if (fromDate != null) {// 输入开始时间,按付款生成时间查询
|
|
|
+ pageInfo.expression(PredicateUtils.gte("paymentAccountDate", new Date(fromDate), false));
|
|
|
+ }
|
|
|
+ if (toDate != null) {// 输入截至时间,按付款生成时间查询
|
|
|
+ pageInfo.expression(PredicateUtils.lte("paymentAccountDate", new Date(toDate), false));
|
|
|
+ }
|
|
|
+
|
|
|
+ return paymentRepository.findAll(new Specification<Payment>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<Payment> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
|
|
+ query.where(pageInfo.getPredicates(root, query, builder));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ },pageInfo);
|
|
|
+
|
|
|
+ };
|
|
|
+}
|
|
|
+
|