자바 위 챗 결제 기능 구현
17120 단어 자바위 챗 페 이 먼 트
도구 류(위 챗 결제 서비스 배경 에서 선불 거래 서 를 생 성 하 는 데 사용)
package com.fengdi.lianmeng.zhifu.weixin;
import com.fengdi.lianmeng.util.MD5;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
*
* : , ,
* 、JSAPI、APP 。
* @author
* 2016 1 8 11:33:21
*/
public class UnifiedorderReq{
/**
* ID
*/
private String appid;
/**
*
*/
private String mch_id;
/**
*
*/
private String device_info;
/**
*
*/
private String nonce_str;
/**
*
*/
private String sign;
/**
*
*/
private String body;
/**
*
*/
private String detail;
/**
*
*/
private String attach;
/**
*
*/
private String out_trade_no;
/**
*
*/
private String fee_type;
/**
*
*/
private Integer total_fee;
/**
* IP
*/
private String spbill_create_ip;
/**
*
*/
private String time_start;
/**
*
*/
private String time_expire;
/**
*
*/
private String goods_tag;
/**
*
*/
private String notify_url;
/**
*
*/
private String trade_type;
/**
* ID
*/
private String product_id;
/**
*
*/
private String limit_pay;
/**
*
*/
private String openid;
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getDevice_info() {
return device_info;
}
public void setDevice_info(String device_info) {
this.device_info = device_info;
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body.length()>40?body.substring(0, 40):body;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getAttach() {
return attach;
}
public void setAttach(String attach) {
this.attach = attach;
}
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getFee_type() {
return fee_type;
}
public void setFee_type(String fee_type) {
this.fee_type = fee_type;
}
public Integer getTotal_fee() {
return total_fee;
}
public void setTotal_fee(Integer total_fee) {
this.total_fee = total_fee;
}
public String getSpbill_create_ip() {
return spbill_create_ip;
}
public void setSpbill_create_ip(String spbill_create_ip) {
this.spbill_create_ip = spbill_create_ip;
}
public String getTime_start() {
return time_start;
}
public void setTime_start(String time_start) {
this.time_start = time_start;
}
public String getTime_expire() {
return time_expire;
}
public void setTime_expire(String time_expire) {
this.time_expire = time_expire;
}
public String getGoods_tag() {
return goods_tag;
}
public void setGoods_tag(String goods_tag) {
this.goods_tag = goods_tag;
}
public String getNotify_url() {
return notify_url;
}
public void setNotify_url(String notify_url) {
this.notify_url = notify_url;
}
public String getTrade_type() {
return trade_type;
}
public void setTrade_type(String trade_type) {
this.trade_type = trade_type;
}
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String getLimit_pay() {
return limit_pay;
}
public void setLimit_pay(String limit_pay) {
this.limit_pay = limit_pay;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UnifiedorderReq [appid=");
builder.append(appid);
builder.append(", mch_id=");
builder.append(mch_id);
builder.append(", device_info=");
builder.append(device_info);
builder.append(", nonce_str=");
builder.append(nonce_str);
builder.append(", sign=");
builder.append(sign);
builder.append(", body=");
builder.append(body);
builder.append(", detail=");
builder.append(detail);
builder.append(", attach=");
builder.append(attach);
builder.append(", out_trade_no=");
builder.append(out_trade_no);
builder.append(", fee_type=");
builder.append(fee_type);
builder.append(", total_fee=");
builder.append(total_fee);
builder.append(", spbill_create_ip=");
builder.append(spbill_create_ip);
builder.append(", time_start=");
builder.append(time_start);
builder.append(", time_expire=");
builder.append(time_expire);
builder.append(", goods_tag=");
builder.append(goods_tag);
builder.append(", notify_url=");
builder.append(notify_url);
builder.append(", trade_type=");
builder.append(trade_type);
builder.append(", product_id=");
builder.append(product_id);
builder.append(", limit_pay=");
builder.append(limit_pay);
builder.append(", openid=");
builder.append(openid);
builder.append("]");
return builder.toString();
}
public String toSignString(){
Class<?> clz = this.getClass();
Field[] fields = clz.getDeclaredFields();
StringBuilder builder = new StringBuilder();
for (Field field : fields) {
builder.append(field.getName());
}
return builder.toString();
}
public String signString(){
StringBuilder builder = new StringBuilder();
builder.append("appid="+appid);
if(attach!=null){
builder.append("&attach="+attach);
}
builder.append("&body="+body);
builder.append("&mch_id="+mch_id);
builder.append("&nonce_str="+nonce_str);
builder.append("¬ify_url="+notify_url);
if(!trade_type.equals("APP")) {
builder.append("&openid="+openid);
}
builder.append("&out_trade_no="+out_trade_no);
builder.append("&spbill_create_ip="+spbill_create_ip);
builder.append("&total_fee="+total_fee);
builder.append("&trade_type="+trade_type);
builder.append("&key="+FgWxConfig.mobilekey);
return builder.toString();
}
public String wapSignString(){
StringBuilder builder = new StringBuilder();
builder.append("appid="+appid);
if(attach!=null){
builder.append("&attach="+attach);
}
builder.append("&body="+body);
builder.append("&mch_id="+mch_id);
builder.append("&nonce_str="+nonce_str);
builder.append("¬ify_url="+notify_url);
if(!trade_type.equals("APP")) {
builder.append("&openid="+openid);
}
builder.append("&out_trade_no="+out_trade_no);
builder.append("&spbill_create_ip="+spbill_create_ip);
builder.append("&total_fee="+total_fee);
builder.append("&trade_type="+trade_type);
builder.append("&key="+FgWxConfig.key);
return builder.toString();
}
public String toXml(){
return "<xml>" +
"<appid>"+appid+"</appid>"+
(attach != null? "<attach>"+attach+"</attach>" : "")+
"<body>"+body+"</body>"+
"<mch_id>"+mch_id+"</mch_id>"+
"<nonce_str>"+nonce_str+"</nonce_str>"+
"<notify_url>"+notify_url+"</notify_url>"+
(trade_type.equals("APP") == true?"":"<openid>"+openid+"</openid>")+
"<out_trade_no>"+out_trade_no+"</out_trade_no>"+
"<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+
"<total_fee>"+total_fee+"</total_fee>"+
"<trade_type>"+trade_type+"</trade_type>"+
"<sign>"+MD5.encryptByMD5(signString()).toUpperCase()+"</sign>"+
"</xml>";
}
public String toWapXml(){
return "<xml>" +
"<appid>"+appid+"</appid>"+
(attach != null? "<attach>"+attach+"</attach>" : "")+
"<body>"+body+"</body>"+
"<mch_id>"+mch_id+"</mch_id>"+
"<nonce_str>"+nonce_str+"</nonce_str>"+
"<notify_url>"+notify_url+"</notify_url>"+
(trade_type.equals("APP") == true?"":"<openid>"+openid+"</openid>")+
"<out_trade_no>"+out_trade_no+"</out_trade_no>"+
"<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"+
"<total_fee>"+total_fee+"</total_fee>"+
"<trade_type>"+trade_type+"</trade_type>"+
"<sign>"+MD5.encryptByMD5(wapSignString()).toUpperCase()+"</sign>"+
"</xml>";
}
/* */
public String signQueryString(String transaction_id){
StringBuilder builder = new StringBuilder();
builder.append("appid="+appid);
builder.append("&body="+body);
builder.append("&mch_id="+mch_id);
builder.append("&nonce_str="+nonce_str);
builder.append("&transaction_id="+transaction_id);
builder.append("&key="+FgWxConfig.mobilekey);
return builder.toString();
}
/* */
public String toQueryXml(String transaction_id){
StringBuilder sBuilder=new StringBuilder();
sBuilder.append("<xml>");
sBuilder.append("<appid>"+appid+"</appid>");
sBuilder.append("<body>"+body+"</body>");
sBuilder.append("<mch_id>"+mch_id+"</mch_id>");
sBuilder.append("<nonce_str>"+nonce_str+"</nonce_str>");
sBuilder.append("<transaction_id>"+transaction_id+"</transaction_id>");
sBuilder.append("<sign>"+MD5.encryptByMD5(signQueryString(transaction_id)).toUpperCase()+"</sign>");
sBuilder.append("</xml>");
return sBuilder.toString();
}
public Map<String,Object> toMap(){
Map<String, Object> params = new HashMap<String, Object>();
params.put("appid", appid);
if(attach!=null){
params.put("attach", attach);
}
params.put("body", body);
params.put("mch_id", mch_id);
params.put("nonce_str", nonce_str);
params.put("notify_url", notify_url);
if(!trade_type.equals("APP")) {
params.put("openid", openid);
}
params.put("out_trade_no", out_trade_no);
params.put("spbill_create_ip", spbill_create_ip);
params.put("total_fee", total_fee);
params.put("trade_type", trade_type);
params.put("sign", MD5.encryptByMD5(signString()).toUpperCase());
return params;
}
조립 도구 류 데이터
private AppWeixinPayResponse appUnifiedorderReq(Order order, Member member, HttpServletRequest request) {
//
UnifiedorderReq unifiedorderReq = new UnifiedorderReq();
unifiedorderReq.setBody(order.getOrderName().length() > 50 ? order.getOrderName().substring(0, 50) : order.getOrderName());
log.error(unifiedorderReq.getBody().length() + "--" + unifiedorderReq.getBody());
unifiedorderReq.setAttach(order.getOrderNo());
unifiedorderReq.setTotal_fee(order.getRealAmt().intValue());
unifiedorderReq.setTrade_type("APP");// JSAPI
unifiedorderReq.setOut_trade_no(order.getOrderNo() + RandomUtils.nextInt(100, 1000));
unifiedorderReq.setSpbill_create_ip(getIpAddr(request));
unifiedorderReq.setOpenid(member.getOpenid());
return payService.appUnifiedorderReq(unifiedorderReq, PayCategory.order.toString());
}
위 챗 통일 주문
public AppWeixinPayResponse appUnifiedorderReq(UnifiedorderReq unifiedorderReq, String payCategory)
throws FdServiceException {
unifiedorderReq.setAppid(FgWxConfig.appid_app);
unifiedorderReq.setMch_id(FgWxConfig.mchId);
unifiedorderReq.setNonce_str(Random2Utils.generateString(20));
if (payCategory.equals(PayCategory.order.toString())) {
unifiedorderReq.setNotify_url(fgProperties.getDomainUrl() + FgWxConfig.weixinOrderNotifyCallBack);
} else if (payCategory.equals(PayCategory.recharge.toString())) {
unifiedorderReq.setNotify_url(fgProperties.getDomainUrl() + FgWxConfig.weixinRechargeNotifyCallBack);
} else {
throw new FdServiceException(ErrorCode.ILLEGAL_ARGUMENT, " [" + payCategory + "] ");
}
unifiedorderReq.setSign(MD5.encryptByMD5(unifiedorderReq.wapSignString()).toUpperCase());
logger.info("unifiedorder unifiedorderReq:" + unifiedorderReq);
String unifiedorder = FgHttp.sendPost(FgWxConfig.unifiedorderUrl, unifiedorderReq.toWapXml());
logger.info("unifiedorder unifiedorder:" + unifiedorder);
AppWeixinPayResponse appWeixinPayResponse = new AppWeixinPayResponse();
try {
Map<String, Object> map = XmlHelper.parseResXML(unifiedorder);
if ("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("result_code"))) {
logger.info(" :" + map);
appWeixinPayResponse.setAppid(FgWxConfig.appid_app);
appWeixinPayResponse.setNoncestr((String) map.get("nonce_str"));
appWeixinPayResponse.setPartnerid(FgWxConfig.mchId);
appWeixinPayResponse.setPrepayid((String) map.get("prepay_id"));
appWeixinPayResponse.setTimestamp((System.currentTimeMillis() + "").substring(0, 10));
appWeixinPayResponse.setSign(MD5.encryptByMD5(appWeixinPayResponse.wapSignString()).toUpperCase());
} else {
logger.info(" :" + map);
throw new FdServiceException(ErrorCode.ILLEGAL_ARGUMENT, " :" + map.get("return_msg"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return appWeixinPayResponse;// ,
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.