本文主要是Java实现支付宝小程序支付功能:

  // 实例化客户端
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                    PropertiesUtils.getProperty("ALIPAY_APPID"), PropertiesUtils.getProperty("ALIPAY_PRAVITEKEY"),
                    "json", "UTF-8", PropertiesUtils.getProperty("ALIPAY_PUBLICKEY"), "RSA2");
            // 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            // SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            if (consumptionType == 0) {
                model.setBody("押金缴纳");
                model.setSubject("用户缴纳押金");
            } else {
                model.setBody("充值");
                model.setSubject("用户充值");
            }

            model.setOutTradeNo(orderNo);
            model.setTimeoutExpress("30m");
            model.setTotalAmount(consumptionAmount + "");
            model.setProductCode("QUICK_MSECURITY_PAY");
            request.setBizModel(model);
            request.setNotifyUrl("https://www.hopefull.cn/smallBed/transaction/alipayNotify/" + orderNo);
            try {
                // 这里和普通的接口调用不同,使用的是sdkExecute
                AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
                System.out.println(response.getBody());// 就是orderString
                                                        // 可以直接给客户端请求,无需再做处理。
                dto.setCode(response.getBody());
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
 /**
 * 支付宝异步回调方法
 * @author Mark
 * @throws AlipayApiException
 * @throws IOException
 */
@RequestMapping("/alipayNotify/{batchNo}")
public void alipayNotify(HttpServletRequest request, HttpServletResponse response, @PathVariable String batchNo)
        throws Exception {
    logger.info("进入支付宝支付回调");
    logger.info("支付编号为:" + batchNo);
    Map<String, String[]> requestParams = request.getParameterMap();
    logger.info("支付宝回调参数:" + requestParams.toString());
    Map<String, String> params = new LinkedHashMap<String, String>();
    for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
        String name = (String) iter.next();
        String[] values = requestParams.get(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
            valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
        }
        params.put(name, valueStr);
    }
    logger.info("支付宝回调参数:" + params.toString());
    String status = params.get("trade_status");
    Boolean signVerified = AlipaySignature.rsaCheckV1(params, PropertiesUtils.getProperty("ALIPAY_PUBLICKEY"),
            "UTF-8","RSA2");
    logger.info("验证签名结果:" + signVerified);
    if (signVerified) {
        logger.info("status" + status);
        if (org.apache.commons.lang.StringUtils.equals(status.toLowerCase(), "trade_success")) {
            int result = transactionService.updatePayStatusByOrderNo(batchNo);
            if (result > 0) {
                Transaction t = transactionService.getTransactionByOrderNo(batchNo);
                if (t.getAfterSale() == 0) {
                    memberService.updateMemberDeposits(1, t.getConsumptionAmount(), t.getMemberId());
                } else {
                    memberService.updateMemberAvailableBalance(t.getConsumptionAmount(), t.getMemberId());
                }
                Transaction ts = transactionService.getMemberByConsumptionAmount(t.getMemberId());
                transactionService.updateCurrentBalance(ts.getConsumptionAmount(), t.getId());
            }
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                logger.error(ExceptionUtils.getMessage(e));
            }
        } else if (org.apache.commons.lang.StringUtils.equals(status.toLowerCase(), "trade_finished")) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                logger.error(ExceptionUtils.getMessage(e));
            }
        }
    } else {
        logger.error("参数验证失败");
        response.getWriter().write("failure");
    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