caimei-wechatpay.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. *Created by ZHJY on 2020/7/14.
  3. */
  4. var payContainer = new Vue({
  5. el:"#payContainer",
  6. data: {
  7. orderID:'',
  8. pageType:'',//页面跳转类型 1为二手 0为订单
  9. payAmount:0,//本次支付金额
  10. wechatcode:'',//微信code
  11. state:0,//随机数
  12. isRequest:false,
  13. productId:'',//二手商品ID
  14. vipId: null, // vip套餐id
  15. vipRecordId: undefined
  16. },
  17. computed: {
  18. },
  19. methods: {
  20. infoPayOrderCheckoutCounter:function(){//初始化订单数据
  21. var _this = this;
  22. _this.wechatcode = CAIMEI.getUrlParam('code');
  23. _this.state = JSON.parse(decodeURIComponent(CAIMEI.getUrlParam('state')));
  24. _this.payAmount = _this.state.payAmount;
  25. _this.pageType = _this.state.pageType;
  26. _this.isRequest = true;
  27. if( _this.pageType == 1){
  28. _this.productId = _this.state.productId;
  29. }else if(_this.pageType == 2){
  30. _this.vipId = _this.state.vipId;
  31. _this.vipRecordId = _this.state.vipRecordId;
  32. }else{
  33. _this.orderID = _this.state.orderID;
  34. }
  35. },
  36. WxPaySubmit:function(){
  37. if (this.pageType == 1) {
  38. this.WxRequestsecondHandPay();
  39. } else if (this.pageType == 2) {
  40. this.WxRequestPaymentVipFn();
  41. } else {
  42. this.WxRequestPaymentFn();
  43. }
  44. },
  45. // 会员支付
  46. WxRequestPaymentVipFn: function(){
  47. var _this = this;
  48. var params = {
  49. code: _this.wechatcode,
  50. state: _this.rand(1000, 9999),
  51. vipId: _this.vipId,
  52. vipRecordId: _this.vipRecordId,
  53. returnUrl: 'https://www.caimei365.com/',
  54. };
  55. PayApi.PayVipMiniWxPay(params,function(response){
  56. if(response.code == 0){
  57. var PayInfo = JSON.parse(response.data.data.payInfo);
  58. if (typeof WeixinJSBridge == "undefined"){
  59. if( document.addEventListener ){
  60. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  61. }else if (document.attachEvent){
  62. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  63. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  64. }
  65. }else{
  66. _this.onBridgeReady(PayInfo);
  67. }
  68. }else{
  69. CAIMEI.dialog("支付链接已失效,请重新扫码支付");
  70. setTimeout(function(){
  71. WeixinJSBridge.call('closeWindow');
  72. },3000);
  73. }
  74. });
  75. },
  76. WxRequestsecondHandPay:function(){ // 二手商品支付
  77. var _this = this;
  78. var params = {
  79. code: _this.wechatcode,
  80. state: _this.rand(1000, 9999),
  81. productId: _this.productId,
  82. returnUrl: 'https://www.caimei365.com/',
  83. };
  84. PayApi.PayOrdersecondWechat(params,function(response){
  85. if(response.code == 0){
  86. var PayInfo = JSON.parse(response.data.data.payInfo);
  87. if (typeof WeixinJSBridge == "undefined"){
  88. if( document.addEventListener ){
  89. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  90. }else if (document.attachEvent){
  91. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  92. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  93. }
  94. }else{
  95. _this.onBridgeReady(PayInfo);
  96. }
  97. }else{
  98. CAIMEI.dialog("支付链接已失效,请重新扫码支付");
  99. setTimeout(function(){
  100. WeixinJSBridge.call('closeWindow');
  101. },3000);
  102. }
  103. });
  104. },
  105. WxRequestPaymentFn:function(){ // 普通订单支付
  106. var _this = this;
  107. var params={
  108. payAmount:Math.round(_this.payAmount*100),
  109. code:_this.wechatcode,
  110. state:_this.rand(1000,9999),
  111. orderId:_this.orderID,
  112. returnUrl:'https://www.caimei365.com/',
  113. };
  114. PayApi.PayOrderMiniWxPay(params,function(response){
  115. if(response.code == 0){
  116. var PayInfo = JSON.parse(response.data.data.payInfo);
  117. if (typeof WeixinJSBridge == "undefined"){
  118. if( document.addEventListener ){
  119. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  120. }else if (document.attachEvent){
  121. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  122. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  123. }
  124. }else{
  125. _this.onBridgeReady(PayInfo);
  126. }
  127. }else{
  128. CAIMEI.dialog('支付链接已失效,请重新扫码支付');
  129. setTimeout(function(){
  130. WeixinJSBridge.call('closeWindow');
  131. },3000);
  132. }
  133. })
  134. },
  135. onBridgeReady:function(data){
  136. console.log('支付验证签名',data);
  137. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  138. "appId":data.appId, //公众号名称,由商户传入
  139. "timeStamp":data.timeStamp,//时间戳,自1970年以来的秒数
  140. "nonceStr":data.nonceStr, //随机串
  141. "package":data.package,
  142. "signType":data.signType, //微信签名方式:
  143. "paySign":data.paySign, //微信签名
  144. },
  145. function(res) {
  146. if(res.err_msg == "get_brand_wcpay_request:ok"){
  147. CAIMEI.dialog('支付成功');
  148. window.open("https://www.caimei365.com/");
  149. }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
  150. CAIMEI.dialog("用户取消支付,请重新扫码支付");
  151. setTimeout(function(){
  152. WeixinJSBridge.call('closeWindow');
  153. },3000);
  154. }else{
  155. CAIMEI.dialog("支付失败,请重新扫码支付");
  156. setTimeout(function(){
  157. WeixinJSBridge.call('closeWindow');
  158. },3000);
  159. }
  160. });
  161. },
  162. rand:function (min,max) {//随机数
  163. return Math.floor(Math.random()*(max-min))+min;
  164. },
  165. toFixedFn:function(text){//处理小数点后两位数
  166. return Number(text).toFixed(2);
  167. },
  168. },
  169. mounted: function () {
  170. var _this = this;
  171. _this.infoPayOrderCheckoutCounter();
  172. }
  173. });