pay.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. var smoney = '';
  2. var sbookid = commont.getQueryString('book_id', window.location.search, '&') || '';
  3. var schapterid = commont.getQueryString('chapter_id', window.location.search, '&') || '';
  4. var float_id = commont.getQueryString('float_id', window.location.search, '&') || '';
  5. var toUrl = $('#tourl').val();
  6. var payurl = '/api/activity/pay';
  7. $(function () {
  8. /* if (typeof cpslog == 'undefined') {
  9. window.cpslog = function (arr, callback) {
  10. console.log('cps_log.js 未加载');
  11. }
  12. }*/
  13. /**
  14. * 充值按钮
  15. */
  16. var paystop = true;
  17. $('.re_but_box a').click(function () {
  18. if (!paystop) {
  19. return false
  20. }
  21. paystop = false;
  22. var $this = $(this),
  23. goodsId = $this.data('goods-id'),
  24. goodsaId = $this.data('goods-aid'),
  25. goodsCategory = $this.data('goods-category');
  26. smoney = $this.data('money');
  27. if (parseInt(goodsCategory) == 1){
  28. goodsCategory = 'alipay';
  29. }else{
  30. goodsCategory = 'wechat';
  31. }
  32. cpslog([702, {
  33. adid: goodsId,
  34. map: {
  35. book_id: sbookid,
  36. chapter_id: schapterid,
  37. money: smoney
  38. }
  39. }, goodsCategory]);
  40. var data = {
  41. id: goodsId,
  42. aid:goodsaId,
  43. category: goodsCategory,
  44. float_id: float_id,
  45. money: smoney
  46. };
  47. $('.js_loading').show();
  48. $.ajax({
  49. url: payurl,
  50. data: data,
  51. type: 'POST',
  52. timeout: 35000,
  53. success: function (data) {
  54. paystop = true;
  55. console.log(data);
  56. if (data.code) {
  57. switch (goodsCategory) {
  58. case 'wechat': //微信公众号支付
  59. if (data.data) {
  60. var oJsonObj = JSON.parse(data.data.json);
  61. wechatPay(oJsonObj, goodsId);
  62. cpslog([703, {
  63. map: {
  64. package: oJsonObj.package,
  65. xdtype: goodsCategory,
  66. result: 1,
  67. book_id: sbookid,
  68. adid: goodsId,
  69. chapter_id: schapterid,
  70. money: smoney
  71. }
  72. }, 'xdjg']);
  73. } else {
  74. consoleMain(data.msg);
  75. }
  76. break;
  77. default: //alipay 支付宝 //wechath5 微信H5支付
  78. break;
  79. }
  80. } else {
  81. consoleMain(data.msg);
  82. cpslog([703, {
  83. map: {
  84. xdtype: goodsCategory,
  85. result: 2,
  86. book_id: sbookid,
  87. adid: goodsId,
  88. chapter_id: schapterid,
  89. money: smoney
  90. }
  91. }, 'xdjg']);
  92. }
  93. },
  94. error: function (XMLHttpRequest, textStatus, errorThrown) {
  95. consoleMain('异常错误,请刷新页面重试');
  96. cpslog([703, {
  97. map: {
  98. xdtype: goodsCategory,
  99. result: 4,
  100. book_id: sbookid,
  101. adid: goodsId,
  102. chapter_id: schapterid,
  103. money: smoney,
  104. XMLHttpRequest: XMLHttpRequest.status,
  105. textStatus: textStatus,
  106. errorThrown: errorThrown
  107. }
  108. }, 'xdjg']);
  109. paystop = true;
  110. },
  111. complete: function () {
  112. $('.js_loading').hide();
  113. }
  114. })
  115. });
  116. /**
  117. * 处理默认充值商品自动下单
  118. */
  119. var check = $('.re_but_box a.check');
  120. if(check.length){
  121. check.click();
  122. }
  123. $(".re_but_box .img_give_act").click(function () {
  124. var payurl = $(this).data("api-url");
  125. $.ajax({
  126. url: payurl,
  127. data: {},
  128. type: 'POST',
  129. timeout: 35000,
  130. success: function (data) {
  131. if (data.code ==0) {
  132. alert(data.msg);
  133. } else {
  134. $("#clear_alert_box2").show();
  135. }
  136. }
  137. });
  138. });
  139. });
  140. /**
  141. * 微信公众号支付
  142. * @param json 微信支付相关数据
  143. * @param goodsId 商品id
  144. */
  145. function wechatPay(json, goodsId) {
  146. if (typeof WeixinJSBridge == "undefined") {
  147. if (document.addEventListener) {
  148. document.addEventListener('WeixinJSBridgeReady', function () {
  149. onBridgeReady(json, goodsId);
  150. }, false);
  151. } else if (document.attachEvent) {
  152. document.attachEvent('WeixinJSBridgeReady', function () {
  153. onBridgeReady(json, goodsId);
  154. });
  155. document.attachEvent('onWeixinJSBridgeReady', function () {
  156. onBridgeReady(json, goodsId);
  157. });
  158. }
  159. } else {
  160. onBridgeReady(json, goodsId);
  161. }
  162. }
  163. /**
  164. * 微信内唤起充值窗口
  165. * @param json
  166. * @param goodsId
  167. */
  168. function onBridgeReady(json, goodsId) {
  169. WeixinJSBridge.invoke(
  170. 'getBrandWCPayRequest', json,
  171. function (res) {
  172. switch (res.err_msg) {
  173. case 'get_brand_wcpay_request:ok': //成功
  174. //consoleMain('充值成功');
  175. cpslog([703, {
  176. map: {
  177. cztype: 'wechat',
  178. package: json.package,
  179. result: 1,
  180. book_id: sbookid,
  181. chapter_id: schapterid,
  182. money: smoney,
  183. res: res
  184. }
  185. }, 'czjg']);
  186. /* if (toUrl) { //回跳到最近阅读页
  187. window.location.href = toUrl;
  188. }else{
  189. // 没有回跳地址,弹窗窗口
  190. $('.bind_alert_box').show();
  191. }*/
  192. if (toUrl) { //回跳链接
  193. $('.js_recent_url').val(toUrl);
  194. }
  195. $('.dialoge_bg').show();
  196. break;
  197. case 'get_brand_wcpay_request:cancel': //取消
  198. consoleMain('您已取消充值');
  199. wechatCancel(goodsId);
  200. cpslog([703, {
  201. map: {
  202. cztype: 'wechat',
  203. package: json.package,
  204. result: 2,
  205. book_id: sbookid,
  206. chapter_id: schapterid,
  207. money: smoney,
  208. res: res
  209. }
  210. }, 'czjg']);
  211. break;
  212. case 'get_brand_wcpay_request:fail': //失败
  213. consoleMain('充值失败,请尝试重新充值或试试其他充值方式');
  214. wechatCancel(goodsId);
  215. cpslog([703, {
  216. map: {
  217. cztype: 'wechat',
  218. package: json.package,
  219. result: 3,
  220. book_id: sbookid,
  221. chapter_id: schapterid,
  222. money: smoney,
  223. res: res
  224. }
  225. }, 'czjg']);
  226. break;
  227. default:
  228. console.log(res);
  229. wechatCancel(goodsId);
  230. if (typeof res.err_msg == 'undefined' && res.errMsg == 'chooseWXPay:fail, the permission value is offline verifying') {
  231. consoleMain('充值失败,原因:模拟器无法充值');
  232. } else {
  233. consoleMain('异常错误,请刷新页面重试');
  234. cpslog([703, {
  235. map: {
  236. cztype: 'wechat',
  237. package: json.package,
  238. result: 4,
  239. book_id: sbookid,
  240. chapter_id: schapterid,
  241. money: smoney,
  242. res: res
  243. }
  244. }, 'czjg']);
  245. }
  246. break;
  247. }
  248. }
  249. );
  250. }
  251. $(".suc_close").click(function () {
  252. $('.dialoge_bg').hide();
  253. window.location.reload();
  254. });
  255. //region 弹框事件
  256. $('.bind_alert_off').click(function () {
  257. var search = window.location.search.replace('?', '');
  258. var aParams = search.split('&');
  259. var aParamsNoGoods = [];
  260. for (var i = 0; i < aParams.length; i++) {
  261. if (aParams[i].indexOf('goods_id=') == -1) {
  262. aParamsNoGoods.push(aParams[i]);
  263. }
  264. }
  265. var strParams = aParamsNoGoods.join('&');
  266. var href = window.location.pathname + '?' + strParams;
  267. window.location.href = href;
  268. });
  269. $('.alert_but_1, .suc_btn_look').click(function () {
  270. window.location.href = $('.js_recent_url').val();
  271. });
  272. //endregion
  273. function wechatCancel(goodsId) {
  274. }