123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package com.mb.payment.handler.wchat;
- import cn.beecloud.bean.TransferParameter;
- import com.mb.payment.context.IPayment;
- import com.mb.payment.context.RefundContext;
- import com.mb.payment.exception.PaymentException;
- import com.mb.payment.handler.AbstractPaymentHandler;
- import net.sf.json.xml.XMLSerializer;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.conn.ssl.SSLContexts;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import javax.net.ssl.SSLContext;
- import java.io.IOException;
- import java.io.InputStream;
- import java.security.*;
- import java.security.cert.CertificateException;
- public abstract class AbstractWechatPaymentHandler extends AbstractPaymentHandler {
- protected String appId;
- /**
- * 商户号id
- */
- protected String mchId;
- /**
- * API密钥
- */
- protected String apiKey;
- protected String notifyPayUrl;
- protected String notifyRefundUrl;
- public AbstractWechatPaymentHandler(String appId, String mchId, String apiKey, String notifyPayUrl, String notifyRefundUrl) {
- this.appId = appId;
- this.mchId = mchId;
- this.apiKey = apiKey;
- this.notifyPayUrl = notifyPayUrl;
- this.notifyRefundUrl = notifyRefundUrl;
- }
- @Override
- public String refundApply(RefundContext refundContext) {
- return null;
- }
- @Override
- public boolean isPayment(String orderNo) {
- return false;
- }
- @Override
- public String findPaymentOrder(String orderNo) {
- return null;
- }
- @Override
- public String transfer(TransferParameter transferParameter){
- throw new UnsupportedOperationException("不支持的操作");
- }
- protected String sendSSLRequest(String url,String mapToXml) {
- CloseableHttpClient httpclient = null;
- try {
- httpclient = getCloseableHttpClient();
- HttpPost httpost = new HttpPost(url); // 设置响应头信息
- httpost.setEntity(new StringEntity(mapToXml, "UTF-8"));
- CloseableHttpResponse response = httpclient.execute(httpost);
- try {
- HttpEntity entity = response.getEntity();
- String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
- EntityUtils.consume(entity);
- XMLSerializer xmlSerializer = new XMLSerializer();
- String result = xmlSerializer.read(jsonStr).toString();
- log.info("微信小程序返回信息:" + result);
- return result;
- } finally {
- response.close();
- }
- } catch (Exception ex) {
- log.error("发送支付请求错误", ex);
- } finally {
- if (httpclient != null) {
- try {
- httpclient.close();
- } catch (IOException e) {
- log.error("关闭httpclient错误", e);
- }
- }
- }
- log.info("支付参数"+mapToXml);
- throw new PaymentException("支付渠道不可用,请稍后再试");
- }
- protected CloseableHttpClient getCloseableHttpClient() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException {
- KeyStore keyStore = KeyStore.getInstance("PKCS12");
- InputStream instream = this.getClass().getResourceAsStream("/cert/apiclient_cert.p12");
- try {
- keyStore.load(instream, this.mchId.toCharArray());
- } finally {
- instream.close();
- }
- SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, this.mchId.toCharArray()).build();
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
- SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
- return HttpClients.custom().setSSLSocketFactory(sslsf).build();
- }
- @Override
- public String doPay(IPayment payment) throws PaymentException {
- return null;
- }
- @Override
- public String refund(RefundContext refundContext) throws Exception {
- return null;
- }
- public String getAppId() {
- return appId;
- }
- public void setAppId(String appId) {
- this.appId = appId;
- }
- public String getMchId() {
- return mchId;
- }
- public void setMchId(String mchId) {
- this.mchId = mchId;
- }
- }
|