ProfitSharing.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\ProfitSharing as ProfitSharingW;
  4. use Illuminate\Console\Command;
  5. class ProfitSharing extends Command
  6. {
  7. public function getprofitsharing($bill_date){
  8. // $bill_date=$request->input('bill_date');
  9. // $sub_mchid=$request->input('sub_mchid');
  10. // $tar_type=$request->input('tar_type');
  11. $url='https://api.mch.weixin.qq.com/v3/profitsharing/bills?bill_date='.$bill_date;// .'&account_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'],'sharing');
  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_profit_sharing($result);
  50. $sharing_date = $res['sharing'][0]['sharing_date'];
  51. $sharing_info = ProfitSharingW::where('sharing_date',$sharing_date)->first();
  52. if(!$sharing_info){
  53. if(count($res['sharing']) > 1000){
  54. $arr = array_chunk($res['sharing'],1000);
  55. foreach($arr as $key=>$val){
  56. ProfitSharingW::insert($val);
  57. }
  58. }else{
  59. ProfitSharingW::insert($res['sharing']);
  60. }
  61. }
  62. return $res;
  63. }
  64. public function deal_profit_sharing($response)
  65. {
  66. $result = array();
  67. $response = str_replace(",", " ", $response);
  68. $response = explode(PHP_EOL, $response);
  69. foreach ($response as $key => $val) {
  70. if (strpos($val, '`') !== false) {
  71. $data = explode('`', $val);
  72. array_shift($data); // 删除第一个元素并下标从0开始
  73. if (count($data) == 9) { // 处理账单数据
  74. $result['sharing'][] = array(
  75. 'sharing_time' => $data[0], // 分账时间
  76. 'sharing_originator' => $data[1], // 分账发起方
  77. 'sharing_user' => $data[2], // 分账方
  78. 'wx_order_no' => $data[3], // 微信订单号
  79. 'wx_refund_no' => $data[4], // 微信分账方/回退单号
  80. 'sharing_detail_no' => $data[5], // 分账明细单号
  81. 'mch_refund_no' => $data[6], // 商户分账/回退单号
  82. 'order_amount' => $data[7], // 订单金额
  83. 'sharing_recipient' => explode(' ',$data[8])[0], // 分账接收方
  84. 'amount' => explode(' ',$data[8])[1], // 金额
  85. 'sharing_type' => explode(' ',$data[8])[2], // 业务类型
  86. 'sharing_status' => explode(' ',$data[8])[3], // 处理状态
  87. 'sharing_desc' => explode(' ',$data[8])[4], // 分账描述
  88. 'remark' => explode(' ',$data[8])[5], // 备注
  89. 'sharing_date' => date("Y-m-d", strtotime($data[0])), // 分账日期
  90. );
  91. }
  92. if(count($data) == 5){ // 统计数据
  93. $result['summary'] = array(
  94. 'sharing_num' => $data[0], // 总条数
  95. 'sharing_success_amount' => $data[1], // 分账成功出资金
  96. 'sharing_fail_turnover' => $data[2], // 分账失败已转回分账方
  97. 'thaw_funds_amount' => $data[3], // 解冻资金给分账方
  98. 'refund_amount' => $data[4], // 分账回退资金
  99. );
  100. }
  101. }
  102. }
  103. return $result;
  104. }
  105. }