Blame view

apps/cart/controllers/pay.js 7.95 KB
郭成尧 authored
1
/*
郭成尧 authored
2
 * 支付
郭成尧 authored
3 4 5
 * @Author: Targaryen
 * @Date: 2017-01-04 15:17:51
 * @Last Modified by: Targaryen
郭成尧 authored
6
 * @Last Modified time: 2017-02-17 09:52:57
lijing authored
7 8 9 10 11 12
 */

'use strict';

const mRoot = '../models';
const payModel = require(`${mRoot}/pay`);
郭成尧 authored
13
const payTool = payModel.payTool;
lijing authored
14
const headerModel = require('../../../doraemon/models/header'); // 头部model
郭成尧 authored
15 16
const co = require('bluebird').coroutine;
const helpers = global.yoho.helpers;
郭成尧 authored
17
const Payment = require('../helpers/payment');
郭成尧 authored
18
const WxPay = require('../helpers/pay/wechat');
郭成尧 authored
19
const common = require('../helpers/pay/common');
lijing authored
20
郭成尧 authored
21 22 23 24 25 26
/**
 * 支付中心
 * @param req
 * @param res
 * @param next
 */
郭成尧 authored
27
const payCenter = (req, res, next) => {
郭成尧 authored
28 29 30
    let orderCode = req.query.order_code;
    let uid = req.user.uid;
    let sessionKey = req.session.TOKEN;
郭成尧 authored
31 32
    let userAgent = req.get('User-Agent');
    let hasWxShare = Boolean(userAgent.match(/MicroMessenger/i) && userAgent.match(/MicroMessenger/i).length > 0);
郭成尧 authored
33
郭成尧 authored
34
    if (!orderCode || !uid) {
郭成尧 authored
35
        res.redirect('/');
郭成尧 authored
36 37
    }
郭成尧 authored
38 39 40
    if (sessionKey) {
        sessionKey = sessionKey.substr(0, sessionKey.length - 8);
    }
郭成尧 authored
41
郭成尧 authored
42 43 44
    co(function* () {
        let orderDetail = yield payModel.payCenter({
            orderCode: orderCode,
郭成尧 authored
45 46
            uid: uid,
            sessionKey: sessionKey
郭成尧 authored
47 48
        });
郭成尧 authored
49 50 51 52 53
        /* 判断订单是否已付款, 已付款跳到订单详情页 */
        if (orderDetail.isPay) {
            return res.redirect(helpers.urlFormat('/cart/shopping/pay/payZero', {order_code: orderCode}));
        }
郭成尧 authored
54
        if (hasWxShare) {
郭成尧 authored
55
            let openId = req.cookies['weixinOpenId' + orderCode];
郭成尧 authored
56 57 58 59 60 61 62

            if (!openId) {
                let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl);

                if (getOpenidResult.openid) {
                    openId = getOpenidResult.openid;
                }
郭成尧 authored
63
郭成尧 authored
64 65 66
                if (getOpenidResult.redirectUrl) {
                    return res.redirect(getOpenidResult.redirectUrl);
                }
郭成尧 authored
67
郭成尧 authored
68
                if (openId) {
郭成尧 authored
69 70 71 72
                    res.cookie('weixinOpenId' + orderCode, openId, {
                        domain: 'yohobuy.com',
                        expires: new Date(Date.now() + 24 * 60 * 60 * 1000)
                    });
郭成尧 authored
73 74 75
                }
            }
        }
郭成尧 authored
76
郭成尧 authored
77 78
        let responseData = {
            pageHeader: headerModel.setNav({
郭成尧 authored
79 80
                navTitle: '支付中心',
                backUrl: 'javascript:void(0);'
郭成尧 authored
81 82 83 84 85 86 87 88 89 90 91 92 93
            }),
            module: 'cart',
            page: 'pay',
            title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停',
            payAppInfo: payTool.payAppInfo(orderCode),
            orderCode: orderCode,
            hasWxShare: hasWxShare,
            orderTotal: orderDetail.payment_amount || 0,
            orderTotalFormat: parseInt(orderDetail.payment_amount, 10),
            orderCount: payTool.calBuyNumCount(orderDetail.order_goods),
            uid: uid,
            isOldUser: Boolean(req.cookies._isOldUser && req.cookies._isOldUser === '4')
        };
郭成尧 authored
94
郭成尧 authored
95
        res.render('pay/pay-center', responseData);
郭成尧 authored
96
    })().catch(next);
