微信服务号开发之jssdk-微信充值、支付见解


关于微信服务号开发的微信支付一般有两种情况。一种为微信充值,然后服务号中余额增加;一种为购买商品,微信支付。在微信支付中(JSSDKUtil.setPayParam(request, notifyUrl, money, orderId, openId);)需要传2个必要参数到方法中,微信支付单号和money  ,关于微信支付单号orderId  ,必须全世界唯一 ,每次和全世界都不一样,可以用(String orderId = UUID.randomUUID().toString().replaceAll("-", "");)UUID工具类生成,以保证每次都在全世界唯一,然后就是在订单表中必须存在一字段保存ordered,因为在支付回调函数成功后的方法里,需要用回调回来的orderid来获取订单对象,从而改变订单状态,进行后续操作。微信充值也是一个道理,需要在会员表中存在一个存放ordered的字段,好利用它进行回调函数成功方法中获取原本会员对象,然后对对象余额加钱。思路就这样,附上重点代码:
//充值
@Controller
@RequestMapping("/chongzhi.do")
public class ChongzhiAction {
 
 public static void main(String[] args) {
  
  
 }
 
    @Autowired
 private TixianService tixianService;
    @Autowired
 private KtdpService  ktdpService;

 @Autowired
 private HttpServletRequest request;
 
 /**
  * 支付回调
  * @param request
  * @return
  * @throws IOException
  */
 @RequestMapping
 public String execute(HttpServletRequest request,HttpServletResponse response) throws IOException
 {
  //请修改数据库状态
  InputStream is = request.getInputStream();
  byte[] b = new byte[1024];
  int size = is.read(b);
  String str = new String(b, 0, size);
  System.out.println(str);
  // 获取请求数据
  PayNotify payNotify = XMLConverUtil.convertToObject(PayNotify.class,
    str);

  // 签名判断略...
  String sign = payNotify.getSign();

  boolean f = SignatureUtil.validateAppSignature(payNotify,
    WeixinConfig.PAY_KEY);
  
  System.out.println("充值进来了1------"+payNotify.getResult_code());
  System.out.println("充值进来了2------"+payNotify.getReturn_code());
 
  // 支付成功
  if ("SUCCESS".equals(payNotify.getResult_code())
    && "SUCCESS".equals(payNotify.getReturn_code())) {
   System.out.println("充值进来了------");
   String orderid = payNotify.getOut_trade_no();
   String money= payNotify.getTotal_fee();
   //String qian = String.format("%.2f", money);
   String opid = payNotify.getOpenid();
   System.out.println(opid+"opid");
            //修改状态
   
    //支付->余额加钱
   Member member = ktdpService.findByOrderId(orderid);
   member.setMenMoney(member.getMenMoney()+Float.parseFloat(money)/100);
   ktdpService.update(member);
    //增加一条记录
   String wpayId=tixianService.getwpayId();
      tixianService.addwpay(opid, money, wpayId);

   response.getOutputStream().write("success".getBytes());

  } else {
   response.getOutputStream().write("error".getBytes());
  }

  return null;
 }
 
 @RequestMapping(params="method=chongzhi")
 public String toPay(HttpServletRequest request)
 {
  // 1.得openId
  String openId = (String) request.getSession().getAttribute("openId");
  
  String orderId = UUID.randomUUID().toString().replaceAll("-", "");
  Member member = ktdpService.findByOpenId(openId);
  member.setOrderId(orderId);
  ktdpService.update(member);
  
  //  参数 
  String chongzhimoney=request.getParameter("chongzhimoney");
  //System.out.println(chongzhimoney);
  // 2.去支付
  String notifyUrl = URLManager.getServerURL(request)+"/chongzhi.do";
  String json = JSSDKUtil.setPayParam(request, notifyUrl, chongzhimoney, orderId,
    openId);
        request.setAttribute("json", json);
  // 4.调用微信支付
  return "/pay_example.jsp";
  
 }
 
}
//订单支付
@Controller
@RequestMapping("/cardd.do")
public class CartoorderAction{
 
