TradeBill.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\TradeBill as TradeBillW;
  4. use Illuminate\Console\Command;
  5. class TradeBill extends Command
  6. {
  7. public function getDownWithdraw($bill_date){
  8. // $bill_type=$request->input('bill_type');
  9. // $bill_date=$request->input('bill_date');
  10. // $tar_type=$request->input('tar_type');
  11. $url='https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date='.$bill_date;// .'&bill_type='.$bill_type.'&tar_type='.$tar_type
  12. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  13. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  14. $handle=new SignHandler();
  15. $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  16. $timestamp=time();//时间戳
  17. $nonce=$handle->nonce_str();//随机字符串
  18. $body="";
  19. $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  20. $header=[
  21. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  22. 'Accept:application/json',
  23. 'User-Agent:' . $merchant_id,
  24. 'Content-Type:application/json',
  25. 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
  26. ];
  27. $result=$handle->curl($url,'',$header,'GET');
  28. $res=json_decode($result,true);
  29. $con= $this->down($res['download_url']);
  30. // return $this->success($con);
  31. }
  32. public function down($url){
  33. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  34. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  35. $handle=new SignHandler();
  36. $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  37. $timestamp=time();//时间戳
  38. $nonce=$handle->nonce_str();//随机字符串
  39. $body="";
  40. $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  41. $header=[
  42. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  43. 'Accept:application/json',
  44. 'User-Agent:' . $merchant_id,
  45. 'Content-Type:application/json',
  46. 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
  47. ];
  48. $result=$handle->curl($url,'',$header,'GET');
  49. $res = $this->deal_WeChat_response($result);
  50. $trade_date = $res['bill'][0]['trade_date'];
  51. $trade_info = TradeBillW::where('trade_date',$trade_date)->first();
  52. if(!$trade_info){
  53. if(count($res['bill']) > 1000){
  54. $arr = array_chunk($res['bill'],1000);
  55. foreach($arr as $key=>$val){
  56. TradeBillW::insert($val);
  57. }
  58. }else{
  59. TradeBillW::insert($res['bill']);
  60. }
  61. }
  62. return $res;
  63. }
  64. public function deal_WeChat_response($response){
  65. $result = array();
  66. $response = str_replace(","," ",$response);
  67. $response = explode(PHP_EOL, $response);
  68. foreach ($response as $key=>$val){
  69. if(strpos($val, '`') !== false){
  70. $data = explode('`', $val);
  71. array_shift($data); // 删除第一个元素并下标从0开始
  72. if(count($data) == 27){ // 处理账单数据
  73. $result['bill'][] = array(
  74. 'pay_time' => $data[0], // 支付时间
  75. 'app_id' => $data[1], // app_id
  76. 'mch_id' => $data[2], // 商户id
  77. 'sub_mch_id' => $data[3], // 特约商户id
  78. 'imei' => $data[4], // 设备号
  79. 'order_sn_wx' => $data[5], // 微信订单号
  80. 'order_sn_sh' => $data[6], // 商户订单号
  81. 'user_tag' => $data[7], // 用户标识
  82. 'pay_type' => $data[8], // 交易类型
  83. 'pay_status' => $data[9], // 交易状态
  84. 'bank' => $data[10], // 付款银行
  85. 'money_type' => $data[11], // 货币种类
  86. 'total_amount' => $data[12], // 应结订单金额
  87. 'coupon_amount' => $data[13], // 代金券金额
  88. 'refund_number_wx' => $data[14], // 微信退款单号
  89. 'refund_number_sh' => $data[15], // 商户退款单号
  90. 'refund_amount' => $data[16], // 退款金额
  91. 'coupon_refund_amount' => $data[17], // 充值券退款退款金额
  92. 'refund_type' => $data[18], // 退款类型
  93. 'refund_status' => $data[19], // 退款状态
  94. 'goods_name' => $data[20], // 商品名称
  95. 'goods_data_bag' => $data[21], // 商户数据包
  96. 'service_charge' => $data[22], // 手续费
  97. 'rate' => $data[23], // 费率
  98. 'order_account' => $data[24], // 订单金额
  99. 'apply_refund_account' => $data[25], // 申请退款金额
  100. 'rate_remark' => $data[26], // 费率备注
  101. 'trade_date' => date("Y-m-d", strtotime($data[0]))
  102. );
  103. }
  104. if(count($data) == 7){ // 统计数据
  105. $result['summary'] = array(
  106. 'order_num' => $data[0], // 总交易单数
  107. 'turnover' => $data[1], // 应结订单总金额
  108. 'refund_turnover' => $data[2], // 退款总金额
  109. 'coupon_turnover' => $data[3], // 充值券退款总金额
  110. 'rate_turnover' => $data[4], // 手续费总金额
  111. 'order_turnover' => $data[5], // 订单总金额
  112. 'apply_refund_turnover' => $data[6], // 申请退款总金额
  113. );
  114. }
  115. }
  116. }
  117. return $result;
  118. }
  119. }