LuYongService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\jdk\luyong\BaseApi;
  4. use addons\AvoidTax\common\models\LyImportSignUser;
  5. use addons\AvoidTax\common\models\LySilentSign;
  6. use addons\AvoidTax\common\models\TaxOrder;
  7. use Exception;
  8. use yii\helpers\Json;
  9. /**
  10. * LuYongService class
  11. * @package addons\AvoidTax\common\service
  12. */
  13. class LuYongService extends BaseService
  14. {
  15. /**
  16. * @var BaseApi
  17. */
  18. private $api;
  19. /**
  20. * @var array
  21. */
  22. protected $response;
  23. /**
  24. * @return array
  25. */
  26. public function getResponse()
  27. {
  28. return $this->response;
  29. }
  30. public function init()
  31. {
  32. parent::init();
  33. $service = new TaxConfigService(['mall_id' => $this->mall_id]);
  34. $config = $service->getLYConfig();
  35. // 去除空格
  36. $config = array_map(function($item) {
  37. return trim($item);
  38. }, $config);
  39. $this->api = new BaseApi([
  40. 'email' => $config['email'],
  41. 'pwd' => $config['pwd'],
  42. 'accountId' => $config['accountId'],
  43. 'publicKey' => $config['publicKey'],
  44. 'privateKey' => $config['privateKey'],
  45. ]);
  46. // $this->api->setTest();
  47. }
  48. /**
  49. * 无感电签
  50. *
  51. * @param integer $uid
  52. * @param string $name
  53. * @param string $phone
  54. * @param string $id_card
  55. * @return boolean
  56. */
  57. public function silentSign(int $uid, string $name, string $phone, string $id_card)
  58. {
  59. try {
  60. $this->response = $this->api->silentSign($name, $phone, $id_card);
  61. LySilentSign::addSilentSign(
  62. $this->mall_id,
  63. $uid,
  64. $name,
  65. $phone,
  66. $id_card,
  67. $this->api->accountId,
  68. $this->response
  69. );
  70. if ($this->response['code'] == '0000') {
  71. return true;
  72. }
  73. return $this->error($this->response['msg']);
  74. } catch (Exception $e) {
  75. return $this->error($e->getMessage());
  76. }
  77. return true;
  78. }
  79. /**
  80. * 查询身份证是否签约
  81. *
  82. * @param string $idCard
  83. * @return boolean
  84. */
  85. public function querySignUserByIdCard(string $idCard)
  86. {
  87. try {
  88. $this->response = $this->api->querySignUserByIdCard($idCard);
  89. if ($this->response['code'] == 200 && $this->response['data']['status'] == 1) {
  90. return true;
  91. }
  92. return $this->error($this->response['msg']);
  93. } catch (Exception $e) {
  94. return $this->error($e->getMessage());
  95. }
  96. }
  97. /**
  98. * 查询用户是否注册
  99. *
  100. * @param string $idCard
  101. * @return boolean
  102. */
  103. public function queryRegisterUser(string $idCard)
  104. {
  105. try {
  106. $this->response = $this->api->queryRegisterUser($idCard);
  107. if ($this->response['code'] == 200 && $this->response['data']['result'] == true) {
  108. return true;
  109. }
  110. return $this->error($this->response['msg']);
  111. } catch (Exception $e) {
  112. return $this->error($e->getMessage());
  113. }
  114. }
  115. /**
  116. * 批量导入签约用户接口
  117. *
  118. * @param integer $uid
  119. * @param string $name
  120. * @param string $phone
  121. * @param string $idCard
  122. * @return boolean
  123. */
  124. public function importSignUser(int $uid, string $name, string $phone, string $idCard)
  125. {
  126. $batchNum = date('YmdHis') . mt_rand(100, 999);
  127. $signUsers = [compact('name', 'phone', 'idCard')];
  128. try {
  129. $this->response = $this->api->importSignUser($batchNum, $signUsers);
  130. LyImportSignUser::addImportSignUser(
  131. $uid, $this->mall_id, $batchNum, $signUsers, $this->response
  132. );
  133. if ($this->response['code'] == 200 && $this->response['msg'] == '处理成功') {
  134. return true;
  135. }
  136. return $this->error($this->response['msg']);
  137. } catch (Exception $e) {
  138. return $this->error($e->getMessage());
  139. }
  140. }
  141. /**
  142. * 支付时异步请求接口
  143. *
  144. * @param TaxOrder $order 用户提现的订单
  145. * @param TaxOrder $tax_order 手续费的订单
  146. * @param LySilentSign $sign 用户签约信息
  147. * @return boolean
  148. */
  149. public function asyncTransferNotify(TaxOrder $order, TaxOrder $tax_order, LySilentSign $sign)
  150. {
  151. $batchNo = $order->batch_num;
  152. try {
  153. $this->response = $this->api->asyncTransferNotify($batchNo);
  154. if ($this->response['code'] == 200) {
  155. return true;
  156. }
  157. return $this->error($this->response['msg']);
  158. } catch (Exception $e) {
  159. return $this->error($e->getMessage());
  160. }
  161. }
  162. /**
  163. * 订单失败通知
  164. *
  165. * @param TaxOrder $user_order
  166. * @return boolean
  167. */
  168. public function asyncTransferResultNotifyNo(TaxOrder $user_order)
  169. {
  170. try {
  171. $this->response = $this->api->asyncTransferResultNotifyNo(
  172. $user_order->order_sn
  173. );
  174. if ($this->response['code'] == 200) {
  175. return true;
  176. }
  177. return $this->error($this->response['msg']);
  178. } catch (Exception $e) {
  179. return $this->error($e->getMessage());
  180. }
  181. }
  182. /**
  183. * 订单完成通知
  184. *
  185. * @param TaxOrder $order
  186. * @return boolean
  187. */
  188. public function asyncTransferResultNotifyOk(TaxOrder $user_order)
  189. {
  190. try {
  191. $alipay = Json::decode($user_order->alipay_response);
  192. $this->response = $this->api->asyncTransferResultNotifyOk(
  193. $user_order->order_sn,
  194. $alipay['pay_fund_order_id'] // 支付宝成功订单号
  195. );
  196. if ($this->response['code'] == 200) {
  197. return true;
  198. }
  199. return $this->error($this->response['msg']);
  200. } catch (Exception $e) {
  201. return $this->error($e->getMessage());
  202. }
  203. }
  204. /**
  205. * 打款批次风控接口
  206. *
  207. * @return boolean
  208. */
  209. public function riskCheck(TaxOrder $order, LySilentSign $sign)
  210. {
  211. try {
  212. $this->response = $this->api->riskCheck($order->batch_num, [
  213. [
  214. 'name' => $sign->name,
  215. 'idCard' => $sign->id_card,
  216. 'afterTaxMoney' => $order->amount,
  217. 'remark' => '',
  218. 'account' => $order->mobile,
  219. 'bizOutNo' => $order->order_sn,
  220. ]
  221. ]);
  222. if ($this->response['code'] == 200) {
  223. return true;
  224. }
  225. return $this->error($this->response['msg']);
  226. } catch (Exception $e) {
  227. return $this->error($e->getMessage());
  228. }
  229. }
  230. }