wxpay.js 5.97 KB
'use strict'
import { API_HOST, SERVICE_HOST, PAYMENT_CODE} from '../libs/config';
import {GET, POST} from '../libs/request';
import { getYHStorageSync } from './util';



import {
  logEvent,
  YB_SC_PAY_RES,
} from '../libs/analytics.js'

let app = getApp();
let orderCode;

//微信支付action
function wexinPay(data,payParam){
     let wetoast = new app.WeToast();
     let order_amount = data.order_amount;
     let order_code = data.order_code;
     let isVirtual = payParam && payParam.isVirtual ? 'true':'false';

     if(!data){
        let params = {
          ORDER_AMOUNT: order_amount,
          PAY_TYPE: 1,
          PAY_RES: 0,
          ORD_NUM: order_code,
        };
        logEvent(YB_SC_PAY_RES, params);

         wx.showModal({
            content: '支付失败,请求参数不完整.',
            showCancel: false,
            confirmText: "确定",
        })
       return
     }

    if(!data.order_code){
        let params = {
          ORDER_AMOUNT: order_amount,
          PAY_TYPE: 1,
          PAY_RES: 0,
          ORD_NUM: order_code,
        };
        logEvent(YB_SC_PAY_RES, params);
         wx.showModal({
            content: '支付失败,订单号未获取到.',
            showCancel: false,
            confirmText: "确定",
        })
        return
     }

    if(!app.globalData.WXThird_session){
        let params = {
          ORDER_AMOUNT: order_amount,
          PAY_TYPE: 1,
          PAY_RES: 0,
          ORD_NUM: order_code,
        };
        logEvent(YB_SC_PAY_RES, params);
         wx.showModal({
            content: '支付失败,用户session未获取到.',
            showCancel: false,
            confirmText: "确定",
        })
       return
     }

    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })

    orderCode = data.order_code

     let param = {
       method:'/payment/weixin_data',
       order_code:data.order_code,
       payment_code: PAYMENT_CODE,

       '3rd_session':app.globalData.WXThird_session,
     }

    GET(SERVICE_HOST+'/payment/weixin_data', param)
    .then(function(data) {
      wx.hideToast()
      if(data && data.data && data.code == 200){
        wx.requestPayment({
        'timeStamp': data.data.timeStamp,
        'nonceStr': data.data.nonceStr,
        'package': data.data.package,
        'signType': data.data.signType,
        'paySign':data.data.paySign,
        'success':function(res){
          let app = getApp()

          // console.log("openID:", app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID','wxpay'))
          //上报formID
          let prepareID = data.data.package
          if (prepareID && prepareID.length > 10) {
            prepareID = prepareID.slice(10)
            let formIdParams = {
              uid: app.getUid(),
              order_code: orderCode,
              openId: app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID','wxpay'),
              miniapp_type: app.globalData.miniapp_type,
              formId: prepareID,
              method: 'wechat.formId.add',
              formType: 'pay',
            }

            // console.log("prepareID:",formIdParams)

            GET(API_HOST, formIdParams)
              .then((data) => {
                // console.log('responsedata', data)
              })
              .catch(error => {
              })
          }
          
          let params = {
            ORDER_AMOUNT: order_amount,
            PAY_TYPE: 1,
            PAY_RES: 1,
            ORD_NUM: order_code,
          };
          logEvent(YB_SC_PAY_RES, params);

          payConfirm(orderCode)
          // wx.showModal({
          //   content: '支付成功',
          //   showCancel: false,
          //   confirmText: "确定",
          //   complete: function(res) {
                // wx.navigateBack({
                //   delta:  getCurrentPages().length-1
                // })
          if (payParam && payParam.fromPage == 'groupPurchaseDetail'){
            wx.navigateTo({
              url: "/pages/groupPurchase/groupPurchaseResult?activity_id=" + payParam.activity_id + '&group_no=' + payParam.group_no + '&page_name=' + 'statements' + '&page_param=' + ''
            });
          }else {
            wx.navigateTo({
              url: "/pages/paymentSuccessed/paymentSuccessed?orderCode=" + order_code + '&hasSuc=true' + '&price=' + parseInt(order_amount).toFixed(2) + '&isVirtual=' + isVirtual

            });
          }
            // }})
          },
        'fail':function(res){

          let params = {
            ORDER_AMOUNT: order_amount,
            PAY_TYPE: 1,
            PAY_RES: 0,
            ORD_NUM: order_code,
          };
          logEvent(YB_SC_PAY_RES, params);


          if(res.errMsg=='requestPayment:fail cancel'){
            wx.redirectTo({
                url: '/pages/orders/orders'
            })
          }else{
            wx.showModal({
                content: res.errMsg,
                showCancel: false,
                confirmText: "确定",
                complete: function(res) {
                   wx.redirectTo({
                    url: '/pages/orders/orders'
                })
            }
            })
          }
          }
      })
        
      }else{
        wx.hideToast()
          wx.showModal({
            content: data.code+':'+data.message,
            showCancel: false,
            confirmText:'确定',
            confirmColor:"#000",
            complete: function(res) {
                wx.redirectTo({
                  url: '/pages/orders/orders'
                })
            }
        })
      }
    })

     .catch(function(error) {
      //  console.log(error+'')
    });
    
  }

//支付确认接口
  function payConfirm(orderCode){
    if(!orderCode||orderCode==0){
      return
    }
    let param = {
        method:'app.SpaceOrders.payConfirm',
        order_code:orderCode,
        payment_id:45,
        uid:app.globalData.userInfo.uid,
      }

    GET(API_HOST, param)
  }

  module.exports = {
    wexinPay: wexinPay,
}