XinlongService.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\jdk\xinlong\CoreApi;
  4. use addons\AvoidTax\common\models\TaxConfig;
  5. use addons\AvoidTax\common\models\TaxOrder;
  6. use addons\AvoidTax\common\models\TaxUserAuth;
  7. use common\models\common\PlatformSetting;
  8. use yii\helpers\Json;
  9. use \Exception;
  10. /**
  11. * Xinlong服务类
  12. * XinlongService class
  13. * @package addons\AvoidTax\common\service
  14. */
  15. class XinlongService extends BaseService
  16. {
  17. /**
  18. * @var CoreApi
  19. */
  20. protected $taxApi;
  21. public function __construct($config = [])
  22. {
  23. if (!isset($config['mall_id'])) {
  24. throw new Exception('未设置 MALL_ID');
  25. }
  26. parent::__construct($config);
  27. $tax_conf = $this->getConfigService()->getXLConfig();
  28. if (empty($tax_conf)) {
  29. throw new Exception('未定义配置');
  30. }
  31. $this->taxApi = new CoreApi($config['mall_id'], $tax_conf);
  32. }
  33. /**
  34. * 创建签约账户
  35. * @param $uid
  36. * @param $mobile
  37. * @param $id_card
  38. * @param $name
  39. *
  40. * @return TaxUserAuth
  41. */
  42. public function createAccount($uid, $mobile, $id_card, $name)
  43. {
  44. // $model = TaxUserAuth::getAccountUser($this->mall_id,$uid);
  45. // 如果有任何一个字段不匹配则去重新创建账户
  46. // if( !empty($model) && $model->id_card_no == $id_card ){
  47. // $model->mobile = $mobile;
  48. // $model->name = $name;
  49. // $model->save();
  50. // return $model;
  51. // }
  52. // 去创建账户
  53. $data = $this->taxApi->submitCreateUser($mobile, $id_card, $name);
  54. if (!isset($data['data']['accountId'])) {
  55. return $this->error(" 创建签约账户失败:" . __LINE__);
  56. }
  57. $model = new TaxUserAuth();
  58. $model->user_id = $uid;
  59. $model->mall_id = $this->mall_id;
  60. $model->mobile = $mobile;
  61. $model->id_card_no = $id_card;
  62. $model->name = $name;
  63. $model->default = 0;
  64. $model->accountId = $data['data']['accountId'];
  65. $model->status = TaxUserAuth::STATUS_WAIT;
  66. $model->created_at = $model->updated_at = time();
  67. if ($model->save() == false) {
  68. return $this->error(" 创建签约账户失败:" . __LINE__);
  69. }
  70. return $model;
  71. }
  72. /**
  73. * 获取签约链接
  74. * @FormData(key="参数名|中文名", rule="")
  75. * @FormData(key="参数名|中文名", rule="")
  76. * @ApiResponse(code="500", template="error")
  77. * @ApiResponse(code="200", template="success")
  78. * @param TaxUserAuth $acc_user
  79. *
  80. * @return string
  81. */
  82. public function goToSign(TaxUserAuth $acc_user, string $redirect_url = null)
  83. {
  84. if ($acc_user->status == TaxUserAuth::STATUS_OK) {
  85. return true;
  86. }
  87. if ($acc_user->status == TaxUserAuth::STATUS_WAIT) {
  88. // 如果签约中状态,查询上一次签约是否成功
  89. $status = $this->signQuery($acc_user);
  90. if ($status == TaxUserAuth::STATUS_OK) {
  91. // 签约成功
  92. return true;
  93. }
  94. }
  95. $type = \Yii::$app->request->headers['x-app-platform'] ?? 'h5';
  96. // 没有签约成功继续去签约
  97. // 异步地址
  98. $notify_url = PlatformSetting::conf('system.api_url', [], true)
  99. . '/avoid-tax/notify/user-sign?' . http_build_query(['auth_id' => $acc_user->id, 'mall_id' => $this->mall_id]);
  100. $notify_url = str_replace("https", "http", $notify_url);
  101. if (empty($redirect_url)) {
  102. // 同步地址
  103. $redirect_url = PlatformSetting::conf('system.h5_url', [], true);
  104. }
  105. if ($type == 'mp-wx') {
  106. // $path = http_build_query(['type'=>"wechat",'msg'=>"签约成功",'jump_url'=>"/pages/index/index"]);
  107. // $redirect_url = trim($redirect_url, "/") . '/#/plugins/pages/schemes/index?'.urldecode($path);
  108. // 微信小程序返回地址
  109. $redirect_url = 'wechat://back';
  110. }
  111. $data = $this->taxApi->submitSign($acc_user->accountId, $notify_url, $redirect_url);
  112. if (!isset($data['data']['longUrl'])) {
  113. return $this->error(" 获取签约链接失败:" . __LINE__);
  114. }
  115. // 修改签约状态
  116. $acc_user->status = TaxUserAuth::STATUS_WAIT;
  117. $acc_user->flowId = $data['data']['flowId'];
  118. $acc_user->auth_url = $data['data']['url'] . '|' . $redirect_url . "|" . $notify_url;
  119. $acc_user->save();
  120. $url = $data['data']['longUrl'];
  121. return $url;
  122. }
  123. /**
  124. * 签约异步回调
  125. * @FormData(key="参数名|中文名", rule="")
  126. * @FormData(key="参数名|中文名", rule="")
  127. * @ApiResponse(code="500", template="error")
  128. * @ApiResponse(code="200", template="success")
  129. * @param array $data
  130. *
  131. * @return bool
  132. */
  133. public function signNotify($auth_id, $data): bool
  134. {
  135. $str = $data['requestBody'];
  136. $str = $this->taxApi->decrypt($str);
  137. $str = preg_replace('/[\x00-\x1F\x7F-\x9F]/u', '', $str);
  138. $data = Json::decode($str);
  139. $this->_handle_sign($auth_id, $data);
  140. return true;
  141. }
  142. /**
  143. * 签约查询
  144. *
  145. * @param TaxUserAuth $userAuth
  146. * @return integer
  147. */
  148. public function signQuery(TaxUserAuth $userAuth)
  149. {
  150. $data = $this->taxApi->getEsign($userAuth->mobile, $userAuth->id_card_no, $userAuth->name);
  151. return $this->_handle_sign($userAuth->id, $data['data']);
  152. }
  153. /**
  154. * 向税务方提交订单
  155. *
  156. * @param TaxOrder $order 用户订单
  157. * @param TaxOrder $tax_order 手续费订单
  158. * @param TaxUserAuth $sign 用户签约信息
  159. * @return boolean
  160. */
  161. public function submitOrder(TaxOrder $order, TaxOrder $tax_order, TaxUserAuth $sign) : bool
  162. {
  163. if (empty($sign)) {
  164. return $this->error(" {$order['user_id']} 用户未签约 ");
  165. }
  166. $data = [
  167. 'natchno' => $order['order_sn'],
  168. 'uid' => $this->taxApi->uid,
  169. 'inAcctName' => $order['name'],
  170. 'inAcctNo' => $order['mobile'],
  171. 'tranAmount' => $order['amount'],
  172. 'mobile' => $sign['mobile'],
  173. 'idcard' => $sign['id_card_no'],
  174. 'idtypename' => '1',
  175. 'ccyCode' => 'ALIPAY',
  176. 'cardType' => 'DC',
  177. 'cardAttribute' => 'C',
  178. // 新增字段
  179. 'pay_fee' => $tax_order['amount'],
  180. 'pay_fee_order' => $tax_order['order_sn'],
  181. ];
  182. try {
  183. // 调风控接口
  184. $res = $this->taxApi->getRiskControl($data);
  185. if ($res['code'] != '0000') {
  186. // 1006 超限额
  187. return $this->error($res['message']);
  188. }
  189. // 提交成功
  190. return true;
  191. } catch (\Exception $e) {
  192. return $this->error($e->getMessage());
  193. }
  194. }
  195. /**
  196. * 签约查看
  197. *
  198. * @param int $auth_id
  199. * @param array $data
  200. * @return integer
  201. */
  202. private function _handle_sign($auth_id, $data)
  203. {
  204. if ($data['sign_status'] == 2) {
  205. // 成功
  206. TaxUserAuth::signOk($auth_id, [
  207. 'id_card_no' => $data['id_number'],
  208. 'sign_data' => Json::encode($data),
  209. ]);
  210. return TaxUserAuth::STATUS_OK;
  211. } else {
  212. // 失败
  213. TaxUserAuth::signFail($auth_id, [
  214. 'remark' => $data['remark'],
  215. 'sign_data' => Json::encode($data),
  216. ]);
  217. return TaxUserAuth::STATUS_NOT;
  218. }
  219. }
  220. /**
  221. * 支付完成后向税务方推送消息
  222. *
  223. * @param TaxOrder $order
  224. * @param TaxOrder $tax_order
  225. * @param TaxUserAuth $sign
  226. * @return boolean
  227. */
  228. public function pushPayFinish(TaxOrder $order, TaxOrder $tax_order, TaxUserAuth $sign): bool
  229. {
  230. if (empty($sign)) {
  231. return $this->error(" {$order['user_id']} 用户未签约 ");
  232. }
  233. $data = [
  234. 'natchno' => $order['order_sn'],
  235. 'outerInstNatchno' => $sign['accountId'],
  236. 'idcard' => $sign['id_card_no'],
  237. 'mobile' => $sign['mobile'],
  238. 'ccyCode' => 'ALIPAY',
  239. 'tranAmount' => $order['amount'],
  240. 'inAcctNo' => $order['mobile'],
  241. 'inAcctName' => $order['name'],
  242. 'cardType' => 'DC',
  243. 'cardAttribute' => 'C',
  244. 'fullname' => $order['name'],
  245. 'payFee' => $tax_order['amount'],
  246. 'payFeeOrder' => $tax_order['order_sn'],
  247. ];
  248. try {
  249. // 调支付完成接口
  250. $res = $this->taxApi->withdrawOrder($data);
  251. if ($res['code'] != '0000') {
  252. // 1006 超限额
  253. return $this->error($res['message']);
  254. }
  255. // 提交成功
  256. return true;
  257. } catch (\Exception $e) {
  258. return $this->error($e->getMessage());
  259. }
  260. }
  261. /**
  262. * 查询封控
  263. *
  264. * @param TaxOrder $order
  265. * @param TaxUserAuth $sign
  266. * @return boolean
  267. */
  268. public function getRiskControl(TaxOrder $order, TaxUserAuth $sign)
  269. {
  270. $data = [
  271. 'natchno' => $order['order_sn'],
  272. 'uid' => $this->getConfigService()->getUid(),
  273. 'idcard' => $sign->id_card_no,
  274. 'idtypename' => '1',
  275. 'inAcctName' => $order['name'],
  276. 'inAcctNo' => $order['mobile'],
  277. 'ccyCode' => 'ALIPAY',
  278. 'cardType' => 'DC',
  279. 'tranAmount' => $order['amount'],
  280. 'cardAttribute' => 'C',
  281. 'mobile' => $order['mobile'],
  282. ];
  283. try {
  284. // 封控接口
  285. $res = $this->taxApi->getRiskControl($data);
  286. if ($res['code'] != '0000') {
  287. // 1006 超限额
  288. return $this->error($res['message']);
  289. }
  290. // 提交成功
  291. return true;
  292. } catch (\Exception $e) {
  293. return $this->error($e->getMessage());
  294. }
  295. }
  296. }