Order.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\OrderException;
  4. use addons\cms\model\Archives;
  5. use think\Exception;
  6. /**
  7. * 订单控制器
  8. * Class Order
  9. * @package addons\cms\controller
  10. */
  11. class Order extends Base
  12. {
  13. /**
  14. * 创建订单并发起支付请求
  15. */
  16. public function submit()
  17. {
  18. $config = get_addon_config('cms');
  19. //是否需要登录后才可以支付
  20. if ($config['ispaylogin'] && !$this->auth->isLogin()) {
  21. $this->error("请登录后再进行操作!", "index/user/login");
  22. }
  23. $id = $this->request->request('id');
  24. $paytype = $this->request->request('paytype');
  25. $archives = Archives::get($id);
  26. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  27. $this->error('未找到指的文档');
  28. }
  29. try {
  30. $response = \addons\cms\library\Order::submit($id, $paytype ? $paytype : $config['defaultpaytype']);
  31. } catch (OrderException $e) {
  32. if ($e->getCode() == 1) {
  33. $this->success($e->getMessage(), $archives->url);
  34. } else {
  35. $this->error($e->getMessage(), $archives->url);
  36. }
  37. } catch (Exception $e) {
  38. $this->error($e->getMessage(), $archives->url);
  39. }
  40. return $response;
  41. }
  42. /**
  43. * 企业支付通知和回调
  44. */
  45. public function epay()
  46. {
  47. $type = $this->request->param('type');
  48. $paytype = $this->request->param('paytype');
  49. if ($type == 'notify') {
  50. $pay = \addons\epay\library\Service::checkNotify($paytype);
  51. if (!$pay) {
  52. echo '签名错误';
  53. return;
  54. }
  55. $data = $pay->verify();
  56. try {
  57. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  58. \addons\cms\library\Order::settle($data['out_trade_no'], $payamount);
  59. } catch (Exception $e) {
  60. }
  61. echo $pay->success();
  62. } else {
  63. $pay = \addons\epay\library\Service::checkReturn($paytype);
  64. if (!$pay) {
  65. $this->error('签名错误');
  66. }
  67. if ($pay === true) {
  68. //微信支付
  69. $data = ['out_trade_no' => $this->request->param('orderid')];
  70. } else {
  71. $data = $pay->verify();
  72. }
  73. $order = \addons\cms\model\Order::getByOrderid($data['out_trade_no']);
  74. if (!$order->archives) {
  75. $this->error('未找到文档信息!');
  76. }
  77. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  78. $this->redirect($order->archives->url);
  79. //$this->success("恭喜你!支付成功!", $order->archives->url);
  80. }
  81. return;
  82. }
  83. }