-
Notifications
You must be signed in to change notification settings - Fork 1.1k
微信支付MCH
liyiorg edited this page Jul 30, 2015
·
10 revisions
###生成web js 微信支付请求json 数据
package weixin.popular.example;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weixin.popular.api.PayMchAPI;
import weixin.popular.bean.paymch.Unifiedorder;
import weixin.popular.bean.paymch.UnifiedorderResult;
import weixin.popular.util.PayUtil;
/**
* 生成WEB JS 支付请求json
* @author LiYi
*
*/
public class PayMchJsServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private String appid; //appid
private String mch_id; //微信支付商户号
private String key; //API密钥
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//payPackage 的商品信息,总价可以通过前端传入
Unifiedorder unifiedorder = new Unifiedorder();
unifiedorder.setAppid(appid);
unifiedorder.setMch_id(mch_id);
unifiedorder.setNonce_str(UUID.randomUUID().toString().toString().replace("-", ""));
unifiedorder.setOpenid(wx_openid);
unifiedorder.setBody("商品信息");
unifiedorder.setOut_trade_no("123456");
unifiedorder.setTotal_fee("1");//单位分
unifiedorder.setSpbill_create_ip(request.getRemoteAddr());//IP
unifiedorder.setNotify_url("http://mydomain.com/test/notify");
unifiedorder.setTrade_type("JSAPI");//JSAPI,NATIVE,APP,WAP
//统一下单,生成预支付订单
UnifiedorderResult unifiedorderResult = PayMchAPI.payUnifiedorder(unifiedorder,key);
String json = PayUtil.generateMchPayJsRequestJson(unifiedorderResult.getPrepay_id(), appid, key);
//将json 传到jsp 页面
request.setAttribute("json", json);
//示例jsp
request.getRequestDispatcher("pay_example.jsp").forward(request,response);
}
}
###jsp 页面发起js 支付请求
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>pay js example</title>
<script type="text/javascript">
//json 数据
var x_json = ${json};
function pay(){
WeixinJSBridge.invoke('getBrandWCPayRequest',x_json,function(res){
if(res.err_msg == 'get_brand_wcpay_request:ok'){
//支付成功,可以做跳转到支付成功的提示页面
}else{
//支付失败
alert(res.err_msg);
}
});
}
</script>
</head>
<body>
</body>
</html>
###notify
package weixin.popular.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weixin.popular.bean.paymch.MchNotifyXml;
import weixin.popular.bean.paymch.MchPayNotify;
import weixin.popular.util.ExpireSet;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.XMLConverUtil;
/**
* 支付回调通知
* @author LiYi
*
*/
public class PayMchNotifyServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private String key; //mch key
//重复通知过滤 时效60秒
private static ExpireSet<String> expireSet = new ExpireSet<String>(60);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求数据
MchPayNotify payNotify = XMLConverUtil.convertToObject(MchPayNotify.class,request.getInputStream());
//已处理 去重
if(expireSet.contains(payNotify.getTransaction_id())){
return;
}
//签名验证
if(SignatureUtil.validateAppSignature(payNotify,key)){
expireSet.add(payNotify.getTransaction_id());
MchNotifyXml baseResult = new MchNotifyXml();
baseResult.setReturn_code("SUCCESS");
baseResult.setReturn_msg("OK");
response.getOutputStream().write(XMLConverUtil.convertToXML(baseResult).getBytes());
}else{
MchNotifyXml baseResult = new MchNotifyXml();
baseResult.setReturn_code("FAIL");
baseResult.setReturn_msg("ERROR");
response.getOutputStream().write(XMLConverUtil.convertToXML(baseResult).getBytes());
}
}
}
获取token
- TokenAPI access_token 获取
- MediaAPI 多媒体上传下载(临时素材)
- MaterialAPI 永久素材
- MenuAPI 菜单、个性化菜单
- MessageAPI 信息发送(客服消息、群发消息、模板消息)
- PayAPI 支付订单相关接口
- PayMchAPI 支付订单、红包、企业付款、代扣费(商户平台版)
- QrcodeAPI 二维码
- SnsAPI 网签授权
- UserAPI 用户管理、分组、标签、黑名单
- ShorturlAPI 长链接转短链接
- TicketAPI JSAPI ticket
- ComponentAPI 第三方平台开发
- CallbackipAPI 获取微信服务器IP地址
- ClearQuotaAPI 接口调用频次清零
- PoiAPI 微信门店 @Moyq5 (贡献)
- CardAPI 微信卡券 @Moyq5 (贡献)
- ShakearoundAPI 微信摇一摇周边 @Moyq5 (贡献)
- DatacubeAPI 数据统计 @Moyq5 (贡献)
- CustomserviceAPI 客服功能 @ConciseA (贡献)