申请二级商户资金账单

2023-01-03  本文已影响0人  毕德申

//获取二级商户资金账单

    public function getecommercebill(Request $request){

        $bill_date=$request->input('bill_date');

        $account_type=$request->input('account_type');

        $algorithm=$request->input('algorithm');

        $url='https://api.mch.weixin.qq.com/v3/ecommerce/bill/fundflowbill?bill_date='.$bill_date.'&account_type='.$account_type.'&algorithm='.$algorithm;

        $merchant_id=config('wechat.payment.default.mch_id');//商户号

        $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号

        $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文

        $timestamp=time();//时间戳

        $nonce=$this->nonce_str();//随机字符串

        $body="";

        $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名

        $header=[

            'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,

            'Accept:application/json',

            'User-Agent:' . $merchant_id,

            'Content-Type:application/json',

            'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号

        ];

        $result=$this->curl($url,'',$header,'GET');

        $res=json_decode($result,true);

        if(!array_key_exists('download_bill_list',$res)){

            Log::info($res);

            return $this->success($res);

        }

        Log::info($bill_date.'下载资金账单,页数为'.count($res['download_bill_list']));

        foreach($res['download_bill_list'] as $key=>$val){

            $ciphertext= $this->down($val['download_url']);

            $key=$this->getDecrypt($val['encrypt_key'],'',$val['nonce']);

            //下载账单文件,得到账单文件密文ciphertext,需要对密文进行base64编码,然后再进行解密。否则密文乱码,解密返回false  

$result_str=$this->decryptToString('',$val['nonce'],base64_encode($ciphertext),$key);

            $res_arr=$this->deal_ecommerce_bill($result_str);

    }

        return $this->success($res);

    }

public function down($url){

        $merchant_id=config('wechat.payment.default.mch_id');//商户号

        $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号

        $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文

        $timestamp=time();//时间戳

        $nonce=$this->nonce_str();//随机字符串

        $body="";

        $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名

        $header=[

            'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,

            'Accept:application/json',

            'User-Agent:' . $merchant_id,

            'Content-Type:application/json;charset=utf-8',

            'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号

        ];

        $result=$this->curl($url,'',$header,'GET');

return $result;

}

//解密加密密钥

    public function getDecrypt($ciphertext, $associatedData, $nonceStr){

        $mch_private_key= file_get_contents(config('wechat.payment.default.key_path'));

//        Log::info($mch_private_key);

        $str = base64_decode($ciphertext);

        openssl_private_decrypt($str,$encrypted,$mch_private_key,OPENSSL_PKCS1_OAEP_PADDING);

        return trim($encrypted);

    }

    //解密返回的信息

    public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){

        if (strlen($aesKey) != 32) {

            throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');

        }

        $ciphertext=\base64_decode($ciphertext);

        if (strlen($ciphertext) <= 16) {

return false;

        }

        if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&

            \sodium_crypto_aead_aes256gcm_is_available()) {

            return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

        }

        if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&

            \Sodium\crypto_aead_aes256gcm_is_available()) {

            return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

        }

        if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){

            $ctext=substr($ciphertext,0,-16);

            $authTag=substr($ciphertext,-16);

            return \openssl_decrypt(

                $ctext,

                'aes-256-gcm',

                $aesKey,

                \OPENSSL_RAW_DATA,

                $nonceStr,

                $authTag,

                $associatedData

            );

        }

        throw new \RuntimeException('php7.1');

    }

上一篇下一篇

猜你喜欢

热点阅读