<?php
namespace app\command;
use app\admin\model\ChargeOrder;
use app\admin\model\ExtractApply;
use app\admin\service\AdminLogService;
use app\common\exception\ExtractException;
use app\common\lib\Jwt;
use app\common\lib\Predis;
use app\index\model\User as UserModel;
use app\index\model\UserGoldLog;
use app\index\model\UserTradeLog;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\facade\Config;
use think\facade\Env;
require_once Env::get('APP_PATH').'../vendor/aliyun-oss-php-sdk-master/aop/AopCertClient.php';
require_once Env::get('APP_PATH').'../vendor/aliyun-oss-php-sdk-master/aop/request/AlipayFundTransUniTransferRequest.php';
class OrderToPayList extends Command
{
protected function configure()
{
$this->setName('orderToPayList')->setDescription('待打款订单');
}
protected function execute(Input $input, Output $output)
{
$starttime = time();
while (true) {
$msg = Predis::getInstance()->lpop('OrderToBePaid');
if ($msg) {
$data = json_decode($msg,true);
$extractModel = new ExtractApply();
$user = $data['user'];
$infos = $extractModel->where('status',1)->where('cw_audit',1)
->where('payment_status',0)->where('payment_audit_status',0)->select();
if (!$infos) {
break;
}
foreach ($infos as $info) {
$cacheKey = 'extract_order_' . $info['id'];
if (Predis::getInstance()->get($cacheKey)) {
continue;
}
Predis::getInstance()->setnx($cacheKey,1,10);
if ($info['payment_status'] != 0 || $info['payment_audit_status'] != 0) {
continue;
}
$th_ret = false;
$status = false;
if ($data['type'] == 1) {
if ($info['status'] != 1 || $info['cw_audit'] != 1) {
continue;
}
$userInfo = (new UserModel())->findUserInfoByIdInDB($info['uid']);
if (!in_array($userInfo['auth_status'],[1,2]) || empty($userInfo['idcard'])) {
continue;
}
$update_param = [
'payment_status' => 3,
'payment_audit_status' => 1,
'payment_approve_time' => time()
];
$order_info = $extractModel->where('id',$info['id'])->where('status',1)->where('cw_audit',1)
->where('payment_status',0)->where('payment_audit_status',0)->find();
if (empty($order_info)){
continue;
}
$audit_ret = $extractModel->updateStatusById($info['id'], $update_param);
if (!$audit_ret) {
continue;
}
if ($info['extract_type'] == 1) {
$tips = '';
$res = false;
$aliConfig = $info['type'] == 1 ? Config::get('AliPay.XQ' ): Config::get('AliPay.AML' );
$c = new \AopCertClient();
$appCertPath = $aliConfig['app_cert_path'];
$alipayCertPath = $aliConfig['alipay_cert_path'];
$rootCertPath = $aliConfig['alipay_root_cert_path'];
$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$c->appId = $aliConfig['app_id'];
$c->rsaPrivateKey = $aliConfig['rsa_private_key']; ;
$c->format = "json";
$c->charset= "utf-8";
$c->signType= "RSA2";
$c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
$c->isCheckAlipayPublicCert = true;
$c->appCertSN = $c->getCertSN($appCertPath);
$c->alipayRootCertSN = $c->getRootCertSN($rootCertPath);
$request = new \AlipayFundTransUniTransferRequest();
$alipaydata = [
'out_biz_no' => $info['order_id'],
'trans_amount' => $info['extract_money'],
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
'biz_scene' => 'DIRECT_TRANSFER',
'order_title' => '交易币提现',
'payee_info' => [
'identity' => $info['alipay_account'],
'identity_type' => 'ALIPAY_LOGON_ID',
'name' => $info['realname'],
],
'remark' => '提现订单号:' . $info['order_id'],
'business_params' => ['payer_show_name'=>'996盒子']
];
$request->setBizContent(json_encode($alipaydata,true));
$result = $c->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$res = $result->$responseNode;
if (!$res || $res->code != 10000) {
$tips = $tips ? $tips : $res->sub_msg;
$output->writeln($info['id']);
$dk_ret = $extractModel->updatePaymentStatusById($info['id'], 4, json_encode($res,true), $tips);
if (!$dk_ret) {
continue;
}
$th_ret = (new UserModel())->moneyCallBack($info['type'], $info['total_extract_money'] * 100, $info['uid']);
if ($th_ret !== true) {
$extractModel->updatePaymentStatusById($info['id'], 2);
continue;
}
$status = 1;
AdminLogService::instance()->recordLog($user, 11,$info['id']);
}
if ($res->code == 10000) {
AdminLogService::instance()->recordLog($user, 9,$info['id']);
$extractModel->updateStatusById($info['id'], ['payment_order_id'=>$res->order_id,'callback_value'=>json_encode($res,true),'payment_time'=>strtotime($res->trans_date),'payment_status'=>1]);
}
}
if ($info['extract_type'] == 2) {
$balance_ret = (new UserModel())->incBalanceByUid($info['uid'], $info['extract_money'] * 100);
if ($balance_ret === true) {
AdminLogService::instance()->recordLog($user, 9,$info['id']);
$extractModel->updateStatusById($info['id'], ['payment_time'=>time(),'payment_status'=>1]);
} else {
$back_ret = $extractModel->updatePaymentStatusById($info['id'], 4);
if (!$back_ret) {
continue;
}
$th_ret = (new UserModel())->moneyCallBack($info['type'], $info['total_extract_money'] * 100, $info['uid']);
if ($th_ret !== true) {
$extractModel->updatePaymentStatusById($info['id'], 2);
continue;
}
$status = 1;
AdminLogService::instance()->recordLog($user, 11,$info['id']);
}
}
} else {
$tips = isset($requestData['tips']) ? $requestData['tips'] : '';
$audit_ret = $extractModel->updateStatusById($info['id'],['payment_status'=>4,'payment_audit_status'=>2,'tips'=>$tips,'payment_approve_time'=>time()]);
if (!$audit_ret) {
continue;
}
$th_ret = (new UserModel())->moneyCallBack($info['type'],$info['total_extract_money'] * 100,$info['uid']);
if (!$th_ret) {
$extractModel->updateStatusById($info['id'],['payment_status' =>2]);
continue;
}
AdminLogService::instance()->recordLog($user, 10,$info['id']);
}
if ($th_ret ) {
$log_param = [
'type' => 1,
'relevant_id' => $info['id'],
'relevant_num' => $info['order_id'],
'uid' => $info['uid'],
'username' => $info['account'],
'origin_balance' => $info['balance'],
'alter_balance' => $info['total_extract_money'] * 100,
'final_balance' => $info['balance'] + $info['total_extract_money'] * 100,
'create_time' => time(),
'specific_types' => $info['type'] == 1 ? 16 : 12
];
$info['type'] == 1 ? (new UserGoldLog())->createGoldLog($log_param) : (new UserTradeLog())->createTradeLog($log_param);
if ($info['order_info']) {
$this->extract_cancel($info['id']);
}
if ($status) {
continue;
}
}
Predis::getInstance()->del($cacheKey);
}
} else {
sleep(5);
}
if (time() - $starttime > 3600) {
exit('timeout');
}
}
}