集成微信支付的代码。兼容小程序,扫码,app,公众号。h5 支付 ,以及 服务商提现

 基于微信支付可能对于初入者来说是一个有点烦的东西  可能对于不同的场景应用有点繁琐  下面的分享一个简易的集成支付代码  各位可看 勿喷

/**
 * 因为微信总是很多个商户号很多和appid。很多个密钥文件,所以全部改成手动传值的方式,就可以支持多商户调用
 *
 * @param appId    商户的appid
 * @param mchId    商户的商户号
 * @param key      商户的支付用的密钥
 * @param certPath 商户的退款用的退款文件地址,非必传,没有就传null,具体看开发文档是否需退款文件
 * @throws Exception
 */
 
调用示例测试代码。扫码支付返回的是一个base64格式的二维码。需要拼接前缀的(有逗号) :  data:image/jpeg;base64,
 
public static void main(String[] args) throws Exception {
        WXPayEntrance wxPayEntrance = new WXPayEntrance("wxab8acb865bb1637e", "11473623", "2ab9071b06b9f739b950ddb41db2690d", null);
        WXPayVO wxPayVO = new WXPayVO();
        wxPayVO.setBody("1231231666623");
        wxPayVO.setTotalFee("1");
        wxPayVO.setNotifyUrl("https://********/api/wxpayreturn");
        wxPayVO.setTradeType("NATIVE");
        wxPayVO.setOutTradeNo("ty190523151415047046");
        wxPayVO.setUid(123123123);
        wxPayVO.setSpbillCreateIp("192.168.17.214");
        Map<String, String> map = wxPayEntrance.doUnifiedOrder(wxPayVO);
        System.out.println(map);
 
//        PayRefundVo payRefundVo = new PayRefundVo();
//        payRefundVo.setOutRefundNo("A190523155119103");
//        payRefundVo.setOutTradeNo("ty190523155047046");
//        payRefundVo.setRefundDesc("退款");
//        payRefundVo.setRefundFee("10");
//        payRefundVo.setUid(123123123);
//        payRefundVo.setRefundFeeType("CNY");
//        payRefundVo.setTotalFee("120");
//        Map<String, String> map = wxPayEntrance.doRefund(payRefundVo);
//        System.out.println(map);
    }
异步回调代码如下:
 
package com.huis.portal.controller;
 
import com.huis.portal.weixinpay.WXPayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
 
@RestController
@Api(description = "支付模块模块")
@CrossOrigin(origins = "*", maxAge = 3600)
@Transactional(rollbackFor = Exception.class)
public class PayRestController {
 
 
   @ApiOperation(value = "微信回调接口", notes = "微信回调接口")
   @RequestMapping(value = "/api/wxpayreturn", method = RequestMethod.POST)
   public String WeixinParentNotifyPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
      // 返回给微信的处理结果
      String result = null;
      String inputLine;
      String notityXml = "";
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      response.setContentType("text/html;charset=UTF-8");
      response.setHeader("Access-Control-Allow-Origin", "*");
      // 微信给返回的东西
      try {
         while ((inputLine = request.getReader().readLine()) != null) {
            notityXml += inputLine;
         }
         request.getReader().close();
      } catch (Exception e) {
         e.printStackTrace();
         result = setXml("fail", "xml获取失败");
         return result;
      }
      if (StringUtils.isEmpty(notityXml)) {
         result = setXml("fail", "xml为空");
         return result;
      }
      Map<String, String> map = WXPayUtil.xmlToMap(notityXml);
      String out_trade_no = (String) map.get("out_trade_no");// 获取商户订单号
      String result_code = (String) map.get("result_code");// 业务结果
      String total_fee = (String) map.get("total_fee");// 获取订单金额
      String appid = (String) map.get("appid");// 获取订单金额
      if (map.get("result_code").equals("SUCCESS")) {
         //TODO
 
 
         result = setXml("SUCCESS", "OK");
         return result;
      }else {
         result = setXml("fail", "xml为空");
         return result;
      }
 
   }
   // 通过xml 发给微信消息
   public static String setXml(String return_code, String return_msg) {
      SortedMap<String, String> parameters = new TreeMap<String, String>();
      parameters.put("return_code", return_code);
      parameters.put("return_msg", return_msg);
      return "<xml><return_code><![CDATA[" + return_code + "]]>" + "</return_code><return_msg><![CDATA[" + return_msg
            + "]]></return_msg></xml>";
   }
 
}

微信支付代码下载地址:链接:https://pan.baidu.com/s/1kZs5hxQuzxmRR50N-DOKSg  密码:u4rj

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值