日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

App微信支付(Java)

發(fā)布時間:2023/12/20 java 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 App微信支付(Java) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這里使用的是Binary Wang 所寫的開源項目 weixin-java-pay

github??Home · Wechat-Group/WxJava Wiki · GitHub

1、導(dǎo)入maven文件

<!--微信支付--> <dependency><groupId>com.github.binarywang</groupId><artifactId>wx-java-pay-spring-boot-starter</artifactId><version>4.1.0</version> </dependency>

2、application.yml配置(對應(yīng)的信息自行去獲取,微信支付不需要使用p12證書)

wx:pay:appId: wxxxxxxxxxxxxx #微信公眾號或者小程序等的appidmchId: xxxxxxxxx #微信支付商戶號mchKey: xxxxxxxxxxxxxx #微信支付商戶密鑰subAppId: #服務(wù)商模式下的子商戶公眾賬號IDsubMchId: #服務(wù)商模式下的子商戶號keyPath: classpath:/apiclient_cert.p12 # p12證書的位置,可以指定絕對路徑,也可以指定類路徑(以classpath:開頭)

3、WxPayConfiguration配置

將 com.github.binarywang.wxpay.service.WxPayService 作為Bean注入到項目中

/*** @author Binary Wang*/ @Configuration @ConditionalOnClass(WxPayService.class) @EnableConfigurationProperties(WxPayProperties.class) @AllArgsConstructor @Slf4j public class WxPayConfiguration {private WxPayProperties properties;@SneakyThrows@Bean@ConditionalOnMissingBeanpublic WxPayService wxService() {WxPayConfig payConfig = new WxPayConfig();payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));// 可以指定是否使用沙箱環(huán)境payConfig.setUseSandboxEnv(false);WxPayService wxPayService = new WxPayServiceImpl();wxPayService.setConfig(payConfig);return wxPayService;} }

4、WxPayProperties配置

import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties;/*** wxpay pay properties.** @author Binary Wang*/ @Data @ConfigurationProperties(prefix = "wx.pay") public class WxPayProperties {/*** 設(shè)置微信公眾號或者小程序等的appid*/private String appId;/*** 微信支付商戶號*/private String mchId;/*** 微信支付商戶密鑰*/private String mchKey;/*** 服務(wù)商模式下的子商戶公眾賬號ID,普通模式請不要配置,請在配置文件中將對應(yīng)項刪除*/private String subAppId;/*** 服務(wù)商模式下的子商戶號,普通模式請不要配置,最好是請在配置文件中將對應(yīng)項刪除*/private String subMchId;/*** apiclient_cert.p12文件的絕對路徑,或者如果放在項目中,請以classpath:開頭指定*/private String keyPath;}

5、IWxPayService(統(tǒng)一下單)

import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;import java.math.BigDecimal;/*** @author jiavDad*/ public interface IWxPayService {WxPayAppOrderResult createDefault(String tradeNo, BigDecimal price, Integer type, String body, Long userId);}

使用方法:

@Autowired private IWxPayService wxPayService; WxPayAppOrderResult wxPayAppOrderResult = wxPayService.createDefault("自己定義的訂單號", "金額", "類型", body, "用戶id");(當(dāng)前支付類型是app,返回類型為WxPayAppOrderResult ,如果是其他支付就用其他類型的)

6、WxPayServiceImpl

/*** @author jiavDad*/ @Service @Slf4j public class WxPayServiceImpl implements IWxPayService {@Autowiredprivate WxPayService wxService;@Value("${wxPay.callbackPath:/}")private String wxPayPath;@Overridepublic WxPayAppOrderResult createDefault(String tradeNo, BigDecimal price, Integer type, String body, Long userId) {WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();//簽名類型orderRequest.setSignType(WxPayConstants.SignType.MD5);//終端IPorderRequest.setSpbillCreateIp(PayUtil.getIp());//商品描述 例如: 騰訊充值中心-QQ會員充值orderRequest.setBody(body);//商戶訂單號 商戶系統(tǒng)內(nèi)部的訂單號,32個字符內(nèi)、可包含字母orderRequest.setOutTradeNo(tradeNo);//回調(diào)地址 // orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type); // Integer type = memberRewardByUserReq.getType().equals(1) ? IWxPayService.Type.REWARDPOST : IWxPayService.Type.REWARDCOMMENT;orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type + "/" + userId);//支付類型orderRequest.setTradeType("APP"); //如果你不是app支付就填其他的 // orderRequest.setOpenid(openId); //app支付不需要openIdorderRequest.setTotalFee(price.multiply(new BigDecimal(100)).intValue());log.error("支付請求參數(shù):[{}]", orderRequest);try {log.error("生成訂單參數(shù):[{}]", orderRequest);WxPayAppOrderResult wxPayAppOrderResult = wxService.createOrder(orderRequest);return wxPayAppOrderResult;} catch (WxPayException e) {log.error("微信支付失敗!原因:{}", e.getMessage());}throw new RuntimeException("微信支付失敗!");}}

7、設(shè)置支付回調(diào)接口的路徑?application.yml

wxPay:callbackPath: http://localhost:8080/api/wxPay/notify/order/

8、WxPayController

import com.github.binarywang.wxpay.exception.WxPayException; import com.mdframework.module.wxpay.IWxPayService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*;/*** @author jiavDad*/ @Validated @RestController @RequestMapping("/wxPay") @Slf4j @Api(description = "微信支付", tags = "微信支付") public class WxPayController {@Autowiredprivate IWxPayService wxPayService;/*** @param xmlData* @param type 類型* @return* @throws WxPayException*/@PostMapping("/notify/order/{tradeNo}/{type}/{userId}")@ResponseBody@ApiOperation(value = "微信支付回調(diào)", notes = "微信支付回調(diào)")public String parseOrderNotifyResult(@RequestBody String xmlData, @PathVariable String tradeNo, @PathVariable Integer type, @PathVariable Long userId) throws WxPayException {return wxPayService.parseOrderNotifyResult(xmlData, tradeNo, type, userId);}}

9、IWxPayService

public interface IWxPayService { String parseOrderNotifyResult(String xmlData, String tradeNo, Integer type, Long userId); }

10、WxPayServiceImpl

/*** @author jiavDad*/ @Service @Slf4j public class WxPayServiceImpl implements IWxPayService {@Autowiredprivate WxPayService wxService;@Autowiredprivate IAdviceService adviceService;@Value("${wxPay.callbackPath:/}")private String wxPayPath;@SneakyThrows@Override@Transactional(rollbackFor = Exception.class)public String parseOrderNotifyResult(String xmlData, String tradeNo, Integer type, Long userId) {log.info("手機(jī)微信支付回調(diào),xml:[{}],tradeNo:[{}],type:[{}],userId:[{}]", xmlData, tradeNo, type, userId);WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);notifyResult.checkResult(wxService, "MD5", true);String openid = notifyResult.getOpenid();String transactionId = notifyResult.getTransactionId();MemberInfo memberInfo = memberInfoService.getById(userId);AssertHelper.isTrue(Objects.isNull(memberInfo));/**/業(yè)務(wù)邏輯**/return WxPayNotifyResponse.success("成功");}} }

11、支付返回結(jié)果:wxPayAppOrderResult

可以自行測試返回來的簽名是否正確

地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1

?

?注意:app端支付前一定要按照下面的地址配好相應(yīng)的東西,不然肯定會支付失敗!!!!!!

APP端開發(fā)步驟https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

總結(jié)

以上是生活随笔為你收集整理的App微信支付(Java)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。