 public static void main(String[] args) {
  
  
 }
 
    @Autowired
 private KtdpService  ktdpService;
   

 @Autowired
 private TixianService tixianService;
 @Autowired
 private DingDanService dingDanService;

 @Autowired
 private HttpServletRequest request;
 
 /**
  * 支付回调
  * @param request
  * @return
  * @throws IOException
  */
 @RequestMapping
 public String execute(HttpServletRequest request,HttpServletResponse response) throws IOException
 {
  //请修改数据库状态
  InputStream is = request.getInputStream();
  byte[] b = new byte[1024];
  int size = is.read(b);
  String str = new String(b, 0, size);
  System.out.println(str);
  // 获取请求数据
  PayNotify payNotify = XMLConverUtil.convertToObject(PayNotify.class,
    str);

  // 签名判断略...
  String sign = payNotify.getSign();

  boolean f = SignatureUtil.validateAppSignature(payNotify,
    WeixinConfig.PAY_KEY);
  
 
  // 支付成功
  if ("SUCCESS".equals(payNotify.getResult_code())
    && "SUCCESS".equals(payNotify.getReturn_code())) {
   String wpayid = payNotify.getOut_trade_no();
   String money = payNotify.getTotal_fee();
   String opid = payNotify.getOpenid();
           
   Member member = ktdpService.findByOrderId(wpayid);
   ktdpService.update(member);
   tixianService.addwpay(opid, money, wpayid);
   //修改状态
   dingDanService.changemember(member,Float.parseFloat(money));
   dingDanService.gethavepuboid(wpayid);
   
   
   response.getOutputStream().write("success".getBytes());

  } else {
   response.getOutputStream().write("error".getBytes());
  }

  return null;
 }
 
 @RequestMapping(params="method=tijiaodd")
 public String toPay(HttpServletRequest request) throws UnsupportedEncodingException
 {
  // 1.得openId
  String openId = (String) request.getSession().getAttribute("openId");
  
  String orderId = UUID.randomUUID().toString().replaceAll("-", "");
  Member member = ktdpService.findByOpenId(openId);
  
  
   //  参数 
  String dizhi = request.getParameter("dizhi");
     dizhi = new String(dizhi.getBytes("ISO-8859-1"), "UTF-8");
     String ids = request.getParameter("ids");
     //System.out.println(ids);
     String money=request.getParameter("money");
  
      // 写订单
     String wpayId=tixianService.getwpayId();
     member.setOrderId(wpayId);
  ktdpService.update(member);
  String[] cid = ids.split("x");
     
     tixianService.addDindan(member, cid, dizhi,wpayId);
       
     
  // 2.去支付
  String notifyUrl = URLManager.getServerURL(request)+"/cardd.do";
  String json=JSSDKUtil.setPayParam(request, notifyUrl, money, wpayId, openId);
        request.setAttribute("json", json);
  // 4.调用微信支付
  return "/pay_example.jsp";
  
 }
 
}


//pay.jsp
<%@ 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>微信支付</title>
<script type="text/javascript">
//json 数据
var x_json = ${json};

  //调用微信JS api 支付
        function jsApiCall()
        {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                 x_json,
                function(res){
                    //alert(res.err_code+res.err_desc+res.err_msg);
                    if (res.err_msg=="get_brand_wcpay_request:ok") //支付成功
                    {
                   
                    }
                    else if (res.err_msg=="get_brand_wcpay_request:cancel") //支付过程中用户取消
                    {
                      
                    }
                    else if (res.err_msg=="get_brand_wcpay_request:fail") //支付失败
                    {
                     
                    }
                   
                   
                }
            );
        }

        function callpay()
        {
            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                jsApiCall();
            }
        }
       
        onload  = function()
        {
          callpay();
        }

</script>
</head>
<body>

</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值