payUtil.js 4.95 KB
import api from '../common/api.js';
import config from '../config';
import tt from "./tt";
// import {
//   logEvent
// } from '../../libs/analytics.js'

let app = getApp();

function pay(data, payParam) {
    let orderCode = data.order_code;
    let order_amount = data.order_amount;

    
    let param = {
        order_code: orderCode,
        payment_code: config.payment_code,
        openid: app.globalData.openID,
    }

    api.get({data: param,url:"/payment/alipay_data"})
        .then(json=>{
            let jsonData = json.data;
            console.log('====================================');
            console.log(json);
            console.log('====================================');

            if(jsonData){
                let payParams = jsonData.payParams;
                // ttPayment(payParams, data);

                let paymentData = JSON.parse(payParams);
                let timestampStr = paymentData.timestamp+"";
                let total_amount = parseFloat(paymentData.total_amount);
                //头条需要该字段为String
                paymentData.timestamp = timestampStr;
                //头条需要该字段为数字
                paymentData.total_amount = total_amount;
  
                tt.requestPayment({
                    data:paymentData,
                    success (res) {
                      let app = getApp()
                      //上报formID
                      let prepareID = paymentData.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',
                        }
                        api.get({data:formIdParams})
                          .then((data) => {
                            // console.log('responsedata', data)
                          })
                          .catch(error => {
                          })
                      }
                      // let params = {
                      //   ORDER_AMOUNT: order_amount,
                      //   PAY_TYPE: 1,
                      //   PAY_RES: 1,
                      //   ORD_NUM: orderCode,
                      // };
                      // logEvent(YB_SC_PAY_RES, params);
            
                      payConfirm(orderCode);
            
                      //支付成功以后跳转到支付成功页
                      if (orderCode && paymentData.fromPage == 'groupPurchaseDetail'){
                        tt.navigateTo({
                          url: "/pages/groupPurchase/groupPurchaseResult?activity_id=" + payParam.activity_id + '&group_no=' + payParam.group_no + '&page_name=' + 'statements' + '&page_param=' + ''
                        });
                      }else {
                        tt.navigateTo({
                          url: "/pages/paymentSuccessed/paymentSuccessed?orderCode=" + orderCode + '&hasSuc=true' + '&price=' + parseInt(order_amount).toFixed(2) + '&isVirtual=' + 'false'
                        });
                      }
                    },
                    fail (res) {
                        console.log("pay fail: " + res.errMsg);
                        // 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') {
                          tt.redirectTo({
                              url: '/pages/orders/orders'
                          })
                        }else{
                          tt.showModal({
                              content: res.errMsg,
                              showCancel: false,
                              confirmText: "确定",
                              complete: function(res) {
                                 tt.redirectTo({
                                  url: '/pages/orders/orders'
                                  })
                              }
                          })
                        }
                    }
                })
            }
    })

}

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

    api.get({data: param})
        .then(json=>{
        
    })
}

module.exports = {
    aliPay: pay,
}