123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Handlers;
- use App\Models\TradeBill as TradeBillW;
- use Illuminate\Console\Command;
- class TradeBill extends Command
- {
- public function getDownWithdraw($bill_date){
- $url='https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date='.$bill_date;
- $merchant_id=config('wechat.payment.default.mch_id');
- $serial_no=config('wechat.payment.default.serial_no');
- $handle=new SignHandler();
- $mch_private_key=$handle->getPublicKey();
- $timestamp=time();
- $nonce=$handle->nonce_str();
- $body="";
- $sign=$handle->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:' . $handle->getzhengshu()
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $res=json_decode($result,true);
- $con= $this->down($res['download_url']);
- }
- public function down($url){
- $merchant_id=config('wechat.payment.default.mch_id');
- $serial_no=config('wechat.payment.default.serial_no');
- $handle=new SignHandler();
- $mch_private_key=$handle->getPublicKey();
- $timestamp=time();
- $nonce=$handle->nonce_str();
- $body="";
- $sign=$handle->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:' . $handle->getzhengshu()
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $res = $this->deal_WeChat_response($result);
- $trade_date = $res['bill'][0]['trade_date'];
- $trade_info = TradeBillW::where('trade_date',$trade_date)->first();
- if(!$trade_info){
- if(count($res['bill']) > 1000){
- $arr = array_chunk($res['bill'],1000);
- foreach($arr as $key=>$val){
- TradeBillW::insert($val);
- }
- }else{
- TradeBillW::insert($res['bill']);
- }
- }
- return $res;
- }
- public function deal_WeChat_response($response){
- $result = array();
- $response = str_replace(","," ",$response);
- $response = explode(PHP_EOL, $response);
- foreach ($response as $key=>$val){
- if(strpos($val, '`') !== false){
- $data = explode('`', $val);
- array_shift($data);
- if(count($data) == 27){
- $result['bill'][] = array(
- 'pay_time' => $data[0],
- 'app_id' => $data[1],
- 'mch_id' => $data[2],
- 'sub_mch_id' => $data[3],
- 'imei' => $data[4],
- 'order_sn_wx' => $data[5],
- 'order_sn_sh' => $data[6],
- 'user_tag' => $data[7],
- 'pay_type' => $data[8],
- 'pay_status' => $data[9],
- 'bank' => $data[10],
- 'money_type' => $data[11],
- 'total_amount' => $data[12],
- 'coupon_amount' => $data[13],
- 'refund_number_wx' => $data[14],
- 'refund_number_sh' => $data[15],
- 'refund_amount' => $data[16],
- 'coupon_refund_amount' => $data[17],
- 'refund_type' => $data[18],
- 'refund_status' => $data[19],
- 'goods_name' => $data[20],
- 'goods_data_bag' => $data[21],
- 'service_charge' => $data[22],
- 'rate' => $data[23],
- 'order_account' => $data[24],
- 'apply_refund_account' => $data[25],
- 'rate_remark' => $data[26],
- 'trade_date' => date("Y-m-d", strtotime($data[0]))
- );
- }
- if(count($data) == 7){
- $result['summary'] = array(
- 'order_num' => $data[0],
- 'turnover' => $data[1],
- 'refund_turnover' => $data[2],
- 'coupon_turnover' => $data[3],
- 'rate_turnover' => $data[4],
- 'order_turnover' => $data[5],
- 'apply_refund_turnover' => $data[6],
- );
- }
- }
- }
- return $result;
- }
- }
|