Order.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use Yansongda\Pay\Exceptions\GatewayException;
  7. use addons\epay\library\Service;
  8. use app\common\model\OrderAction;
  9. use app\common\model\TemplateMsg;
  10. use traits\model\SoftDelete;
  11. use app\common\Enum\OrderEnum;
  12. use app\common\Traits\OrderStatusTrait;
  13. /**
  14. * 订单模型
  15. * @property mixed $address 订单地址信息(动态属性)
  16. */
  17. class Order extends Model
  18. {
  19. use SoftDelete;
  20. use OrderStatusTrait;
  21. // 表名
  22. protected $name = 'shop_order';
  23. // 开启自动写入时间戳字段
  24. protected $autoWriteTimestamp = 'int';
  25. // 定义时间戳字段名
  26. protected $createTime = 'createtime';
  27. protected $updateTime = 'updatetime';
  28. protected $deleteTime = 'deletetime';
  29. // 追加属性
  30. protected $append = [
  31. ];
  32. protected static $config = [];
  33. protected static $tagCount = 0;
  34. protected static function init()
  35. {
  36. $config = get_addon_config('shop');
  37. self::$config = $config;
  38. }
  39. public function getOrderStatusTextAttr($value, $data)
  40. {
  41. return OrderEnum::getOrderStatusText($data['order_status']);
  42. }
  43. public function getShippingstateList()
  44. {
  45. return ['0' => __('Shippingstate 0'), '1' => __('Shippingstate 1'), '2' => __('Shippingstate 2'), '3' => __('Shippingstate 3')];
  46. }
  47. public function getPaystateList()
  48. {
  49. return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
  50. }
  51. public function getShippingstateTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : $data['shippingstate'];
  54. $list = $this->getShippingstateList();
  55. return $list[$value] ?? '';
  56. }
  57. public function getPaystateTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : $data['paystate'];
  60. $list = $this->getPaystateList();
  61. return $list[$value] ?? '';
  62. }
  63. //获取订单剩余有效时长
  64. public function getRemainsecondsAttr($value, $data)
  65. {
  66. return max(0, $data['expiretime'] - time());
  67. }
  68. /**
  69. * @ 支付
  70. * @param string $orderid
  71. * @param int $user_id
  72. * @param string $paytype
  73. * @param string $method
  74. * @param string $openid
  75. * @param string $notifyurl
  76. * @param string $returnurl
  77. * @return \addons\epay\library\Collection|\addons\epay\library\RedirectResponse|\addons\epay\library\Response|null
  78. * @throws \Exception
  79. */
  80. public static function pay($orderid, $user_id, $paytype, $method = 'web', $openid = '', $notifyurl = null, $returnurl = null)
  81. {
  82. $request = \think\Request::instance();
  83. $order = self::getDetail($orderid, $user_id);
  84. if (!$order) {
  85. throw new \Exception('订单不存在!');
  86. }
  87. if ($order->paystate) {
  88. throw new \Exception('订单已支付!');
  89. }
  90. if ($order->orderstate) {
  91. throw new \Exception('订单已失效!');
  92. }
  93. //支付金额为0,无需支付
  94. if ($order->saleamount == 0) {
  95. throw new \Exception('无需支付!');
  96. }
  97. $order_sn = $order->order_sn;
  98. // 启动事务
  99. Db::startTrans();
  100. try {
  101. //支付方式变更
  102. if (($order['paytype'] == $paytype && $order['method'] != $method)) {
  103. $order_sn = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  104. //更新电子面单
  105. $electronics = $order->order_electronics;
  106. foreach ($electronics as $aftersales) {
  107. $aftersales->order_sn = $order_sn;
  108. $aftersales->save();
  109. }
  110. //更新操作日志
  111. $orderAction = $order->order_action;
  112. foreach ($orderAction as $action) {
  113. $action->order_sn = $order_sn;
  114. $action->save();
  115. }
  116. $order->save(['order_sn' => $order_sn]);
  117. //更新订单明细
  118. foreach ($order->order_goods as $item) {
  119. $item->order_sn = $order_sn;
  120. $item->save();
  121. }
  122. }
  123. //更新支付类型和方法
  124. $order->allowField(true)->save(['paytype' => $paytype, 'method' => $method, 'openid' => $openid]);
  125. //提交事务
  126. Db::commit();
  127. } catch (\Exception $e) {
  128. // 回滚事务
  129. Db::rollback();
  130. throw new \Exception($e->getMessage());
  131. }
  132. $response = null;
  133. $epay = get_addon_info('epay');
  134. if ($epay && $epay['state']) {
  135. $notifyurl = $notifyurl ? $notifyurl : $request->root(true) . '/addons/shop/order/epay/type/notify/paytype/' . $paytype;
  136. $returnurl = $returnurl ? $returnurl : $request->root(true) . '/addons/shop/order/epay/type/return/paytype/' . $paytype . '/order_sn/' . $order_sn;
  137. //保证取出的金额一致,不一致将导致订单重复错误
  138. $amount = sprintf("%.2f", $order->saleamount);
  139. $params = [
  140. 'amount' => $amount,
  141. 'orderid' => $order_sn,
  142. 'type' => $paytype,
  143. 'title' => "支付{$amount}元",
  144. 'notifyurl' => $notifyurl,
  145. 'returnurl' => $returnurl,
  146. 'method' => $method,
  147. 'openid' => $openid
  148. ];
  149. try {
  150. $response = Service::submitOrder($params);
  151. } catch (GatewayException $e) {
  152. throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败,请稍后重试");
  153. }
  154. } else {
  155. throw new \Exception("请在后台安装配置微信支付宝整合插件");
  156. }
  157. return $response;
  158. }
  159. /**
  160. * @ DateTime 2021-06-01
  161. * @ 订单结算
  162. * @param string $order_sn 订单号
  163. * @param float $payamount 支付金额
  164. * @param string $transactionid 流水号
  165. * @return bool
  166. */
  167. public static function settle($order_sn, $payamount, $transactionid = '')
  168. {
  169. $order = self::with(['orderGoods'])->where('order_sn', $order_sn)->find();
  170. if (!$order || $order->paystate == 1) {
  171. return false;
  172. }
  173. if ($payamount != $order->saleamount) {
  174. \think\Log::write("[shop][pay][{$order_sn}][订单支付金额不一致]");
  175. return false;
  176. }
  177. // 启动事务
  178. Db::startTrans();
  179. try {
  180. $order->paystate = 1;
  181. $order->transactionid = $transactionid;
  182. $order->payamount = $payamount;
  183. $order->paytime = time();
  184. $order->paytype = !$order->paytype ? 'system' : $order->paytype;
  185. $order->method = !$order->method ? 'system' : $order->method;
  186. $order->save();
  187. if ($order->payamount == $order->saleamount) {
  188. //支付完成后,商品销量+1
  189. foreach ($order->order_goods as $item) {
  190. $goods = $item->goods;
  191. $sku = $item->sku;
  192. if ($goods) {
  193. $goods->setInc('sales', $item->nums);
  194. }
  195. if ($sku) {
  196. $sku->setInc('sales', $item->nums);
  197. }
  198. }
  199. }
  200. // 提交事务
  201. Db::commit();
  202. } catch (\Exception $e) {
  203. // 回滚事务
  204. Db::rollback();
  205. return false;
  206. }
  207. //记录操作
  208. OrderAction::push($order_sn, '系统', '订单支付成功');
  209. //发送通知
  210. TemplateMsg::sendTempMsg(0, $order->order_sn);
  211. return true;
  212. }
  213. public function user()
  214. {
  215. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  216. }
  217. public function address()
  218. {
  219. return $this->belongsTo('Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
  220. }
  221. public function orderGoods()
  222. {
  223. return $this->hasMany('OrderGoods', 'order_sn', 'order_sn');
  224. }
  225. public function orderElectronics()
  226. {
  227. return $this->hasMany('OrderElectronics', 'order_sn', 'order_sn');
  228. }
  229. public function orderAction()
  230. {
  231. return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
  232. }
  233. }