微信支付按天提供微信支付账户的资金流水账单文件,电商平台可以通过该接口获取二级商户账单文件的下载地址。文件内包含电商平台二级商户资金操作相关的业务单号、收支金额、记账时间等信息,供电商平台进行核对。
注意:
• 调用该接口前,电商平台需提前在【服务商平台 -> 产品中心】开通“下载二级商户资金账单”产品权限。
• 资金账单中的数据反映的是电商平台二级商户微信支付账户资金变动情况;
• 当日账单在次日上午9点开始生成,建议商户在上午10点以后获取;
• 资金账单中涉及金额的字段单位为“元”;
• 该接口只能下载三个月以内的资金流水账单。
//获取二级商户资金账单
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');
}
请问这个接口可用了吗?服务商调用申请单个子商户资金账单API,需要在【服务商平台 -> 产品中心】开通“下载二级商户资金账单”产品权限,但是在服务商平台并未找到该产品,请问如何处理?