Order.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 订单管理
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\controller;
  15. use addons\cms\model\Order as OrderModel;
  16. use think\Exception;
  17. class Order extends Cmsbase
  18. {
  19. //创建订单并发起支付请求
  20. public function submit()
  21. {
  22. if (!$this->auth->isLogin()) {
  23. $this->error('请先登录!', url('index/user/login'));
  24. }
  25. $catid = $this->request->param('catid/d');
  26. $id = $this->request->param('id/d');
  27. $pay_type = $this->request->param('pay_type');
  28. try {
  29. $response = OrderModel::submitOrder($catid, $id, $pay_type ? $pay_type : 'wechat');
  30. } catch (Exception $e) {
  31. $this->error($e->getMessage());
  32. }
  33. //处理余额支付
  34. if (isset($response['status']) && $response['status'] === 'success') {
  35. $this->success($response['message']);
  36. }
  37. return $response;
  38. }
  39. //企业支付通知和回调
  40. public function epay()
  41. {
  42. $type = $this->request->param('type');
  43. $pay_type = $this->request->param('pay_type');
  44. if ($type == 'notify') {
  45. $pay = \addons\pay\library\Service::checkNotify($pay_type);
  46. if (!$pay) {
  47. echo '签名错误';
  48. return;
  49. }
  50. try {
  51. $data = $pay->callback();
  52. if ($pay_type === 'wechat') {
  53. $data = $data['resource']['ciphertext'];
  54. $data['total_fee'] = $data['amount']['total'];
  55. }
  56. $payamount = $pay_type == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  57. OrderModel::settle($data['out_trade_no'], $payamount);
  58. } catch (Exception $e) {
  59. //写入日志
  60. // $e->getMessage();
  61. }
  62. return $pay->success();
  63. } else {
  64. $out_trade_no = $this->request->param('trade_sn');
  65. $order = OrderModel::getByTradeSn($out_trade_no);
  66. if (!$order) {
  67. $this->error('订单不存在!');
  68. }
  69. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  70. $this->success("恭喜你!支付成功!", buildContentUrl($order['catid'], $order['content_id'], '', true, true));
  71. }
  72. return;
  73. }
  74. }