123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- use think\Model;
- use Yansongda\Pay\Exceptions\GatewayException;
- use addons\epay\library\Service;
- use app\common\model\OrderAction;
- use app\common\model\TemplateMsg;
- use traits\model\SoftDelete;
- use app\common\Enum\OrderEnum;
- use app\common\Traits\OrderStatusTrait;
- /**
- * 订单模型
- * @property mixed $address 订单地址信息(动态属性)
- */
- class Order extends Model
- {
- use SoftDelete;
- use OrderStatusTrait;
- // 表名
- protected $name = 'shop_order';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'order_status_text',
- ];
- protected static $config = [];
- protected static $tagCount = 0;
- protected static function init()
- {
- $config = get_addon_config('shop');
- self::$config = $config;
- }
- public function getOrderStatusTextAttr($value, $data)
- {
- return OrderEnum::getOrderStatusText($data['order_status']);
- }
- /**
- * @ 支付
- * @param string $orderid
- * @param int $user_id
- * @param string $paytype
- * @param string $method
- * @param string $openid
- * @param string $notifyurl
- * @param string $returnurl
- * @return \addons\epay\library\Collection|\addons\epay\library\RedirectResponse|\addons\epay\library\Response|null
- * @throws \Exception
- */
- public static function pay($orderid, $user_id, $paytype, $method = 'web', $openid = '', $notifyurl = null, $returnurl = null)
- {
- $request = \think\Request::instance();
- $order = self::getDetail($orderid, $user_id);
- if (!$order) {
- throw new \Exception('订单不存在!');
- }
- if ($order->paystate) {
- throw new \Exception('订单已支付!');
- }
- if ($order->orderstate) {
- throw new \Exception('订单已失效!');
- }
- //支付金额为0,无需支付
- if ($order->saleamount == 0) {
- throw new \Exception('无需支付!');
- }
- $order_sn = $order->order_sn;
- // 启动事务
- Db::startTrans();
- try {
- //支付方式变更
- if (($order['paytype'] == $paytype && $order['method'] != $method)) {
- $order_sn = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
- //更新电子面单
- $electronics = $order->order_electronics;
- foreach ($electronics as $aftersales) {
- $aftersales->order_sn = $order_sn;
- $aftersales->save();
- }
- //更新操作日志
- $orderAction = $order->order_action;
- foreach ($orderAction as $action) {
- $action->order_sn = $order_sn;
- $action->save();
- }
- $order->save(['order_sn' => $order_sn]);
- //更新订单明细
- foreach ($order->order_goods as $item) {
- $item->order_sn = $order_sn;
- $item->save();
- }
- }
- //更新支付类型和方法
- $order->allowField(true)->save(['paytype' => $paytype, 'method' => $method, 'openid' => $openid]);
- //提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- $response = null;
- $epay = get_addon_info('epay');
- if ($epay && $epay['state']) {
- $notifyurl = $notifyurl ? $notifyurl : $request->root(true) . '/api/shop/order/epay/type/notify/paytype/' . $paytype;
- $returnurl = $returnurl ? $returnurl : $request->root(true) . '/api/shop/pay//epay/type/return/paytype/' . $paytype . '/order_sn/' . $order_sn;
- //保证取出的金额一致,不一致将导致订单重复错误
- $amount = sprintf("%.2f", $order->saleamount);
- $params = [
- 'amount' => $amount,
- 'orderid' => $order_sn,
- 'type' => $paytype,
- 'title' => "支付{$amount}元",
- 'notifyurl' => $notifyurl,
- 'returnurl' => $returnurl,
- 'method' => $method,
- 'openid' => $openid
- ];
- try {
- $response = Service::submitOrder($params);
- } catch (GatewayException $e) {
- throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败,请稍后重试");
- }
- } else {
- throw new \Exception("请在后台安装配置微信支付宝整合插件");
- }
- return $response;
- }
- /**
- *
- * @ 订单结算
- * @param string $order_sn 订单号
- * @param float $payamount 支付金额
- * @param string $transactionid 流水号
- * @return bool
- */
- public static function settle($order_sn, $payamount, $transactionid = '')
- {
- $order = self::with(['orderGoods'])->where('order_sn', $order_sn)->find();
- if (!$order || $order->paystate == 1) {
- return false;
- }
- if ($payamount != $order->saleamount) {
- \think\Log::write("[shop][pay][{$order_sn}][订单支付金额不一致]");
- return false;
- }
- // 启动事务
- Db::startTrans();
- try {
- $order->paystate = 1;
- $order->transactionid = $transactionid;
- $order->payamount = $payamount;
- $order->paytime = time();
- $order->paytype = !$order->paytype ? 'system' : $order->paytype;
- $order->method = !$order->method ? 'system' : $order->method;
- $order->save();
- if ($order->payamount == $order->saleamount) {
- //支付完成后,商品销量+1
- foreach ($order->order_goods as $item) {
- $goods = $item->goods;
- $sku = $item->sku;
- if ($goods) {
- $goods->setInc('sales', $item->nums);
- }
- if ($sku) {
- $sku->setInc('sales', $item->nums);
- }
- }
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return false;
- }
- //记录操作
- OrderAction::push($order_sn, '系统', '订单支付成功');
- //发送通知
- TemplateMsg::sendTempMsg(0, $order->order_sn);
- return true;
- }
- public function user()
- {
- return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function address()
- {
- return $this->belongsTo('Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function orderGoods()
- {
- return $this->hasMany('OrderGoods', 'order_sn', 'order_sn');
- }
- public function orderElectronics()
- {
- return $this->hasMany('OrderElectronics', 'order_sn', 'order_sn');
- }
- public function orderAction()
- {
- return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
- }
- public function orderAddress()
- {
- return $this->hasOne('OrderAddress', 'order_id', 'id');
- }
- }
|