• 微信支付系统


    目录

    1.微信支付的流程

    2.微信的接口文档

    3.新建一个spring-boot的项目

    4.导入需要的依赖

    5.配置application文件

    6.创建一个Httpclient的工具类-默认浏览器进行远程调用

    7.自动生成器--帮我们自动生成类,接口等

    8. 前端代码

    8.1.新建vue项目

    8.1.1.打开cmd命令窗口,输入命令打开窗口

    8.1.2. 新建

    8.2.安装element插件和axios的依赖(省略)

    8.3.引入axios和设置axios基础路径

    8.4.前端页面

    9.根据订单号创建二维码--后端

    9.1.controller层

    9.2.Iservice

    9.3.service层

    9.4.实体类

    9.4.1.CommonResult

    9.4.2.Order

    10.根据订单状态查询微信支付情况

    10.1.前端

    10.2.后端

    10.2.1.controller

    10.2.2.Iservice

    10.2.3.service层


    1.微信支付的流程

    2.微信的接口文档

    https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1

    3.新建一个spring-boot的项目

    4.导入需要的依赖

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <parent>
    5. <groupId>org.springframework.bootgroupId>
    6. <artifactId>spring-boot-starter-parentartifactId>
    7. <version>2.3.12.RELEASEversion>
    8. <relativePath/>
    9. parent>
    10. <groupId>springbootgroupId>
    11. <artifactId>demoartifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <name>WeChatPayname>
    14. <description>Demo project for Spring Bootdescription>
    15. <properties>
    16. <java.version>1.8java.version>
    17. properties>
    18. <dependencies>
    19. <dependency>
    20. <groupId>com.github.wxpaygroupId>
    21. <artifactId>wxpay-sdkartifactId>
    22. <version>0.0.3version>
    23. dependency>
    24. <dependency>
    25. <groupId>repMaven.org.apache.httpcomponentsgroupId>
    26. <artifactId>httpclientartifactId>
    27. <version>4.5.3version>
    28. dependency>
    29. <dependency>
    30. <groupId>com.baomidougroupId>
    31. <artifactId>mybatis-plus-generatorartifactId>
    32. <version>3.5.2version>
    33. dependency>
    34. <dependency>
    35. <groupId>org.freemarkergroupId>
    36. <artifactId>freemarkerartifactId>
    37. <version>2.3.31version>
    38. dependency>
    39. <dependency>
    40. <groupId>com.baomidougroupId>
    41. <artifactId>mybatis-plus-boot-starterartifactId>
    42. <version>3.5.2version>
    43. dependency>
    44. <dependency>
    45. <groupId>com.alibabagroupId>
    46. <artifactId>druid-spring-boot-starterartifactId>
    47. <version>1.2.8version>
    48. dependency>
    49. <dependency>
    50. <groupId>com.github.xiaoymingroupId>
    51. <artifactId>swagger-bootstrap-uiartifactId>
    52. <version>1.9.6version>
    53. dependency>
    54. <dependency>
    55. <groupId>com.spring4allgroupId>
    56. <artifactId>swagger-spring-boot-starterartifactId>
    57. <version>1.9.1.RELEASEversion>
    58. dependency>
    59. <dependency>
    60. <groupId>org.springframework.bootgroupId>
    61. <artifactId>spring-boot-starter-webartifactId>
    62. dependency>
    63. <dependency>
    64. <groupId>org.mybatis.spring.bootgroupId>
    65. <artifactId>mybatis-spring-boot-starterartifactId>
    66. <version>2.2.2version>
    67. dependency>
    68. <dependency>
    69. <groupId>mysqlgroupId>
    70. <artifactId>mysql-connector-javaartifactId>
    71. <scope>runtimescope>
    72. dependency>
    73. <dependency>
    74. <groupId>org.projectlombokgroupId>
    75. <artifactId>lombokartifactId>
    76. <optional>trueoptional>
    77. dependency>
    78. <dependency>
    79. <groupId>org.springframework.bootgroupId>
    80. <artifactId>spring-boot-starter-testartifactId>
    81. <scope>testscope>
    82. dependency>
    83. dependencies>
    84. <build>
    85. <plugins>
    86. <plugin>
    87. <groupId>org.springframework.bootgroupId>
    88. <artifactId>spring-boot-maven-pluginartifactId>
    89. <configuration>
    90. <excludes>
    91. <exclude>
    92. <groupId>org.projectlombokgroupId>
    93. <artifactId>lombokartifactId>
    94. exclude>
    95. excludes>
    96. configuration>
    97. plugin>
    98. plugins>
    99. build>
    100. project>

    5.配置application文件

    1. server.port=8888
    2. spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
    3. spring.datasource.username=root
    4. spring.datasource.druid.password=grt081141
    5. spring.datasource.druid.url=jdbc:mysql://localhost:3306/wechatpay?serverTimezone=Asia/Shanghai
    6. #微信app的id 商家的id 秘钥--我们自己没有办法申请,因为申请需要营业执照
    7. weixin.appid=wx8087d8149331d27c
    8. weixin.mch_id=1532192611
    9. weixin.api_key=Cc158380629071583806290715838062

    6.创建一个Httpclient的工具类-默认浏览器进行远程调用

    1. package springboot.util;
    2. import org.apache.http.Consts;
    3. import org.apache.http.HttpEntity;
    4. import org.apache.http.NameValuePair;
    5. import org.apache.http.client.ClientProtocolException;
    6. import org.apache.http.client.entity.UrlEncodedFormEntity;
    7. import org.apache.http.client.methods.*;
    8. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    9. import org.apache.http.conn.ssl.SSLContextBuilder;
    10. import org.apache.http.conn.ssl.TrustStrategy;
    11. import org.apache.http.entity.StringEntity;
    12. import org.apache.http.impl.client.CloseableHttpClient;
    13. import org.apache.http.impl.client.HttpClients;
    14. import org.apache.http.message.BasicNameValuePair;
    15. import org.apache.http.util.EntityUtils;
    16. import javax.net.ssl.SSLContext;
    17. import java.io.IOException;
    18. import java.security.cert.CertificateException;
    19. import java.security.cert.X509Certificate;
    20. import java.text.ParseException;
    21. import java.util.HashMap;
    22. import java.util.LinkedList;
    23. import java.util.List;
    24. import java.util.Map;
    25. /**
    26. * http请求客户端
    27. *
    28. * @author 必须引入httpclient的依赖:在java端模拟浏览器的效果。
    29. *
    30. */
    31. public class HttpClient {
    32. private String url;
    33. private Map param;
    34. private int statusCode;
    35. private String content;
    36. private String xmlParam;
    37. private boolean isHttps;
    38. public boolean isHttps() {
    39. return isHttps;
    40. }
    41. public void setHttps(boolean isHttps) {
    42. this.isHttps = isHttps;
    43. }
    44. public String getXmlParam() {
    45. return xmlParam;
    46. }
    47. public void setXmlParam(String xmlParam) {
    48. this.xmlParam = xmlParam;
    49. }
    50. public HttpClient(String url, Map param) {
    51. this.url = url;
    52. this.param = param;
    53. }
    54. public HttpClient(String url) {
    55. this.url = url;
    56. }
    57. public void setParameter(Map map) {
    58. param = map;
    59. }
    60. public void addParameter(String key, String value) {
    61. if (param == null)
    62. param = new HashMap();
    63. param.put(key, value);
    64. }
    65. public void post() throws ClientProtocolException, IOException {
    66. HttpPost http = new HttpPost(url);
    67. setEntity(http);
    68. execute(http);
    69. }
    70. public void put() throws ClientProtocolException, IOException {
    71. HttpPut http = new HttpPut(url);
    72. setEntity(http);
    73. execute(http);
    74. }
    75. public void get() throws ClientProtocolException, IOException {
    76. if (param != null) {
    77. StringBuilder url = new StringBuilder(this.url);
    78. boolean isFirst = true;
    79. for (String key : param.keySet()) {
    80. if (isFirst)
    81. url.append("?");
    82. else
    83. url.append("&");
    84. url.append(key).append("=").append(param.get(key));
    85. }
    86. this.url = url.toString();
    87. }
    88. HttpGet http = new HttpGet(url);
    89. execute(http);
    90. }
    91. /**
    92. * set http post,put param
    93. */
    94. private void setEntity(HttpEntityEnclosingRequestBase http) {
    95. if (param != null) {
    96. List nvps = new LinkedList();
    97. for (String key : param.keySet())
    98. nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
    99. http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
    100. }
    101. if (xmlParam != null) {
    102. http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
    103. }
    104. }
    105. private void execute(HttpUriRequest http) throws ClientProtocolException,
    106. IOException {
    107. CloseableHttpClient httpClient = null;
    108. try {
    109. if (isHttps) {
    110. SSLContext sslContext = new SSLContextBuilder()
    111. .loadTrustMaterial(null, new TrustStrategy() {
    112. // 信任所有
    113. public boolean isTrusted(X509Certificate[] chain,
    114. String authType)
    115. throws CertificateException {
    116. return true;
    117. }
    118. }).build();
    119. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
    120. sslContext);
    121. httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
    122. .build();
    123. } else {
    124. httpClient = HttpClients.createDefault();
    125. }
    126. CloseableHttpResponse response = httpClient.execute(http);
    127. try {
    128. if (response != null) {
    129. if (response.getStatusLine() != null)
    130. statusCode = response.getStatusLine().getStatusCode();
    131. HttpEntity entity = response.getEntity();
    132. // 响应内容
    133. content = EntityUtils.toString(entity, Consts.UTF_8);
    134. }
    135. } finally {
    136. response.close();
    137. }
    138. } catch (Exception e) {
    139. e.printStackTrace();
    140. } finally {
    141. httpClient.close();
    142. }
    143. }
    144. public int getStatusCode() {
    145. return statusCode;
    146. }
    147. public String getContent() throws ParseException, IOException {
    148. return content;
    149. }
    150. }

    7.自动生成器--帮我们自动生成类,接口等

    1. public class Generator {
    2. public static void main(String[] args) {
    3. FastAutoGenerator.create("jdbc:mysql://localhost:3306/wechatpay?serverTimezone=Asia/Shanghai", "root", "grt081141" +
    4. "")
    5. .globalConfig(builder -> {
    6. builder.author("guan") // 设置作者
    7. .enableSwagger() // 开启 swagger 模式
    8. .fileOverride() // 覆盖已生成文件
    9. .outputDir(".\\src\\main\\java\\"); // 指定输出目录
    10. })
    11. .packageConfig(builder -> {
    12. builder.parent("springboot") // 设置父包名
    13. .moduleName("system") // 设置父包模块名
    14. .pathInfo(Collections.singletonMap(OutputFile.xml, "src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
    15. })
    16. .strategyConfig(builder -> {
    17. builder.addInclude("t_order")// 设置需要生成的表名
    18. .addTablePrefix("t_"); // 设置过滤表前缀
    19. })
    20. .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
    21. .execute();
    22. }

    8. 前端代码

    8.1.新建vue项目

    8.1.1.打开cmd命令窗口,输入命令打开窗口

    vue ui

    8.1.2. 新建

    8.2.安装element插件和axios的依赖(省略)

    8.3.引入axios和设置axios基础路径

    //引入axios
    import axios from "axios";
    Vue.config.productionTip = false
    //设置axios基础路径
    axios.defaults.baseURL="http://localhost:8888"
    
    Vue.prototype.axios=axios;

    8.4.前端页面

    1. <template>
    2. <div id="app">
    3. <el-button type="primary" @click="pay">支付el-button>
    4. <el-dialog
    5. title="收银台"
    6. :visible.sync="dislogVisible"
    7. width="30%">
    8. <div style="text-align: center">
    9. <p>微信支付{{payResult.price}}元p>
    10. <div style="border: 1px solid #f3f3f3; width: 220px;padding: 0px;margin: 0px auto">
    11. <vue-qr
    12. :text="payResult.codeUrl"
    13. :margin="0"
    14. colorDark="green"
    15. :logo="require('@/assets/logo.png')"
    16. colorLight="#fff"
    17. :size="200">
    18. vue-qr>
    19. div>
    20. div>
    21. <el-divider>el-divider>
    22. <div style="font-size: 13px;">
    23. 提示:<br>
    24. 二维码两小时内有效,请及时扫码支付<br>
    25. div>
    26. el-dialog>
    27. div>
    28. template>
    29. <script>
    30. //引入vue-qr
    31. import vueQr from 'vue-qr'
    32. export default {
    33. name: 'app',
    34. //注册vue-qr
    35. components:{
    36. vueQr
    37. },
    38. data(){
    39. return{
    40. orderNo:"c60801fbdd2d45f9adg",
    41. codeUrl:"",
    42. //定时器
    43. timer1:"",
    44. dislogVisible:false,
    45. //得到响应的结果
    46. payResult:{
    47. //价格
    48. price:0,
    49. //二维码路径 借助vue-qr 可以把二维码地址转为二维码图片
    50. codeUrl:"",
    51. orderNo:"",
    52. }
    53. }
    54. },
    55. methods:{
    56. pay(){
    57. this.dislogVisible=true;
    58. this.axios.post("/system/order/createNavite/"+this.orderNo).then(result=>{
    59. if(result.data.code===2000){
    60. console.log(result)
    61. this.payResult=result.data.data;
    62. //设置一个定时器任务,每隔3秒调用一次
    63. this.timer1=setInterval(()=>{
    64. this.queryPayStatus(this.payResult.orderNo)
    65. },3000);
    66. }
    67. })
    68. }
    69. }
    70. }
    71. script>
    72. <style>
    73. style>

    9.根据订单号创建二维码--后端

    9.1.controller层

    1. @CrossOrigin
    2. @RestController
    3. @RequestMapping("/system/order")
    4. public class OrderController {
    5. //自动注入
    6. @Autowired
    7. private IOrderService orderService;
    8. //根据订单号创建二维码
    9. @RequestMapping("createNavite/{orderNo}")
    10. public CommonResult createNative(@PathVariable String orderNo){
    11. return orderService.createNative(orderNo);
    12. }
    13. }

    9.2.Iservice

    CommonResult createNative(String orderNo);

    9.3.service层

    1. @Service
    2. public class OrderServiceImpl extends ServiceImpl implements IOrderService {
    3. @Autowired
    4. private OrderMapper orderMapper;
    5. @Value("${weixin.appid}")
    6. private String appId;
    7. @Value("${weixin.mch_id}")
    8. private String mchId;
    9. @Value("${weixin.api_key}")
    10. private String apikey;
    11. @Override
    12. public CommonResult createNative(String orderNo) {
    13. //1.根据订单号查询出订单信息
    14. QueryWrapper wrapper=new QueryWrapper();
    15. //前端传过来的订单号跟数据库中的订单号一致
    16. wrapper.eq("order_no",orderNo);
    17. //订单状态为0
    18. wrapper.eq("status",0);
    19. Order order=orderMapper.selectOne(wrapper);
    20. //判断订单信息不为空
    21. if (order != null) {
    22. try {
    23. //接口里面的参数要的是xml类型
    24. //设置请求的参数个数格式为xml格式
    25. //将请求参数封装成map
    26. Map params = new HashMap<>();
    27. //添加公众账号Id
    28. params.put("appid", appId);
    29. //添加商品号
    30. params.put("mch_id", mchId);
    31. //添加随机字符串--微信自带算法
    32. params.put("nonce_str", WXPayUtil.generateNonceStr());
    33. //添加商品描述
    34. params.put("body", order.getCourseTitle());
    35. //添加商品订单号
    36. params.put("out_trade_no", orderNo);
    37. //添加标价金额 --单位是分,要转换
    38. params.put("total_fee", new BigDecimal(0.01).multiply(new BigDecimal(100)).longValue() + "");
    39. //添加终端ip
    40. params.put("spbill_create_ip", "127.0.0.1");
    41. //添加通知地址
    42. params.put("notify_url", "http://localhost:8888/pay/back");
    43. //添加交易类型
    44. params.put("trade_type", "NATIVE");
    45. //创建HttpClient对象--作用远程调用
    46. HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
    47. //支持https协议
    48. client.setHttps(true);
    49. //将map转为xml格式--设置请求的参数
    50. client.setXmlParam(WXPayUtil.generateSignedXml(params,apikey));
    51. //发送请求
    52. client.post();
    53. //获取请求响应响应的结果
    54. String content = client.getContent();
    55. System.out.println(content);
    56. //将String类型转换为map返货给前端
    57. Map map = WXPayUtil.xmlToMap(content);
    58. if (map.get("result_code").equals("SUCCESS")){
    59. Map result=new HashMap<>();
    60. result.put("codeUrl",map.get("code_url"));
    61. result.put("price",order.getTotalFee());
    62. result.put("orderNo",orderNo);
    63. return new CommonResult(2000,"生成二维码",result);
    64. }
    65. }catch (Exception e){
    66. e.printStackTrace();
    67. }
    68. }
    69. return new CommonResult(5000,"订单失效",null);
    70. }
    71. }

    9.4.实体类

    9.4.1.CommonResult

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @ApiModel("返回同一的信息")
    5. public class CommonResult {
    6. @ApiModelProperty("状态码 2000成功,5000失败")
    7. private int code;
    8. @ApiModelProperty("信息")
    9. private String msg;
    10. @ApiModelProperty("数据")
    11. private Object data;
    12. }

    9.4.2.Order

    1. package springboot.system.entity;
    2. import com.baomidou.mybatisplus.annotation.TableName;
    3. import java.io.Serializable;
    4. import java.math.BigDecimal;
    5. import java.time.LocalDateTime;
    6. import java.util.Date;
    7. import io.swagger.annotations.ApiModel;
    8. import io.swagger.annotations.ApiModelProperty;
    9. /**
    10. *

    11. * 订单
    12. *

    13. *
    14. * @author guan
    15. * @since 2022-08-13
    16. */
    17. @TableName("t_order")
    18. @ApiModel(value = "Order对象", description = "订单")
    19. public class Order implements Serializable {
    20. private static final long serialVersionUID = 1L;
    21. private String id;
    22. @ApiModelProperty("订单号")
    23. private String orderNo;
    24. @ApiModelProperty("课程id")
    25. private String courseId;
    26. @ApiModelProperty("课程名称")
    27. private String courseTitle;
    28. @ApiModelProperty("课程封面")
    29. private String courseCover;
    30. @ApiModelProperty("讲师名称")
    31. private String teacherName;
    32. @ApiModelProperty("会员id")
    33. private String memberId;
    34. @ApiModelProperty("会员昵称")
    35. private String nickname;
    36. @ApiModelProperty("会员手机")
    37. private String mobile;
    38. @ApiModelProperty("订单金额(分)")
    39. private BigDecimal totalFee;
    40. @ApiModelProperty("支付类型(0:微信 1:支付宝)")
    41. private Integer payType;
    42. @ApiModelProperty("订单状态(0:未支付 1:已支付)")
    43. private Integer status;
    44. @ApiModelProperty("逻辑删除 1(true)已删除, 0(false)未删除")
    45. private Boolean isDeleted;
    46. @ApiModelProperty("创建时间")
    47. private LocalDateTime gmtCreate;
    48. @ApiModelProperty("更新时间")
    49. private LocalDateTime gmtModified;
    50. public String getId() {
    51. return id;
    52. }
    53. public void setId(String id) {
    54. this.id = id;
    55. }
    56. public String getOrderNo() {
    57. return orderNo;
    58. }
    59. public void setOrderNo(String orderNo) {
    60. this.orderNo = orderNo;
    61. }
    62. public String getCourseId() {
    63. return courseId;
    64. }
    65. public void setCourseId(String courseId) {
    66. this.courseId = courseId;
    67. }
    68. public String getCourseTitle() {
    69. return courseTitle;
    70. }
    71. public void setCourseTitle(String courseTitle) {
    72. this.courseTitle = courseTitle;
    73. }
    74. public String getCourseCover() {
    75. return courseCover;
    76. }
    77. public void setCourseCover(String courseCover) {
    78. this.courseCover = courseCover;
    79. }
    80. public String getTeacherName() {
    81. return teacherName;
    82. }
    83. public void setTeacherName(String teacherName) {
    84. this.teacherName = teacherName;
    85. }
    86. public String getMemberId() {
    87. return memberId;
    88. }
    89. public void setMemberId(String memberId) {
    90. this.memberId = memberId;
    91. }
    92. public String getNickname() {
    93. return nickname;
    94. }
    95. public void setNickname(String nickname) {
    96. this.nickname = nickname;
    97. }
    98. public String getMobile() {
    99. return mobile;
    100. }
    101. public void setMobile(String mobile) {
    102. this.mobile = mobile;
    103. }
    104. public BigDecimal getTotalFee() {
    105. return totalFee;
    106. }
    107. public void setTotalFee(BigDecimal totalFee) {
    108. this.totalFee = totalFee;
    109. }
    110. public Integer getPayType() {
    111. return payType;
    112. }
    113. public void setPayType(Integer payType) {
    114. this.payType = payType;
    115. }
    116. public Integer getStatus() {
    117. return status;
    118. }
    119. public void setStatus(Integer status) {
    120. this.status = status;
    121. }
    122. public Boolean getIsDeleted() {
    123. return isDeleted;
    124. }
    125. public void setIsDeleted(Boolean isDeleted) {
    126. this.isDeleted = isDeleted;
    127. }
    128. public LocalDateTime getGmtCreate() {
    129. return gmtCreate;
    130. }
    131. public void setGmtCreate(LocalDateTime gmtCreate) {
    132. this.gmtCreate = gmtCreate;
    133. }
    134. public LocalDateTime getGmtModified() {
    135. return gmtModified;
    136. }
    137. public void setGmtModified(LocalDateTime gmtModified) {
    138. this.gmtModified = gmtModified;
    139. }
    140. @Override
    141. public String toString() {
    142. return "Order{" +
    143. "id=" + id +
    144. ", orderNo=" + orderNo +
    145. ", courseId=" + courseId +
    146. ", courseTitle=" + courseTitle +
    147. ", courseCover=" + courseCover +
    148. ", teacherName=" + teacherName +
    149. ", memberId=" + memberId +
    150. ", nickname=" + nickname +
    151. ", mobile=" + mobile +
    152. ", totalFee=" + totalFee +
    153. ", payType=" + payType +
    154. ", status=" + status +
    155. ", isDeleted=" + isDeleted +
    156. ", gmtCreate=" + gmtCreate +
    157. ", gmtModified=" + gmtModified +
    158. "}";
    159. }
    160. }

    10.根据订单状态查询微信支付情况

    10.1.前端

    1. //根据订单号查询支付状态
    2. queryPayStatus(orderNo){
    3. this.axios.post("system/order/queryPayStatus/"+orderNo).then(result=>{
    4. if (result.data.code===2000){
    5. //清除定时器
    6. clearInterval(this.timer1)
    7. this.timer1=null;
    8. this.$message.success("支付成功")
    9. //关闭弹出层
    10. this.dislogVisible=false;
    11. }
    12. })
    13. },

    10.2.后端

    10.2.1.controller

    1. @RequestMapping("queryPayStatus/{orderNo}")
    2. public CommonResult queryPayStatus(@PathVariable String orderNo){
    3. return orderService.queryPayStatus(orderNo);
    4. }

    10.2.2.Iservice

     CommonResult queryPayStatus(String orderNo);

    10.2.3.service层

    1. @Override
    2. public CommonResult queryPayStatus(String orderNo) {
    3. //1.根据订单状态查询微信支付情况
    4. try {
    5. HttpClient client=new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
    6. //设置参数--分装成map在转为xml格式
    7. Map params = new HashMap<>();
    8. //添加公众账号Id
    9. params.put("appid", appId);
    10. //添加商品号
    11. params.put("mch_id", mchId);
    12. //添加商品订单号
    13. params.put("out_trade_no",orderNo);
    14. //添加随机字符串
    15. params.put("nonce_str",WXPayUtil.generateNonceStr());
    16. //支持Https
    17. client.setHttps(true);
    18. client.setXmlParam(WXPayUtil.generateSignedXml(params,apikey));
    19. client.post();
    20. String content = client.getContent();
    21. //转换为map
    22. Map map = WXPayUtil.xmlToMap(content);
    23. if (map.get("trade_state").equals("SUCCESS")){
    24. //1.修改订单的状态
    25. Order order=new Order();
    26. order.setStatus(1);
    27. order.setGmtModified(LocalDateTime.now() );
    28. QueryWrapper wrapper=new QueryWrapper<>();
    29. //根据订单号
    30. wrapper.eq("order_no",orderNo);
    31. wrapper.eq("status",0);
    32. orderMapper.update(order,wrapper);
    33. return new CommonResult(2000,"支付成功",null);
    34. }
    35. }catch (Exception e){
    36. e.printStackTrace();
    37. }
    38. return new CommonResult(5000,"支付失败",null);
    39. }
  • 相关阅读:
    《HTTP/2 in Action》阅读笔记(一)
    苹果U盘格式化了怎么恢复
    Jmeter的学习
    避坑必看:C++移植C语言结构体char短整形导致生产事故分析
    C++ 空类中有哪些成员函数
    三家前装出货超2万台,激光雷达明年「放量」是大概率事件
    AI:74-基于深度学习的宠物品种识别
    数字营销行业如何用CRM做好客户资源沉淀与业务能力传承
    【k8s资源调度-Deployment】
    深度学习第二章
  • 原文地址:https://blog.csdn.net/ne_123456/article/details/126316335