Commit 72438ed5f691fa53fec5d8fa4f9ac61678d1f70a
1 parent
60f5898c
增加 pay.html 前端,对接微信 JS SDK
Showing
5 changed files
with
72 additions
and
10 deletions
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/service/order/dto/PayOrderCreateReqDTO.java
| @@ -18,7 +18,7 @@ public class PayOrderCreateReqDTO implements Serializable { | @@ -18,7 +18,7 @@ public class PayOrderCreateReqDTO implements Serializable { | ||
| 18 | /** | 18 | /** |
| 19 | * 应用编号 | 19 | * 应用编号 |
| 20 | */ | 20 | */ |
| 21 | - @NotEmpty(message = "应用编号不能为空") | 21 | + @NotNull(message = "应用编号不能为空") |
| 22 | private Long appId; | 22 | private Long appId; |
| 23 | /** | 23 | /** |
| 24 | * 用户 IP | 24 | * 用户 IP |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/service/order/dto/PayOrderSubmitRespDTO.java
| @@ -18,6 +18,6 @@ public class PayOrderSubmitRespDTO implements Serializable { | @@ -18,6 +18,6 @@ public class PayOrderSubmitRespDTO implements Serializable { | ||
| 18 | /** | 18 | /** |
| 19 | * 调用支付渠道的响应结果 | 19 | * 调用支付渠道的响应结果 |
| 20 | */ | 20 | */ |
| 21 | - private String invokeResponse; | 21 | + private Object invokeResponse; |
| 22 | 22 | ||
| 23 | } | 23 | } |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/service/order/impl/PayOrderCoreServiceImpl.java
| @@ -134,7 +134,7 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService { | @@ -134,7 +134,7 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService { | ||
| 134 | // TODO 轮询三方接口,是否已经支付的任务 | 134 | // TODO 轮询三方接口,是否已经支付的任务 |
| 135 | // 返回成功 | 135 | // 返回成功 |
| 136 | return new PayOrderSubmitRespDTO().setExtensionId(orderExtension.getId()) | 136 | return new PayOrderSubmitRespDTO().setExtensionId(orderExtension.getId()) |
| 137 | - .setInvokeResponse(JsonUtils.toJsonString(unifiedOrderResult)); | 137 | + .setInvokeResponse(unifiedOrderResult.getData()); |
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | private String generateOrderExtensionNo() { | 140 | private String generateOrderExtensionNo() { |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/pay/controller/order/vo/PayOrderSubmitRespVO.java
yudao-user-server/src/main/resources/static/pay.html
| @@ -13,10 +13,14 @@ | @@ -13,10 +13,14 @@ | ||
| 13 | </div> | 13 | </div> |
| 14 | </body> | 14 | </body> |
| 15 | <script> | 15 | <script> |
| 16 | + let shopOrderId = undefined; | ||
| 17 | + let payOrderId = undefined; | ||
| 18 | + let server = 'http://127.0.0.1:28080'; | ||
| 16 | $(function() { | 19 | $(function() { |
| 17 | // 获得 JsapiTicket | 20 | // 获得 JsapiTicket |
| 21 | + // 参考 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html 文档 | ||
| 18 | $.ajax({ | 22 | $.ajax({ |
| 19 | - url: "http://127.0.0.1:28080/api/wx/mp/create-jsapi-signature?url=" + document.location.href, | 23 | + url: server + "/api/wx/mp/create-jsapi-signature?url=" + document.location.href, |
| 20 | method: 'POST', | 24 | method: 'POST', |
| 21 | success: function( result ) { | 25 | success: function( result ) { |
| 22 | if (result.code !== 0) { | 26 | if (result.code !== 0) { |
| @@ -24,19 +28,77 @@ | @@ -24,19 +28,77 @@ | ||
| 24 | return; | 28 | return; |
| 25 | } | 29 | } |
| 26 | var jsapiTicket = result.data; | 30 | var jsapiTicket = result.data; |
| 27 | - jsapiTicket.jsApiList = []; | ||
| 28 | - jsapiTicket.debug = true; | 31 | + jsapiTicket.jsApiList = ['chooseWXPay']; |
| 32 | + jsapiTicket.debug = false; | ||
| 29 | 33 | ||
| 30 | // 初始化 JS | 34 | // 初始化 JS |
| 31 | wx.config(jsapiTicket); | 35 | wx.config(jsapiTicket); |
| 32 | } | 36 | } |
| 33 | }); | 37 | }); |
| 38 | + | ||
| 39 | + // 自动发起商城订单编号 | ||
| 40 | + $.ajax({ | ||
| 41 | + url: server + "/api/shop/order/create", | ||
| 42 | + method: 'POST', | ||
| 43 | + success: function( result ) { | ||
| 44 | + if (result.code !== 0) { | ||
| 45 | + alert('创建商城订单失败,原因:' + result.msg) | ||
| 46 | + return; | ||
| 47 | + } | ||
| 48 | + shopOrderId = result.data.id; | ||
| 49 | + payOrderId = result.data.payOrderId; | ||
| 50 | + console.log("商城订单:" + shopOrderId) | ||
| 51 | + console.log("支付订单:" + payOrderId) | ||
| 52 | + } | ||
| 53 | + }) | ||
| 34 | }) | 54 | }) |
| 35 | 55 | ||
| 56 | + // 微信公众号 | ||
| 36 | $( "#wx_pub").on( "click", function() { | 57 | $( "#wx_pub").on( "click", function() { |
| 37 | - wx.ready(function(){ | ||
| 38 | - alert('ok'); | ||
| 39 | - }); | 58 | + if (typeof WeixinJSBridge == "undefined") { |
| 59 | + if (document.addEventListener) { | ||
| 60 | + document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); | ||
| 61 | + } else if (document.attachEvent) { | ||
| 62 | + document.attachEvent('WeixinJSBridgeReady', onBridgeReady); | ||
| 63 | + document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); | ||
| 64 | + } | ||
| 65 | + } else { | ||
| 66 | + onBridgeReady() | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + function onBridgeReady() { | ||
| 70 | + $.ajax({ | ||
| 71 | + url: server + "/api/pay/order/submit", | ||
| 72 | + method: 'POST', | ||
| 73 | + dataType: "json", | ||
| 74 | + contentType: "application/json", | ||
| 75 | + data: JSON.stringify({ | ||
| 76 | + "id": payOrderId, | ||
| 77 | + "channelCode": 'wx_pub', | ||
| 78 | + }), | ||
| 79 | + success: function( result ) { | ||
| 80 | + if (result.code !== 0) { | ||
| 81 | + alert('提交支付订单失败,原因:' + result.msg) | ||
| 82 | + return; | ||
| 83 | + } | ||
| 84 | + alert('开始调用微信支付'); | ||
| 85 | + let data = result.data.invokeResponse; | ||
| 86 | + wx.chooseWXPay({ | ||
| 87 | + timestamp: data.timeStamp, | ||
| 88 | + nonceStr: data.nonceStr, | ||
| 89 | + package: data.package, | ||
| 90 | + signType: data.signType, | ||
| 91 | + paySign: data.paySign, | ||
| 92 | + success: function (res) { | ||
| 93 | + alert(JSON.stringify(res)); | ||
| 94 | + }, | ||
| 95 | + error: function(e) { | ||
| 96 | + alert(JSON.stringify(e)); | ||
| 97 | + } | ||
| 98 | + }); | ||
| 99 | + } | ||
| 100 | + }) | ||
| 101 | + } | ||
| 40 | }); | 102 | }); |
| 41 | </script> | 103 | </script> |
| 42 | </html> | 104 | </html> |