| | |
| | | package com.yeshi.fanli.util;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | |
|
| | | import com.alipay.api.AlipayApiException;
|
| | | import com.alipay.api.CertAlipayRequest;
|
| | | import com.alipay.api.DefaultAlipayClient;
|
| | | import com.alipay.api.request.AlipayFundTransUniTransferRequest;
|
| | | import com.alipay.api.request.AlipayTradeQueryRequest;
|
| | | import com.alipay.api.response.AlipayFundTransUniTransferResponse;
|
| | | import com.yeshi.fanli.exception.user.AlipayTransferException;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class AlipayUtil {
|
| | |
|
| | | private static DefaultAlipayClient alipayClient = null;
|
| | |
|
| | | static {
|
| | | String privateKey = Constant.alipayConfig.getPrivateKey();
|
| | | CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
| | | certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
|
| | | certAlipayRequest.setAppId(Constant.alipayConfig.getAppId());
|
| | | certAlipayRequest.setPrivateKey(privateKey);
|
| | | certAlipayRequest.setFormat("json");
|
| | | certAlipayRequest.setCharset("GBK");
|
| | | certAlipayRequest.setSignType("RSA2");
|
| | | certAlipayRequest.setCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAppCertPath()).getPath());
|
| | | certAlipayRequest.setAlipayPublicCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAlipayCertPath()).getPath());
|
| | | certAlipayRequest.setRootCertPath(
|
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAlipayRootCertPath()).getPath());
|
| | | try {
|
| | | alipayClient = new DefaultAlipayClient(certAlipayRequest);
|
| | | } catch (AlipayApiException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | public static AlipayFundTransUniTransferResponse transfer(String outBizNo, String account, String name,
|
| | | BigDecimal money, String orderTitle, String mark)
|
| | | throws AlipayApiException, NumberFormatException, AlipayTransferException {
|
| | | AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
| | | JSONObject json = new JSONObject();
|
| | | json.put("out_biz_no", outBizNo);
|
| | | json.put("trans_amount", money.toString());
|
| | | json.put("product_code", "TRANS_ACCOUNT_NO_PWD");
|
| | | json.put("order_title", orderTitle);
|
| | | JSONObject payee_info = new JSONObject();
|
| | | payee_info.put("identity", account);
|
| | | payee_info.put("identity_type", "ALIPAY_LOGON_ID");
|
| | | payee_info.put("name", name);
|
| | | json.put("payee_info", payee_info);
|
| | | json.put("remark", mark);
|
| | | json.put("biz_scene", "DIRECT_TRANSFER");
|
| | |
|
| | | request.setBizContent(json.toString());
|
| | | AlipayFundTransUniTransferResponse response = null;
|
| | | response = alipayClient.certificateExecute(request);
|
| | | // 成功转账
|
| | | if (response != null && response.isSuccess() && "10000".equals(response.getCode())) {
|
| | | return response;
|
| | | } else// 转账失败
|
| | | {
|
| | | throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(),
|
| | | response.getSubMsg());
|
| | | }
|
| | | }
|
| | |
|
| | | public static AlipayFundTransUniTransferResponse transferNoThrowException(String outBizNo, String account, String name,
|
| | | BigDecimal money, String orderTitle, String mark) throws AlipayApiException{
|
| | | AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
| | | JSONObject json = new JSONObject();
|
| | | json.put("out_biz_no", outBizNo);
|
| | | json.put("trans_amount", money.toString());
|
| | | json.put("product_code", "TRANS_ACCOUNT_NO_PWD");
|
| | | json.put("order_title", orderTitle);
|
| | | JSONObject payee_info = new JSONObject();
|
| | | payee_info.put("identity", account);
|
| | | payee_info.put("identity_type", "ALIPAY_LOGON_ID");
|
| | | payee_info.put("name", name);
|
| | | json.put("payee_info", payee_info);
|
| | | json.put("remark", mark);
|
| | | json.put("biz_scene", "DIRECT_TRANSFER");
|
| | |
|
| | | request.setBizContent(json.toString());
|
| | | AlipayFundTransUniTransferResponse response = null;
|
| | | response = alipayClient.certificateExecute(request);
|
| | | return response;
|
| | | }
|
| | |
|
| | | }
|
| | | package com.yeshi.fanli.util; |
| | | |
| | | import java.math.BigDecimal; |
| | | |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.CertAlipayRequest; |
| | | import com.alipay.api.DefaultAlipayClient; |
| | | import com.alipay.api.request.AlipayFundTransUniTransferRequest; |
| | | import com.alipay.api.request.AlipayTradeQueryRequest; |
| | | import com.alipay.api.response.AlipayFundTransUniTransferResponse; |
| | | import com.yeshi.fanli.exception.user.AlipayTransferException; |
| | | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | public class AlipayUtil { |
| | | |
| | | private static DefaultAlipayClient alipayClient = null; |
| | | |
| | | static { |
| | | String privateKey = Constant.alipayConfig.getPrivateKey(); |
| | | CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); |
| | | certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do"); |
| | | certAlipayRequest.setAppId(Constant.alipayConfig.getAppId()); |
| | | certAlipayRequest.setPrivateKey(privateKey); |
| | | certAlipayRequest.setFormat("json"); |
| | | certAlipayRequest.setCharset("GBK"); |
| | | certAlipayRequest.setSignType("RSA2"); |
| | | certAlipayRequest.setCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAppCertPath()).getPath()); |
| | | certAlipayRequest.setAlipayPublicCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAlipayCertPath()).getPath()); |
| | | certAlipayRequest.setRootCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource(Constant.alipayConfig.getAlipayRootCertPath()).getPath()); |
| | | try { |
| | | alipayClient = new DefaultAlipayClient(certAlipayRequest); |
| | | } catch (AlipayApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static AlipayFundTransUniTransferResponse transfer(String outBizNo, String account, String name, |
| | | BigDecimal money, String orderTitle, String mark) |
| | | throws AlipayApiException, NumberFormatException, AlipayTransferException { |
| | | AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); |
| | | JSONObject json = new JSONObject(); |
| | | json.put("out_biz_no", outBizNo); |
| | | json.put("trans_amount", money.toString()); |
| | | json.put("product_code", "TRANS_ACCOUNT_NO_PWD"); |
| | | json.put("order_title", orderTitle); |
| | | JSONObject payee_info = new JSONObject(); |
| | | payee_info.put("identity", account); |
| | | payee_info.put("identity_type", "ALIPAY_LOGON_ID"); |
| | | payee_info.put("name", name); |
| | | json.put("payee_info", payee_info); |
| | | json.put("remark", mark); |
| | | json.put("biz_scene", "DIRECT_TRANSFER"); |
| | | |
| | | request.setBizContent(json.toString()); |
| | | AlipayFundTransUniTransferResponse response = null; |
| | | response = alipayClient.certificateExecute(request); |
| | | // 成功转账 |
| | | if (response != null && response.isSuccess() && "10000".equals(response.getCode())) { |
| | | return response; |
| | | } else// 转账失败 |
| | | { |
| | | throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(), |
| | | response.getSubMsg()); |
| | | } |
| | | } |
| | | |
| | | public static AlipayFundTransUniTransferResponse transferNoThrowException(String outBizNo, String account, String name, |
| | | BigDecimal money, String orderTitle, String mark) throws AlipayApiException{ |
| | | AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); |
| | | JSONObject json = new JSONObject(); |
| | | json.put("out_biz_no", outBizNo); |
| | | json.put("trans_amount", money.toString()); |
| | | json.put("product_code", "TRANS_ACCOUNT_NO_PWD"); |
| | | json.put("order_title", orderTitle); |
| | | JSONObject payee_info = new JSONObject(); |
| | | payee_info.put("identity", account); |
| | | payee_info.put("identity_type", "ALIPAY_LOGON_ID"); |
| | | payee_info.put("name", name); |
| | | json.put("payee_info", payee_info); |
| | | json.put("remark", mark); |
| | | json.put("biz_scene", "DIRECT_TRANSFER"); |
| | | |
| | | request.setBizContent(json.toString()); |
| | | AlipayFundTransUniTransferResponse response = null; |
| | | response = alipayClient.certificateExecute(request); |
| | | return response; |
| | | } |
| | | |
| | | public static void main(String[] args) throws AlipayTransferException, AlipayApiException { |
| | | |
| | | AlipayUtil.transfer("test-test","18581318252","贺小辉",new BigDecimal("0.1"),"板栗快省提现","提现"); |
| | | } |
| | | |
| | | } |