Recharge.php 3.7 KB

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