DragondepositController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/10/15
  6. * Time: 14:39
  7. */
  8. namespace app\payment\controllers;
  9. use app\backend\modules\refund\services\RefundOperationService;
  10. use app\backend\modules\refund\services\RefundMessageService;
  11. use app\common\events\order\AfterOrderRefundSuccessEvent;
  12. use app\common\helpers\Url;
  13. use app\common\models\AccountWechats;
  14. use app\payment\PaymentController;
  15. use app\common\services\Pay;
  16. use app\frontend\modules\order\services\OrderService;
  17. use Yunshop\DragonDeposit\common\LcgLog;
  18. use Yunshop\DragonDeposit\common\sdk\business\Notice;
  19. use Yunshop\DragonDeposit\models\LcgRefund;
  20. use Yunshop\DragonDeposit\models\LcgWithdraw;
  21. use Yunshop\DragonDeposit\models\Merchant;
  22. use Yunshop\DragonDeposit\models\MerchantMajor;
  23. use Yunshop\DragonDeposit\models\MerchantOrder;
  24. use Yunshop\DragonDeposit\models\MerchantUpdate;
  25. use Yunshop\DragonDeposit\services\OrderTypeService;
  26. class DragondepositController extends PaymentController
  27. {
  28. //支付类型
  29. protected $pay_type = [
  30. 'A' => [
  31. 'name' => '存管银行-余额支付',
  32. 'id' => 42,
  33. ],
  34. 'G' => [
  35. 'name' => '存管银行-银行卡支付',
  36. 'id' => 43,
  37. ],
  38. ];
  39. protected $parameters;
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. if (empty(\YunShop::app()->uniacid)) {
  44. $this->setShopUniacid();
  45. }
  46. if (!app('plugins')->isEnabled('dragon-deposit')) {
  47. echo 'Not turned on dragon deposit';exit();
  48. }
  49. LcgLog::debug('GET------------------------', $_GET);
  50. LcgLog::debug('POST------------------------', $_POST);
  51. $this->parameterDecryption();
  52. }
  53. public function setShopUniacid()
  54. {
  55. $i = $_GET['i'];
  56. if (isset($i)) {
  57. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $i;
  58. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  59. }
  60. }
  61. public function parameterDecryption()
  62. {
  63. $return_info = (new Notice())->asynchroNotice($_POST);
  64. if (isset($return_info['returnCode'])) {
  65. //todo 记录接口返回错误信息
  66. LcgLog::debug('通知错误----------------------->', $return_info);
  67. echo $return_info['returnCode'];exit();
  68. }
  69. $this->parameters = $return_info;
  70. }
  71. //通用异步通知
  72. public function bgRetUrl()
  73. {
  74. LcgLog::debug('lcg异步通知------------------------', $this->parameters);
  75. echo 'SUCCESS';exit();
  76. }
  77. //通用同步通知
  78. public function pageRetUrl()
  79. {
  80. //LcgLog::debug('<-----------lcg同步通知---------------->', $this->parameters);
  81. redirect(Url::absoluteApp('myWallet', ['i' => \YunShop::app()->uniacid]))->send();
  82. }
  83. //支付异步通知
  84. public function payBgRetUrl()
  85. {
  86. LcgLog::debug('支付异步通知返回参数------------------------', $this->parameters);
  87. if ($this->verify() && $this->getInfoParameter('retCode') == 'MCG00000') {
  88. $this->log();
  89. (new OrderTypeService())->merchantWithdraw($this->getBodyParameter('Lists'));
  90. $data = [
  91. 'total_fee' => floatval($this->getBodyParameter('trAmt')),
  92. 'out_trade_no' => $this->getBodyParameter('mercOrdNo'),
  93. 'trade_no' => $this->getBodyParameter('jrnno'),
  94. 'unit' => 'fen',
  95. 'pay_type' => $this->pay_type[$this->getBodyParameter('payTyp')]['name'],
  96. 'pay_type_id' => $this->pay_type[$this->getBodyParameter('payTyp')]['id'],
  97. ];
  98. $this->payResutl($data);
  99. echo 'SUCCESS';exit();
  100. } else {
  101. LcgLog::debug('支付异步通知返回错误信息------------------------', $this->getInfoParameter('errMsg'));
  102. echo 'failure';exit();
  103. }
  104. }
  105. //支付同步通知
  106. public function payPageRetUrl()
  107. {
  108. //LcgLog::debug('<-----------lcg支付同步通知---------------->', $this->parameters);
  109. redirect(Url::absoluteApp('member/orderList/0', ['i' => \YunShop::app()->uniacid]))->send();
  110. }
  111. //用户确认订单退款异步通知
  112. public function orderAcceptRefund()
  113. {
  114. LcgLog::debug('用户确认订单退款异步通知------------------------merchantNotifyUrl', $this->parameters);
  115. $resCode = substr($this->getInfoParameter('retCode'), -5);
  116. if ($resCode != '00000') { exit('failure'); }
  117. $lcgRefund = LcgRefund::uniacid()->where('jrnno', $this->getBodyParameter('jrnno'))->with(['hasOneOrder'])->first();
  118. if (is_null($lcgRefund->hasOneOrder)) {
  119. exit('failure');
  120. }
  121. $refundApply = \app\common\models\refund\RefundApply::where('order_id', $lcgRefund->hasOneOrder->id)->first();
  122. if ($refundApply) {
  123. //退款状态设为完成
  124. RefundOperationService::refundComplete(['id' => $refundApply->id]);
  125. }
  126. MerchantOrder::uniacid()->where('tradeOrdNo',$lcgRefund->tradeOrdNo)->update(['status' => MerchantOrder::STATUS_CANCEL]);
  127. echo 'SUCCESS';exit();
  128. }
  129. //用户确认订单退款同步通知
  130. public function orderAcceptRefundUrl()
  131. {
  132. //LcgLog::debug('<-----------lcg同步通知---------------->', $this->parameters);
  133. if (isset($_GET['admin'])) {
  134. redirect(Url::absoluteWeb('index.index'))->send();
  135. } else {
  136. redirect(Url::absoluteApp('myWallet', ['i' => \YunShop::app()->uniacid]))->send();
  137. }
  138. }
  139. //添加电子登记簿异步
  140. public function merchantNotifyUrl()
  141. {
  142. LcgLog::debug('添加电子登记簿异步------------------------merchantNotifyUrl', $this->parameters);
  143. //if ($this->getInfoParameter('retCode') != 'MCA00000') {
  144. //exit('failure');
  145. //}
  146. $reqSn = $this->getInfoParameter('reqSn');
  147. $mbrCode = $this->getBodyParameter('mbrCode');
  148. $jrnno = $this->getBodyParameter('jrnno');
  149. $userSts = $this->getBodyParameter('userSts');
  150. $bankMbl = $this->getBodyParameter('bankMbl');
  151. $bankName = $this->getBodyParameter('bankName');
  152. $bankCode = $this->getBodyParameter('bankCode');
  153. $bankNum = $this->getBodyParameter('bankNum');
  154. $bankNm = $this->getBodyParameter('bankNm');
  155. $major = MerchantMajor::uniacid()->where("reqSn",$reqSn)->first();
  156. if (empty($major)) {
  157. lcgLog::debug('添加信息报错-------------merchantUpdateNotifyUrl',$reqSn);
  158. exit('failure');
  159. }
  160. $user = Merchant::uniacid()->where("member_id",$major['member_id'])->first();
  161. is_null($user) && $user = new Merchant();
  162. //记录信息
  163. $data = array(
  164. 'uniacid' => $major['uniacid'],
  165. 'member_id' => $major['member_id'],
  166. 'reqSn' => $reqSn,
  167. 'platCusNO' => $major['platCusNO'],
  168. 'mbrCode' => $mbrCode,
  169. 'platRoleID' => $major['platRoleID'],
  170. 'jrnno' => $jrnno,
  171. 'userSts' => $userSts,
  172. 'rstMess' => $this->getBodyParameter('rstMess'),
  173. );
  174. $user->fill($data);
  175. $user->save();
  176. $major_data = array(
  177. 'jrnno' => $jrnno,
  178. 'userSts' => $userSts,
  179. 'mbrCode' => $mbrCode,
  180. 'bankMbl' => $bankMbl,
  181. 'bankName' => $bankName,
  182. 'bankCode' => $bankCode,
  183. 'bankNum' => $bankNum,
  184. 'bankNm' => $bankNm,
  185. 'account' => $this->getBodyParameter('account'),
  186. 'walt_status' => 1,
  187. );
  188. $major_data = array_filter($major_data);
  189. $major_data['userSts'] = $userSts;
  190. $major->fill($major_data);
  191. $major->save();
  192. echo 'SUCCESS';exit;
  193. }
  194. //添加电子登记簿同步
  195. public function merchantReturnUrl()
  196. {
  197. LcgLog::debug('添加电子登记簿同步------------------------merchantReturnUrl', $this->parameters);
  198. $reqSn = $this->getInfoParameter('reqSn');
  199. MerchantMajor::uniacid()->where("reqSn",$reqSn)->update(['walt_status' => 1]);
  200. redirect(Url::absoluteApp('member', ['i' => \YunShop::app()->uniacid]))->send();
  201. }
  202. //修改电子登记簿异步
  203. public function merchantUpdateNotifyUrl()
  204. {
  205. LcgLog::debug('修改电子登记簿异步------------------------merchantUpdateNotifyUrl', $this->parameters);
  206. $reqSn = $this->getInfoParameter('reqSn');
  207. $update = MerchantUpdate::uniacid()->where("reqSn",$reqSn)->first();
  208. if (empty($update))
  209. {
  210. lcgLog::debug('修改信息报错-------------merchantUpdateNotifyUrl',$reqSn);
  211. exit('failure');
  212. }
  213. $type = ['08','09','10','11'];
  214. $rstCode = $this->getBodyParameter('rstCode');
  215. $operType = $this->getBodyParameter('operType');
  216. $person = ['realNm','idNo','account','bankMbl','mbrCode'];
  217. $company = ['accountNm','bankCodeName','account','bankNum','bankNm','legalPerIdTyp','legalPerIdNo','agent','agentIdNo','agentMbl','authorizer1Nm','authorizer1IdNum','authorizer1Mbl','authorizer2Nm','authorizer2IdTyp','authorizer2IdNum','authorizer2Mbl'];
  218. if ($rstCode == 0) {
  219. $up_data = array();
  220. if (in_array($operType,$type)) {
  221. foreach ($person as $v) {
  222. $up_data[$v] = $this->getBodyParameter($v);
  223. }
  224. } else {
  225. foreach($company as $v) {
  226. $up_data[$v] = $this->getBodyParameter($v);
  227. }
  228. $up_data['legalFront'] = $update['legalFront'];
  229. $up_data['legalBack'] = $update['legalBack'];
  230. $up_data['cert'] = $update['cert'];
  231. }
  232. //修改信息
  233. $up_data = array_filter($up_data);
  234. MerchantMajor::uniacid()->where("mbrCode",$update['mbrCode'])->update($up_data);
  235. echo 'SUCCESS';exit;
  236. } elseif ($rstCode == 1) {
  237. MerchantUpdate::uniacid()->where("reqSn",$reqSn)->update(['upStatus'=> 1]);
  238. echo 'SUCCESS';exit;
  239. }
  240. MerchantUpdate::uniacid()->where("reqSn",$reqSn)->update(['upStatus'=> 2]);
  241. echo 'SUCCESS';exit;
  242. }
  243. //修改电子登记簿同步
  244. public function merchantUpdateReturnUrl()
  245. {
  246. LcgLog::debug('修改电子登记簿同步------------------------merchantReturnUrl', $this->parameters);
  247. redirect(Url::absoluteApp('myWallet', ['i' => \YunShop::app()->uniacid]))->send();
  248. }
  249. //密码修改回调地址
  250. public function passwdUpdateNotifyUrl()
  251. {
  252. LcgLog::debug('修改密码异步------------------------merchantReturnUrl', $this->parameters);
  253. }
  254. //密码修改同步地址
  255. public function passwdUpdateReturnUrl()
  256. {
  257. //LcgLog::debug('密码修改同步地址------------------------merchantReturnUrl', $this->parameters);
  258. redirect(Url::absoluteApp('myWallet', ['i' => \YunShop::app()->uniacid]))->send();
  259. }
  260. //出金异步
  261. public function withdrawBgRetUrl()
  262. {
  263. LcgLog::debug('出金异步------------------------withdrawBgRetUrl', $this->parameters);
  264. if ($this->getInfoParameter('retCode') != 'MCG00000') {
  265. exit('failure');
  266. }
  267. $withdraw = LcgWithdraw::uniacid()->where('reqSn', $this->getInfoParameter('reqSn'))->first();
  268. if (empty($withdraw)) {
  269. LcgLog::debug('withdrawBgRetUrl------------------------提现记录不存在');
  270. exit('failure');
  271. }
  272. $withdraw->status = $this->getBodyParameter('rstCode');
  273. $withdraw->save();
  274. echo 'SUCCESS';exit;
  275. }
  276. //出金同步
  277. public function withdrawPageRetUrl()
  278. {
  279. //LcgLog::debug('出金同步------------------------withdrawPageRetUrl', $this->parameters);
  280. redirect(Url::absoluteApp('myWallet', ['i' => \YunShop::app()->uniacid]))->send();
  281. }
  282. /**
  283. * 用户确认收货异步通知
  284. * @throws \app\common\exceptions\AppException
  285. */
  286. public function orderReceiveBgRetUrl()
  287. {
  288. LcgLog::debug('用户确认收货异步通知------------------------orderReceiveBgRetUrl', $this->parameters);
  289. if ($this->getInfoParameter('retCode') != 'MCG00000') {
  290. exit('failure');
  291. }
  292. $MerchantOrder = MerchantOrder::uniacid()->sentSn($this->getBodyParameter('jrnno'))->with(['hasOneOrder'])->first();
  293. if (empty($MerchantOrder)) {
  294. exit('failure');
  295. }
  296. //跟新商户订单状态
  297. MerchantOrder::updateData(['id'=>$MerchantOrder->id],['status'=>MerchantOrder::STATUS_END]);
  298. //订单收货
  299. OrderService::orderReceive(['order_id' => $MerchantOrder->hasOneOrder->id]);
  300. echo 'SUCCESS';exit();
  301. }
  302. //用户确认收货同步返回
  303. public function orderReceivePageRetUrl()
  304. {
  305. //LcgLog::debug('用户确认收货同步返回------------------------orderReceivePageRetUrl', $this->parameters);
  306. redirect(Url::absoluteApp('member/orderList/0', ['i' => \YunShop::app()->uniacid]))->send();
  307. }
  308. protected function verify()
  309. {
  310. return ($this->parameters['body'] && $this->parameters['info']);
  311. }
  312. protected function getBodyParameter($key)
  313. {
  314. return isset($this->parameters['body'][$key])?$this->parameters['body'][$key]: '';
  315. }
  316. protected function getInfoParameter($key)
  317. {
  318. return isset($this->parameters['info'][$key])?$this->parameters['info'][$key]: '';
  319. }
  320. /**
  321. * 支付日志
  322. *
  323. * @param $post
  324. */
  325. public function log()
  326. {
  327. //访问记录
  328. Pay::payAccessLog();
  329. //保存响应数据
  330. Pay::payResponseDataLog($this->getBodyParameter('mercOrdNo'), '龙存管', json_encode($this->parameters));
  331. }
  332. }