<?php

namespace Extend\VerifyDun;

use App\Exception\BusinessException;
use GuzzleHttp\Exception\GuzzleException;

/**
 * 银行卡三/四要素认证
 */
class BankCard extends Base
{
    /**
     * @param array $params
     * @return string
     * @throws GuzzleException
     */
    public function checkBankCard(array $params): string
    {
        try {
            // 组合请求地址
            $api_url = $this->api_url . $this->version . '/bankcard/check';

            $this->params['businessId'] = "3cb726bd85104161b25613153c4fba7c";

            $this->params = array_merge($this->params,$params);

            // 获取签名
            $this->params['signature'] = $this->gen_signature();

            $this->options["form_params"] = $this->params;

            $result = $this->httpRuest->postRequest($api_url,$this->options);

            if (empty($result)){
                return "银行卡认证失败";
            }

            if ($result['code'] != "200"){
                throw new BusinessException("银行卡认证失败");
            }

            if (empty($result['result'])){
                return "银行卡认证失败";
            }

            // 处理不通过情况
            if ($result['result']['status'] == 2){
                switch ($result['result']['reasonType']) {
                    case 2:
                        return "持卡人信息与输入信息不一致";
                        break;
                    case 3:
                        return "查无此银行卡";
                        break;
                    case 4:
                        return "查无此身份证";
                        break;
                    case 5:
                        return "手机号码格式不正确";
                        break;
                    case 6:
                        return "银行卡号不正确";
                        break;
                    case 7:
                        return "其他出错，请联系客服";
                        break;

                    default:
                        return "银行卡认证失败";
                        break;
                }
            }

            // 处理status为其他情况
            if ($result['result']['status'] != 1){
                return "身份证认证失败";
            }

            return "";
        } catch (\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }
}