微信及支付宝退款记录.

公司用到支付宝及微信退款业务,无特殊要求,仅需完成订单的退款.

1.微信退款

private Boolean weixinRefund(String orderCode, BigDecimal actualAmount) {
		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;
		httpclient = initCert();
		// 构造http请求
		HttpPost httpPost = new HttpPost(cthirdPartyService.findValueByConfig("weixinRefundUrl"));
		SortedMap<String, String> xmlList = refundArg(orderCode, actualAmount);
		// 把参数组合成xml形式
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		xmlList.keySet().stream().forEach(entry -> {
			if (StringUtils.isNotBlank(xmlList.get(entry))) {
				sb.append("<" + entry + ">" + xmlList.get(entry) + "</" + entry + ">");
			}
		});
		sb.append("</xml>");
		StringEntity stringEntity = new StringEntity(sb.toString());
		httpPost.setEntity(stringEntity);
		// 发送请求
		response = httpclient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		if (Objects.nonNull(entity)) {
			BufferedReader reader1 = new BufferedReader(new InputStreamReader(entity.getContent()));
			SAXBuilder jdomBuilder = new SAXBuilder();
			Document document = jdomBuilder.build(reader1);
			Element rootElement = document.getRootElement();
			// 结果码
			String returnCode = rootElement.getChild("return_code").getText();
			if (Objects.equals(returnCode, "SUCCESS")) {
				log.info("微信退款成功。");
				return true;
			} else {
				log.error("微信退款失败!");
				return false;
			}
		}
		return false;
	}

	private SortedMap<String, String> refundArg(String orderCode, BigDecimal actualAmount) {

		SortedMap<String, String> xmlList = Maps.newTreeMap();
		try {

			// 随机生成32位随机数
			String code = GenerateCode.randomCode(32);
			xmlList.put("nonce_str", code);

			BigDecimal multiply = actualAmount.setScale(2, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));
			int amount = multiply.intValue();
			xmlList.put("appid", cthirdPartyService.findValueByConfig("weixinAppId"));

			xmlList.put("refund_fee_type", "CNY");
			xmlList.put("out_refund_no", code);
			xmlList.put("total_fee", String.valueOf(amount));
			xmlList.put("refund_fee", String.valueOf(amount));
			xmlList.put("op_user_id", cthirdPartyService.findValueByConfig("weixinMchId"));
			xmlList.put("mch_id", cthirdPartyService.findValueByConfig("weixinMchId"));
			xmlList.put("out_trade_no", orderCode);

			String sign = PayCommonUtil.createSign(cthirdPartyService.findValueByConfig("weixinPublicKey"), xmlList);
			xmlList.put("sign", sign);
			return xmlList;
		} catch (Exception e) {
			log.error("e", "genProductArgs fail, 异常: " + e.getMessage());
			return null;
		}
	}

具体参数意义参见微信官方.

2.支付宝退款

	private Boolean aLiRefund(String orderCode, BigDecimal actualAmount) {
		try {
			AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
					cthirdPartyService.findValueByConfig("alipayAppId"),
					cthirdPartyService.findValueByConfig("alipayPrivateKey"), "json", "UTF-8",
					cthirdPartyService.findValueByConfig("aliPublicKey"));
			AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("out_trade_no", orderCode);
			jsonObject.put("refund_amount", actualAmount);
			request.setBizContent(jsonObject.toString());
			log.debug("request参数==>" + jsonObject.toString());
			AlipayTradeRefundResponse response = alipayClient.execute(request);
			String refundStatu = response.getCode();
			if (Objects.equals(refundStatu, "10000")) {
				log.info("支付宝退款成功。");
				return true;
			} else {
				log.error("支付宝退款失败!!");
				return false;
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
			return false;
		}
	}

相较于微信,支付宝操作更为简便.具体参数意义,参见支付宝官方.


仅作为个人工作中的一点记录,无任何其他用途.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值