Client.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace crmeb\services\easywechat\merchant;
  11. use crmeb\services\easywechat\BaseClient;
  12. use think\exception\ValidateException;
  13. /**
  14. * Class Client.
  15. *
  16. * @author ClouderSky <clouder.flow@gmail.com>
  17. */
  18. class Client extends BaseClient
  19. {
  20. protected $isService = true;
  21. /**
  22. * 二级商户进件成为微信支付商户
  23. * @param $params
  24. * @return mixed
  25. * @author Qinii
  26. * @day 6/24/21
  27. */
  28. public function submitApplication($params)
  29. {
  30. $params = $this->processParams($params);
  31. $res = $this->request('/v3/ecommerce/applyments/', 'POST', ['sign_body' => json_encode($params, JSON_UNESCAPED_UNICODE)], true);
  32. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  33. return $res;
  34. }
  35. /**
  36. * 申请单ID查询申请状态
  37. * @param $id
  38. * @return mixed
  39. * @author Qinii
  40. * @day 6/24/21
  41. */
  42. public function getApplicationById($id)
  43. {
  44. $url = '/v3/ecommerce/applyments/'.$id;
  45. $res = $this->request($url, 'GET');
  46. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  47. return $res;
  48. }
  49. /**
  50. * 业务申请编号查询申请状
  51. * @param $no
  52. * @return mixed
  53. * @author Qinii
  54. * @day 6/24/21
  55. */
  56. public function getApplicationByNo($no)
  57. {
  58. $url = '/v3/ecommerce/applyments/out-request-no/'.$no;
  59. $res = $this->request($url, 'GET');
  60. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  61. return $res;
  62. }
  63. /**
  64. * 修改结算账号
  65. * @param $mchid
  66. * @param $params
  67. * @return mixed
  68. * @author Qinii
  69. * @day 6/24/21
  70. */
  71. public function updateSubMerchat($mchid,$params)
  72. {
  73. $url = "/v3/apply4sub/sub_merchants/{$mchid}/modify-settlement";
  74. $res = $this->request($url, 'POST',['sign_body' => json_encode($params, JSON_UNESCAPED_UNICODE)], true);
  75. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  76. return $res;
  77. }
  78. /**
  79. * 查询结算账户
  80. * @param $mchid
  81. * @return mixed
  82. * @author Qinii
  83. * @day 6/24/21
  84. */
  85. public function getSubMerchant($mchid)
  86. {
  87. $url = "/v3/apply4sub/sub_merchants/{$mchid}/settlement";
  88. $res = $this->request($url, 'GET');
  89. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  90. return $res;
  91. }
  92. /**
  93. * 添加分账接收方
  94. * @param array $params
  95. * @return mixed
  96. * @author Qinii
  97. * @day 6/24/21
  98. */
  99. public function profitsharingAdd(array $params)
  100. {
  101. $url = '/v3/ecommerce/profitsharing/receivers/add';
  102. $app_id = !empty($this->app->config->app_id) ? $this->app->config->app_id : $this->app->config->routine_appId;
  103. $params['appid'] = $app_id;
  104. $options['sign_body'] = json_encode($params,JSON_UNESCAPED_UNICODE);
  105. $res = $this->request($url, 'POST',$options,true);
  106. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  107. return $res;
  108. }
  109. /**
  110. * 删除分账接收方
  111. * @param array $params
  112. * @return mixed
  113. * @author Qinii
  114. * @day 6/24/21
  115. */
  116. public function profitsharingDel(array $params)
  117. {
  118. $url = '/v3/ecommerce/profitsharing/receivers/delete';
  119. $app_id = !empty($this->app->config->app_id) ? $this->app->config->app_id : $this->app->config->routine_appId;
  120. $params['appid'] = $app_id;
  121. $options['sign_body'] = json_encode($params,JSON_UNESCAPED_UNICODE);
  122. $res = $this->request($url, 'POST',$options,true);
  123. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  124. return $res;
  125. }
  126. /**
  127. * 上传图片
  128. * @param $filepath
  129. * @param $filename
  130. * @author Qinii
  131. * @day 6/21/21
  132. */
  133. public function upload($filepath,$filename)
  134. {
  135. $boundary = uniqid();
  136. try{
  137. // $file = file_get_contents($filepath);
  138. $file = fread(fopen($filepath,'r'),filesize($filepath));
  139. }catch (\Exception $exception){
  140. throw new ValidateException($exception->getMessage());
  141. }
  142. $options['headers'] = ['Content-Type' => 'multipart/form-data;boundary='.$boundary];
  143. $options['sign_body'] = json_encode(['filename' => $filename,'sha256' => hash_file("sha256",$filepath)]);
  144. $boundaryStr = "--{$boundary}\r\n";
  145. $body = $boundaryStr;
  146. $body .= 'Content-Disposition: form-data; name="meta"'."\r\n";
  147. $body .= 'Content-Type: application/json'."\r\n";
  148. $body .= "\r\n";
  149. $body .= $options['sign_body']."\r\n";
  150. $body .= $boundaryStr;
  151. $body .= 'Content-Disposition: form-data; name="file"; filename="'.$filename.'"'."\r\n";
  152. $body .= 'Content-Type: image/jpeg'.';'."\r\n";
  153. $body .= "\r\n";
  154. $body .= $file."\r\n";
  155. $body .= "--{$boundary}--";
  156. $options['data'] = (($body));
  157. try {
  158. $res = $this->request('/v3/merchant/media/upload', 'POST', $options, true);
  159. }catch(\Exception $exception){
  160. throw new ValidateException($exception->getMessage());
  161. }
  162. if(isset($res['code'])) throw new ValidateException('[微信接口返回]:' . $res['message']);
  163. return $res;
  164. }
  165. }