支付宝支付java服务端处理

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>3.7.110.ALL</version>
</dependency>
private YJResult aliPay(String orderNo, String totalAmount) {
    String appId = environment.getProperty("alipay.appId");
    String charset = environment.getProperty("alipay.charset");
    String signType = environment.getProperty("alipay.signType");
    String version = environment.getProperty("alipay.version");
    String pri = environment.getProperty("alipay.private");
    String pub = environment.getProperty("alipay.public");

    //实例化客户端
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, pri, "json", charset, pub, signType);
    //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
    AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
    //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
    AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
    model.setBody("充值订单:" + orderNo);
    model.setSubject("支付订单");
    model.setOutTradeNo(orderNo);
    model.setTimeoutExpress("30m");
    model.setTotalAmount(totalAmount);
    model.setProductCode("QUICK_MSECURITY_PAY");
    request.setBizModel(model);
    // 回调路径可选
    request.setNotifyUrl(aliPay);
    request.setApiVersion(version);
    try {
        //这里和普通的接口调用不同,使用的是sdkExecute
        log.info("请求支付宝支付请求参数:{}", JSONObject.toJSONString(request));
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        log.info("请求支付宝支付响应结果:{}", JSONObject.toJSONString(response));
        return YJResult.success(response);
        //System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
    } catch (Exception e) {
        e.printStackTrace();
    }
    return YJResult.error(1005, "生成支付数据失败");
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页