Order.php 8.2 KB

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