Order.php 8.1 KB

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