DdriveRefund.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use Yansongda\Pay\Pay;
  9. /**
  10. * 退款申请管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class DdriveRefund extends Backend
  15. {
  16. /**
  17. * DdriveRefund模型对象
  18. * @var \app\admin\model\DdriveRefund
  19. */
  20. protected $model = null;
  21. protected $noNeedLogin = ['notifyx'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\DdriveRefund;
  27. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  28. $this->view->assign("checkStatusList", $this->model->getCheckStatusList());
  29. }
  30. public function import()
  31. {
  32. parent::import();
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. //当前是否为关联查询
  45. $this->relationSearch = true;
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags', 'trim']);
  48. if ($this->request->isAjax()) {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $list = $this->model
  55. ->with(['user'])
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. foreach ($list as $row) {
  60. $row->getRelation('user')->visible(['username']);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 编辑
  69. */
  70. public function edit($ids = null)
  71. {
  72. $row = $this->model->get($ids);
  73. if (!$row) {
  74. $this->error(__('No Results were found'));
  75. }
  76. $adminIds = $this->getDataLimitAdminIds();
  77. if (is_array($adminIds)) {
  78. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  79. $this->error(__('You have no permission'));
  80. }
  81. }
  82. if ($this->request->isPost()) {
  83. $params = $this->request->post("row/a");
  84. if ($params) {
  85. $params = $this->preExcludeFields($params);
  86. $result = false;
  87. Db::startTrans();
  88. try {
  89. //是否采用模型验证
  90. if ($this->modelValidate) {
  91. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  92. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  93. $row->validateFailException(true)->validate($validate);
  94. }
  95. if($params['check_status'] ==1){
  96. switch ($params['pay_type']) {
  97. case '1':
  98. //余额退款
  99. $this->refund_update(2,$params['apply_money'],$params['number'],$params['number'],1);
  100. break;
  101. case '2':
  102. //微信退款
  103. $this->Refund($params['order_number'],$params['number'],$params['apply_money'],$params['apply_money'],$params['pay_type']);
  104. break;
  105. case '3':
  106. //支付宝退款
  107. $this->Refund($params['order_number'],$params['number'],$params['apply_money'],$params['apply_money'],$params['pay_type']);
  108. break;
  109. default:
  110. }
  111. }
  112. if($params['check_status'] == -1){
  113. Db::name('city_order') ->where('number',$params['number'])->update(['status'=>5,'updatetime'=>time()]);
  114. }
  115. if($params['check_status'] ==1 && $params['pay_type']==3){
  116. unset($params['check_status']);
  117. }
  118. $result = $row->allowField(true)->save($params);
  119. Db::commit();
  120. } catch (ValidateException $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. } catch (PDOException $e) {
  124. Db::rollback();
  125. $this->error($e->getMessage());
  126. } catch (\Exception $e) {
  127. Db::rollback();
  128. $this->error($e->getMessage());
  129. }
  130. if ($result !== false) {
  131. $this->success();
  132. } else {
  133. $this->error(__('No rows were updated'));
  134. }
  135. }
  136. $this->error(__('Parameter %s can not be empty', ''));
  137. }
  138. $this->view->assign("row", $row);
  139. return $this->view->fetch();
  140. }
  141. /**
  142. * 退款
  143. * @param $orderId int 对应lj_pay表id
  144. * @param $refundOrderNo int 对应退款的订单id
  145. * @param $totalFee
  146. * @param $refundFee
  147. * @return array|bool
  148. * @throws Exception
  149. */
  150. public function Refund($orderId, $refundOrderNo, $totalFee = '0.01', $refundFee,$pay_type = '2')
  151. {
  152. $form = 'user_wechat';
  153. //创建支付对象
  154. if($pay_type == 3){
  155. $form = 'alipay';
  156. $params = [
  157. 'refund_amount' => $refundFee,
  158. 'out_trade_no' => $orderId,
  159. 'out_refund_no' => $refundOrderNo,
  160. ];
  161. }else{
  162. $params = [
  163. 'type' => 'app',
  164. 'total_fee' => $totalFee * 100,
  165. 'refund_fee' => $refundFee * 100,
  166. 'out_trade_no' => $orderId,
  167. 'out_refund_no' => $refundOrderNo,
  168. 'notify_url' => $this->request->domain() . '/eadmin.php/ddrive_refund/notifyx/paytype/wechat'
  169. ];
  170. }
  171. $config = Service::getConfig($form);
  172. try {
  173. if($pay_type == 2){
  174. $pay = Pay::wechat($config);
  175. $res = $pay->refund($params);
  176. }else{
  177. $pay = Pay::alipay($config);
  178. $res = $pay->refund($params);
  179. }
  180. } catch (\Exception $exception) {
  181. exception($exception->getMessage());
  182. }
  183. $data = json_decode($res,true);
  184. if (is_array($data)) {
  185. if($pay_type==2){
  186. if ($data['return_code'] == 'SUCCESS') {
  187. return true;
  188. } else {
  189. exception($data['return_msg']);
  190. }
  191. }else{
  192. if ($data['code'] == '10000') {
  193. $this->refund_update(2,$data['refund_fee'],$data['trade_no'],$data['trade_no'],3);
  194. } else {
  195. exception($data['msg']);
  196. }
  197. }
  198. } else {
  199. exception('发起退款失败');
  200. }
  201. return false;
  202. }
  203. /**
  204. * 微信退款异步回调
  205. *
  206. * @return void
  207. */
  208. public function notifyx()
  209. {
  210. $xml = file_get_contents("php://input");
  211. $notifyx = $this->xml_to_array($xml);
  212. $data = $this->decode($notifyx['req_info']);
  213. $notifyx_data = $this->xml_to_array($data);
  214. if($notifyx_data['refund_status'] == 'SUCCESS'){
  215. $this->refund_update(2,$notifyx_data['total_fee']/100,$notifyx_data['refund_id'],$notifyx_data['out_refund_no'],2);
  216. echo 'SUCCESS';
  217. }else{
  218. echo 'FAIL';
  219. }
  220. }
  221. /**
  222. * 格式数据
  223. *
  224. * @return void
  225. */
  226. private function xml_to_array($xml)
  227. {
  228. if (!$xml) {
  229. return false;
  230. }
  231. libxml_disable_entity_loader(true);
  232. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  233. return $data;
  234. }
  235. /**
  236. * 退款解密
  237. *
  238. * @return void
  239. */
  240. private function decode($xml){
  241. $key = Service::getConfig('user_wechat')['key'];
  242. $decrypt = base64_decode($xml, true);
  243. $data = openssl_decrypt($decrypt , 'aes-256-ecb', md5($key), OPENSSL_RAW_DATA);
  244. return $data;
  245. }
  246. /**
  247. * 退款数据更改
  248. *
  249. * @return void
  250. */
  251. private function refund_update($check_status,$money,$refund_number = '',$out_refund_no = '',$pay_type){
  252. try {
  253. $ddrive_refund = Db::name('ddrive_refund')->where('number',$out_refund_no)->find();
  254. $updata = [];
  255. $updata['check_status'] = $check_status;
  256. $updata['money'] = $money;
  257. $updata['refund_number'] = $refund_number;
  258. $updata['success_time'] = time();
  259. $updata['admin_id'] = 1;
  260. Db::name('ddrive_refund')->where('number',$out_refund_no)->update($updata);
  261. Db::name('ddrive_hy_order') ->where('pay_number',$out_refund_no)->update(['status'=>-2,'updatetime'=>time()]);
  262. if($pay_type == 1){
  263. Db::name('user')->where('id',$ddrive_refund['user_id'])->setInc('money',$money);
  264. }
  265. } catch (\Exception $exception) {
  266. trace($exception->getMessage(), 'pay');
  267. }
  268. }
  269. }