|
|
import { API_HOST, SERVICE_HOST, PAYMENT_CODE} from '../libs/config';
|
|
|
import {GET, POST} from '../libs/request';
|
|
|
// import tt from "./tt";
|
|
|
import { getYHStorageSync } from './util';
|
|
|
|
|
|
import {
|
|
|
logEvent,
|
|
|
YB_SC_PAY_RES,
|
|
|
} from '../libs/analytics.js'
|
|
|
|
|
|
let app = getApp();
|
|
|
|
|
|
function pay(data, payParam) {
|
|
|
console.log(data.payment_amount);
|
|
|
console.log(data.order_amount);
|
|
|
|
|
|
let wetoast = new app.WeToast();
|
|
|
let order_amount = data.payment_amount ? data.payment_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);
|
|
|
|
|
|
tt.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);
|
|
|
tt.showModal({
|
|
|
content: '支付失败,订单号未获取到.',
|
|
|
showCancel: false,
|
|
|
confirmText: "确定",
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if(!app.getOpenID()){
|
|
|
let params = {
|
|
|
ORDER_AMOUNT: order_amount,
|
|
|
PAY_TYPE: 1,
|
|
|
PAY_RES: 0,
|
|
|
ORD_NUM: order_code,
|
|
|
};
|
|
|
logEvent(YB_SC_PAY_RES, params);
|
|
|
tt.showModal({
|
|
|
content: '支付失败,用户openID未获取到.',
|
|
|
showCancel: false,
|
|
|
confirmText: "确定",
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
tt.showToast({
|
|
|
title: '加载中',
|
|
|
icon: 'loading',
|
|
|
duration: 10000
|
|
|
})
|
|
|
|
|
|
let param = {
|
|
|
order_code: order_code,
|
|
|
payment_code: PAYMENT_CODE,
|
|
|
openid: app.getOpenID(),
|
|
|
}
|
|
|
GET(SERVICE_HOST+'/payment/alipay_data', param)
|
|
|
.then(json=>{
|
|
|
let jsonData = json.data;
|
|
|
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: order_code,
|
|
|
openId: app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID','wxpay'),
|
|
|
miniapp_type: app.globalData.miniapp_type,
|
|
|
formId: prepareID,
|
|
|
method: 'wechat.formId.add',
|
|
|
formType: 'pay',
|
|
|
}
|
|
|
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(order_code);
|
|
|
|
|
|
//支付成功以后跳转到支付成功页
|
|
|
if (order_code && 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=" + order_code + '&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
|
|
|
}
|
|
|
|
|
|
GET(API_HOST, param)
|
|
|
.then(json=>{
|
|
|
|
|
|
})
|
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
|
aliPay: pay,
|
|
|
}
|
|
|
|
...
|
...
|
|