欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

U支付

程序员文章站 2022-03-19 16:22:48
package com.chongxuan.web.common.pay.processor;import com.chongxuan.web.ConstantsPay;import com.chongxuan.web.common.dto.req.ReqPayRechargeNew;import com.chongxuan.web.common.entity.MemberPayJour;import com.chongxuan.web.common.entity.PayChannelNew;....
package com.chongxuan.web.common.pay.processor;


import com.chongxuan.web.ConstantsPay;
import com.chongxuan.web.common.dto.req.ReqPayRechargeNew;
import com.chongxuan.web.common.entity.MemberPayJour;
import com.chongxuan.web.common.entity.PayChannelNew;
import com.chongxuan.web.common.entity.PayPlatformNew;
import com.chongxuan.web.common.pay.AbstractPay;
import com.chongxuan.web.common.util.JsonUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Repository(value = ConstantsPay.U_PAY + "Processor")
@Log4j2
public class UPayProcessor extends AbstractPay {
    @Override
    public String orderPay(PayChannelNew payChannelNew, PayPlatformNew payPlatformNew, ReqPayRechargeNew reqPayRecharge) {
        SortedMap<String, Object> params = new TreeMap<>();
        params.put("pid", payPlatformNew.getMerId());
        params.put("cid", payChannelNew.getPayMethod());
        params.put("type", null);
        params.put("oid", reqPayRecharge.getOrderNo());
//params.put("uid", memberCacheManager.getMemberId( request ));
        params.put("uid", "我是用户id");
        params.put("amount", reqPayRecharge.getMoney().multiply(BigDecimal.valueOf(100)).setScale(0,
                BigDecimal.ROUND_HALF_UP).toString());
//    params.put("sname", "买东西");
        params.put("burl", payReturnUrl);
        params.put("nurl", payCallbackUrl + ConstantsPay.U_PAY);
        params.put("eparam", "eparam");
        params.put("ip", reqPayRecharge.getRealIp());
        params.put("stype", "MD5");

        String signStr = this.assemblyUrl( params ) + "&key=" + payPlatformNew.getSignMd5();
        String sign    = DigestUtils.md5Hex( signStr ).toUpperCase();
        params.put("sign", sign);

        //MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
        //requestMap.setAll(params);
        //log.warn(JsonUtil.object2Json(requestMap));

        //发送请求
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity(params, httpHeaders);

        Map<String, Object> resultMap = null;
        try {
            resultMap = restTemplate.postForObject(payPlatformNew.getPlatPayUrl(), httpEntity, Map.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.warn(resultMap);
        if (!CollectionUtils.isEmpty(resultMap) && resultMap.containsKey("payurl")) {
            return resultMap.get("payurl").toString();
        }
        return null;
    }

    @Override
    public boolean queryPay(MemberPayJour payJour, PayPlatformNew payPlatformNew, PayChannelNew payChannelNew) {
        SortedMap<String, Object> params = new TreeMap<>();
        params.put("pid", payPlatformNew.getMerId());
        params.put("oid", payJour.getOrder_no());

        String signStr = this.assemblyUrl( params ) + "&key=" + payPlatformNew.getSignMd5();
        String sign    = DigestUtils.md5Hex( signStr ).toUpperCase();
        params.put("sign", sign);

        //MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
        //requestMap.setAll(params);
        //log.warn(JsonUtil.object2Json(requestMap));

        //发送请求
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity(params, httpHeaders);

        Map<String, Object> resultMap = null;
        try {
            resultMap = restTemplate.postForObject(payPlatformNew.getPlatQueryUrl(), httpEntity, Map.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        log.warn(JsonUtil.object2Json(resultMap));
        if (!CollectionUtils.isEmpty(resultMap)) {
            int status = Integer.parseInt(resultMap.getOrDefault("status", 0).toString());
            if (status == 1) {
                BigDecimal ramount = new BigDecimal(resultMap.getOrDefault("ramount", 0).toString());
                String sid = resultMap.getOrDefault("sid", "").toString();
                payJour.setSub_money(ramount);
                payJour.setTrade_sn(sid);
                return true;
            }
        }
        return false;
    }

    @Override
    public String callbackPay(Map<String, Object> requestMap, String realIp) {
        // 订单id
        String oid = requestMap.getOrDefault("oid", "").toString();
        // 上游订单ID
        String sid = requestMap.getOrDefault("sid", "").toString();

        MemberPayJour payJour = payJourMapper.findByOrderNo(oid);

        if ("1".equals(payJour.getStatus())) {
            log.warn("订单已成功,无需继续回调");
            return "success";
        }

        PayPlatformNew payPlatformNew = payPlatformNewMapper.selectByPrimaryKey(Integer.valueOf(payJour.getPlatform_id()));
        PayChannelNew payChannelNew = payChannelNewMapper.selectByPrimaryKey(Integer.valueOf(payJour.getChannel_id()));

        if ( this.verifyIP( requestMap, realIp, payPlatformNew ) ) {
            return "fail";
        }

        String pid = requestMap.getOrDefault("pid", "").toString();
        String cid = requestMap.getOrDefault("cid", "").toString();
        String uid = requestMap.getOrDefault("uid", "").toString();
        BigDecimal amount = new BigDecimal(requestMap.getOrDefault("amount", 0).toString());
        BigDecimal ramount = new BigDecimal(requestMap.getOrDefault("ramount", 0).toString());
        String stime = requestMap.getOrDefault("stime", "").toString();
        String code = requestMap.getOrDefault("code", "").toString();
        String sign = requestMap.getOrDefault("sign", "").toString();

        SortedMap<String, Object> bodyMap = new TreeMap<>();
        bodyMap.put("oid", oid);
        bodyMap.put("sid", sid);
        bodyMap.put("pid", pid);
        bodyMap.put("cid", cid);
        bodyMap.put("uid", uid);
        bodyMap.put("amount", amount);
        bodyMap.put("stime", stime);
        bodyMap.put("code", code);

        String signStr = this.assemblyUrl(bodyMap) + "&key=" + payPlatformNew.getSignMd5();
        log.warn(signStr);
        String rel = DigestUtils.md5Hex(signStr);
        log.warn(rel + ":" + sign);

        //验签
        if (rel.equalsIgnoreCase(sign)) {
            if ("1".equals(code)) {
                payJour.setSub_money(ramount);
                payJour.setTrade_sn(sid);
                return payService.updatePayJourStatus(payJour, payChannelNew.getDiscountBill(), new String[]{"success",
                        "fail"}, payChannelNew.getName());
            }
        }
        return "fail";
    }
}

本文地址:https://blog.csdn.net/programmer188/article/details/110223196