Order.php 7.5 KB

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