pay.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // 点击充值
  2. //充值地址
  3. var paystop = true;
  4. var payurl = '/api/recharge/pay';
  5. var args = {};
  6. $('.btn-pay').click(function(){
  7. args.goodsId = $(this).attr('data-goods_id');
  8. args.price = $(this).attr('data-price');
  9. args.actId = $(this).attr('data-act_id');
  10. args.subCode = $('#sub_code').val();
  11. // toastSuccess(data);exit;
  12. var pdata = {
  13. id: args.goodsId,
  14. activityId: 0,
  15. business_line:0,
  16. category: 'wechat',
  17. orderExtendType:2,//订阅活动
  18. actId:args.actId,
  19. subCode:args.subCode,
  20. };
  21. var acting = $('#acting').val();
  22. if ( acting == 1 ){
  23. $('#success_normal .apply').text('您已经报名');
  24. $('#success_normal .today').text('明日开始打卡,别忘记了来参加啊~');
  25. $('#success_normal').show();
  26. }else if(acting == 2){
  27. $('#success_normal .apply').text('您已经报名');
  28. $('#success_normal').show();
  29. }else if( acting == 3 ){
  30. $('#success_normal .apply').text('活动结束');
  31. $('#success_normal .today').text('');
  32. $('#success_normal').show();
  33. }else{
  34. gopay(pdata);
  35. }
  36. })
  37. // console.log(window.log_host)
  38. function gopay(pdata) {
  39. if ( !paystop ){
  40. return false;
  41. }
  42. paystop = false;
  43. $.ajax({
  44. url: payurl,
  45. data: pdata,
  46. type: 'POST',
  47. timeout: 35000,
  48. success: function (data) {
  49. paystop = true;
  50. if (data.code) {
  51. switch (pdata.business_line) {
  52. case 0: //微信公众号支付
  53. if (data.data) {
  54. var oJsonObj = JSON.parse(data.data.json);
  55. wechatPay(oJsonObj, pdata.goodsId);
  56. } else {
  57. consoleMain(data.msg);
  58. }
  59. break;
  60. default: //alipay 支付宝 //wechath5 微信H5支付
  61. break;
  62. }
  63. } else {
  64. alert(22);
  65. alert(data.msg);
  66. }
  67. },
  68. error: function (XMLHttpRequest, textStatus, errorThrown) {
  69. paystop = true;
  70. alert(errorThrown);
  71. paystop = true;
  72. },
  73. complete: function () {
  74. $('.js_loading').hide();
  75. }
  76. })
  77. }
  78. /**
  79. * 微信公众号支付
  80. * @param json 微信支付相关数据
  81. * @param goodsId 商品id
  82. */
  83. function wechatPay(json, goodsId) {
  84. if (typeof WeixinJSBridge == "undefined") {
  85. if (document.addEventListener) {
  86. document.addEventListener('WeixinJSBridgeReady', function () {
  87. onBridgeReady(json, goodsId);
  88. }, false);
  89. } else if (document.attachEvent) {
  90. document.attachEvent('WeixinJSBridgeReady', function () {
  91. onBridgeReady(json, goodsId);
  92. });
  93. document.attachEvent('onWeixinJSBridgeReady', function () {
  94. onBridgeReady(json, goodsId);
  95. });
  96. }
  97. } else {
  98. onBridgeReady(json, goodsId);
  99. }
  100. }
  101. /**
  102. * 微信内唤起充值窗口
  103. * @param json
  104. * @param goodsId
  105. */
  106. function onBridgeReady(json, goodsId) {
  107. args.money = $(".count_item_active div .kandian").attr('data-money');
  108. WeixinJSBridge.invoke(
  109. 'getBrandWCPayRequest', json,
  110. function (res) {
  111. switch (res.err_msg) {
  112. case 'get_brand_wcpay_request:ok': //成功
  113. // consoleMain('充值成功,正在跳转');
  114. cpslog([703, {
  115. map: {
  116. cztype: 'wechat',
  117. package: json.package,
  118. result: 1,
  119. book_id: 0,
  120. chapter_id: 0,
  121. money: args.price,
  122. res: res
  123. }
  124. }, 'subscription']);
  125. toastChecking();
  126. break;
  127. case 'get_brand_wcpay_request:cancel': //取消
  128. // consoleMain('您已取消充值');
  129. wechatCancel(goodsId);
  130. cpslog([703, {
  131. map: {
  132. cztype: 'wechat',
  133. package: json.package,
  134. result: 2,
  135. book_id: 0,
  136. chapter_id: 0,
  137. money: args.price,
  138. res: res
  139. }
  140. }, 'subscription']);
  141. break;
  142. case 'get_brand_wcpay_request:fail': //失败
  143. consoleMain('充值失败,请尝试重新充值或试试其他充值方式');
  144. wechatCancel(goodsId);
  145. cpslog([703, {
  146. map: {
  147. cztype: 'wechat',
  148. package: json.package,
  149. result: 3,
  150. book_id: 0,
  151. chapter_id: 0,
  152. money: args.price,
  153. res: res
  154. }
  155. }, 'subscription']);
  156. break;
  157. default:
  158. wechatCancel(goodsId);
  159. if (typeof res.err_msg == 'undefined' && res.errMsg == 'chooseWXPay:fail, the permission value is offline verifying') {
  160. consoleMain('充值失败,原因:模拟器无法充值');
  161. } else {
  162. consoleMain('异常错误,请刷新页面重试');
  163. cpslog([703, {
  164. map: {
  165. cztype: 'wechat',
  166. package: json.package,
  167. result: 4,
  168. book_id: 0,
  169. chapter_id: 0,
  170. money: args.price,
  171. res: res
  172. }
  173. }, 'subscription']);
  174. }
  175. break;
  176. }
  177. }
  178. );
  179. }
  180. //endregion
  181. function wechatCancel(goodsId) {
  182. $.ajax({
  183. url: '/api/recharge/wechatCancel',
  184. data: {
  185. url: window.location.href,
  186. goods_id: goodsId
  187. },
  188. type: 'POST',
  189. timeout: 10000,
  190. success: function (data) {
  191. },
  192. error: function (err) {
  193. }
  194. })
  195. }
  196. //充值成功后显示报名中,3s 消失
  197. function toastChecking(){
  198. $('#success_normal .today').text('明日开始打卡,别忘记了来参加啊~');
  199. $('#success_normal').show();
  200. setTimeout(function(){
  201. $('#success_normal').hide();
  202. window.location.reload()
  203. },3000)
  204. }