UserRecharge.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\finance;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\user\UserRecharge as UserRechargeModel;
  14. use app\wap\model\user\UserBill;
  15. use service\JsonService as Json;
  16. use think\Url;
  17. use service\FormBuilder as Form;
  18. use think\Request;
  19. use service\HookService;
  20. use behavior\wechat\PaymentBehavior;
  21. use service\WechatTemplateService;
  22. use app\wap\model\user\WechatUser as WechatUserWap;
  23. /**
  24. * 微信充值记录
  25. * Class UserRecharge
  26. * @package app\admin\controller\user
  27. */
  28. class UserRecharge extends AuthController
  29. {
  30. /**
  31. * 显示操作记录
  32. */
  33. public function index()
  34. {
  35. $where = parent::getMore([
  36. ['order_id', ''],
  37. ], $this->request);
  38. $list = UserRechargeModel::systemPage($where);
  39. $recharge_type_cn = ['yue' => "余额", 'weixin' => "微信", 'zhifubao' => "支付宝"];
  40. $this->assign('recharge_type_cn', $recharge_type_cn);
  41. $this->assign('where', $where);
  42. $this->assign($list);
  43. return $this->fetch();
  44. }
  45. /**退款
  46. * @param $id
  47. * @return mixed|void
  48. */
  49. public function edit($id)
  50. {
  51. if (!$id) return $this->failed('数据不存在');
  52. $UserRecharge = UserRechargeModel::get($id);
  53. if (!$UserRecharge) return Json::fail('数据不存在!');
  54. if ($UserRecharge['paid'] == 1) {
  55. $f = array();
  56. $f[] = Form::input('order_id', '退款单号', $UserRecharge->getData('order_id'))->disabled(1);
  57. $f[] = Form::number('refund_price', '退款金额', $UserRecharge->getData('price'))->precision(2)->min(0)->max($UserRecharge->getData('price'));
  58. $form = Form::make_post_form('编辑', $f, Url::build('updateRefundY', array('id' => $id)));
  59. $this->assign(compact('form'));
  60. return $this->fetch('public/form-builder');
  61. } else return Json::fail('数据不存在!');
  62. }
  63. /**退款更新
  64. * @param Request $request
  65. * @param $id
  66. */
  67. public function updateRefundY(Request $request, $id)
  68. {
  69. $data = parent::postMore([
  70. 'refund_price',
  71. ], $request);
  72. if (!$id) return $this->failed('数据不存在');
  73. $UserRecharge = UserRechargeModel::get($id);
  74. if (!$UserRecharge) return Json::fail('数据不存在!');
  75. if ($UserRecharge['price'] == $UserRecharge['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  76. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  77. $refund_price = $data['refund_price'];
  78. $data['refund_price'] = bcadd($data['refund_price'], $UserRecharge['refund_price'], 2);
  79. $bj = bccomp((float)$UserRecharge['price'], (float)$data['refund_price'], 2);
  80. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  81. $refund_data['pay_price'] = $UserRecharge['price'];
  82. $refund_data['refund_price'] = $refund_price;
  83. try {
  84. HookService::listen('user_recharge_refund', $UserRecharge['order_id'], $refund_data, true, PaymentBehavior::class);
  85. } catch (\Exception $e) {
  86. return Json::fail($e->getMessage());
  87. }
  88. UserRechargeModel::edit($data, $id);
  89. WechatTemplateService::sendTemplate(WechatUserWap::uidToOpenid($UserRecharge['uid']), WechatTemplateService::ORDER_REFUND_STATUS, [
  90. 'first' => '亲,您充值的金额已退款,本次退款' .
  91. $data['refund_price'] . '金额',
  92. 'keyword1' => $UserRecharge['order_id'],
  93. 'keyword2' => $UserRecharge['price'],
  94. 'keyword3' => date('Y-m-d H:i:s', $UserRecharge['add_time']),
  95. 'remark' => '请查看账单'
  96. ], '');
  97. UserBill::expend('系统退款', $UserRecharge['uid'], 'now_money', 'user_recharge_refund', $refund_price, $id, $UserRecharge['price'], '退款给用户' . $refund_price . '元');
  98. return Json::successful('退款成功!');
  99. }
  100. }