Commit fc2d30cfaf64ee886bc84547494951036a175d45
1 parent
7bff98b6
pay:修复单元测试报错
Showing
8 changed files
with
26 additions
and
168 deletions
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/api/transfer/dto/PayTransferCreateReqDTO.java
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/package-info.java deleted
| 1 | -package cn.iocoder.yudao.module.pay; |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/wallet/PayWalletController.java
| ... | ... | @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollectionUtil; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 8 | 7 | import cn.iocoder.yudao.module.member.api.user.MemberUserApi; |
| 9 | 8 | import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; |
| 10 | 9 | import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.wallet.PayWalletPageReqVO; |
| ... | ... | @@ -26,12 +25,10 @@ import javax.annotation.Resource; |
| 26 | 25 | import javax.validation.Valid; |
| 27 | 26 | import java.util.List; |
| 28 | 27 | import java.util.Map; |
| 29 | -import java.util.Set; | |
| 30 | 28 | |
| 31 | 29 | import static cn.iocoder.yudao.framework.common.enums.UserTypeEnum.MEMBER; |
| 32 | 30 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 33 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 34 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 31 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | |
| 35 | 32 | |
| 36 | 33 | @Tag(name = "管理后台 - 用户钱包") |
| 37 | 34 | @RestController |
| ... | ... | @@ -50,6 +47,7 @@ public class PayWalletController { |
| 50 | 47 | @Operation(summary = "获得用户钱包明细") |
| 51 | 48 | public CommonResult<PayWalletRespVO> getWallet(PayWalletUserReqVO reqVO) { |
| 52 | 49 | PayWalletDO wallet = payWalletService.getOrCreateWallet(reqVO.getUserId(), MEMBER.getValue()); |
| 50 | + // TODO jason:如果为空,返回给前端只要 null 就可以了 | |
| 53 | 51 | MemberUserRespDTO memberUser = memberUserApi.getUser(reqVO.getUserId()); |
| 54 | 52 | String nickname = memberUser == null ? "" : memberUser.getNickname(); |
| 55 | 53 | String avatar = memberUser == null ? "" : memberUser.getAvatar(); |
| ... | ... | @@ -61,9 +59,8 @@ public class PayWalletController { |
| 61 | 59 | @PreAuthorize("@ss.hasPermission('pay:wallet:query')") |
| 62 | 60 | public CommonResult<PageResult<PayWalletRespVO>> getWalletPage(@Valid PayWalletPageReqVO pageVO) { |
| 63 | 61 | if (StrUtil.isNotEmpty(pageVO.getNickname())) { |
| 64 | - Set<Long> userIds = CollectionUtils.convertSet(memberUserApi.getUserListByNickname(pageVO.getNickname()), | |
| 65 | - MemberUserRespDTO::getId); | |
| 66 | - pageVO.setUserIds(userIds); | |
| 62 | + List<MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageVO.getNickname()); | |
| 63 | + pageVO.setUserIds(convertSet(users, MemberUserRespDTO::getId)); | |
| 67 | 64 | } |
| 68 | 65 | // TODO @jason:管理员也可以先查询下。。 |
| 69 | 66 | // 暂时支持查询 userType 会员类型。管理员类型还不知道使用场景 |
| ... | ... | @@ -71,8 +68,8 @@ public class PayWalletController { |
| 71 | 68 | if (CollectionUtil.isEmpty(pageResult.getList())) { |
| 72 | 69 | return success(new PageResult<>(pageResult.getTotal())); |
| 73 | 70 | } |
| 74 | - List<Long> userIds = convertList(pageResult.getList(), PayWalletDO::getUserId); | |
| 75 | - Map<Long, MemberUserRespDTO> userMap = convertMap(memberUserApi.getUserList(userIds),MemberUserRespDTO::getId); | |
| 71 | + List<MemberUserRespDTO> users = memberUserApi.getUserList(convertList(pageResult.getList(), PayWalletDO::getUserId)); | |
| 72 | + Map<Long, MemberUserRespDTO> userMap = convertMap(users, MemberUserRespDTO::getId); | |
| 76 | 73 | return success(PayWalletConvert.INSTANCE.convertPage(pageResult, userMap)); |
| 77 | 74 | } |
| 78 | 75 | ... | ... |
yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceTest.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.pay.service.order; |
| 3 | 3 | import cn.hutool.extra.spring.SpringUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; |
| 6 | -import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | |
| 7 | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; |
| 8 | 7 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| 9 | 8 | import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; |
| ... | ... | @@ -68,8 +67,6 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 68 | 67 | private PayOrderExtensionMapper orderExtensionMapper; |
| 69 | 68 | |
| 70 | 69 | @MockBean |
| 71 | - private PayClientFactory payClientFactory; | |
| 72 | - @MockBean | |
| 73 | 70 | private PayProperties properties; |
| 74 | 71 | @MockBean |
| 75 | 72 | private PayAppService appService; |
| ... | ... | @@ -351,7 +348,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 351 | 348 | .thenReturn(channel); |
| 352 | 349 | // mock 方法(client) |
| 353 | 350 | PayClient client = mock(PayClient.class); |
| 354 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 351 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 355 | 352 | // mock 方法() |
| 356 | 353 | PayOrderRespDTO unifiedOrderResp = randomPojo(PayOrderRespDTO.class, o -> |
| 357 | 354 | o.setChannelErrorCode("001").setChannelErrorMsg("模拟异常")); |
| ... | ... | @@ -405,7 +402,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 405 | 402 | .thenReturn(channel); |
| 406 | 403 | // mock 方法(client) |
| 407 | 404 | PayClient client = mock(PayClient.class); |
| 408 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 405 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 409 | 406 | // mock 方法(支付渠道的调用) |
| 410 | 407 | PayOrderRespDTO unifiedOrderResp = randomPojo(PayOrderRespDTO.class, o -> o.setChannelErrorCode(null).setChannelErrorMsg(null) |
| 411 | 408 | .setDisplayMode(PayOrderDisplayModeEnum.URL.getMode()).setDisplayContent("tudou")); |
| ... | ... | @@ -463,7 +460,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 463 | 460 | orderExtensionMapper.insert(orderExtension); |
| 464 | 461 | // mock 方法(PayClient 已支付) |
| 465 | 462 | PayClient client = mock(PayClient.class); |
| 466 | - when(payClientFactory.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); | |
| 463 | + when(channelService.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); | |
| 467 | 464 | when(client.getOrder(eq(orderExtension.getNo()))).thenReturn(randomPojo(PayOrderRespDTO.class, |
| 468 | 465 | o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus()))); |
| 469 | 466 | |
| ... | ... | @@ -482,7 +479,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 482 | 479 | orderExtensionMapper.insert(orderExtension); |
| 483 | 480 | // mock 方法(PayClient 已支付) |
| 484 | 481 | PayClient client = mock(PayClient.class); |
| 485 | - when(payClientFactory.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); | |
| 482 | + when(channelService.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); | |
| 486 | 483 | when(client.getOrder(eq(orderExtension.getNo()))).thenReturn(randomPojo(PayOrderRespDTO.class, |
| 487 | 484 | o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus()))); |
| 488 | 485 | |
| ... | ... | @@ -639,7 +636,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 639 | 636 | orderExtensionMapper.insert(orderExtension); |
| 640 | 637 | // 准备参数 |
| 641 | 638 | PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L) |
| 642 | - .setFeeRate(0.1D)); | |
| 639 | + .setFeeRate(10D)); | |
| 643 | 640 | PayOrderRespDTO notify = randomPojo(PayOrderRespDTO.class, |
| 644 | 641 | o -> o.setStatus(PayOrderStatusRespEnum.SUCCESS.getStatus()) |
| 645 | 642 | .setOutTradeNo("P110")); |
| ... | ... | @@ -656,7 +653,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 656 | 653 | .setChannelId(10L).setChannelCode(channel.getCode()) |
| 657 | 654 | .setSuccessTime(notify.getSuccessTime()).setExtensionId(orderExtension.getId()).setNo(orderExtension.getNo()) |
| 658 | 655 | .setChannelOrderNo(notify.getChannelOrderNo()).setChannelUserId(notify.getChannelUserId()) |
| 659 | - .setChannelFeeRate(0.1D).setChannelFeePrice(1); | |
| 656 | + .setChannelFeeRate(10D).setChannelFeePrice(1); | |
| 660 | 657 | assertPojoEquals(order, orderMapper.selectOne(null), |
| 661 | 658 | "updateTime", "updater"); |
| 662 | 659 | // 断言,调用 |
| ... | ... | @@ -874,7 +871,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 874 | 871 | orderExtensionMapper.insert(orderExtension); |
| 875 | 872 | // mock 方法(PayClient) |
| 876 | 873 | PayClient client = mock(PayClient.class); |
| 877 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 874 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 878 | 875 | // mock 方法(PayClient 异常) |
| 879 | 876 | when(client.getOrder(any())).thenThrow(new RuntimeException()); |
| 880 | 877 | |
| ... | ... | @@ -901,7 +898,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 901 | 898 | orderExtensionMapper.insert(orderExtension); |
| 902 | 899 | // mock 方法(PayClient) |
| 903 | 900 | PayClient client = mock(PayClient.class); |
| 904 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 901 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 905 | 902 | // mock 方法(PayClient 成功返回) |
| 906 | 903 | PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, |
| 907 | 904 | o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus())); |
| ... | ... | @@ -935,7 +932,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 935 | 932 | orderExtensionMapper.insert(orderExtension); |
| 936 | 933 | // mock 方法(PayClient) |
| 937 | 934 | PayClient client = mock(PayClient.class); |
| 938 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 935 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 939 | 936 | // mock 方法(PayClient 成功返回) |
| 940 | 937 | PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, |
| 941 | 938 | o -> o.setStatus(PayOrderStatusEnum.CLOSED.getStatus())); |
| ... | ... | @@ -966,7 +963,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 966 | 963 | orderExtensionMapper.insert(orderExtension); |
| 967 | 964 | // mock 方法(PayClient) |
| 968 | 965 | PayClient client = mock(PayClient.class); |
| 969 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 966 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 970 | 967 | |
| 971 | 968 | // 调用 |
| 972 | 969 | int count = orderService.expireOrder(); |
| ... | ... | @@ -1013,7 +1010,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 1013 | 1010 | orderExtensionMapper.insert(orderExtension); |
| 1014 | 1011 | // mock 方法(PayClient) |
| 1015 | 1012 | PayClient client = mock(PayClient.class); |
| 1016 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 1013 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 1017 | 1014 | // mock 方法(PayClient 退款返回) |
| 1018 | 1015 | PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, |
| 1019 | 1016 | o -> o.setStatus(PayOrderStatusEnum.REFUND.getStatus())); |
| ... | ... | @@ -1047,7 +1044,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 1047 | 1044 | orderExtensionMapper.insert(orderExtension); |
| 1048 | 1045 | // mock 方法(PayClient) |
| 1049 | 1046 | PayClient client = mock(PayClient.class); |
| 1050 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 1047 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 1051 | 1048 | // mock 方法(PayClient 成功返回) |
| 1052 | 1049 | PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, |
| 1053 | 1050 | o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus())); |
| ... | ... | @@ -1081,7 +1078,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { |
| 1081 | 1078 | orderExtensionMapper.insert(orderExtension); |
| 1082 | 1079 | // mock 方法(PayClient) |
| 1083 | 1080 | PayClient client = mock(PayClient.class); |
| 1084 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 1081 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 1085 | 1082 | // mock 方法(PayClient 关闭返回) |
| 1086 | 1083 | PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, |
| 1087 | 1084 | o -> o.setStatus(PayOrderStatusEnum.CLOSED.getStatus())); | ... | ... |
yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceTest.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.pay.service.refund; |
| 3 | 3 | import cn.hutool.extra.spring.SpringUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; |
| 6 | -import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | |
| 7 | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; |
| 8 | 7 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; |
| 9 | 8 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| ... | ... | @@ -67,8 +66,6 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 67 | 66 | @MockBean |
| 68 | 67 | private PayProperties payProperties; |
| 69 | 68 | @MockBean |
| 70 | - private PayClientFactory payClientFactory; | |
| 71 | - @MockBean | |
| 72 | 69 | private PayOrderService orderService; |
| 73 | 70 | @MockBean |
| 74 | 71 | private PayAppService appService; |
| ... | ... | @@ -335,7 +332,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 335 | 332 | when(channelService.validPayChannel(eq(1L))).thenReturn(channel); |
| 336 | 333 | // mock 方法(client) |
| 337 | 334 | PayClient client = mock(PayClient.class); |
| 338 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 335 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 339 | 336 | // mock 数据(refund 已存在) |
| 340 | 337 | PayRefundDO refund = randomPojo(PayRefundDO.class, o -> |
| 341 | 338 | o.setAppId(1L).setMerchantRefundId("200")); |
| ... | ... | @@ -367,7 +364,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 367 | 364 | when(channelService.validPayChannel(eq(10L))).thenReturn(channel); |
| 368 | 365 | // mock 方法(client) |
| 369 | 366 | PayClient client = mock(PayClient.class); |
| 370 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 367 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 371 | 368 | // mock 方法(client 调用发生异常) |
| 372 | 369 | when(client.unifiedRefund(any(PayRefundUnifiedReqDTO.class))).thenThrow(new RuntimeException()); |
| 373 | 370 | |
| ... | ... | @@ -411,7 +408,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 411 | 408 | when(channelService.validPayChannel(eq(10L))).thenReturn(channel); |
| 412 | 409 | // mock 方法(client) |
| 413 | 410 | PayClient client = mock(PayClient.class); |
| 414 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 411 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 415 | 412 | // mock 方法(client 成功) |
| 416 | 413 | PayRefundRespDTO refundRespDTO = randomPojo(PayRefundRespDTO.class); |
| 417 | 414 | when(client.unifiedRefund(argThat(unifiedReqDTO -> { |
| ... | ... | @@ -668,7 +665,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 668 | 665 | refundMapper.insert(refund); |
| 669 | 666 | // mock 方法(client) |
| 670 | 667 | PayClient client = mock(PayClient.class); |
| 671 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 668 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 672 | 669 | // mock 方法(client 返回指定状态) |
| 673 | 670 | PayRefundRespDTO respDTO = randomPojo(PayRefundRespDTO.class, o -> o.setStatus(status)); |
| 674 | 671 | when(client.getRefund(eq("P110"), eq("R220"))).thenReturn(respDTO); |
| ... | ... | @@ -690,7 +687,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { |
| 690 | 687 | refundMapper.insert(refund); |
| 691 | 688 | // mock 方法(client) |
| 692 | 689 | PayClient client = mock(PayClient.class); |
| 693 | - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); | |
| 690 | + when(channelService.getPayClient(eq(10L))).thenReturn(client); | |
| 694 | 691 | // mock 方法(client 抛出异常) |
| 695 | 692 | when(client.getRefund(eq("P110"), eq("R220"))).thenThrow(new RuntimeException()); |
| 696 | 693 | ... | ... |
yudao-server/src/main/resources/static/MP_verify_DKOvVzFP7vPwwHx2.txt deleted
yudao-server/src/main/resources/static/READMD.md deleted
| 1 | -## 微信公众号 | |
| 2 | - | |
| 3 | -参考文章:https://www.yuque.com/docs/share/0e2966dd-89f8-4b69-980d-b876168725df | |
| 4 | - | |
| 5 | -① 访问 social-login.html 选择【微信公众号】 | |
| 6 | - | |
| 7 | -② 微信公众号授权完成后,跳转回 social-login2.html,输入手机号 + 密码,进行绑定 | |
| 8 | - | |
| 9 | -## 微信小程序 | |
| 10 | - | |
| 11 | -参考文章:https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa | |
| 12 | - | |
| 13 | -① 暂时使用 mini-program-test 项目 |
yudao-server/src/main/resources/static/pay_wx_pub.html deleted
| 1 | -<!DOCTYPE html> | |
| 2 | -<html> | |
| 3 | -<head> | |
| 4 | - <meta charset="UTF-8"> | |
| 5 | - <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/> | |
| 6 | - <title>支付测试页</title> | |
| 7 | - <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> | |
| 8 | - <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> | |
| 9 | -</head> | |
| 10 | -<body> | |
| 11 | -<div>点击如下按钮,发起支付的测试</div> | |
| 12 | -<div> | |
| 13 | - <button id="wx_pub">微信公众号</button> | |
| 14 | -</div> | |
| 15 | -</body> | |
| 16 | -<script> | |
| 17 | - let shopOrderId = undefined; | |
| 18 | - let payOrderId = undefined; | |
| 19 | - let server = 'http://127.0.0.1:48080'; | |
| 20 | - // let server = 'http://niubi.natapp1.cc'; | |
| 21 | - // TODO openid | |
| 22 | - let openid = "ockUAwIZ-0OeMZl9ogcZ4ILrGba0"; | |
| 23 | - $(function() { | |
| 24 | - // 获得 JsapiTicket | |
| 25 | - // 参考 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html 文档 | |
| 26 | - $.ajax({ | |
| 27 | - url: server + "/app-api/wx/mp/create-jsapi-signature?url=" + document.location.href, | |
| 28 | - method: 'POST', | |
| 29 | - headers: { | |
| 30 | - 'tenant-id': 1 | |
| 31 | - }, | |
| 32 | - success: function( result ) { | |
| 33 | - if (result.code !== 0) { | |
| 34 | - alert('获取 JsapiTicket 失败,原因:' + result.msg) | |
| 35 | - return; | |
| 36 | - } | |
| 37 | - var jsapiTicket = result.data; | |
| 38 | - jsapiTicket.jsApiList = ['chooseWXPay']; | |
| 39 | - jsapiTicket.debug = false; | |
| 40 | - | |
| 41 | - // 初始化 JS | |
| 42 | - wx.config(jsapiTicket); | |
| 43 | - } | |
| 44 | - }); | |
| 45 | - | |
| 46 | - // 自动发起商城订单编号 | |
| 47 | - $.ajax({ | |
| 48 | - url: server + "/app-api/shop/order/create", | |
| 49 | - method: 'POST', | |
| 50 | - success: function( result ) { | |
| 51 | - if (result.code !== 0) { | |
| 52 | - alert('创建商城订单失败,原因:' + result.msg) | |
| 53 | - return; | |
| 54 | - } | |
| 55 | - shopOrderId = result.data.id; | |
| 56 | - payOrderId = result.data.payOrderId; | |
| 57 | - console.log("商城订单:" + shopOrderId) | |
| 58 | - console.log("支付订单:" + payOrderId) | |
| 59 | - } | |
| 60 | - }) | |
| 61 | - }) | |
| 62 | - | |
| 63 | - // 微信公众号 | |
| 64 | - $( "#wx_pub").on( "click", function() { | |
| 65 | - if (typeof WeixinJSBridge == "undefined") { | |
| 66 | - // if (document.addEventListener) { | |
| 67 | - // document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); | |
| 68 | - // } else if (document.attachEvent) { | |
| 69 | - // document.attachEvent('WeixinJSBridgeReady', onBridgeReady); | |
| 70 | - // document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); | |
| 71 | - // } | |
| 72 | - alert('微信支付,只支持在微信客户端中使用!'); | |
| 73 | - return; | |
| 74 | - } | |
| 75 | - if (navigator.userAgent.indexOf('wechatdevtools') >= 0) { | |
| 76 | - alert('微信支付,无法在微信开发者工具中使用!请使用微信客户端!'); | |
| 77 | - return; | |
| 78 | - } | |
| 79 | - | |
| 80 | - // 提交支付 | |
| 81 | - // 参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 文档 | |
| 82 | - // 参考 https://segmentfault.com/a/1190000020704650 文档 | |
| 83 | - $.ajax({ | |
| 84 | - url: server + "/app-api/pay/order/submit", | |
| 85 | - method: 'POST', | |
| 86 | - dataType: "json", | |
| 87 | - contentType: "application/json", | |
| 88 | - data: JSON.stringify({ | |
| 89 | - "id": payOrderId, | |
| 90 | - "channelCode": 'wx_pub', | |
| 91 | - "channelExtras": { | |
| 92 | - "openid": openid | |
| 93 | - } | |
| 94 | - }), | |
| 95 | - success: function( result ) { | |
| 96 | - if (result.code !== 0) { | |
| 97 | - alert('提交支付订单失败,原因:' + result.msg) | |
| 98 | - return; | |
| 99 | - } | |
| 100 | - alert('点击确定,开始微信支付'); | |
| 101 | - // 开始调用微信支付 | |
| 102 | - let data = result.data.invokeResponse; | |
| 103 | - wx.chooseWXPay({ | |
| 104 | - timestamp: data.timeStamp, | |
| 105 | - nonceStr: data.nonceStr, | |
| 106 | - package: data.packageValue, | |
| 107 | - signType: data.signType, | |
| 108 | - paySign: data.paySign, | |
| 109 | - success: function (res) { | |
| 110 | - alert(JSON.stringify(res)); | |
| 111 | - }, | |
| 112 | - error: function(e) { | |
| 113 | - alert(JSON.stringify(e)); | |
| 114 | - } | |
| 115 | - }); | |
| 116 | - } | |
| 117 | - }) | |
| 118 | - }); | |
| 119 | -</script> | |
| 120 | -</html> |