<?php

namespace App\Console\Pay;

use App\Cache\UserCache;
use App\Libs\Utils;
use App\Models\Channel\Channel;
use App\Models\Pay\PayMerchant;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Ycpay\Byte;
use App\Servicess\WithdrawCashService;
use App\Services\Order\OrderService;
use Ycpay\Factory as PayFactory;


/**
 * 退款脚本
 */
class RefundCommand extends Command
{
    /**
     * @var string
     */
    protected $signature = 'order_refund {--trade_no=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单退款';


    /**
     * handle
     */
    public function handle()
    {
        // 传参
        $trade_no = $this->option('trade_no');
        \Log::info('order_refund_start:'.$trade_no);
        
        $order = OrderService::getByTradeNo($trade_no);
        \Log::info('$order:'.json_encode($order));
        $distribution_channel_id = $order->distribution_channel_id;
        
//         $channel = Channel::find($distribution_channel_id);
//         \Log::info('$channel:'.$distribution_channel_id.' param:'.json_encode($channel));
        
        $pay_merchant = PayMerchant::find($order->pay_merchant_id);
        if(empty($pay_merchant)){
            \Log::info('create_order_pay_merchant_null:'.$order->pay_merchant_id);
            
            return $this->error('50013:参数异常');
        }
        $pay_config_info = $pay_merchant->config_info;
        $pay_config_infos = json_decode($pay_config_info,true);
        
        \Log::info('create_order_pay_merchant:'.json_encode($pay_merchant));
        \Log::info('$pay_config_infos:');\Log::info($pay_config_infos);
        
        $byte = new Byte();
        
        $config = [
            'app_id'=>isset($pay_config_infos['appid'])?$pay_config_infos['appid']:'',
            'secret'=>isset($pay_config_infos['secret'])?$pay_config_infos['secret']:'',
            'notify_url'=>isset($pay_config_infos['notify_url'])?$pay_config_infos['notify_url']:'',
            'refund_notify_url'=>isset($pay_config_infos['refund_notify_url'])?$pay_config_infos['refund_notify_url']:'',
            'token'=>isset($pay_config_infos['token'])?$pay_config_infos['token']:'',
            'salt'=>isset($pay_config_infos['salt'])?$pay_config_infos['salt']:'',
            'store_uid'=>isset($pay_config_infos['store_uid'])?$pay_config_infos['store_uid']:'',// 收款商户号，1个主体下面有多个商户号
            'merchant_id'=>$order->pay_merchant_id,
            'title'=>isset($pay_config_infos['title'])?$pay_config_infos['title']:'',
            'desc'=>isset($pay_config_infos['desc'])?$pay_config_infos['desc']:'',
        ];
        \Log::info('$config:'.json_encode($config));
        
        $pay_client = PayFactory::getInstance('Byte');
        
        $order_param = [
            'out_order_no'=>$trade_no,
            'out_refund_no'=>generateOrderSn('refund-'),
            'reason'=>'退款',
            'refund_amount'=>$order->price * 100,
            'cp_extra'=>'',
            'notify_url'=>$config['refund_notify_url'],
        ];
        
        \Log::info('$order_param:');
        \Log::info(json_encode($order_param));
        
        $order_refund_res = $pay_client->init($config)->applyOrderRefund($order_param);
        if(isset($order_refund_res->err_no) && $order_refund_res->err_no == 0 &&$order_refund_res->err_tips == 'success'){
            
        }
        \Log::info('order_refund_res:'.json_encode($order_refund_res));
  
        
    }


}
