Recharge.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\index\controller;
  3. use addons\recharge\model\MoneyLog;
  4. use app\common\controller\Frontend;
  5. use think\Exception;
  6. /**
  7. * 充值
  8. */
  9. class Recharge extends Frontend
  10. {
  11. protected $layout = 'default';
  12. protected $noNeedLogin = ['pay', 'epay'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 充值余额
  16. * @return string
  17. */
  18. public function recharge()
  19. {
  20. $config = get_addon_config('recharge');
  21. $moneyList = [];
  22. foreach ($config['moneylist'] as $index => $item) {
  23. $moneyList[] = ['value' => $item, 'text' => $index, 'default' => $item === $config['defaultmoney']];
  24. }
  25. $paytypeList = [];
  26. foreach (explode(',', $config['paytypelist']) as $index => $item) {
  27. $paytypeList[] = ['value' => $item, 'image' => '/assets/addons/recharge/img/' . $item . '.png', 'default' => $item === $config['defaultpaytype']];
  28. }
  29. $this->view->assign('addonConfig', $config);
  30. $this->view->assign('moneyList', $moneyList);
  31. $this->view->assign('paytypeList', $paytypeList);
  32. $this->view->assign('title', __('Recharge'));
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 余额日志
  37. * @return string
  38. */
  39. public function moneylog()
  40. {
  41. $moneyloglist = MoneyLog::where(['user_id' => $this->auth->id])
  42. ->order('id desc')
  43. ->paginate(10);
  44. $this->view->assign('title', __('Balance log'));
  45. $this->view->assign('moneyloglist', $moneyloglist);
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 创建订单并发起支付请求
  50. * @throws \think\exception\DbException
  51. */
  52. public function submit()
  53. {
  54. $money = $this->request->request('money');
  55. $paytype = $this->request->request('paytype');
  56. if ($money <= 0) {
  57. $this->error('充值金额不正确');
  58. }
  59. $config = get_addon_config('recharge');
  60. if (isset($config['minmoney']) && $money < $config['minmoney']) {
  61. $this->error('充值金额不能低于' . $config['minmoney'] . '元');
  62. }
  63. try {
  64. \addons\recharge\model\Order::submitOrder($money, $paytype ? $paytype : 'wechat');
  65. } catch (Exception $e) {
  66. $this->error($e->getMessage());
  67. }
  68. return;
  69. }
  70. /**
  71. * 企业支付通知和回调
  72. * @throws \think\exception\DbException
  73. */
  74. public function epay()
  75. {
  76. $type = $this->request->param('type');
  77. $paytype = $this->request->param('paytype');
  78. if ($type == 'notify') {
  79. $pay = \addons\epay\library\Service::checkNotify($paytype);
  80. if (!$pay) {
  81. echo '签名错误';
  82. return;
  83. }
  84. $data = $pay->verify();
  85. try {
  86. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  87. \addons\recharge\model\Order::settle($data['out_trade_no'], $payamount);
  88. } catch (Exception $e) {
  89. }
  90. echo $pay->success();
  91. } else {
  92. $pay = \addons\epay\library\Service::checkReturn($paytype);
  93. if (!$pay) {
  94. $this->error('签名错误');
  95. }
  96. //微信支付没有返回链接
  97. if ($pay === true) {
  98. $this->success("请返回网站查看支付状态!", url("user/index"));
  99. }
  100. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  101. $this->success("恭喜你!充值成功!", url("user/index"));
  102. }
  103. return;
  104. }
  105. }