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: 0.9rem;
  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="请输入金额" autofocus="autofocus" 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. var out_trade_no;//订单
  245. //动态获取金额
  246. function myFunction() {
  247. money = $("#textInput").val()
  248. }
  249. //获取参数
  250. var userpay = parseParms()
  251. get_mer_info()//调用二维码接口
  252. //跳转微信授权登陆
  253. let local = window.location.href;
  254. if (local.split(':')[0] == 'http') {
  255. local = local.split(':')[0] + 's:' + local.split(':')[1]
  256. console.log(local)
  257. } else {
  258. local = window.location.href;
  259. }
  260. // wx66f5f8def2b755da
  261. let wechatHrefurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + 'wx6ac5b448b75117d9' +
  262. "&redirect_uri=" + encodeURIComponent(local) +
  263. "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  264. //不是微信并且不是支付宝
  265. if (ua.match(/MicroMessenger/i) != "micromessenger" && ua.match(/AlipayClient/i) != "alipayclient") {
  266. alert('请使用微信或支付宝扫码');
  267. }
  268. //判断是不是微信
  269. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  270. trade_type = "WX"
  271. if (userpay.code == "" || userpay.code == null) {
  272. window.location.href = wechatHrefurl;
  273. } else {
  274. user()
  275. }
  276. }
  277. //判断是不是支付宝
  278. if (ua.match(/AlipayClient/i) == "alipayclient") {
  279. trade_type = "ALI"
  280. if (userpay.auth_code == null || userpay.auth_code == "") {
  281. local = local.replace('#/', '');
  282. let aliHrefurl =
  283. "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2021001197698727&scope=auth_user&redirect_uri=" +
  284. encodeURIComponent(local)
  285. window.location.href = aliHrefurl;
  286. } else {
  287. aliUser()
  288. }
  289. }
  290. //获取二维码
  291. function get_mer_info() {
  292. $.post("https://app.bjtdba.com/api/merchantcheckcodeid/" + userpay.id, function (data) {
  293. if (data.status == 200) {
  294. if (data.data.code == 1) {
  295. mer_id = data.data.mer_id; //获取店铺id
  296. getQrcode(data)
  297. } else {
  298. //如果不可以收款清空body
  299. alert("商户未开通")
  300. $("body").html("")
  301. }
  302. } else {
  303. alert(data.message)
  304. //400收款清空body
  305. $("body").html("")
  306. }
  307. })
  308. }
  309. //获取商户信息
  310. function getQrcode(event) {
  311. $.get("https://app.bjtdba.com/api/merchant/get_mer_info", { mer_id: event.data.mer_id }, function (data) {
  312. if (data.status == 200) {
  313. $("#mername").text(data.data.mer_name)//商户名称
  314. $("#merbanner").attr("src", data.data.mer_banner)//商户头像
  315. }
  316. })
  317. }
  318. //wx获取用户信息
  319. function user() {
  320. $.get("https://app.bjtdba.com/api/wechat/wechatAuthorization", { code: userpay.code }, function (data) {
  321. if (data.status == 200) {
  322. Wxuserinfor = data.data
  323. }
  324. })
  325. }
  326. //支付宝获取用户信息
  327. function aliUser() {
  328. $.get("https://app.bjtdba.com/api/ali/aliSign", { code: userpay.auth_code }, function (data) {
  329. if (data.status == 200) {
  330. ZfBuserinfor = data.data
  331. }
  332. })
  333. }
  334. //点击支付创建订单
  335. function btn() {
  336. //开始loading
  337. let data = {
  338. mer_id: mer_id,
  339. money: money,
  340. trade_type: trade_type,
  341. }
  342. if (trade_type == 'WX') {
  343. data.avatar = Wxuserinfor.headimgurl;
  344. data.nickname = Wxuserinfor.nickname;
  345. data.pay_user_id = Wxuserinfor.unionid;
  346. } else if (trade_type == 'ALI') {
  347. data.pay_user_id = ZfBuserinfor.user_id;
  348. }
  349. //支付宝user_id
  350. if (userpay.user_id) {
  351. data.user_id = userpay.user_id;
  352. }
  353. $.post("https://app.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) {
  354. console.log(data, '订单创建')
  355. if (data.status == 200) {
  356. out_trade_no = data.data.out_trade_no
  357. jsapiPay(data.data)//订单数据
  358. } else {
  359. alert(data.message)
  360. //结束loading
  361. }
  362. })
  363. }
  364. // 创建订单成功后调用接口区分微信支付还是支付宝,分别调用支付
  365. function jsapiPay(event) {
  366. $.post("https://app.bjtdba.com/api/merchant/jsapiPay", { out_trade_no: event.out_trade_no, type: 'HF' }, function (data) {
  367. console.log(data, '订单创建完区分WZ')
  368. if (data.status == 200) {
  369. if (trade_type == 'WX') {
  370. console.log("WX")
  371. paymentByWxpay(data.data)
  372. } else if (trade_type == 'ALI') {
  373. paymentByAlipay(data.data)
  374. }
  375. } else {
  376. alert(data.message)
  377. //结束loading
  378. }
  379. })
  380. }
  381. // 微信支付
  382. function paymentByWxpay(data) {
  383. console.log(data)
  384. var appid = ''
  385. var timestamp = ''
  386. var noncestr = ''
  387. var paysign = ''
  388. var signType = ''
  389. var pay_info = JSON.parse(data.expend.pay_info)
  390. var package1 = ''
  391. appid = pay_info.appId
  392. timestamp = pay_info.timeStamp
  393. noncestr = pay_info.nonceStr
  394. paysign = pay_info.paySign
  395. signType = pay_info.signType
  396. package1 = pay_info.package
  397. wx.config({
  398. debug: false, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印
  399. appId: appid, // 必填,公众号的唯一标识
  400. timestamp: timestamp, // 必填,生成签名的时间戳
  401. nonceStr: noncestr, // 必填,生成签名的随机串
  402. signature: paysign,// 必填,签名
  403. jsApiList: ['chooseWXPay'], // 必填,需要使用的 JS 接口列表 支付
  404. openTagList: ["wx-open-launch-weapp"] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  405. });
  406. wx.ready(function () {
  407. wx.checkJsApi({
  408. jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  409. success: function (res) {
  410. console.log(res, '微信支付返回值');
  411. },
  412. fail: function (res) {
  413. console.log(res, '错误');
  414. }
  415. });
  416. wx.chooseWXPay({
  417. appId: appid,
  418. timestamp: timestamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  419. nonceStr: noncestr, // 支付签名随机串,不长于 32 位
  420. package: package1, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  421. signType: signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  422. paySign: paysign, // 支付签名
  423. success: (res) => {
  424. // 支付成功后的回调函数
  425. // location.replace("./Paytocomplete.html")
  426. // window.location.replace("./Paytocomplete.html")
  427. // window.location.href = path + "./Paytocomplete.html";
  428. //结束loading
  429. },
  430. // cancel: function (res) {
  431. // 结束loading
  432. // alert('取消支付');
  433. // },
  434. fail: function (res) {
  435. //结束loading
  436. alert('支付失败');
  437. }
  438. });
  439. });
  440. wx.error(function (res) {
  441. console.log('res', res);
  442. });
  443. }
  444. //支付宝支付
  445. function paymentByAlipay(data) {
  446. var pay_info = JSON.parse(data.expend.pay_info)
  447. AlipayJSBridge.call("tradePay", {
  448. tradeNO: pay_info.tradeNO, // 必传,此使用方式下该字段必传
  449. }, function (result) {
  450. if (result.resultCode == '9000') {
  451. location.replace("./Paytocomplete.html?out_trade_no=" + out_trade_no);
  452. money = '';
  453. } else {
  454. alert("支付失败")
  455. }
  456. });
  457. }
  458. //hash参数转对象
  459. function parseParms() {
  460. var str = window.location.href.split("?")[1];
  461. var pieces = str.split("&"), data = {}, i, parts;
  462. // process each query pair
  463. for (i = 0; i < pieces.length; i++) {
  464. parts = pieces[i].split("=");
  465. if (parts.length < 2) {
  466. parts.push("");
  467. }
  468. data[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
  469. }
  470. return data
  471. }
  472. </script>
  473. </body>
  474. </html>