Bootstrap

异步处理支付宝提现订单

<?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('待打款订单');
    }

    /**
     * 这里是具体业务的实现
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     * @throws \ErrorException
     */
    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_order_id' => $orderNo,
                            'payment_status' => 3,
                            'payment_audit_status' => 1,
                            'payment_approve_time' => time()
                        ];
                        //todo  判断订单状态
                        $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";
                            //调用getPublicKey从支付宝公钥证书中提取公钥
                            $c->alipayrsaPublicKey = $c->getPublicKey($alipayCertPath);
                            //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
                            $c->isCheckAlipayPublicCert = true;
                            //调用getCertSN获取证书序列号
                            $c->appCertSN = $c->getCertSN($appCertPath);
                            //调用getRootCertSN获取支付宝根证书序列号
                            $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');
            }
        }

    }
;