alipay.js
3.37 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/7/22
*/
'use strict';
// const config = global.yoho.config;
const helpers = global.yoho.helpers;
// const common = require('./common');
const sign = require('./sign');
const _ = require('lodash');
const md5 = require('md5');
const logger = global.yoho.logger;
const PAY_URL = 'https://mapi.alipay.com/gateway.do?';
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
// // 原node生成预支付订单
// pay(user, order, param, protocol) {
// let payParams = JSON.parse(param.payParams);
// let extraParam = JSON.stringify({
// sign_id_ext: user.uid,
// defaultbank: param.bankCode || ''
// });
// let params = {
// service: 'create_direct_pay_by_user',
// partner: payParams.merchant_id,
// _input_charset: 'utf-8',
// notify_url: config.domains.serviceNotify + 'payment/alipay_notify',
// return_url: protocol + ':' + helpers.urlFormat('/shopping/newpay/callback/alipay'),
// subject: '有货订单号:' + order.order_code,
// out_trade_no: order.order_code,
// it_b_pay: common.getPayExpireMin(order.pay_expire) + 'm',
// total_fee: order.payment_amount,
// payment_type: '1',
// seller_email: payParams.merchant_other_code,
// extra_common_param: extraParam
// };
// let signStr = md5(sign.raw(params) + payParams.merchant_key);
// let body = sign.rawUncode(params) + '&sign=' + signStr + '&sign_type=MD5';
// return {
// code: 200,
// data: {
// href: PAY_URL + body
// }
// };
// }
pay(user, order, param, protocol) {
let options = {
uid: user.uid,
order_code: order.order_code,
payment_code: param.paymentCode,
return_url: protocol + ':' + helpers.urlFormat('/shopping/newpay/callback/alipay')
};
return this.post({
api: global.yoho.ServiceAPI,
url: '/payment/alipay_data',
data: options
}).then(result => {
if (result.code === 200) {
return {
code: 200,
data: {
href: PAY_URL + _.get(result, 'data.payParams', '')
}
};
}
return result;
});
}
notify(data, param) { // eslint-disable-line
// 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: data.trade_status === 'TRADE_SUCCESS' ? 200 : 400,
totalFee: data.total_fee,
tradeNo: data.trade_no,
bankBillNo: ''
};
}
checkNotify(data, payParams) {
let signValue = data.sign;
delete data.sign;
delete data.sign_type;
delete data.code;
let signStr = md5(sign.raw(data) + payParams.merchant_key);
return signValue === signStr;
}
};