多平台统一管理软件接口,如何实现多平台统一管理软件接口
267
2022-12-27
java实现微信支付功能
微信支付的具体实现方法,供大家参考,具体内容如下
工具类(用于在微信支付服务后台生成预支付交易单)
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(cgzIfTyeI", 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 "
"
(attach != null? "
"
"
"
"
(trade_type.equals("APP") == true?"":"
"
"
"
"
"
"";
}
public String toWapXml(){
return "
"
(attach != null? "
"
"
"
"
(trade_type.equals("APP") == true?"":"
"
"
"
"
"
"";
}
/*支付查询签名字符串*/
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("
sBuilder.append("
sBuilder.append("
sBuilder.append("
sBuilder.append("
sBuilder.append("
sBuilder.append("
sBuilder.append("");
return sBuilder.toString();
}
public Map
Map
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, Strinhttp://g 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
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;//直接将参数返回给前端,发起支付
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~