Commit 3aac0bf0 authored by Hsinli's avatar Hsinli

Merge branch 'master' of t-git.51gjj.com:fangbin/51business

parents 900d860d 3eea291f
Pipeline #13582 passed with stage
in 12 seconds
...@@ -32,7 +32,7 @@ class OrderController extends Controller { ...@@ -32,7 +32,7 @@ class OrderController extends Controller {
const { ctx } = this; const { ctx } = this;
const order_id = ctx.params.order_id; const order_id = ctx.params.order_id;
const result = await ctx.service.credit.common.unifiedOrder('wexin', order_id); const result = await ctx.service.credit.order.unifiedOrder('wexin', order_id);
ctx.success({ result }); ctx.success({ result });
} }
...@@ -53,21 +53,23 @@ class OrderController extends Controller { ...@@ -53,21 +53,23 @@ class OrderController extends Controller {
const ret_obj = await ctx.helper.xmlTojson(getxml); const ret_obj = await ctx.helper.xmlTojson(getxml);
ctx.logger.info({ notice_params: ret_obj }); ctx.logger.info({ notice_params: ret_obj });
const ret = await ctx.service.credit.common.WexinNotify(ret_obj); const ret = await ctx.service.credit.common.WexinNotify(ret_obj);
ctx.success({ ret_obj, ret }); if (ret) {
ctx.success("<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>");
return;
}
ctx.success("<xml><return_code>FAIL</return_code><return_msg>UNKONW</return_msg></xml>");
return; return;
ctx.success("<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>");
} }
async checkPay() { async checkPay() {
const { ctx } = this; const { ctx } = this;
const order_id = ctx.params.order_id; const order_id = ctx.params.order_id;
const is_pay = ctx.service.credit.order.checkOrderPay(order_id); const is_pay = await ctx.service.credit.order.checkOrderPay(order_id);
let result = false; let result = false;
if (is_pay) { if (is_pay) {
result = true; result = true;
} }
ctx.success({ result }); ctx.success({ result });
} }
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
module.exports = app => { module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/credit'); const router = app.router.namespace(app.config.projectRootPath + '/credit');
router.get('third', '/home', 'credit.order.getMyCredit'); router.get('/home', 'credit.order.getMyCredit');
router.get('third', '/history/:type', 'credit.order.getRecord'); router.get('/history/:type', 'credit.order.getRecord');
router.get('third', '/order/pay/:order_id', 'credit.order.payOrder'); router.post('/order/pay/:order_id', 'credit.order.payOrder');
router.post('third', '/order/pay_notice', 'credit.order.payNotice'); router.get('/order/pay/:order_id', 'credit.order.payOrder');
router.post('/order/pay_notice', 'credit.order.payNotice');
router.get('/order/check_pay/:order_id', 'credit.order.checkPay');
//我的信用-黑名单报告 //我的信用-黑名单报告
router.get('/blacklist/report/:report_id', 'credit.blacklist.getBlacklistReport');//获取报告信息 router.get('/blacklist/report/:report_id', 'credit.blacklist.getBlacklistReport');//获取报告信息
......
...@@ -64,66 +64,21 @@ class CommonService extends Service { ...@@ -64,66 +64,21 @@ class CommonService extends Service {
return sign; return sign;
} }
async unifiedOrder(type, order_id) {
const { ctx } = this;
const order = await ctx.service.credit.order.getOrderInfo(order_id);
const price = order.price * 100 - order.preferential_price * 100;
console.info(price);
if (price <= 0) {
ctx.failed('error price');
}
if (type === 'wexin') {
let body = '黑名单报告检测支付';
if (order.type === 2) {
body = '个人通话风险检测支付';
}
const data = {
body,
order_id,
trade_no: moment().valueOf() + ctx.helper.PrefixInteger(order_id, 11),
total_fee: price,
notify_url: '',
product_id: order.order_no,
scene_info: { h5_info: { type: 'Wap', wap_url: this.config.PHP_URL, wap_name: '我的信用' }, },
};
const ret = await this.WexinUnifiedOrder(data);
const pay_data = {
order_id,
trade_no: data.trade_no,
description: data.body,
prepay_id: ret.prepay_id || 0,
amount: data.total_fee,
unifiedorder_result: JSON.stringify(ret),
client_ip: ctx.helper.getClientIP(),
};
await this.addCreditPay(pay_data);
return { url: ret.mweb_url, order_id: order_id, trade_no: data.trade_no };
} else if (type === 'alipay') {
//TODO
}
return false;
}
async WexinUnifiedOrder(params) { async WexinUnifiedOrder(params) {
const { ctx } = this; const { ctx } = this;
const { body, trade_no, total_fee, notify_url, product_id, scene_info } = params; const { body, trade_no, total_fee, notify_url, product_id, scene_info } = params;
const appid = ''; const appid = this.config.MCH_APPID;
const mch_id = ''; const mch_id = this.config.MCH_ID;
const trade_type = 'MWEB'; const trade_type = 'MWEB';
const nonce_str = ctx.helper.createNoncestr(); const nonce_str = ctx.helper.createNoncestr();
const spbill_create_ip = ctx.helper.getClientIP(); // const spbill_create_ip = ctx.helper.getClientIP();
const spbill_create_ip = '122.224.130.226';
let json_data = { let json_data = {
appid, appid,
mch_id, mch_id,
body, body,
trade_no, out_trade_no: trade_no,
total_fee, total_fee,
spbill_create_ip, spbill_create_ip,
notify_url, notify_url,
...@@ -135,9 +90,10 @@ class CommonService extends Service { ...@@ -135,9 +90,10 @@ class CommonService extends Service {
const sign = this.getWexinSign(json_data); const sign = this.getWexinSign(json_data);
json_data.sign = sign; json_data.sign = sign;
const xml_data = ctx.helper.jsonToxml(json_data); const xml_data = ctx.helper.jsonToxml(json_data);
console.info(xml_data);
const url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; const url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
const result_wexin = await ctx.curl(url, { timeout: 3000, method: 'POST', data: xml_data, headers: { 'content-type': 'text/html', }, streaming: false, dataType: 'text', }); const result_wexin = await ctx.curl(url, { timeout: 3000, method: 'POST', data: xml_data, });
console.info(result_wexin);
if (result_wexin.status !== 200) { if (result_wexin.status !== 200) {
ctx.failed('wexin pay failed'); ctx.failed('wexin pay failed');
} }
...@@ -150,34 +106,16 @@ class CommonService extends Service { ...@@ -150,34 +106,16 @@ class CommonService extends Service {
let error_msg = 'wexin pay error'; let error_msg = 'wexin pay error';
if (!ret.return_code || ret.return_code !== 'SUCCESS') { if (!ret.return_code || ret.return_code !== 'SUCCESS') {
error_msg = ret.return_msg || error_msg; error_msg = ret.return_msg || error_msg;
// ctx.failed(error_msg); ctx.failed(error_msg);
} }
if (!ret.result_code || ret.result_code !== 'SUCCESS') { if (!ret.result_code || ret.result_code !== 'SUCCESS') {
error_msg = ret.err_code_des || error_msg; error_msg = ret.err_code_des || error_msg;
// ctx.failed(error_msg); ctx.failed(error_msg);
} }
return ret; return ret;
} }
async addCreditPay(params) {
const { ctx } = this;
const { order_id, trade_no, prepay_id, amount, description, unifiedorder_result, client_ip } = params;
const data = {
order_id,
trade_no,
prepay_id,
amount,
description,
unifiedorder_result,
client_ip,
}
const ret = await ctx.prometheusModel.CreditPay.add(data);
return ret;
}
async alipay() { async alipay() {
} }
...@@ -186,11 +124,14 @@ class CommonService extends Service { ...@@ -186,11 +124,14 @@ class CommonService extends Service {
const { ctx } = this; const { ctx } = this;
const { transaction_id, trade_no } = params; const { transaction_id, trade_no } = params;
const pay_info = await ctx.prometheusModel.CreditPay.one({ where: { trade_no: out_trade_no } }); if (!transaction_id && !trade_no) {
if (!pay_info || pay_info.order_id) { return false;
}
const pay_info = await ctx.prometheusModel.CreditPay.one({ where: { trade_no } });
if (!pay_info || !pay_info.order_id) {
ctx.failed('pay info error'); ctx.failed('pay info error');
} }
const order_info = await ctx.service.credit.common.getOrderInfo(pay_info.order_id); const order_info = await ctx.service.credit.order.getOrderInfo(pay_info.order_id);
const nonce_str = ctx.helper.createNoncestr(); const nonce_str = ctx.helper.createNoncestr();
const url = 'https://api.mch.weixin.qq.com/pay/orderquery'; const url = 'https://api.mch.weixin.qq.com/pay/orderquery';
let data_obj = { let data_obj = {
...@@ -206,17 +147,22 @@ class CommonService extends Service { ...@@ -206,17 +147,22 @@ class CommonService extends Service {
const xml_data = ctx.helper.jsonToxml(data_obj); const xml_data = ctx.helper.jsonToxml(data_obj);
const result_wexin = await ctx.curl(url, { timeout: 3000, method: 'POST', data: xml_data, headers: { 'content-type': 'text/html', }, streaming: false, dataType: 'text', }); const result_wexin = await ctx.curl(url, { timeout: 3000, method: 'POST', data: xml_data, headers: { 'content-type': 'text/html', }, streaming: false, dataType: 'text', });
if (result_wexin.status) {
if (result_wexin.status !== 200) {
ctx.failed('wexin orderquery error1'); ctx.failed('wexin orderquery error1');
} }
const ret = await ctx.helper.xmlTojson(result_wexin.data); const ret = await ctx.helper.xmlTojson(result_wexin.data);
if (ret.return_code !== 'SUCCESS' && ret.result_code !== 'SUCCESS') {
ctx.failed('wexin orderquery error2');
}
let ret_data = {}; let ret_data = {};
for (let key in ret) { for (let key in ret) {
ret_data[key] = ret[key][0]; ret_data[key] = ret[key][0];
} }
ctx.logger.info(JSON.stringify({ query_wexinpay_param: params, query_wexinpay_result: ret_data }));
let error_msg = 'wexin orderquery error2';
if (ret_data.return_code !== 'SUCCESS' && ret_data.result_code !== 'SUCCESS') {
error_msg = ret_data.return_msg ? ret_data.return_msg : error_msg;
ctx.failed(error_msg);
}
if (ret_data.trade_state === 'SUCCESS') { if (ret_data.trade_state === 'SUCCESS') {
return false; return false;
} }
...@@ -237,8 +183,7 @@ class CommonService extends Service { ...@@ -237,8 +183,7 @@ class CommonService extends Service {
async WexinNotify(result) { async WexinNotify(result) {
const { ctx } = this; const { ctx } = this;
// && result.appid === this.config.MCH_APPID if (result && result.appid[0] === this.config.MCH_APPID) {
if (result) {
let sign_data = {}; let sign_data = {};
for (let key in result) { for (let key in result) {
sign_data[key] = result[key][0]; sign_data[key] = result[key][0];
...@@ -246,7 +191,7 @@ class CommonService extends Service { ...@@ -246,7 +191,7 @@ class CommonService extends Service {
if (sign_data.return_code && sign_data.return_code === 'SUCCESS' && sign_data.result_code && sign_data.result_code === 'SUCCESS') { if (sign_data.return_code && sign_data.return_code === 'SUCCESS' && sign_data.result_code && sign_data.result_code === 'SUCCESS') {
const wexin_return_sign = sign_data.sign; const wexin_return_sign = sign_data.sign;
delete sign_data.sign; delete sign_data.sign;
const sign = this.getSign(sign_data); const sign = this.getWexinSign(sign_data);
const out_trade_no = sign_data.out_trade_no; const out_trade_no = sign_data.out_trade_no;
const total_fee = sign_data.total_fee; const total_fee = sign_data.total_fee;
const pay_info = await ctx.prometheusModel.CreditPay.findOne({ where: { trade_no: out_trade_no }, order: [['id', 'desc']] }); const pay_info = await ctx.prometheusModel.CreditPay.findOne({ where: { trade_no: out_trade_no }, order: [['id', 'desc']] });
...@@ -257,13 +202,14 @@ class CommonService extends Service { ...@@ -257,13 +202,14 @@ class CommonService extends Service {
if (!order || !order.id) { if (!order || !order.id) {
ctx.failed('error order'); ctx.failed('error order');
} }
if (order && order.pay_status === 1) {
return true;
}
const price = order.price * 100 - order.preferential_price * 100; const price = order.price * 100 - order.preferential_price * 100;
if (total_fee == price && wexin_return_sign === sign) {
if (total_fee == price && sign == wexin_return_sign) {
const state_time = moment().format('YYYY-MM-DD HH:mm:ss'); const state_time = moment().format('YYYY-MM-DD HH:mm:ss');
await ctx.prometheusModel.CreditPay.edit({ where: { id: pay_info.id }, params: { pay_result: JSON.stringify(result), status: 1 } }); await ctx.prometheusModel.CreditPay.edit({ where: { id: pay_info.id }, params: { pay_result: JSON.stringify(result), transaction_id: sign_data.transaction_id, status: 1 } });
await ctx.prometheusModel.CreditOrder.update({ state: '已支付', state_time: state_time, pay_status: 1 }, { where: { id: order.id } }); await ctx.prometheusModel.CreditOrder.update({ state: '已支付', state_time: state_time, pay_status: 1 }, { where: { id: order.id } });
console.info('7777777777777777777777');
return true; return true;
} }
} }
...@@ -288,8 +234,9 @@ class CommonService extends Service { ...@@ -288,8 +234,9 @@ class CommonService extends Service {
for (let key in ret) { for (let key in ret) {
str += key + '=' + ret[key] + '&'; str += key + '=' + ret[key] + '&';
} }
const mch_key = this.config.MCH_KEY; const mch_key = app.config.MCH_KEY;
// console.info(str + 'key=' + mch_key);
const sign = ctx.helper.md5(str + 'key=' + mch_key).toLocaleUpperCase(); const sign = ctx.helper.md5(str + 'key=' + mch_key).toLocaleUpperCase();
return sign; return sign;
......
...@@ -137,7 +137,22 @@ class OrderService extends Service { ...@@ -137,7 +137,22 @@ class OrderService extends Service {
if (order && order.pay_status === 1) { if (order && order.pay_status === 1) {
return true; return true;
} }
if (!order || !order.id) {
ctx.failed('error order');
}
if (order && order.pay_status === 1) {
return true;
}
const credit_pay = await ctx.prometheusModel.CreditPay.one({ where: { order_id }, order: [['id', 'desc']] });
if (!credit_pay || !credit_pay.id) {
ctx.failed('error credit pay');
}
const { transaction_id, trade_no } = credit_pay;
const wexin_check_pay_ret = await ctx.service.credit.common.WexinCheckPay({ transaction_id, trade_no });
if (wexin_check_pay_ret) {
return true;
}
return false;
} }
/** /**
...@@ -183,6 +198,75 @@ class OrderService extends Service { ...@@ -183,6 +198,75 @@ class OrderService extends Service {
} }
} }
} }
}
async unifiedOrder(type, order_id) {
const { ctx } = this;
const order = await ctx.service.credit.order.getOrderInfo(order_id);
if (order && order.pay_status === 1) {
ctx.failed('订单已支付无需重复支付');
}
const price = order.price * 100 - order.preferential_price * 100;
if (price <= 0) {
ctx.failed('error price');
}
if (type === 'wexin') {
let body = '黑名单报告检测支付';
if (order.type === 2) {
body = '个人通话风险检测支付';
}
const data = {
body,
order_id,
trade_no: moment().valueOf() + ctx.helper.PrefixInteger(order_id, 11),
total_fee: price,
notify_url: 'https://dev-nginx.jianbing.com/51business/api/credit/order/pay_notice',
product_id: order.order_no,
scene_info: JSON.stringify({ h5_info: { type: 'Wap', wap_url: this.config.PHP_URL, wap_name: '我的信用' }, }),
};
const ret = await ctx.service.credit.common.WexinUnifiedOrder(data);
const pay_data = {
order_id,
trade_no: data.trade_no,
description: data.body,
prepay_id: ret.prepay_id || 0,
amount: data.total_fee,
unifiedorder_result: JSON.stringify(ret),
client_ip: ctx.helper.getClientIP(),
};
await this.addCreditPay(pay_data);
return { url: ret.mweb_url, order_id: order_id, trade_no: data.trade_no };
} else if (type === 'alipay') {
//TODO
}
return false;
}
async addCreditPay(params) {
const { ctx } = this;
const { order_id, trade_no, prepay_id, amount, description, unifiedorder_result, client_ip } = params;
const data = {
order_id,
trade_no,
prepay_id,
amount,
description,
unifiedorder_result,
client_ip,
}
const ret = await ctx.prometheusModel.CreditPay.add(data);
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment