pay.js 7.0 KB

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