pay.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 toUrl = $('#tourl').val();
  5. $(function () {
  6. $('.show_pay_close').click(function(){
  7. $('.show_pay_box').hide();
  8. });
  9. $(document).on('click','.select_pay',function(){
  10. $('input[name="payment_id"]').removeAttr("checked");
  11. $(this).find('input[name="payment_id"]').prop("checked", 'checked');
  12. });
  13. $('.re_but_box a').click(function () {
  14. $('.show_pay_box').show();
  15. });
  16. /**
  17. * 充值按钮
  18. */
  19. var paystop = true;
  20. $(document).on('click','.pay_btn',function(){
  21. if (!paystop) {
  22. return false
  23. }
  24. paystop = false;
  25. var $this = $(this),
  26. goodsId = $this.data('goods-id'),
  27. goodsaId = $this.data('goods-aid'),
  28. goodsCategory = $this.data('goods-category');
  29. smoney = $this.data('money');
  30. business_line = parseInt($this.data('business_line'));
  31. payment_id = $('input[name="payment_id"]:checked').val();
  32. cpslog([702, {
  33. adid: goodsId,
  34. map: {
  35. book_id: sbookid,
  36. chapter_id: schapterid,
  37. money: smoney
  38. }
  39. }, business_line]);
  40. var data = {
  41. id: goodsId,
  42. aid:goodsaId,
  43. business: business_line,
  44. payment:payment_id,
  45. category: goodsCategory
  46. };
  47. $('.js_loading').show();
  48. $.ajax({
  49. url: '/clientwebapi/recharge/pay',
  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 (business_line) {
  58. case 1: //微信公众号支付
  59. if (data.data) {
  60. if(data.data.callback !== undefined){
  61. eval(data.data.callback);
  62. }
  63. cpslog([703, {
  64. map: {
  65. package: oJsonObj.package,
  66. xdtype: goodsCategory,
  67. result: 1,
  68. book_id: sbookid,
  69. adid: goodsId,
  70. chapter_id: schapterid,
  71. business: business_line,
  72. money: smoney
  73. }
  74. }, 'xdjg']);
  75. } else {
  76. consoleMain(data.msg);
  77. }
  78. break;
  79. default: //alipay 支付宝 //wechath5 微信H5支付
  80. break;
  81. }
  82. } else {
  83. consoleMain(data.msg);
  84. cpslog([703, {
  85. map: {
  86. xdtype: goodsCategory,
  87. result: 2,
  88. book_id: sbookid,
  89. adid: goodsId,
  90. chapter_id: schapterid,
  91. business: business_line,
  92. money: smoney
  93. }
  94. }, 'xdjg']);
  95. }
  96. },
  97. error: function (XMLHttpRequest, textStatus, errorThrown) {
  98. consoleMain('异常错误,请刷新页面重试');
  99. cpslog([703, {
  100. map: {
  101. xdtype: goodsCategory,
  102. result: 4,
  103. book_id: sbookid,
  104. adid: goodsId,
  105. chapter_id: schapterid,
  106. business: business_line,
  107. money: smoney,
  108. XMLHttpRequest: XMLHttpRequest.status,
  109. textStatus: textStatus,
  110. errorThrown: errorThrown
  111. }
  112. }, 'xdjg']);
  113. paystop = true;
  114. },
  115. complete: function () {
  116. $('.js_loading').hide();
  117. }
  118. })
  119. });
  120. /**
  121. * 处理默认充值商品自动下单
  122. */
  123. var check = $('.re_but_box a.check');
  124. if(check.length){
  125. check.click();
  126. }
  127. });
  128. /**
  129. * 微信公众号支付
  130. * @param json 微信支付相关数据
  131. * @param goodsId 商品id
  132. */
  133. function wechatPay(json, goodsId) {
  134. if (typeof WeixinJSBridge == "undefined") {
  135. if (document.addEventListener) {
  136. document.addEventListener('WeixinJSBridgeReady', function () {
  137. onBridgeReady(json, goodsId);
  138. }, false);
  139. } else if (document.attachEvent) {
  140. document.attachEvent('WeixinJSBridgeReady', function () {
  141. onBridgeReady(json, goodsId);
  142. });
  143. document.attachEvent('onWeixinJSBridgeReady', function () {
  144. onBridgeReady(json, goodsId);
  145. });
  146. }
  147. } else {
  148. onBridgeReady(json, goodsId);
  149. }
  150. }
  151. /**
  152. * 微信内唤起充值窗口
  153. * @param json
  154. * @param goodsId
  155. */
  156. function onBridgeReady(json, goodsId) {
  157. WeixinJSBridge.invoke(
  158. 'getBrandWCPayRequest', json,
  159. function (res) {
  160. switch (res.err_msg) {
  161. case 'get_brand_wcpay_request:ok': //成功
  162. consoleMain('充值成功');
  163. cpslog([703, {
  164. map: {
  165. cztype: 'wechat',
  166. package: json.package,
  167. result: 1,
  168. book_id: sbookid,
  169. chapter_id: schapterid,
  170. money: smoney,
  171. res: res
  172. }
  173. }, 'czjg']);
  174. if (toUrl) { //回跳到最近阅读页
  175. window.location.href = toUrl;
  176. }else{
  177. setTimeout(function () {
  178. window.location.reload(true);
  179. },1000);
  180. }
  181. break;
  182. case 'get_brand_wcpay_request:cancel': //取消
  183. consoleMain('您已取消充值');
  184. wechatCancel(goodsId);
  185. cpslog([703, {
  186. map: {
  187. cztype: 'wechat',
  188. package: json.package,
  189. result: 2,
  190. book_id: sbookid,
  191. chapter_id: schapterid,
  192. money: smoney,
  193. res: res
  194. }
  195. }, 'czjg']);
  196. break;
  197. case 'get_brand_wcpay_request:fail': //失败
  198. consoleMain('充值失败,请尝试重新充值或试试其他充值方式');
  199. wechatCancel(goodsId);
  200. cpslog([703, {
  201. map: {
  202. cztype: 'wechat',
  203. package: json.package,
  204. result: 3,
  205. book_id: sbookid,
  206. chapter_id: schapterid,
  207. money: smoney,
  208. res: res
  209. }
  210. }, 'czjg']);
  211. break;
  212. default:
  213. console.log(res);
  214. wechatCancel(goodsId);
  215. if (typeof res.err_msg == 'undefined' && res.errMsg == 'chooseWXPay:fail, the permission value is offline verifying') {
  216. consoleMain('充值失败,原因:模拟器无法充值');
  217. } else {
  218. consoleMain('异常错误,请刷新页面重试');
  219. cpslog([703, {
  220. map: {
  221. cztype: 'wechat',
  222. package: json.package,
  223. result: 4,
  224. book_id: sbookid,
  225. chapter_id: schapterid,
  226. money: smoney,
  227. res: res
  228. }
  229. }, 'czjg']);
  230. }
  231. break;
  232. }
  233. }
  234. );
  235. }
  236. function wechatCancel(goodsId) {
  237. }