tenpay.js
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
*
* @author: xiaoxiao.hao<xiaoxiao.hao>
* @date: 2017/3/24
*/
'use strict';
const helpers = global.yoho.helpers;
const moment = require('moment');
const sign = require('./sign');
const md5 = require('md5');
const logger = global.yoho.logger;
const PAY_URL = 'https://gw.tenpay.com/gateway/pay.htm?';
const Tenpay = {
pay(user, order, param, protocol) {
let payParams = JSON.parse(param.payParams);
let extraParam = JSON.stringify({
sign_id_ext: user.uid
});
let params = {
partner: payParams.merchant_id, // 商户号
out_trade_no: order.order_code, // 商户订单号
total_fee: order.payment_amount * 100, // 总金额 以分为单位
return_url: protocol + ':' + helpers.urlFormat('/shopping/pay/callback/tenpay'), // 同步地址
notify_url: protocol + ':' + helpers.urlFormat('/shopping/pay/notify/tenpay'), // 异步通知地址
body: '有货订单号:' + order.order_code, // 商品描述
bank_type: 'DEFAULT', // 银行类型,默认为财付通
spbill_create_ip: user.IP, // 客户端IP
fee_type: '1', // 币种
subject: '有货订单号:' + order.order_code, // 商品名称,(中介交易时必填)
sign_type: 'MD5',
service_version: '1.0', // 接口版本号
input_charset: 'UTF-8', // 字符集
sign_key_index: '1', // 密钥序号
time_start: moment(order.create_time * 1000).format('YYYYMMDDHHmmss'), // 订单生成时间-yyyymmddhhmmss
time_expire: moment(order.pay_expire).format('YYYYMMDDHHmmss'), // 订单失效时间--yyyymmddhhmmss
attach: extraParam // 附加数据,原样返回
};
let signStr = md5(sign.raw(params) + '&key=' + payParams.merchant_key).toLocaleUpperCase();
let body = sign.rawUncode(params) + '&sign=' + signStr;
return {
code: 200,
data: {
href: PAY_URL + body
}
};
},
notify(data, param) {
let payParams = JSON.parse(param.payParams || '{}');
let orderCode = parseInt(data.out_trade_no, 10);
logger.info(`Alipay notify, params = ${JSON.stringify(data)}`);
if (!this.checkNotify(data, payParams)) {
return {payResult: -1};
}
return {
bankName: '',
orderCode: orderCode,
payResult: 200,
totalFee: data.total_fee * 0.01,
tradeNo: data.transaction_id,
bankBillNo: ''
};
},
checkNotify(data, payParams) {
let signValue;
if (data && parseInt(data.trade_state, 10) !== 0) {
return false;
}
signValue = data.sign;
delete data.sign;
let signStr = md5(sign.raw(data) + '&key=' + payParams.merchant_key).toLocaleUpperCase();
return signValue === signStr;
}
};
module.exports = Tenpay;