一.微信小程序接入微信支付流程圖:
重點(diǎn)步驟說明:
步驟4:用戶下單發(fā)起支付,商戶可通過[JSAPI下單](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml)創(chuàng)建支付訂單。
步驟9:商戶小程序內(nèi)使用[小程序調(diào)起支付API](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml)(wx.requestPayment)發(fā)起微信支付,詳見小程序API文檔
步驟16:用戶支付成功后,商戶可接收到微信支付支付結(jié)果通知[支付通知API](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml)。
步驟21:商戶在沒有接收到微信支付結(jié)果通知的情況下需要主動(dòng)調(diào)用[查詢訂單API](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_2.shtml)查詢支付結(jié)果。
二.準(zhǔn)備工作:
官方文檔地址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml
1. 小程序開通微信支付
首先需要在微信支付的官網(wǎng)注冊(cè)一個(gè)商戶;
在管理頁面中申請(qǐng)關(guān)聯(lián)小程序,通過小程序的 appid 進(jìn)行關(guān)聯(lián);
進(jìn)入微信公眾平臺(tái),功能-微信支付中確認(rèn)關(guān)聯(lián)(如果服務(wù)商和小程序的注冊(cè)主體不一樣,還要經(jīng)過微信的審核)
?2. 獲取各種證書、密鑰文件-基于微信支付 V3 的版本:
商戶 id:這個(gè)可以在小程序微信公眾平臺(tái)-功能-微信支付 頁面中的已關(guān)聯(lián)商戶號(hào)中得到
商戶密鑰:這個(gè)需要在微信支付的管理后臺(tái)中申請(qǐng)獲取
證書編號(hào): 同樣在微信支付的管理后臺(tái)中申請(qǐng)證書,申請(qǐng)證書后就會(huì)看到證書編號(hào)
證書私鑰:上一步申請(qǐng)證書的時(shí)候同時(shí)也會(huì)獲取到證書的公鑰、私鑰文件。開發(fā)中只需要私鑰文件
三.后端代碼
> 1.引入微信支付官方的pom依賴:
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.4.9</version>
</dependency>
> 2.微信支付配置信息:
? wx-pay:
? v3:
? ? #微信關(guān)聯(lián)的小程序的appid
? ? appId: xxxxx
? ? #微信支付商戶號(hào)
? ? mchId: xxxxxx
? ? #微信支付證書序列號(hào)
? ? serialnumber: xxxxxx
? ? #微信支付apiv3的密鑰
? ? apiKey3: xxxxx
? ? #微信支付證書
? ? certKeyPath: /cert/xxxxx.pem
? ? #微信支付回調(diào)商戶線上地址api
? ? notifyUrl: https://xxxxx/pay/callback
? ? #微信支付關(guān)閉訂單api
? ? closeOrderUrl: https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close
? ? #微信支付小程序預(yù)下單api
? ? jsApiUrl: https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
? ? #微信支付訂單查詢api
? ? queryOrder: https://api.mch.weixin.qq.com/v3/pay/transactions/id/
@Data
@Component
@ConfigurationProperties(prefix="wx-pay.v3")
public class WxPayConfig {
? ? //appId
? ? private String appId;
? ? //商戶id
? ? private String mchId;
? ? //證書序列號(hào)
? ? private String serialnumber;
? ? //商戶秘鑰
? ? private String apiKey3;
? ? //商戶的key【API密匙】存放路徑
? ? private String certKeyPath;
? ? //回調(diào)通知地址
? ? private String notifyUrl;
? ? //jsapi預(yù)支付url
? ? private String jsApiUrl;
? ? //關(guān)閉訂單接口
? ? private String closeOrderUrl;
? ? //查詢訂單地址
? ? private String queryOrder;
}
?3.初始化微信支付的參數(shù)
@Service
@Transactional
public class SysPayServiceImpl implements SysPayService {
private static final Logger log = LoggerFactory.getLogger(SysPayServiceImpl.class);
? ? @Autowired
? ? private WxPayConfig wxPayConfig;
? ? //微信專業(yè)httpClient
? ? private static CloseableHttpClient httpClient;
? ? //生成簽名用
? ? private static Sign sign;
? ? //證書管理器
? ? private static CertificatesManager certificatesManager;
? ? @Autowired
? ? private SysOrderDao sysOrderDao;
//初始化微信支付的參數(shù)
? ? @PostConstruct
? ? public void init() throws Exception {
? ? ? ? //獲取商戶私鑰
? ? ? ? PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(getFileInputStream(wxPayConfig.getCertKeyPath()));
? ? ? ? // 獲取證書管理器實(shí)例
? ? ? ? certificatesManager = CertificatesManager.getInstance();
? ? ? ? sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA, merchantPrivateKey.getEncoded(), null);
? ? ? ? // 向證書管理器增加需要自動(dòng)更新平臺(tái)證書的商戶信息
? ? ? ? certificatesManager.putMerchant(wxPayConfig.getMchId(), new WechatPay2Credentials(wxPayConfig.getMchId(),
? ? ? ? ? ? ? ? new PrivateKeySigner(wxPayConfig.getSerialnumber(), merchantPrivateKey)), wxPayConfig.getApiKey3().getBytes(StandardCharsets.UTF_8));
? ? ? ? // 從證書管理器中獲取verifier
? ? ? ? Verifier verifier = certificatesManager.getVerifier(wxPayConfig.getMchId());
? ? ? ? //用于構(gòu)造HttpClient
? ? ? ? WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
? ? ? ? ? ? ? ? .withMerchant(wxPayConfig.getMchId(), wxPayConfig.getSerialnumber(), merchantPrivateKey)
? ? ? ? ? ? ? ? .withValidator(new WechatPay2Validator(verifier));
? ? ? ? httpClient = builder.build();
? ? }
/**
? ? * 使用getResourceAsStream直接從resources根路徑下獲取文件流
? ? * @param path
? ? */
? ? private InputStream getFileInputStream(String path) {
? ? ? ? InputStream in = this.getClass().getResourceAsStream(path);
? ? ? ? return in;
? ? }
}
?4.預(yù)訂單生成返回給小程序請(qǐng)求參數(shù)
//預(yù)訂單生成返回給小程序請(qǐng)求參數(shù)
? ? @Override
? ? public AjaxResult prePay(WxPayDto dto) {
? ? ? ? log.info("微信小程序支付JSAPI預(yù)下單開始------");
? ? ? ? AjaxResult ajaxResult = AjaxResult.success();
? ? ? ? JSONObject obj = new JSONObject();
? ? ? ? //應(yīng)用ID
? ? ? ? obj.put("appid", wxPayConfig.getAppId());
? ? ? ? //直連商戶號(hào)
? ? ? ? obj.put("mchid", wxPayConfig.getMchId());
? ? ? ? //商品描述
? ? ? ? obj.put("description", dto.getDescription());
? ? ? ? //商戶訂單號(hào)
? ? ? ? obj.put("out_trade_no", dto.getOrderCode());
? ? ? ? //通知地址
? ? ? ? obj.put("notify_url", wxPayConfig.getNotifyUrl());
? ? ? ? //附加數(shù)據(jù) 查詢API和支付通知中原樣返回,可作為自定義參數(shù)使用,實(shí)際情況下只有支付完成狀態(tài)才會(huì)返回該字段。
? ? ? ? Map<String, String> attach = new HashMap<>();
? ? ? ? attach.put("orderCode", dto.getOrderCode());
? ? ? ? obj.put("attach", JSON.toJSONString(attach));
? ? ? ? //訂單金額
? ? ? ? JSONObject amount = new JSONObject();
? ? ? ? amount.put("total", dto.getPrice());
? ? ? ? obj.put("amount", amount);
? ? ? ? //支付者
? ? ? ? JSONObject payer = new JSONObject();
? ? ? ? payer.put("openid", dto.getOpenId());
? ? ? ? obj.put("payer", payer);
? ? ? ? log.info("微信小程序支付JSAPI預(yù)下單請(qǐng)求參數(shù):{}" + obj.toJSONString());
? ? ? ? HttpPost httpPost = new HttpPost(wxPayConfig.getJsApiUrl());
? ? ? ? httpPost.addHeader("Accept", "application/json");
? ? ? ? httpPost.addHeader("Content-type", "application/json; charset=utf-8");
? ? ? ? httpPost.setEntity(new StringEntity(obj.toJSONString(), "UTF-8"));
? ? ? ? String bodyAsString ;
? ? ? ? try {
? ? ? ? ? ? if(httpClient == null){
? ? ? ? ? ? ? ? log.info("微信小程序支付JSAPI預(yù)下單請(qǐng)求失敗");
? ? ? ? ? ? ? ? return AjaxResult.error("預(yù)下單失敗,請(qǐng)重試,無法連接微信支付服務(wù)器!" );
? ? ? ? ? ? }
? ? ? ? ? ? //執(zhí)行請(qǐng)求
? ? ? ? ? ? CloseableHttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? bodyAsString = EntityUtils.toString(response.getEntity());
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? log.info("微信小程序支付JSAPI預(yù)下單請(qǐng)求失敗{}", e.getMessage());
? ? ? ? ? ? return AjaxResult.error("預(yù)下單失敗,請(qǐng)重試!" + e.getMessage());
? ? ? ? }
? ? ? ? String prePayId = JSONObject.parseObject(bodyAsString).getString("prepay_id");
? ? ? ? if (prePayId == null){
? ? ? ? ? ? String message = JSONObject.parseObject(bodyAsString).getString("message");
? ? ? ? ? ? log.info("微信小程序支付JSAPI預(yù)下單請(qǐng)求失敗{}", message);
? ? ? ? ? ? return AjaxResult.error("預(yù)下單失敗,請(qǐng)重試!" + message);
? ? ? ? }
? ? ? ? //準(zhǔn)備小程序端的請(qǐng)求參數(shù)
? ? ? ? ajaxResult.put("appId", wxPayConfig.getAppId());
? ? ? ? String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
? ? ? ? ajaxResult.put("timeStamp", timeStamp);
? ? ? ? String nonceStr = IdUtil.fastSimpleUUID();
? ? ? ? ajaxResult.put("nonceStr", nonceStr);
? ? ? ? String packageStr = "prepay_id=" + prePayId;
? ? ? ? ajaxResult.put("package", packageStr);
? ? ? ? ajaxResult.put("signType", "RSA");
? ? ? ? String signString =? wxPayConfig.getAppId() + "\n" + timeStamp + "\n" + nonceStr + "\n" + packageStr + "\n";
? ? ? ? ajaxResult.put("paySign", Base64.encode(sign.sign(signString.getBytes())));
? ? ? ? return ajaxResult;
? ? }
> 5.微信支付回調(diào)通知
? ? //微信支付回調(diào)通知
? ? @Override
? ? public void callback(HttpServletRequest servletRequest, HttpServletResponse response) {
? ? ? ? log.info("----------->微信支付回調(diào)開始");
? ? ? ? Map<String, String> map = new HashMap<>(12);
? ? ? ? String timeStamp = servletRequest.getHeader("Wechatpay-Timestamp");
? ? ? ? String nonce = servletRequest.getHeader("Wechatpay-Nonce");
? ? ? ? String signature = servletRequest.getHeader("Wechatpay-Signature");
? ? ? ? String certSn = servletRequest.getHeader("Wechatpay-Serial");
? ? ? ? try (BufferedReader reader = new BufferedReader(new InputStreamReader(servletRequest.getInputStream()))) {
? ? ? ? ? ? StringBuilder stringBuilder = new StringBuilder();
? ? ? ? ? ? String line;
? ? ? ? ? ? while ((line = reader.readLine()) != null) {
? ? ? ? ? ? ? ? stringBuilder.append(line);
? ? ? ? ? ? }
? ? ? ? ? ? String obj = stringBuilder.toString();
? ? ? ? ? ? log.info("微信支付回調(diào)請(qǐng)求參數(shù):{},{},{},{},{}", obj, timeStamp, nonce, signature, certSn);
? ? ? ? ? ? Verifier verifier = certificatesManager.getVerifier(wxPayConfig.getMchId());
? ? ? ? ? ? String sn = verifier.getValidCertificate().getSerialNumber().toString(16).toUpperCase(Locale.ROOT);
? ? ? ? ? ? NotificationRequest request = new NotificationRequest.Builder().withSerialNumber(sn)
? ? ? ? ? ? ? ? ? ? .withNonce(nonce)
? ? ? ? ? ? ? ? ? ? .withTimestamp(timeStamp)
? ? ? ? ? ? ? ? ? ? .withSignature(signature)
? ? ? ? ? ? ? ? ? ? .withBody(obj)
? ? ? ? ? ? ? ? ? ? .build();
? ? ? ? ? ? NotificationHandler handler = new NotificationHandler(verifier, wxPayConfig.getApiKey3().getBytes(StandardCharsets.UTF_8));
? ? ? ? ? ? // 驗(yàn)簽和解析請(qǐng)求體
? ? ? ? ? ? Notification notification = handler.parse(request);
? ? ? ? ? ? JSONObject res = JSON.parseObject(notification.getDecryptData());
? ? ? ? ? ? log.info("微信支付回調(diào)響應(yīng)參數(shù):{}", res.toJSONString());
? ? ? ? ? ? //做一些操作
? ? ? ? ? ? if (res != null) {
? ? ? ? ? ? ? ? //如果支付成功
? ? ? ? ? ? ? ? String tradeState = res.getString("trade_state");
? ? ? ? ? ? ? ? if("SUCCESS".equals(tradeState)){
? ? ? ? ? ? ? ? ? ? //拿到商戶訂單號(hào)
? ? ? ? ? ? ? ? ? ? String outTradeNo = res.getString("out_trade_no");
? ? ? ? ? ? ? ? ? ? JSONObject amountJson = res.getJSONObject("amount");
? ? ? ? ? ? ? ? ? ? Integer payerTotal = amountJson.getInteger("payer_total");
? ? ? ? ? ? ? ? ? ? SysOrder sysOrder = sysOrderDao.queryByOrderCode(outTradeNo);
? ? ? ? ? ? ? ? ? ? if(sysOrder != null){
? ? ? ? ? ? ? ? ? ? ? ? if(sysOrder.getPayStatus() == 1){
? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果支付狀態(tài)為1 說明訂單已經(jīng)支付成功了,直接響應(yīng)微信服務(wù)器返回成功
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.setStatus(200);
? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("code", "SUCCESS");
? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("message", "SUCCESS");
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.setHeader("Content-type", ContentType.JSON.toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.flushBuffer();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? //驗(yàn)證用戶支付的金額和訂單金額是否一致
? ? ? ? ? ? ? ? ? ? ? ? if(payerTotal.equals(sysOrder.getOrderPrice())){
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改訂單狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? ? ? String successTime = res.getString("success_time");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sysOrder.setPayStatus(1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? sysOrderDao.update(sysOrder);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //響應(yīng)微信服務(wù)器
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.setStatus(200);
? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("code", "SUCCESS");
? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put("message", "SUCCESS");
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.setHeader("Content-type", ContentType.JSON.toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
? ? ? ? ? ? ? ? ? ? ? ? ? ? response.flushBuffer();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? response.setStatus(500);
? ? ? ? ? ? map.put("code", "ERROR");
? ? ? ? ? ? map.put("message", "簽名錯(cuò)誤");
? ? ? ? ? ? response.setHeader("Content-type", ContentType.JSON.toString());
? ? ? ? ? ? response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
? ? ? ? ? ? response.flushBuffer();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? log.error("微信支付回調(diào)失敗", e);
? ? ? ? }
? ? }
> 6.當(dāng)用戶10分鐘內(nèi)未支付,系統(tǒng)自動(dòng)關(guān)閉微信支付訂單并刪除
//當(dāng)用戶10分鐘內(nèi)未支付,系統(tǒng)自動(dòng)關(guān)閉微信支付訂單并刪除
? ? @Override
? ? public void deleteClosePayOrder(String orderCode) {
? ? ? ? JSONObject obj = new JSONObject();
? ? ? ? //直連商戶號(hào)
? ? ? ? obj.put("mchid", wxPayConfig.getMchId());
? ? ? ? String ret2 = wxPayConfig.getCloseOrderUrl().replace("{out_trade_no}", orderCode);
? ? ? ? log.info("關(guān)閉和刪除微信訂單--請(qǐng)求地址{}" , ret2);
? ? ? ? log.info("關(guān)閉和刪除微信訂單--請(qǐng)求參數(shù){}" , obj.toJSONString());
? ? ? ? HttpPost httpPost = new HttpPost(ret2);
? ? ? ? httpPost.addHeader("Accept", "application/json");
? ? ? ? httpPost.addHeader("Content-type", "application/json; charset=utf-8");
? ? ? ? httpPost.setEntity(new StringEntity(obj.toJSONString(), "UTF-8"));
? ? ? ? String bodyAsString ;
? ? ? ? try {
? ? ? ? ? ? if(httpClient == null){
? ? ? ? ? ? ? ? log.info("關(guān)閉和刪除微信訂單--關(guān)閉訂單失敗,請(qǐng)重試,無法連接微信支付服務(wù)器!");
? ? ? ? ? ? }
? ? ? ? ? ? //執(zhí)行請(qǐng)求
? ? ? ? ? ? CloseableHttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? //狀態(tài)碼
? ? ? ? ? ? int statusCode = response.getStatusLine().getStatusCode();
? ? ? ? ? ? if (statusCode == 204) {
? ? ? ? ? ? ? ? //關(guān)閉訂單成功!
? ? ? ? ? ? ? ? //刪除訂單
? ? ? ? ? ? ? ? log.info("關(guān)閉和刪除微信訂單--關(guān)閉訂單成功orderCode:{}!", orderCode);
? ? ? ? ? ? ? ? sysOrderDao.deleteByOrderCode(orderCode);
? ? ? ? ? ? }else if(statusCode == 202){
? ? ? ? ? ? ? ? //用戶支付中,需要輸入密碼
? ? ? ? ? ? ? ? log.info("關(guān)閉和刪除微信訂單--用戶支付中,需要輸入密碼,暫時(shí)不做處理!");
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? log.info("關(guān)閉和刪除微信訂單--關(guān)閉支付訂單失敗,出現(xiàn)未知原因{}", EntityUtils.toString(response.getEntity()));
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? log.info("關(guān)閉和刪除微信訂單--關(guān)閉訂單失敗{}", e.getMessage());
? ? ? ? }
? ? }
> 7.查詢訂單支付結(jié)果接口
//查詢訂單支付結(jié)果
? ? @Override
? ? public AjaxResult queryPayResult(String payId){
? ? ? ? //請(qǐng)求URL
? ? ? ? URIBuilder uriBuilder = null;
? ? ? ? try {
? ? ? ? ? ? uriBuilder = new URIBuilder(wxPayConfig.getQueryOrder() + payId);
? ? ? ? ? ? uriBuilder.setParameter("mchid", wxPayConfig.getMchId());
? ? ? ? ? ? if(httpClient == null){
? ? ? ? ? ? ? ? return AjaxResult.error("查詢訂單失敗,請(qǐng)重試,無法連接微信支付服務(wù)器!" );
? ? ? ? ? ? }
? ? ? ? ? ? //完成簽名并執(zhí)行請(qǐng)求
? ? ? ? ? ? HttpGet httpGet = new HttpGet(uriBuilder.build());
? ? ? ? ? ? httpGet.addHeader("Accept", "application/json");
? ? ? ? ? ? CloseableHttpResponse response = httpClient.execute(httpGet);
? ? ? ? ? ? int statusCode = response.getStatusLine().getStatusCode();
? ? ? ? ? ? if (statusCode == 200 || statusCode == 204) {
? ? ? ? ? ? ? ? String ss = EntityUtils.toString(response.getEntity());
? ? ? ? ? ? ? ? if(StringUtils.isNotEmpty(ss)){
? ? ? ? ? ? ? ? ? ? JSONObject res = JSON.parseObject(ss);
? ? ? ? ? ? ? ? ? ? log.info("success,return body = " + ss);
? ? ? ? ? ? ? ? ? ? //拿到商戶訂單號(hào)
? ? ? ? ? ? ? ? ? ? String outTradeNo = res.getString("out_trade_no");
? ? ? ? ? ? ? ? ? ? JSONObject amountJson = res.getJSONObject("amount");
? ? ? ? ? ? ? ? ? ? Integer payerTotal = amountJson.getInteger("payer_total");
? ? ? ? ? ? ? ? ? ? SysOrder sysOrder = sysOrderDao.queryByOrderCode(outTradeNo);
? ? ? ? ? ? ? ? ? ? if(sysOrder != null){
? ? ? ? ? ? ? ? ? ? ? ? if(sysOrder.getPayStatus() == 1){
? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果支付狀態(tài)為1 說明訂單已經(jīng)支付成功了,直接響應(yīng)成功
? ? ? ? ? ? ? ? ? ? ? ? ? ? return AjaxResult.success("查詢支付成功!" );
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? //驗(yàn)證用戶支付的金額和訂單金額是否一致
? ? ? ? ? ? ? ? ? ? ? ? if(payerTotal.equals(sysOrder.getOrderPrice())){
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改訂單狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? ? ? String successTime = res.getString("success_time");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sysOrder.setPayStatus(1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? sysOrderDao.update(sysOrder);
? ? ? ? ? ? ? ? ? ? ? ? ? ? return AjaxResult.success("查詢支付成功!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return AjaxResult.error("查詢支付失敗:" + "failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? return AjaxResult.error("查詢支付失敗:" + e.getMessage());
? ? ? ? }
? ? }
### 四.前端小程序代碼
>微信小程序支付需要用到官方的支付API:wx.requestPayment(Object object)
>官方文檔地址:https://developers.weixin.qq.com/miniprogram/dev/api/payment/wx.requestPayment.html
wx.requestPayment({
? ? ? ? ? ? timeStamp: 'xxx',? // 時(shí)間戳,從 1970 年 1 月 1 日 00:00:00 至今的秒數(shù),即當(dāng)前的時(shí)間
? ? ? ? ? ? nonceStr: 'xxxxxx',? // 隨機(jī)字符串,長(zhǎng)度為32個(gè)字符以下
? ? ? ? ? ? package: 'xxxxxxxxxxxx',? // 統(tǒng)一下單接口返回的 prepay_id 參數(shù)值,提交格式如:prepay_id=***
? ? ? ? ? ? signType: 'xxx',? // 簽名算法,應(yīng)與后臺(tái)下單時(shí)的值一致
? ? ? ? ? ? paySign: 'xxxxxxxxxx', // 簽名,具體見微信支付文檔
? ? ? ? ? ? success (res) { // 成功的回調(diào)
? ? ? ? ? ? ? ? wx.showToast({title: '付款成功'})
? ? ? ? ? ? },
? ? ? ? ? ? fail (res) { // 失敗的回調(diào)
? ? ? ? ? ? ? ? wx.showToast({title: '支付失敗',icon: 'none'})
? ? ? ? ? ? }
? ? ? ? ? })
})