| | |
| | | import com.alipay.api.response.AlipayFundTransUniTransferResponse; |
| | | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | import org.slf4j.Logger; |
| | | |
| | | |
| | | public class AlipayUtil { |
| | | |
| | | private final static Logger logger = org.slf4j.LoggerFactory.getLogger("alipayLogger"); |
| | | |
| | | private static DefaultAlipayClient alipayClient = null; |
| | | |
| | | static { |
| | | String privateKey = ""; |
| | | String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDhLhILE3YPWHOTJ9ndbdAQ+5DzrQ/+esTwAFnvo7YN+Bto8njOX9CI4/HE78bj7fFfTsPAJv0ga+b6iTCw0HhkI7oPZIJ1S4WH9w/EvzcsfoZvSEOJLHhnqOu8rkn8OVwzMZ5DtmRC0Sy2LZ+zIvdOqzJYAPlbJb+Q99ccVgr6nu+JouO6fHXMQ1xKvv5lSFgGy8EFiEU71UHcxdsA21Qn90ZNXAOSt8qHFydANVSE90pjAj3iLlpxleJGTCgLuAfGzN6pxyykjfR1m2YwWvEu7xcMgSlzO6/Hp0nWS8USehY2llRUAYW5QOq3+rhvZRE3rd9eESeDSpkkMWwQPY8RAgMBAAECggEAeL7BghrvZY8iejgWRPwLY+/hbCjSuLyQObmWgD+i8Z6m5AGQ8ejC44SHMfj5B2Oh2F9d6/18IhR6CMHj0AwZYmkd7f8RTWAvvBao5/E43q0B3U48mKO19K/JqKU0mwEPBYyHtvtoi8OqXA3e4qnPPunh1vq+uzdkn5GmUVV6EaSCES+BUBusxwYrBGvnZM//1Z7n3JqsmN+QZZ6c/uWeK0Gzl2zIONPCUnGJzp9gSeDPu6RxM1NLCMcxwrmfhmW+Jy5uhNAjUkfWC8Os6XCneVUTxoUGxZrEsFQbq7fRMUZM/DU5/1zOJpkAPzFEoFsBMp6cWl9ZwYawn9bF+kB4OQKBgQD+sdGPdK8vzZbRKrlPLk+k1JvN7SEDiJc5ZJ3AI5FzKsFL7DP9DLFV1Yev2sk8TrcjksDYHTJlxnNqPMdgJV31Wf+dvw2Wrsl7YucnbtvU6Odl1Vd3GtfIilsZ+oY+sdK3yHxDkg6q668VsSt8KcW47DArliRicQRYrfZnc0HrawKBgQDiVYavq3Ie07diAXpX7ILqbn/d6jbZH7X1qofOp7ZTf6qwFOWmS4slZ7V2M0e1PztgK4+dZbm4aJ+WZojbxbx0nc8mWQLPmAEqvGlECVO+vNrEO0e6v0w2ERpFoGPjaQF6pb21SUdNhvyewQLBHnobziMDzcix5D8J8GZdDpPqcwKBgAGN9El73NXd213LdPYWwkWvRoQrPlhXLRUcmBnkHa8mQ6fC+U5CFZMAiqN4ImJL2LJO4v5urzAP1gR/+neLwtcMhZLg56RTzMcEA4UqGA1knoTwmGgRsd93eLbf4JHhY/fbMWb4LT690YPAYROQ7bQDPQgHEpHHPs+LwEVBXtWhAoGAXaAPdP+w13uw8kO2PkTB6wQGtN+8DJkHhjYis6PA0k2PaANc9CtvxeterySLVzhu8VtTr0JzTwtIaMjFeFQVnw/grvZHKWSxpuIFkXViRRaOUHpNM70KY5sziPXOHDZFvFxvW1ySuElMmscwwn4++N2OsnYfvSazxQLC9V+gZG0CgYB7dHbzpBi4JgggeXo0nWVH82pBQTvCsPQVRvznY6MQxVBbkOPFvdy3bB1g6Br3iyS5heS25JX7/3AOX4rU1Sfiqvn7rLKVRbhxPimcfGULNQUyeKX/+FKKG9Yi3KTV8Mzy8LxDxwJPpPgmV88fdUKrjuVLZxmrluo10czBM5DLjw=="; |
| | | CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); |
| | | certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do"); |
| | | certAlipayRequest.setAppId("2021004141681244"); |
| | |
| | | certAlipayRequest.setFormat("json"); |
| | | certAlipayRequest.setCharset("GBK"); |
| | | certAlipayRequest.setSignType("RSA2"); |
| | | certAlipayRequest.setCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource("alipay/alipayCertPublicKey_RSA2.crt").getPath()); |
| | | certAlipayRequest.setAlipayPublicCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource("alipay/appCertPublicKey_2021004141681244.crt").getPath()); |
| | | certAlipayRequest.setRootCertPath( |
| | | AlipayUtil.class.getClassLoader().getResource("alipay/alipayRootCert.crt").getPath()); |
| | | // certAlipayRequest.setCertPath( |
| | | // AlipayUtil.class.getClassLoader().getResource("alipay/appCertPublicKey_2021004141681244.crt").getPath()); |
| | | // certAlipayRequest.setAlipayPublicCertPath( |
| | | // AlipayUtil.class.getClassLoader().getResource("alipay/alipayCertPublicKey_RSA2.crt").getPath()); |
| | | // certAlipayRequest.setRootCertPath( |
| | | // AlipayUtil.class.getClassLoader().getResource("alipay/alipayRootCert.crt").getPath()); |
| | | |
| | | certAlipayRequest.setCertPath("/www/wwwroot/alipay/appCertPublicKey_2021004141681244.crt"); |
| | | certAlipayRequest.setAlipayPublicCertPath("/www/wwwroot/alipay/alipayCertPublicKey_RSA2.crt"); |
| | | certAlipayRequest.setRootCertPath("/www/wwwroot/alipay/alipayRootCert.crt"); |
| | | |
| | | logger.info("证书路径:{}", certAlipayRequest.getCertPath()); |
| | | try { |
| | | alipayClient = new DefaultAlipayClient(certAlipayRequest); |
| | | } catch (AlipayApiException e) { |
| | | e.printStackTrace(); |
| | | logger.error("支付宝初始化失败", e); |
| | | } |
| | | |
| | | } |
| | |
| | | response = alipayClient.certificateExecute(request); |
| | | // 成功转账 |
| | | if (response != null && response.isSuccess() && "10000".equals(response.getCode())) { |
| | | logger.info("转账成功:{}-{}-{}-{}",outBizNo, name, account,money ); |
| | | return response; |
| | | } else// 转账失败 |
| | | { |
| | | logger.warn("转账失败:{}-{}",outBizNo, response!=null? response.getSubMsg():"未知原因"); |
| | | throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(), |
| | | response.getSubMsg()); |
| | | } |
| | |
| | | |
| | | public static void main(String[] args) throws AlipayTransferException, AlipayApiException { |
| | | |
| | | AlipayUtil.transfer("test-test","18581318252","贺小辉",new BigDecimal("0.1"),"板栗快省提现","提现"); |
| | | AlipayUtil.transfer("test-1","18323283413@163.com","夏程培",new BigDecimal("0.1"),"积分兑换","积分兑换"); |
| | | } |
| | | |
| | | |
| | | static class AlipayTransferException extends Exception { |
| | | public static class AlipayTransferException extends Exception { |
| | | public AlipayTransferException() { |
| | | |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | } |