Payment.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\UserCoupon;
  4. use addons\shop\model\OrderGoods;
  5. use think\Config;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 支付
  10. * Class Payment
  11. * @package addons\shop\controller
  12. */
  13. class Payment extends Base
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 支付页
  23. */
  24. public function index()
  25. {
  26. $orderid = $this->request->get('orderid');
  27. $orderInfo = \addons\shop\model\Order::getByOrderSn($orderid);
  28. if (!$orderInfo) {
  29. $this->error("未找到指定的订单");
  30. }
  31. if ($orderInfo['paystate']) {
  32. $this->error("订单已经支付", url('index/shop.order/index'));
  33. }
  34. if ($orderInfo['orderstate'] > 0) {
  35. $this->error("订单无法进行支付", url('index/shop.order/index'));
  36. }
  37. //订单过期
  38. if (!$orderInfo['orderstate'] && time() > $orderInfo['expiretime']) {
  39. // 启动事务
  40. Db::startTrans();
  41. try {
  42. $orderInfo->save(['orderstate' => 2]);
  43. //库存恢复
  44. OrderGoods::setGoodsStocksInc($orderInfo->order_sn);
  45. //恢复优惠券
  46. UserCoupon::resetUserCoupon($orderInfo->user_coupon_id, $orderInfo->order_sn);
  47. // 提交事务
  48. Db::commit();
  49. } catch (\Exception $e) {
  50. // 回滚事务
  51. Db::rollback();
  52. }
  53. $this->error("订单已失效", url("index/shop.order/index"));
  54. }
  55. $config = get_addon_config('shop');
  56. $paytypeList = [];
  57. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  58. foreach (explode(',', $config['paytypelist']) as $index => $item) {
  59. $paytypeList[] = ['value' => $item, 'image' => '/assets/addons/shop/img/paytype/' . $item . '.png', 'default' => $item === $config['defaultpaytype']];
  60. }
  61. $this->view->assign('paytypeList', $paytypeList);
  62. if ($this->request->isPost()) {
  63. $paytype = $this->request->post("paytype");
  64. $response = null;
  65. try {
  66. $response = \addons\shop\model\Order::pay($orderid, $orderInfo['user_id'], $paytype);
  67. } catch (Exception $e) {
  68. $this->error($e->getMessage());
  69. }
  70. return $response;
  71. } else {
  72. $this->view->assign('orderInfo', $orderInfo);
  73. Config::set('shop.title', "立即支付");
  74. return $this->view->fetch('/payment');
  75. }
  76. }
  77. }