郭成尧 authored
97 98
};
郭成尧 authored
99
/**
100
 * 统一支付入口
郭成尧 authored
101 102 103
 * @param req
 * @param res
 */
104
const pay = (req, res, next) => {
郭成尧 authored
105
    let orderCode = req.query.order_code;
郭成尧 authored
106
    let user = req.user;
郭成尧 authored
107 108
    let uid = req.user.uid;
    let sessionKey = req.session.TOKEN;
郭成尧 authored
109
    let payment = req.query.payment;
郭成尧 authored
110
    let paymentCode = common.getPaymentCode(payment);
郭成尧 authored
111
    let openId = req.cookies['weixinOpenId' + orderCode];
郭成尧 authored
112
郭成尧 authored
113
    if (!orderCode || !uid || !sessionKey) {
114
        return res.redirect('/');
郭成尧 authored
115 116
    }
郭成尧 authored
117
    co(function* () {
郭成尧 authored
118 119 120 121 122 123
        let orderDetail = yield payModel.getOtherDetail({
            uid: uid,
            orderCode: orderCode,
            sessionKey: sessionKey
        });
郭成尧 authored
124
        if (!orderDetail || !orderDetail.data) {
郭成尧 authored
125 126 127 128 129 130
            return res.json({
                code: 400,
                msg: '没有找到该订单!'
            });
        }
郭成尧 authored
131
        if (orderDetail.data.is_cancel === 'Y') {
郭成尧 authored
132
            let url = helpers.urlFormat('/home/orders/detail', { order_code: orderCode });
郭成尧 authored
133 134 135 136 137 138 139

            return res.json({
                code: 400,
                msg: '订单已经取消'
            }).redirect(url);
        }
郭成尧 authored
140 141 142 143
        Payment.pay(user, orderDetail.data, payment, {
            protocol: req.protocol,
            openId: openId
        }).then(result => {
郭成尧 authored
144 145 146 147 148
            if (result && paymentCode === payModel.payments.wechat) {
                return res.json(result);
            }
            if (result && result.data && result.data.href && paymentCode === payModel.payments.alipay) {
                return res.redirect(result.data.href);
郭成尧 authored
149
            } else {
郭成尧 authored
150
                return res.redirect('/');
郭成尧 authored
151
            }
郭成尧 authored
152 153
        });
    })().catch(next);
郭成尧 authored
154
};
lijing authored
155
lijing authored
156
// 货到付款
lijing authored
157 158
const payCod = (req, res, next) => {
    let headerData = headerModel.setNav({
lijing authored
159
        navTitle: '支付完成'
lijing authored
160 161 162 163 164 165
    });

    let responseData = {
        pageHeader: headerData,
        module: 'cart',
        page: 'pay',
lijing authored
166
        title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停'
lijing authored
167 168
    };
郭成尧 authored
169 170 171
    let sessionKey = req.session.TOKEN;

lijing authored
172
    let param = {
lijing authored
173
        uid: req.user.uid,
lijing authored
174
        udid: req.sessionID || require('md5')(req.ip) || 'yoho',
lijing authored
175
        orderCode: req.query.order_code,
zhangxiaoru authored
176
        contentCode: '78d0fb6c97d691863286edcb4d8abfa9',
郭成尧 authored
177 178
        client_id: req.cookies._yasvd || '',
        sessionKey: sessionKey && sessionKey.substr(0, sessionKey.length - 8) || ''
lijing authored
179 180 181 182 183
    };

    // 如果没有uid,跳转到首页
    if (!param.uid) {
        res.redirect('/');
lijing authored
184
        return;
lijing authored
185 186 187 188 189 190
    }

    payModel.getPayCod(param).then(result => {
        if (result.match === true) {
            res.render('pay/pay-cod', Object.assign(responseData, result));
        } else {
lijing authored
191
            res.redirect('/');
lijing authored
192 193 194 195 196
        }

    }).catch(next);
};
郭成尧 authored
197
// 支付宝支付结果页
lijing authored
198 199
const payAli = (req, res, next) => {
    let headerData = headerModel.setNav({
lijing authored
200
        navTitle: '支付完成'
lijing authored
201 202 203 204 205 206 207 208 209
    });

    let responseData = {
        pageHeader: headerData,
        module: 'cart',
        page: 'pay',
        title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停'
    };
zhangxiaoru authored
210 211 212 213 214 215 216 217
    let responseFailure = {
        pageHeader: headerModel.setNav({
            navTitle: '支付中心',
            navBtn: false
        }),
        title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停'
    };
lijing authored
218
    let param = {
lijing authored
219
        uid: req.user.uid,
lijing authored
220
        udid: req.sessionID || require('md5')(req.ip) || 'yoho',
lijing authored
221
        orderCode: req.query.out_trade_no,
zhangxiaoru authored
222
        contentCode: '78d0fb6c97d691863286edcb4d8abfa9'
lijing authored
223 224 225 226 227
    };

    // 如果没有uid,跳转到首页
    if (!param.uid) {
        res.redirect('/');
lijing authored
228
        return;
lijing authored
229 230
    }
郭成尧 authored
231 232
    let verifyResult = payModel.alipayResultVerify(req.query);
郭成尧 authored
233
    // 支付宝支付校验
郭成尧 authored
234
    if (!verifyResult.payResult) {
zhangxiaoru authored
235
        return res.render('pay/pay-failure', responseFailure);
ccbikai(👎🏻🍜) authored
236 237
    }
lijing authored
238 239 240 241
    payModel.getPayAli(param).then(result => {
        if (result.match === true) {
            res.render('pay/pay-ali', Object.assign(responseData, result));
        } else {
lijing authored
242
            res.redirect('/');
lijing authored
243 244
        }
lijing authored
245 246 247
    }).catch(next);
};
zhangxiaoru authored
248
// 零元支付
zhangxiaoru authored
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
const payZero = (req, res, next) => {
    let headerData = headerModel.setNav({
        navTitle: '支付完成'
    });

    let responseData = {
        pageHeader: headerData,
        module: 'cart',
        page: 'pay',
        title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停'
    };

    let param = {
        uid: req.user.uid,
        udid: req.sessionID || require('md5')(req.ip) || 'yoho',
        orderCode: req.query.order_code,
zhangxiaoru authored
265 266
        contentCode: '78d0fb6c97d691863286edcb4d8abfa9',
        isPay: true
zhangxiaoru authored
267 268
    };
zhangxiaoru authored
269 270 271 272 273 274
    // 如果没有uid,跳转到首页
    if (!param.uid) {
        res.redirect('/');
        return;
    }
zhangxiaoru authored
275
    payModel.getPayAli(param).then(result => {
zhangxiaoru authored
276 277 278 279 280 281 282 283 284
        if (result.match === true) {
            res.render('pay/pay-ali', Object.assign(responseData, result));
        } else {
            res.redirect('/');
        }

    }).catch(next);
};
lijing authored
285
module.exports = {
郭成尧 authored
286
    payCenter,
287
    pay,
lijing authored
288
    payCod,
zhangxiaoru authored
289 290
    payAli,
    payZero
lijing authored
291
};