AbstractWechatPaymentHandler.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.mb.payment.handler.wchat;
  2. import cn.beecloud.bean.TransferParameter;
  3. import com.mb.payment.context.IPayment;
  4. import com.mb.payment.context.RefundContext;
  5. import com.mb.payment.exception.PaymentException;
  6. import com.mb.payment.handler.AbstractPaymentHandler;
  7. import net.sf.json.xml.XMLSerializer;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  12. import org.apache.http.conn.ssl.SSLContexts;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.util.EntityUtils;
  17. import javax.net.ssl.SSLContext;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.security.*;
  21. import java.security.cert.CertificateException;
  22. public abstract class AbstractWechatPaymentHandler extends AbstractPaymentHandler {
  23. protected String appId;
  24. /**
  25. * 商户号id
  26. */
  27. protected String mchId;
  28. /**
  29. * API密钥
  30. */
  31. protected String apiKey;
  32. protected String notifyPayUrl;
  33. protected String notifyRefundUrl;
  34. public AbstractWechatPaymentHandler(String appId, String mchId, String apiKey, String notifyPayUrl, String notifyRefundUrl) {
  35. this.appId = appId;
  36. this.mchId = mchId;
  37. this.apiKey = apiKey;
  38. this.notifyPayUrl = notifyPayUrl;
  39. this.notifyRefundUrl = notifyRefundUrl;
  40. }
  41. @Override
  42. public String refundApply(RefundContext refundContext) {
  43. return null;
  44. }
  45. @Override
  46. public boolean isPayment(String orderNo) {
  47. return false;
  48. }
  49. @Override
  50. public String findPaymentOrder(String orderNo) {
  51. return null;
  52. }
  53. @Override
  54. public String transfer(TransferParameter transferParameter){
  55. throw new UnsupportedOperationException("不支持的操作");
  56. }
  57. protected String sendSSLRequest(String url,String mapToXml) {
  58. CloseableHttpClient httpclient = null;
  59. try {
  60. httpclient = getCloseableHttpClient();
  61. HttpPost httpost = new HttpPost(url); // 设置响应头信息
  62. httpost.setEntity(new StringEntity(mapToXml, "UTF-8"));
  63. CloseableHttpResponse response = httpclient.execute(httpost);
  64. try {
  65. HttpEntity entity = response.getEntity();
  66. String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
  67. EntityUtils.consume(entity);
  68. XMLSerializer xmlSerializer = new XMLSerializer();
  69. String result = xmlSerializer.read(jsonStr).toString();
  70. log.info("微信小程序返回信息:" + result);
  71. return result;
  72. } finally {
  73. response.close();
  74. }
  75. } catch (Exception ex) {
  76. log.error("发送支付请求错误", ex);
  77. } finally {
  78. if (httpclient != null) {
  79. try {
  80. httpclient.close();
  81. } catch (IOException e) {
  82. log.error("关闭httpclient错误", e);
  83. }
  84. }
  85. }
  86. log.info("支付参数"+mapToXml);
  87. throw new PaymentException("支付渠道不可用,请稍后再试");
  88. }
  89. protected CloseableHttpClient getCloseableHttpClient() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException {
  90. KeyStore keyStore = KeyStore.getInstance("PKCS12");
  91. InputStream instream = this.getClass().getResourceAsStream("/cert/apiclient_cert.p12");
  92. try {
  93. keyStore.load(instream, this.mchId.toCharArray());
  94. } finally {
  95. instream.close();
  96. }
  97. SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, this.mchId.toCharArray()).build();
  98. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
  99. SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
  100. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
  101. }
  102. @Override
  103. public String doPay(IPayment payment) throws PaymentException {
  104. return null;
  105. }
  106. @Override
  107. public String refund(RefundContext refundContext) throws Exception {
  108. return null;
  109. }
  110. public String getAppId() {
  111. return appId;
  112. }
  113. public void setAppId(String appId) {
  114. this.appId = appId;
  115. }
  116. public String getMchId() {
  117. return mchId;
  118. }
  119. public void setMchId(String mchId) {
  120. this.mchId = mchId;
  121. }
  122. }