index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
  7. <title>向商家付款</title>
  8. <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  9. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  10. <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>
  11. <script type="text/javascript">
  12. function adapter() {
  13. const dpWidth = document.documentElement.clientWidth
  14. const rootFonstSize = (dpWidth * 100) / 750
  15. if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
  16. document.documentElement.style.fontSize = rootFonstSize + 'px'
  17. } else {
  18. document.documentElement.style.fontSize = dpWidth
  19. }
  20. }
  21. adapter()
  22. window.onresize = adapter
  23. </script>
  24. <style>
  25. body {
  26. background: #F8F8F8;
  27. }
  28. * {
  29. margin: 0;
  30. padding: 0;
  31. }
  32. #pay {
  33. width: 100%;
  34. height: 100%;
  35. display: flex;
  36. flex-direction: column;
  37. align-items: center;
  38. }
  39. /* .pay_header {
  40. width: 100%;
  41. height: 1.76rem;
  42. display: flex;
  43. justify-content: center;
  44. align-items: center;
  45. background: #FFFFFF;
  46. }
  47. .pay-header-nei {
  48. width: 6.86rem;
  49. height: 100%;
  50. display: flex;
  51. }
  52. .pay-header-nei-in {
  53. margin-top: 1.07rem;
  54. font-size: 0.36rem;
  55. font-weight: 500;
  56. color: #333333;
  57. text-align: center;
  58. margin-left: 2.17rem;
  59. }
  60. .pay-header-nei-left {
  61. width: 0.36rem;
  62. height: 0.36rem;
  63. margin-top: 1.17rem;
  64. }
  65. .pay-header-nei-left img {
  66. width: 100%;
  67. height: 100%;
  68. display: block;
  69. } */
  70. .pay_name {
  71. width: 6.2rem;
  72. height: 0.9rem;
  73. margin: 0.5rem 0 0.5rem 0;
  74. display: flex;
  75. justify-content: space-between;
  76. align-items: center;
  77. }
  78. .pay_name_left {
  79. height: 100%;
  80. display: flex;
  81. flex-direction: column;
  82. align-items: center;
  83. justify-content: space-between;
  84. }
  85. .pay_name_left_top {
  86. font-size: 0.34rem;
  87. font-weight: 600;
  88. color: #333333;
  89. }
  90. .pay_name_left_bottom {
  91. font-size: 0.26rem;
  92. font-weight: 400;
  93. color: #999999;
  94. }
  95. .pay_name_right {
  96. width: 0.9rem;
  97. height: 0.9rem;
  98. background: #D8D8D8;
  99. border: 0.01rem solid #979797;
  100. }
  101. .pay_name_right img {
  102. width: 100%;
  103. height: 100%;
  104. display: block;
  105. }
  106. input::-webkit-input-placeholder {
  107. /*WebKit browsers*/
  108. color: #999999;
  109. font-size: 0.4rem;
  110. margin-top: 13rem;
  111. }
  112. input::-moz-input-placeholder {
  113. /*Mozilla Firefox*/
  114. color: #999999;
  115. font-size: 0.4rem;
  116. margin-top: 13rem;
  117. }
  118. input::-ms-input-placeholder {
  119. /*Internet Explorer*/
  120. color: #999999;
  121. font-size: 0.4rem;
  122. margin-top: 13rem;
  123. }
  124. .pay_input {
  125. width: 100%;
  126. height: 3.48rem;
  127. background: #FFFFFF;
  128. border-radius: 0.16rem;
  129. display: flex;
  130. flex-direction: column;
  131. align-items: center;
  132. justify-content: space-between;
  133. }
  134. .pay_input-top {
  135. width: 6.36rem;
  136. height: 2.63rem;
  137. border-bottom: 0.01rem solid #E5E5E5;
  138. }
  139. .pay_input-top-shifu {
  140. margin: 0.54rem 0 0 0.41rem;
  141. font-size: 0.26rem;
  142. font-weight: 400;
  143. color: #333333;
  144. }
  145. .pay_input-top-manei {
  146. margin: 0.12rem 0 0 0.41rem;
  147. height: 1.19rem;
  148. display: flex;
  149. align-items: center;
  150. }
  151. .pay_input-top-manei span {
  152. font-size: 0.64rem;
  153. font-weight: 600;
  154. color: #333333;
  155. }
  156. .pay_input-top-manei input {
  157. width: 100%;
  158. height: 0.99rem;
  159. border: none;
  160. outline: medium;
  161. margin-left: 0.27rem;
  162. font-size: 1.1rem;
  163. }
  164. .pay_input-bottom {
  165. width: 6.36rem;
  166. height: 0.84rem;
  167. display: flex;
  168. justify-content: center;
  169. align-items: center;
  170. font-size: 0.24rem;
  171. font-weight: 400;
  172. color: #FE0032;
  173. }
  174. .pay_input-bottom-left {
  175. width: 0.3rem;
  176. height: 0.3rem;
  177. }
  178. .pay_input-bottom-left img {
  179. width: 100%;
  180. height: 100%;
  181. display: block;
  182. }
  183. .pay_input-bottom-right {
  184. margin-left: 0.08rem;
  185. }
  186. .pay_bottom {
  187. width: 6.86rem;
  188. height: 0.88rem;
  189. background: linear-gradient(179deg, #FE7577 0%, #FE0032 100%);
  190. border-radius: 1rem;
  191. margin-top: 7.5rem;
  192. text-align: center;
  193. line-height: 0.88rem;
  194. font-size: 0.32rem;
  195. font-weight: 600;
  196. color: #FFFFFF;
  197. }
  198. </style>
  199. </head>
  200. <body>
  201. <div id="pay">
  202. <!-- <div class="pay_header">
  203. <div class="pay-header-nei">
  204. <div class="pay-header-nei-left">
  205. <img src="https://cdn.bjtdba.com/vod/image/2022-10-18/20221018150019704.png" alt="">
  206. </div>
  207. <div class="pay-header-nei-in">向商家付款</div>
  208. </div>
  209. </div> -->
  210. <div class="pay_name">
  211. <div class="pay_name_left">
  212. <div class="pay_name_left_top">付款给商家</div>
  213. <div class="pay_name_left_bottom" id="mername"></div>
  214. </div>
  215. <div class="pay_name_right">
  216. <img src="" id="merbanner" alt="">
  217. </div>
  218. </div>
  219. <div class="pay_input">
  220. <div class="pay_input-top">
  221. <div class="pay_input-top-shifu">实付金额</div>
  222. <div class="pay_input-top-manei">
  223. <span>¥</span>
  224. <input type="text" id="textInput" placeholder="请输入金额" maxLength="8"
  225. onkeyup="value=value.replace(/[^\d^\.]+/g,'')" oninput="myFunction()">
  226. </div>
  227. </div>
  228. <div class="pay_input-bottom">
  229. <div class="pay_input-bottom-left">
  230. <img src="https://cdn.bjtdba.com/vod/image/2022-10-18/20221018152247280.png" alt="">
  231. </div>
  232. <div class="pay_input-bottom-right">储蓄保全国合作伙伴招募中</div>
  233. </div>
  234. </div>
  235. <div class="pay_bottom" onclick="btn()">立即支付</div>
  236. </div>
  237. <script type="text/javascript">
  238. var ua = window.navigator.userAgent.toLowerCase();
  239. var Wxuserinfor;//微信user
  240. var ZfBuserinfor;//支付宝user
  241. var mer_id;//商户Id
  242. var money;//金额
  243. var trade_type;//微信|支付宝
  244. //动态获取金额
  245. function myFunction() {
  246. money = $("#textInput").val()
  247. }
  248. //获取参数
  249. var userpay = parseParms()
  250. get_mer_info()//调用二维码接口
  251. //跳转微信授权登陆
  252. let local = window.location.href;
  253. if (local.split(':')[0] == 'http') {
  254. local = local.split(':')[0] + 's:' + local.split(':')[1]
  255. console.log(local)
  256. } else {
  257. local = window.location.href;
  258. }
  259. let wechatHrefurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + 'wx66f5f8def2b755da' +
  260. "&redirect_uri=" + encodeURIComponent(local) +
  261. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  262. //不是微信并且不是支付宝
  263. if (ua.match(/MicroMessenger/i) != "micromessenger" && ua.match(/AlipayClient/i) != "alipayclient") {
  264. alert('请使用微信或支付宝扫码');
  265. }
  266. //判断是不是微信
  267. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  268. trade_type = "WX"
  269. if (userpay.code == "" || userpay.code == null) {
  270. window.location.href = wechatHrefurl;
  271. } else {
  272. user()
  273. }
  274. }
  275. //判断是不是支付宝
  276. if (ua.match(/AlipayClient/i) == "alipayclient") {
  277. trade_type = "ALI"
  278. if (userpay.auth_code == null || userpay.auth_code == "") {
  279. local = local.replace('#/', '');
  280. let aliHrefurl =
  281. "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2021001197698727&scope=auth_user&redirect_uri=" +
  282. encodeURIComponent(local)
  283. window.location.href = aliHrefurl;
  284. } else {
  285. aliUser()
  286. }
  287. }
  288. //获取二维码
  289. function get_mer_info() {
  290. $.post("https://test.bjtdba.com/api/merchantcheckcodeid/" + userpay.id, function (data) {
  291. if (data.status == 200) {
  292. if (data.data.code == 1) {
  293. mer_id = data.data.mer_id; //获取店铺id
  294. getQrcode(data)
  295. } else {
  296. //如果不可以收款清空body
  297. alert("商户未开通")
  298. $("body").html("")
  299. }
  300. } else {
  301. alert(data.message)
  302. //400收款清空body
  303. $("body").html("")
  304. }
  305. })
  306. }
  307. //获取商户信息
  308. function getQrcode(event) {
  309. $.get("https://test.bjtdba.com/api/merchant/get_mer_info", { mer_id: event.data.mer_id }, function (data) {
  310. if (data.status == 200) {
  311. $("#mername").text(data.data.mer_name)//商户头像
  312. $("#merbanner").attr("src", data.data.mer_banner)//商户头像
  313. }
  314. })
  315. }
  316. //wx获取用户信息
  317. function user() {
  318. $.get("https://test.bjtdba.com/api/wechat/wechatAuthorization", { code: userpay.code }, function (data) {
  319. if (data.status == 200) {
  320. Wxuserinfor = data.data
  321. }
  322. })
  323. }
  324. //支付宝获取用户信息
  325. function aliUser() {
  326. $.get("https://test.bjtdba.com/api/ali/aliSign", { code: userpay.auth_code }, function (data) {
  327. if (data.status == 200) {
  328. ZfBuserinfor = data.data
  329. }
  330. })
  331. }
  332. //点击支付创建订单
  333. function btn() {
  334. //开始loading
  335. let data = {
  336. mer_id: mer_id,
  337. money: money,
  338. trade_type: trade_type,
  339. }
  340. if (trade_type == 'WX') {
  341. data.avatar = Wxuserinfor.headimgurl;
  342. data.nickname = Wxuserinfor.nickname;
  343. data.pay_user_id = Wxuserinfor.unionid;
  344. } else if (trade_type == 'ALI') {
  345. data.pay_user_id = ZfBuserinfor.user_id;
  346. }
  347. //支付宝user_id
  348. if (userpay.user_id) {
  349. data.user_id = userpay.user_id;
  350. }
  351. $.post("https://test.bjtdba.com/api/merchant/createOrder", { money: money, mer_id: data.mer_id, trade_type: data.trade_type, avatar: data.avatar, nickname: data.nickname, pay_user_id: data.pay_user_id }, function (data) {
  352. console.log(data, '订单创建')
  353. if (data.status == 200) {
  354. jsapiPay(data.data)//订单数据
  355. } else {
  356. alert(data.message)
  357. //结束loading
  358. }
  359. })
  360. }
  361. // 创建订单成功后调用接口区分微信支付还是支付宝,分别调用支付
  362. function jsapiPay(event) {
  363. $.post("https://test.bjtdba.com/api/merchant/jsapiPay", { out_trade_no: event.out_trade_no, type: 'HF' }, function (data) {
  364. console.log(data, '订单创建完区分WZ')
  365. if (data.status == 200) {
  366. if (trade_type == 'WX') {
  367. console.log("WX")
  368. paymentByWxpay(data.data)
  369. } else if (trade_type == 'ALI') {
  370. paymentByAlipay(data.data)
  371. }
  372. } else {
  373. alert(data.message)
  374. //结束loading
  375. }
  376. })
  377. }
  378. // 微信支付
  379. function paymentByWxpay(data) {
  380. console.log(data)
  381. var appid = ''
  382. var timestamp = ''
  383. var noncestr = ''
  384. var paysign = ''
  385. var signType = ''
  386. var pay_info = JSON.parse(data.expend.pay_info)
  387. var package1 = ''
  388. appid = pay_info.appId
  389. timestamp = pay_info.timeStamp
  390. noncestr = pay_info.nonceStr
  391. paysign = pay_info.paySign
  392. signType = pay_info.signType
  393. package1 = pay_info.package
  394. wx.config({
  395. debug: false, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印
  396. appId: appid, // 必填,公众号的唯一标识
  397. timestamp: timestamp, // 必填,生成签名的时间戳
  398. nonceStr: noncestr, // 必填,生成签名的随机串
  399. signature: paysign,// 必填,签名
  400. jsApiList: ['chooseWXPay'], // 必填,需要使用的 JS 接口列表 支付
  401. openTagList: ["wx-open-launch-weapp"] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  402. });
  403. wx.ready(function () {
  404. wx.checkJsApi({
  405. jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  406. success: function (res) {
  407. console.log(res, '微信支付返回值');
  408. },
  409. fail: function (res) {
  410. console.log(res, '错误');
  411. }
  412. });
  413. wx.chooseWXPay({
  414. appId: appid,
  415. timestamp: timestamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  416. nonceStr: noncestr, // 支付签名随机串,不长于 32 位
  417. package: package1, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  418. signType: signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  419. paySign: paysign, // 支付签名
  420. success: (res) => {
  421. // 支付成功后的回调函数
  422. // location.replace("./Paytocomplete.html")
  423. // // window.location.replace("./Paytocomplete.html")
  424. // window.location.href = path + "./Paytocomplete.html";
  425. //结束loading
  426. alert(res,'支付成功');
  427. },
  428. // cancel: function (res) {
  429. // 结束loading
  430. // alert('取消支付');
  431. // },
  432. fail: function (res) {
  433. //结束loading
  434. alert('支付失败');
  435. }
  436. });
  437. });
  438. wx.error(function (res) {
  439. console.log('res', res);
  440. alert(111)
  441. });
  442. }
  443. //支付宝支付
  444. function paymentByAlipay(data) {
  445. var pay_info = JSON.parse(data.expend.pay_info)
  446. AlipayJSBridge.call("tradePay", {
  447. tradeNO: pay_info.tradeNO, // 必传,此使用方式下该字段必传
  448. }, function (result) {
  449. if (result.resultCode == '9000') {
  450. location.replace("./Paytocomplete.html");
  451. money = '';
  452. } else {
  453. alert("支付失败")
  454. }
  455. });
  456. }
  457. //hash参数转对象
  458. function parseParms() {
  459. var str = window.location.href.split("?")[1];
  460. var pieces = str.split("&"), data = {}, i, parts;
  461. // process each query pair
  462. for (i = 0; i < pieces.length; i++) {
  463. parts = pieces[i].split("=");
  464. if (parts.length < 2) {
  465. parts.push("");
  466. }
  467. data[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
  468. }
  469. return data
  470. }
  471. </script>
  472. </body>
  473. </html>