Java实现微信支付;本文章使用Wxjava SDK开发包

前置条件:已申请微信小程序和微信公众号,并且拥有一个已备案的https域名。

开发步骤:

1.微信小程序开发
2.SpringBoot 接口开发

编辑配置文件application.yml配置文件

wx:
   app-id: xxxxxxxxxxxx
   app-secret: xxxxxxxxxxxx
   // 商户号
   mch-id: xxxxxxxxxxxx
   // 商户密钥
   mch-key: xxxxxxxxxxxx
   // 回调地址 保证外网能访问
   notify-url: xxxxxxxxxxxx
   // p12证书的位置,可以绝对路径,可以指定类路径 以classpath:开头
   key-path: xxxxxxxxxxxx

Bean代码:用来获取微信商户号和各种信息

@Configuration
@ConfigurationProperties(prefix = "wx")
public class WxProperties {
    private String appId;
    private String appSecret;
    private String mchId;
    private String mchKey;
    private String notifyUrl;
    private String keyPath;
}

@Configuration

public class WxPayConfiguration {
    @Autowired
    private WxProperties properties;

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}

支付相关的方法都在WxPayService中,附加api文档

// 调用支付接口

微信小程序获取订单信息;如我的接口地址为/order/payorder

微信订单结构:
//订单对象,从服务器获取

var orderInfo = {
  "appid": res.data.orderInfo.appId,  // 应用ID(AppID)
  "partnerid": res.data.orderInfo.partnerId, // 商户号(PartnerID)
  "prepayid": res.data.orderInfo.prepayId,  // 预支付交易会话ID
  "package": res.data.orderInfo.packageValue,        // 固定值
  "noncestr": res.data.orderInfo.nonceStr, // 随机字符串
  "timestamp": res.data.orderInfo.timeStamp, // 时间戳(单位:秒)
  "sign": res.data.orderInfo.sign, // 签名,这里用的 MD5 签名
};

调用微信支付函数

@PreAuthorize("@ss.hasPermi('manage:order:add')" )
@Log(title = "订单" , businessType = BusinessType.INSERT)
@PostMapping
@Transactional
public AjaxResult add(@RequestBody YshopOrder yshopOrder, HttpServletRequest request) {

   WxPayMpOrderResult result = null;
   try {
      WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
      orderRequest.setOutTradeNo(order.getOrderSn());
      orderRequest.setTotalFee(order.getPrice().multiply(new BigDecimal(100)).intValue());
      orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));
      orderRequest.setOpenid(wxuser.getOpenId());
      result = wxPayService.createOrder(orderRequest);
   } catch (WxPayException e) {
      // 异常信息
  }

}

前端页面(UNIAPP)

https://blog.csdn.net/weixin_44571502/article/details/124325871
https://blog.csdn.net/weixin_48596030/article/details/125805895

标签: uniapp, wxjava, 小程序支付

相关阅读

  • 测试信息
  • 拼多多2023年度财报分析
  • 2023年最后一个工作日
  • 2023山东社会责任企业(企业家)” 推选活动结果
  • 测试信息
  • 测试信息
  • 测试信息