| | |
| | | package com.yeshi.fanli.util.alipay;
|
| | |
|
| | | import java.net.URLEncoder;
|
| | |
|
| | | import com.alipay.api.AlipayApiException;
|
| | | import com.alipay.api.CertAlipayRequest;
|
| | | import com.alipay.api.DefaultAlipayClient;
|
| | | import com.alipay.api.request.AlipayTradeQueryRequest;
|
| | | import com.alipay.api.request.AlipayTradeWapPayRequest;
|
| | | import com.alipay.api.response.AlipayTradeQueryResponse;
|
| | | import com.yeshi.fanli.dto.AlipayTradeWapPayDTO;
|
| | | import com.yeshi.fanli.entity.config.AlipayWapConfig;
|
| | | import com.yeshi.fanli.util.AlipayUtil;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class AlipayApi {
|
| | |
|
| | | private static DefaultAlipayClient alipayClient = null;
|
| | |
|
| | | static {
|
| | | AlipayWapConfig alipayWapConfig = Constant.alipayWapConfig;
|
| | | |
| | | CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
| | | certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
|
| | | certAlipayRequest.setAppId(alipayWapConfig.getAppId());
|
| | | certAlipayRequest.setPrivateKey(alipayWapConfig.getPrivateKey());
|
| | | certAlipayRequest.setFormat("json");
|
| | | certAlipayRequest.setCharset("UTF-8");
|
| | | certAlipayRequest.setSignType("RSA2");
|
| | | certAlipayRequest.setCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAppCertPath()).getPath());
|
| | | certAlipayRequest.setAlipayPublicCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAlipayCertPath()).getPath());
|
| | | certAlipayRequest.setRootCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAlipayRootCertPath()).getPath());
|
| | | try {
|
| | | alipayClient = new DefaultAlipayClient(certAlipayRequest);
|
| | | } catch (AlipayApiException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 创建支付请求
|
| | | * @param map
|
| | | * @return
|
| | | * @throws Exception
|
| | | */
|
| | | public static String tradeWapPayRequest(AlipayTradeWapPayDTO payDTO) throws Exception{
|
| | | // 待请求参数数组
|
| | | JSONObject json = new JSONObject();
|
| | | // 收款方账号
|
| | | json.put("seller_id", payDTO.getSellerId());
|
| | | // 订单号
|
| | | json.put("out_trade_no", payDTO.getOutTradeNo());
|
| | | // 订单金额:0.01元,精准到分
|
| | | json.put("total_amount",payDTO.getTotalAmount());
|
| | | // 订单标题
|
| | | json.put("subject", payDTO.getSubject());
|
| | | // 销售产品码,商家和支付宝签约的产品码
|
| | | json.put("product_code", payDTO.getProductCode());
|
| | | // 该笔订单允许的最晚付款时间,逾期将关闭交易 30分钟
|
| | | json.put("timeout_express", payDTO.getTimeoutExpress());
|
| | | |
| | | |
| | | AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
|
| | | // 前台回调地址
|
| | | alipayRequest.setReturnUrl(payDTO.getReturnUrl());
|
| | | // 成功付款回调
|
| | | alipayRequest.setNotifyUrl(payDTO.getNotifyUrl());
|
| | | |
| | | alipayRequest.setBizContent(URLEncoder.encode(json.toString(), "UTF-8"));
|
| | | |
| | | return alipayClient.pageExecute(alipayRequest).getBody();
|
| | | }
|
| | | |
| | | /**
|
| | | * 查询是否交易完成
|
| | | * @param outTradeNo
|
| | | * @param tradeNo
|
| | | * @param orgPid
|
| | | * @param queryOptions
|
| | | * @return
|
| | | * @throws Exception
|
| | | */
|
| | | public static AlipayTradeQueryResponse tradeQuery(String outTradeNo, String tradeNo, String orgPid, String queryOptions) throws Exception {
|
| | | // 订单支付时传入的商户订单号,和支付宝交易号不能同时为空
|
| | | if (StringUtil.isNullOrEmpty(outTradeNo) || StringUtil.isNullOrEmpty(outTradeNo)) {
|
| | | return null;
|
| | | }
|
| | | |
| | | // 待请求参数数组
|
| | | JSONObject json = new JSONObject();
|
| | | if (!StringUtil.isNullOrEmpty(outTradeNo))
|
| | | json.put("out_trade_no", outTradeNo);
|
| | | if (!StringUtil.isNullOrEmpty(tradeNo))
|
| | | json.put("trade_no", tradeNo);
|
| | | if (!StringUtil.isNullOrEmpty(orgPid))
|
| | | json.put("org_pid", orgPid);
|
| | | if (!StringUtil.isNullOrEmpty(queryOptions))
|
| | | json.put("query_options", queryOptions);
|
| | | |
| | | AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
| | | request.setBizContent(json.toString());
|
| | | |
| | | return alipayClient.certificateExecute(request);
|
| | | }
|
| | | |
| | | }
|
| | | package com.yeshi.fanli.util.alipay; |
| | | |
| | | import java.net.URLEncoder; |
| | | |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.CertAlipayRequest; |
| | | import com.alipay.api.DefaultAlipayClient; |
| | | import com.alipay.api.request.AlipayTradeQueryRequest; |
| | | import com.alipay.api.request.AlipayTradeWapPayRequest; |
| | | import com.alipay.api.response.AlipayTradeQueryResponse; |
| | | import com.yeshi.fanli.dto.AlipayTradeWapPayDTO; |
| | | import com.yeshi.fanli.entity.config.AlipayWapConfig; |
| | | import com.yeshi.fanli.util.AlipayUtil; |
| | | import com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.StringUtil; |
| | | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | public class AlipayApi { |
| | | |
| | | private static DefaultAlipayClient alipayClient = null; |
| | | |
| | | static { |
| | | AlipayWapConfig alipayWapConfig = Constant.alipayWapConfig; |
| | | |
| | | CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); |
| | | certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do"); |
| | | certAlipayRequest.setAppId(alipayWapConfig.getAppId()); |
| | | certAlipayRequest.setPrivateKey(alipayWapConfig.getPrivateKey()); |
| | | certAlipayRequest.setFormat("json"); |
| | | certAlipayRequest.setCharset("UTF-8"); |
| | | certAlipayRequest.setSignType("RSA2"); |
| | | certAlipayRequest.setCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAppCertPath()).getPath()); |
| | | certAlipayRequest.setAlipayPublicCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAlipayCertPath()).getPath()); |
| | | certAlipayRequest.setRootCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(alipayWapConfig.getAlipayRootCertPath()).getPath()); |
| | | try { |
| | | alipayClient = new DefaultAlipayClient(certAlipayRequest); |
| | | } catch (AlipayApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 创建支付请求 |
| | | * @param map |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String tradeWapPayRequest(AlipayTradeWapPayDTO payDTO) throws Exception{ |
| | | // 待请求参数数组 |
| | | JSONObject json = new JSONObject(); |
| | | // 收款方账号 |
| | | json.put("seller_id", payDTO.getSellerId()); |
| | | // 订单号 |
| | | json.put("out_trade_no", payDTO.getOutTradeNo()); |
| | | // 订单金额:0.01元,精准到分 |
| | | json.put("total_amount",payDTO.getTotalAmount()); |
| | | // 订单标题 |
| | | json.put("subject", payDTO.getSubject()); |
| | | // 销售产品码,商家和支付宝签约的产品码 |
| | | json.put("product_code", payDTO.getProductCode()); |
| | | // 该笔订单允许的最晚付款时间,逾期将关闭交易 30分钟 |
| | | json.put("timeout_express", payDTO.getTimeoutExpress()); |
| | | |
| | | |
| | | AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); |
| | | // 前台回调地址 |
| | | alipayRequest.setReturnUrl(payDTO.getReturnUrl()); |
| | | // 成功付款回调 |
| | | alipayRequest.setNotifyUrl(payDTO.getNotifyUrl()); |
| | | |
| | | alipayRequest.setBizContent(URLEncoder.encode(json.toString(), "UTF-8")); |
| | | |
| | | return alipayClient.pageExecute(alipayRequest).getBody(); |
| | | } |
| | | |
| | | /** |
| | | * 查询是否交易完成 |
| | | * @param outTradeNo |
| | | * @param tradeNo |
| | | * @param orgPid |
| | | * @param queryOptions |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static AlipayTradeQueryResponse tradeQuery(String outTradeNo, String tradeNo, String orgPid, String queryOptions) throws Exception { |
| | | // 订单支付时传入的商户订单号,和支付宝交易号不能同时为空 |
| | | if (StringUtil.isNullOrEmpty(outTradeNo) || StringUtil.isNullOrEmpty(outTradeNo)) { |
| | | return null; |
| | | } |
| | | |
| | | // 待请求参数数组 |
| | | JSONObject json = new JSONObject(); |
| | | if (!StringUtil.isNullOrEmpty(outTradeNo)) |
| | | json.put("out_trade_no", outTradeNo); |
| | | if (!StringUtil.isNullOrEmpty(tradeNo)) |
| | | json.put("trade_no", tradeNo); |
| | | if (!StringUtil.isNullOrEmpty(orgPid)) |
| | | json.put("org_pid", orgPid); |
| | | if (!StringUtil.isNullOrEmpty(queryOptions)) |
| | | json.put("query_options", queryOptions); |
| | | |
| | | AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); |
| | | request.setBizContent(json.toString()); |
| | | |
| | | return alipayClient.certificateExecute(request); |
| | | } |
| | | |
| | | } |