123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 'use strict';
- const Service = require('egg').Service;
- const WxPay = require('wechatpay-node-v3');
- const fs = require('fs');
- const path = require('path');
- class BusinessPayService extends Service {
-
- async transfer(data) {
- const that = this;
- const userInfo = await that.app.model.Users.findOne({
- where: { user_id: data.user_id },
- attributes: [ 'openid' ],
- raw: true,
- });
- const keys = [ 'wx_pay_serial_no', 'wechat_account_appid', 'wechat_pay_key', 'wx_pay_mchid' ];
- const config = await that.service.configs.getConfigMoreValue(keys);
-
- const wxPay = new WxPay({
- appid: config.wechat_account_appid,
- mchid: config.wx_pay_mchid,
- serial_no: config.wx_pay_serial_no,
- key: config.wechat_pay_key,
-
- publicKey: fs.readFileSync(path.join(__dirname, './apiclient_cert.pem'), 'utf8'),
-
- privateKey: fs.readFileSync(path.join(__dirname, './apiclient_key.pem'), 'utf8'),
- });
- const certificates = await wxPay.get_certificates(config.wechat_pay_key);
-
- const certificate = certificates.pop();
-
- const timestamp = parseInt(+new Date() + '')
- .toString();
-
- const cashAmount = Math.floor(data.cash_amount * 100);
- const bodyData = {
- out_batch_no: 'cash' + timestamp,
- batch_name: '餐币提现',
- batch_remark: data.remark ? data.remark : '餐币提现',
- total_amount: cashAmount,
- total_num: 1,
- wx_serial_no: certificate.serial_no,
- transfer_detail_list: [
- {
- out_detail_no: 'cashd' + timestamp,
- transfer_amount: cashAmount,
- transfer_remark: data.remark ? data.remark : '餐币提现',
- openid: userInfo.openid,
-
- },
- ],
- };
- const res = await wxPay.batches_transfer(bodyData);
- if (res.status == 200) {
-
-
- const logData = {
- out_batch_no: bodyData.out_batch_no,
- out_detail_no: 'cashd' + timestamp,
- batch_id: res.data.batch_id,
- detail_status: 'INIT',
- batch_name: '餐币提现',
- batch_remark: data.remark ? data.remark : '餐币提现',
- user_id: data.user_id,
- partner_id: data.partner_id,
- cash: data.cash_amount,
- type: 2,
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- };
- await that.service.cash.addCashLog(logData);
-
- const logParams = {
- user_id: data.user_id, type: 2, account: -data.cash_amount, log_desc: '提现转帐中...',
- out_batch_no: logData.out_batch_no, out_detail_no: logData.out_detail_no,
- partner_id: data.partner_id,
- };
- await that.service.diningCoin.addDiningCoinChangeLog(logParams);
- setTimeout(() => {
-
- that.getTransferResult(logData);
- }, 12000);
- } else {
- that.logger.error('商铺发起提现失败:status %s ,errorCode:%s ,msg:%s', res.status, res.error.code, res.error.message);
- }
- return res;
- }
-
- async getTransferResult(param) {
- const that = this;
- const keys = [ 'wx_pay_serial_no', 'wechat_account_appid', 'wechat_pay_key', 'wx_pay_mchid' ];
- const config = await that.service.configs.getConfigMoreValue(keys);
-
- const wxPay = new WxPay({
- appid: config.wechat_account_appid,
- mchid: config.wx_pay_mchid,
- serial_no: config.wx_pay_serial_no,
- key: config.wechat_pay_key,
-
- publicKey: fs.readFileSync(path.join(__dirname, './apiclient_cert.pem'), 'utf8'),
-
- privateKey: fs.readFileSync(path.join(__dirname, './apiclient_key.pem'), 'utf8'),
- });
-
- const res = await wxPay.query_batches_transfer_detail({
- out_batch_no: param.out_batch_no,
- out_detail_no: param.out_detail_no,
- });
- if (res.status == 200) {
- let cashType;
- let coinLogType;
- let log_desc;
- if (res.data.detail_status == 'SUCCESS') {
- cashType = 1;
- coinLogType = 0;
- log_desc = '提现成功';
-
- const updateBean = await that.app.comoBean.instance({
- account: that.app.Sequelize.literal('account - ' + param.cash),
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- }, { where: { user_id: param.user_id, partner_id: param.partner_id, expired: false } });
- await that.service.base.update(updateBean, that.app.model.DinnerCoins, '商家餐饮币余额更新失败,请重试');
- } else if (res.data.detail_status == 'FAIL') {
- cashType = 0;
- coinLogType = -1;
- log_desc = '提现失败';
- } else {
- cashType = 2;
- coinLogType = 2;
- log_desc = '提现转账中...';
- }
-
- const updateBean = await that.app.comoBean.instance({
- type: cashType,
- detail_id: res.data.detail_id,
- detail_status: res.data.detail_status,
- fail_reason: res.data.fail_reason,
- update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- }, {
- where: {
- out_batch_no: param.out_batch_no,
- out_detail_no: param.out_detail_no,
- },
- });
- await that.service.base.update(updateBean, that.app.model.UsersCashLogs, '更新提现记录失败');
-
- const commBean = await that.app.comoBean.instance({
- type: coinLogType,
- log_desc,
- }, {
- where: {
- out_batch_no: param.out_batch_no,
- out_detail_no: param.out_detail_no,
- },
- });
- await that.service.base.update(commBean, that.app.model.DinnerCoinLogs, '更新餐币变化失败');
- } else {
- that.logger.error('提现状态查询失败:status %s ,msg:400 PARAM_ERROR参数错误;404 NOT_FOUND记录不存在;429 FREQUENCY_LIMITED频率超限',
- res.status);
- }
- return res;
- }
- }
- module.exports = BusinessPayService;
|