Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refund #6

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/com/github/cuter44/wxpay/WxpayFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,60 @@ public GetBrandWCPayRequest newGetBrandWCPayRequest(Properties p)
));
}

public OrderQueryRequest newOrderQueryRequest()
{
return (
new OrderQueryRequest(
new Properties(this.conf)
));
}

public RefundRequest newRefundRequest()
{
return (
new RefundRequest(
new Properties(this.conf)
));
}

/**
* init with apiclient_cert.p12's absolute path
* @param cert_path absolute path of apiclient_cert.p12
* @return
*/
public RefundRequest newRefundRequest(String cert_path)
{
return (
new RefundRequest(
new Properties(this.conf),
cert_path
));
}

public RefundQueryRequest newRefundQueryRequest()
{
return (
new RefundQueryRequest(
new Properties(this.conf)
));
}

public TemplateMsgAPI newTemplateMsgAPI()
{
return (
new TemplateMsgAPI(
this.getTokenKeeper().getAccessToken()
));
}

public PayTransfersRequest newPayTransfersRequest()
{
return (
new PayTransfersRequest(
new Properties(this.conf)
));
}

// MISC
protected static Properties buildConf(Properties prop, Properties defaults)
{
Expand Down
114 changes: 114 additions & 0 deletions src/com/github/cuter44/wxpay/reqs/ClientCustomSSL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.github.cuter44.wxpay.reqs;

import com.github.cuter44.wxpay.WxpayException;
import com.github.cuter44.wxpay.WxpayProtocolException;
import com.github.cuter44.wxpay.resps.ResponseBase;
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.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Properties;

import static com.github.cuter44.wxpay.util.XMLParser.parseXML;

/**
* Created by Mklaus on 15/4/25.
*/
public class ClientCustomSSL {
private static final String CERT_PATH = "apiclient_cert.p12";

private static ClassLoader cl = ClientCustomSSL.class.getClassLoader();

public static final String KEY_RETURN_CODE = "return_code";
public static final String KEY_RETURN_MSG = "return_msg";
public static final String KEY_ERR_CODE = "err_code";

public static final String VALUE_FAIL = "FAIL";

public static ResponseBase getResponseBase(String mch_id,String baseURL,String data) throws Exception{
String path = cl.getResource(CERT_PATH).toURI().getPath();

return getResponseBase(path, mch_id, baseURL, data);
}

public static ResponseBase getResponseBase(String path,String mch_id,String baseURL,String data) throws Exception {

if (path == null) {
path = cl.getResource("apiclient_cert.p12").toURI().getPath();
}

KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream inputStream = new FileInputStream(new File(path));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is improper to use File here. Data encapsulated in some virtual source such as java archive(.jar)/URL may not and should not access via File interfaces. In this case, certificates are provided in classpath, which requires you to access it via the "classpath" way. Such like this:

            InputStream stream = this.getClass()
                .getResourceAsStream(resourcePath);

You may refer to javadoc or SO for further information.


Properties prop = new Properties();

try{
keyStore.load(inputStream,mch_id.toCharArray());
}finally {
inputStream.close();
}

// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, mch_id.toCharArray())
.build();

// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();

try {
HttpPost httpPost = new HttpPost(baseURL);
httpPost.setHeader("Accept-Charset","UTF-8");
httpPost.setEntity(new StringEntity(data, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();

String content = EntityUtils.toString(response.getEntity(), "UTF-8");

prop.putAll(parseXML(content));

EntityUtils.consume(entity);

if (VALUE_FAIL.equals(prop.getProperty(KEY_RETURN_CODE)))
throw(
new WxpayProtocolException(
prop.getProperty(KEY_RETURN_MSG)
));

if (VALUE_FAIL.equals(prop.getProperty(KEY_ERR_CODE)))
throw(
new WxpayException(
prop.getProperty(KEY_ERR_CODE)
));

return(new ResponseBase(entity.toString(), prop));

} finally {
response.close();
}
}catch (Exception ex) {
throw(new WxpayException(ex));
}finally {
httpclient.close();
}
}
}
87 changes: 87 additions & 0 deletions src/com/github/cuter44/wxpay/reqs/OrderQueryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.cuter44.wxpay.reqs;

import com.github.cuter44.wxpay.resps.OrderQueryResponse;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
* Created by Mklaus on 15/4/21.
*/
public class OrderQueryRequest extends RequestBase{

public static final String URL_API_BASE = "https://api.mch.weixin.qq.com/pay/orderquery";

protected static final String KEY_OUT_TRADE_NO = "out_trade_no";
protected static final String KEY_TRANSACTION_ID = "transaction_id";

public static final List<String> KEYS_PARAM_NAME = Arrays.asList(
"appid",
"mch_id",
"nonce_str",
"out_trade_no",
"transaction_id",
"sign"
);

//CONSTRUCT
public OrderQueryRequest(Properties prop){
super(prop);

return;
}

//BUILD
@Override
public OrderQueryRequest build()
{
return (this);
}

//SIGN
@Override
public OrderQueryRequest sign() throws UnsupportedEncodingException
{
this.sign(KEYS_PARAM_NAME);
return (this);
}

// TO_URL
public String toURL()
throws UnsupportedOperationException
{
throw(
new UnsupportedOperationException("This request does not execute on client side")
);
}

// EXECUTE
@Override
public OrderQueryResponse execute()
{
return(
new OrderQueryResponse(
this.execute(URL_API_BASE, KEYS_PARAM_NAME)
));
}

/** 商户系统内部的订单号,32个字符内、可包含字母
*/
public OrderQueryRequest setOutTradeNo(String outTradeNo)
{
this.setProperty(KEY_OUT_TRADE_NO, outTradeNo);

return(this);
}

/** 微信的订单号, 优先使用
*/
public OrderQueryRequest setTransactionId(String transactionId)
{
this.setProperty(KEY_TRANSACTION_ID,transactionId);

return (this);
}
}
Loading