Order.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\recharge\model;
  3. use app\common\library\Auth;
  4. use app\common\model\User;
  5. use think\Exception;
  6. use think\Model;
  7. /**
  8. * 充值订单模型
  9. */
  10. class Order extends Model
  11. {
  12. // 表名
  13. protected $name = 'recharge_order';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = '';
  19. // 追加属性
  20. protected $append = [
  21. ];
  22. /**
  23. * 发起订单支付
  24. * @param float $money
  25. * @param string $paytype
  26. */
  27. public static function submitOrder($money, $paytype = 'wechat')
  28. {
  29. $auth = Auth::instance();
  30. $user_id = $auth->isLogin() ? $auth->id : 0;
  31. $order = null;
  32. $config = get_addon_config('recharge');
  33. if ($config && $config['ordercreatelimit']) {
  34. $order = self::where('user_id', $user_id)
  35. ->where('amount', $money)
  36. ->where('status', 'created')
  37. ->order('id', 'desc')
  38. ->find();
  39. }
  40. $request = \think\Request::instance();
  41. if (!$order) {
  42. $orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  43. $data = [
  44. 'orderid' => $orderid,
  45. 'user_id' => $user_id,
  46. 'amount' => $money,
  47. 'payamount' => 0,
  48. 'paytype' => $paytype,
  49. 'ip' => $request->ip(),
  50. 'useragent' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
  51. 'status' => 'created'
  52. ];
  53. $order = self::create($data);
  54. }
  55. $epay = get_addon_info('epay');
  56. if ($epay && $epay['state']) {
  57. $notifyurl = $request->root(true) . '/index/recharge/epay/type/notify/paytype/' . $paytype;
  58. $returnurl = $request->root(true) . '/index/recharge/epay/type/return/paytype/' . $paytype;
  59. \addons\epay\library\Service::submitOrder($money, $order->orderid, $paytype, "充值{$money}元", $notifyurl, $returnurl);
  60. exit;
  61. } else {
  62. $result = \think\Hook::listen('recharge_order_submit', $order);
  63. if (!$result) {
  64. throw new Exception("请先在后台安装并配置微信支付宝整合插件");
  65. }
  66. }
  67. }
  68. /**
  69. * 订单结算
  70. * @param int $orderid
  71. * @param string $payamount
  72. * @param string $memo
  73. * @return bool
  74. * @throws \think\exception\DbException
  75. */
  76. public static function settle($orderid, $payamount = null, $memo = '')
  77. {
  78. $order = self::getByOrderid($orderid);
  79. if (!$order) {
  80. return false;
  81. }
  82. if ($order['status'] != 'paid') {
  83. $order->payamount = $payamount ? $payamount : $order->amount;
  84. $order->paytime = time();
  85. $order->status = 'paid';
  86. $order->memo = $memo;
  87. $order->save();
  88. // 最新版本可直接使用User::money($order->user_id, $order->amount, '充值');来更新
  89. // 更新会员余额
  90. $user = User::get($order->user_id);
  91. if ($user) {
  92. $before = $user->money;
  93. $after = $user->money + $order->amount;
  94. //更新会员信息
  95. $user->save(['money' => $after]);
  96. //写入日志
  97. MoneyLog::create(['user_id' => $order->user_id, 'money' => $order->amount, 'before' => $before, 'after' => $after, 'memo' => '充值']);
  98. }
  99. $result = \think\Hook::listen('recharge_order_settled', $order);
  100. }
  101. return true;
  102. }
  103. }