diff --git a/app/api/controller/order/OrderController.php b/app/api/controller/order/OrderController.php new file mode 100644 index 000000000..b8a355635 --- /dev/null +++ b/app/api/controller/order/OrderController.php @@ -0,0 +1,376 @@ +dataLists(new OrderList()); + } + + /** + * 摊贩订单列表 + */ + /* public function merchant_order_list() + { + $number = $this->request->get('number'); + $page_no = $this->request->get('page_no', 1); + $date = $this->request->get('date', date('Y-m-d')); + if (!$this->userInfo['merchant']['mer_id']) { + return $this->fail('没有权限'); + } + $where = []; + if ($number) { + $where[] = ['number', 'like', '%' . $number . '%']; + } + $where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']]; + $where[] = ['paid', '=', 1]; + $where[] = ['pay_type', '<>', 9]; + $where[] = ['is_opurchase', '=', 0]; + $res = StoreOrder::where($where)->page($page_no, 25)->whereDay('create_time', $date) + ->order('address_id asc,id desc') + ->select()->each(function ($item) { + $item['goods_list'] = Cashierinfo::where('pid', $item['id'])->with('goodsName')->field('goods,nums,price sell')->limit(5)->select(); + $item['goods_count'] = count(explode(',', $item['cart_id'])); + }); + $data['count'] = StoreOrder::where($where)->whereDay('create_time', $date)->count(); + $data['lists'] = $res?->toArray(); + $data['page_no'] = $page_no; + $data['page_siz'] = 15; + return $this->success('ok', $data); + }*/ + /** + * 摊贩订单统计 + */ + /* public function merchant_order_count() + { + $date = $this->request->get('date', date('Y-m-d')); + $where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']]; + $where[] = ['paid', '=', 1]; + $where[] = ['pay_type', '<>', 9]; + $where[] = ['is_opurchase', '=', 0]; + $res = StoreOrder::where($where)->whereDay('create_time', $date)->count(); + return $this->success('ok', ['order_count' => $res]); + } + public function order_count() + { + $userId = $this->request->userId; + $where = ['uid' => $userId, 'paid' => 0]; + $no_pay = StoreOrder::where($where)->count(); + $where['paid'] = 1; + $where['status'] = 0; + $waiting = StoreOrder::where($where)->count(); + $where['status'] = 1; + $receiving = StoreOrder::where($where)->count(); + return $this->success('ok', ['no_pay' => $no_pay, 'waiting' => $waiting, 'receiving' => $receiving]); + }*/ + /** + * @notes 检测零售订单 + */ + public function checkOrder() + { + $cartId = (array)$this->request->post('cart_id', []); + $addressId = (int)$this->request->post('address_id'); + // $pay_type = (int)$this->request->post('pay_type'); + // $auth_code = $this->request->post('auth_code'); //微信支付条码 + $params = $this->request->post(); + $res = OrderLogic::cartIdByOrderInfo($cartId, $addressId, null, $params); + if ($res == false) { + $msg = OrderLogic::getError(); + if ($msg == '购物车为空') { + return $this->data([]); + } + return $this->fail(OrderLogic::getError()); + } + return $this->data($res); + } + + /** + * @notes 创建零售订单 + */ + public function createOrder() + { + + // d(WeChatConfigService::getPayConfigByTerminal(1)); + $user = User::where('id', $this->request->userId)->find(); + $cartId = (array)$this->request->post('cart_id', []); + $mer_id = (array)$this->request->post('mer_id', 0); + $pay_type = (int)$this->request->post('pay_type'); + $addressId = (int)$this->request->post('address_id'); + $auth_code = $this->request->post('auth_code'); //微信支付条码 + $params = $this->request->post(); + if ($mer_id <= 0 && $pay_type != 9) { + return $this->fail('自提点不能为空'); + } + if (count($cartId) > 100) { + return $this->fail('购物车商品不能超过100个'); + } + + if ($pay_type == 9 || $pay_type == 17 ||$pay_type==13) { + if (empty($this->request->userInfo['merchant'])) { + return $this->fail('请先绑定商户'); + } + $mer_id = $this->request->userInfo['merchant']['mer_id']; + $params['mer_id'] = $mer_id; + } + $order = OrderLogic::createOrder($cartId, $addressId, null, $params); + if ($order != false) { + switch ($pay_type) { + case PayEnum::BALANCE_PAY: + //余额支付 + $user = User::where('id', $this->request->userId)->find(); + $res = OrderLogic::payBalance($user, $order); + if (OrderLogic::hasError()) { + return $this->fail(OrderLogic::getError()); + } else { + $res = OrderLogic::paySuccess($order, ['money' => $order['actual']]); + if (OrderLogic::hasError()) { + return $this->fail(OrderLogic::getError()); + } + return $this->success('余额支付成功'); + } + case PayEnum::CASH_PAY: + //现金支付 + PayNotifyLogic::handle('cash_pay', $order['number']); + return $this->success('现金支付成功'); + case PayEnum::WECHAT_PAY: + //微信支付 + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + $result = PaymentLogic::pay($pay_type, 'StoreOrder', $order, $this->userInfo['terminal'], $redirectUrl); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + return $this->success('', $result); + break; + case PayEnum::WECHAT_PAY_BARCODE: + //微信条码支付 + $result = PaymentLogic::codepay($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') { + PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result); + } else { + Redis::send('send-code-pay', ['number' => $order['number']]); + return $this->success('用户支付中'); + } + return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::WECHAT_PAY_BARCODE,'transaction_id'=>$result['transaction_id']]); + break; + case PayEnum::ALIPAY_BARCODE: + //支付宝条码支付 + $result = PaymentLogic::ali_auth_code($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if ($result['msg'] !== 'Success') { + return $this->success('用户支付中'); + } + $result['create_time'] = $order['create_time']; + return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::ALIPAY_BARCODE,'transaction_id'=>$result['trade_no']]); + break; + default: + return $this->fail('支付方式错误'); + } + return $this->data(['order_id' => $order->id]); + } else { + return $this->fail(OrderLogic::getError()); + } + } + + /** + * 重新支付 + */ + public function Repayment() + { + $order_id = (int)$this->request->post('order_id'); + $addressId = (int)$this->request->post('address_id'); + $pay_type = (int)$this->request->post('pay_type'); + $auth_code = $this->request->post('auth_code'); //微信支付条码 + $params = $this->request->post(); + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 0, + ]; + $order = StoreOrder::where($where)->find(); + if (!$order) return $this->fail('订单不存在或已支付'); + + switch ($pay_type) { + case PayEnum::BALANCE_PAY: + //余额支付 + $user = User::where('id', $this->request->userId)->find(); + $res = OrderLogic::payBalance($user, $order); + if (!OrderLogic::hasError()) { + $res = OrderLogic::paySuccess($order, ['money' => $order['actual']]); + if (OrderLogic::hasError()) { + return $this->fail(OrderLogic::getError()); + } + return $this->success('余额支付成功'); + } else { + return $this->fail(OrderLogic::getError()); + } + break; + case PayEnum::CASH_PAY: + //现金支付 + PayNotifyLogic::handle('cash_pay', $order['number']); + return $this->success('现金支付成功'); + break; + case PayEnum::WECHAT_PAY: + //微信支付 + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + if ($addressId != $order['address_id']) { + $address = UserAddress::where(['address_id' => $addressId, 'uid' => Request()->userId])->find(); + if ($address) { + $_order['real_name'] = $address['real_name']; + $_order['user_phone'] = $address['phone']; + $_order['user_address'] = $address['detail']; + $_order['address_id'] = $addressId; + StoreOrder::where(['id' => $order_id, 'uid' => Request()->userId])->update($_order); + } + } + $result = PaymentLogic::pay($pay_type, 'StoreOrder', $order, $this->userInfo['terminal'], $redirectUrl); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError()); + } + return $this->success('', $result); + break; + case PayEnum::WECHAT_PAY_BARCODE: + //微信条码支付 + $result = PaymentLogic::codepay($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') { + PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result); + } else { + Redis::send('send-code-pay', ['number' => $order['number']]); + return $this->success('用户支付中'); + } + return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::WECHAT_PAY_BARCODE,'transaction_id'=>$result['transaction_id']]); + break; + case PayEnum::ALIPAY_BARCODE: + //支付宝条码支付 + $result = PaymentLogic::ali_auth_code($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + if ($result['msg'] !== 'Success') { + return $this->success('用户支付中'); + } + return $this->success('支付成功', ['out_trade_no'=>$result['out_trade_no'],'pay_type'=>PayEnum::ALIPAY_BARCODE,'transaction_id'=>$result['trade_no']]); + break; + default: + return $this->fail('支付方式错误'); + } + return $this->fail('支付失败'); + } + + public function detail() + { + $order_id = (int)$this->request->get('order_id'); + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + ]; + $order = OrderLogic::detail($where); + if ($order) { + return $this->data($order); + } else { + return $this->fail('订单不存在'); + } + } + + /** + * 获取用户常用购买记录 + */ + public function frequently_purchase() + { + $params = $this->request->get(); + $res = OrderLogic::frequentlyPurchase($params); + if (OrderLogic::hasError()) { + return $this->fail(OrderLogic::getError()); + } else { + return $this->data($res); + } + } + + /** + * 取消订单 + */ + public function cancel_order() + { + $order_id = (int)$this->request->post('order_id'); + $value = (int)$this->request->post('value'); + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 0, + ]; + $order = StoreOrder::where($where)->find(); + if ($order) { + $data = ['data' => $value, 'delete_time' => time()]; + StoreOrder::where($where)->update($data); + return $this->success('取消成功'); + } + return $this->fail('取消失败'); + } + + /** + * 确认收货 + */ + public function confirm_receipt() + { + $order_id = (int)$this->request->post('order_id'); + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 1, + 'status' => 1, + ]; + $order = StoreOrder::where($where)->find(); + if ($order) { + $data = ['status' => 2]; + StoreOrder::where($where)->update($data); + return $this->success('确认成功'); + } + return $this->fail('确认失败'); + } + + /** + * 再次购买 + */ + public function purchase_again() + { + $order_id = (int)$this->request->get('order_id'); + + $res = OrderLogic::purchaseAgain($order_id); + if ($res == false) { + return $this->fail('添加失败'); + } else { + return $this->success('添加成功'); + } + } +} diff --git a/app/api/controller/store/StoreController.php b/app/api/controller/store/StoreController.php index 4d378a6cd..02e112b60 100644 --- a/app/api/controller/store/StoreController.php +++ b/app/api/controller/store/StoreController.php @@ -4,12 +4,16 @@ namespace app\api\controller\store; use app\api\lists\store\SystemStoreLists; use app\api\controller\BaseApiController; +use app\common\service\pay\PayService; +use Webman\Config; class StoreController extends BaseApiController { - + public $notNeedLogin = ['index','app_update','lists']; public function lists() { + $config = Config::get('payment'); + d($config); return $this->dataLists(new SystemStoreLists()); } diff --git a/app/api/lists/order/OrderList.php b/app/api/lists/order/OrderList.php new file mode 100644 index 000000000..486ee9b50 --- /dev/null +++ b/app/api/lists/order/OrderList.php @@ -0,0 +1,70 @@ + ['paid','status','source'], + 'between_time' => 'create_time', + '%like%' => ['number'], + ]; + } + + + /** + * @notes 零售订单列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @date 2024/04/27 11:26 + */ + public function lists(): array + { + $userId=$this->request->userId; + if(!$userId) return []; + return Cashierclass::where($this->searchWhere)->where('uid',$userId) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->each(function($item){ + $item['goods_list']=Cashierinfo::where('pid',$item['id'])->with('goodsName')->field('goods,nums,price sell')->limit(3)->select(); + $item['goods_count']=count(explode(',',$item['cart_id'])); + }) + ->toArray(); + } + + + /** + * @notes 零售订单数量 + * @return int + * @date 2024/04/27 11:26 + */ + public function count(): int + { + return Cashierclass::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php new file mode 100644 index 000000000..a9abe3447 --- /dev/null +++ b/app/api/logic/order/OrderLogic.php @@ -0,0 +1,386 @@ + 0, 'is_fail' => 0]; + $cart_select = Cart::whereIn('cart_id', $cartId)->where($where)->field('goods_id as goods,cart_num')->select()->toArray(); + if (empty($cart_select)) { + self::setError('购物车为空'); + return false; + } + try { + self::$total = 0; + /** 计算价格 */ + foreach ($cart_select as $k => $v) { + $find = Goods::where(['id' => $v['goods']])->field('name,imgs,unit,sell')->find(); + if(!$find){ + continue; + } + $cart_select[$k]['total'] = bcmul($v['cart_num'], $find['sell'], 2); + $cart_select[$k]['price'] = $find['sell']; + $cart_select[$k]['name'] = $find['name']; + $cart_select[$k]['imgs'] = $find['imgs']; + $cart_select[$k]['unit_name'] = Unit::where(['id' => $find['unit']])->value('name'); + self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2); + } + $order = [ + 'time' => time(), + 'number' => getNewOrderId('PF'), + 'total' => self::$total, + 'pay_type' => $params['pay_type'] ?? 0, + 'cart_id' => implode(',', $cartId), + 'delivery_msg'=>' 预计48小时发货 ' + ]; + } catch (\Exception $e) { + self::setError($e->getMessage()); + return false; + } + return ['order' => $order, 'cart_list' => $cart_select]; + } + + /** + * 创建新订单 + * @return Object|bool + */ + static public function createOrder($cartId, $addressId, $user = null, $params = []) + { + $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); + if(!$orderInfo){ + return false; + } + $_order = $orderInfo['order']; + $_order['deduction_price'] = 0; + $_order['merchant'] = $params['mer_id']; + $_order['uid'] = request()->userId; + $_order['money'] = 0; + $_order['user'] = request()->userId; + $_order['account'] = 0; + $_order['payinfo'] = ''; + $_order['type'] = 0; + $_order['source'] = 0; + $_order['actual'] = $_order['total']; + if($addressId>0){ + $address=UserAddress::where(['address_id'=>$addressId,'uid'=>Request()->userId])->find(); + if($address){ + $_order['real_name'] = $address['real_name']; + $_order['user_phone'] = $address['phone']; + $_order['user_address'] = $address['detail']; + $_order['address_id'] = $addressId; + } + } + if($params['pay_type']==PayEnum::WECHAT_PAY_BARCODE){ + $_order['source']=1; + } + if($params['pay_type']==PayEnum::CASH_PAY){ + $_order['money']=$_order['total']; + } + Db::startTrans(); + try { + $order = Cashierclass::create($_order); + $goods_list = $orderInfo['cart_list']; + foreach ($goods_list as $k => $v) { + $goods_list[$k]['pid'] = $order->id; + $goods_list[$k]['merchant'] = $params['mer_id']; + $goods_list[$k]['uid'] = request()->userId; + $goods_list[$k]['room'] = 0; + $goods_list[$k]['discount'] = 0; + $goods_list[$k]['warehouse'] = 0; + $goods_list[$k]['nums'] = $v['cart_num']; + } + (new Cashierinfo())->saveAll($goods_list); + $where = ['is_pay' => 0, 'is_fail' => 0]; + Cart::whereIn('cart_id', $cartId)->where($where)->update(['is_pay'=>1]); + Db::commit(); + return $order; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + /** + * @notes 获取购货订单购物车商品信息 + * @param $params + * @return array + */ + static public function cartIdByPurchaseOrderInfo($user, $params) + { + if (!$user) { + self::setError('没有用户信息,请先登录'); + return false; + } + $mer_id = $user['merchant']['mer_id']; + $where1 = ['paid' => 1,'is_opurchase'=>0]; + $where1[] = ['pay_type','<>',9]; + + $arrs = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('cart_id,id,address_id'); + // $order_id = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('id'); + $cart_arr = []; + $order_id = []; + foreach ($arrs as $k => $v) { + if (empty($v['cart_id'])) { + self::setError('没有购物车信息'); + return false; + } + // if (empty($v['address_id'])) { + // self::setError('请先设置配送地址'); + // return false; + // } + $arr = explode(',',$v['cart_id']); + foreach ($arr as $kk => $vv) { + $cart_arr[] = $vv; + } + $order_id[] = $v['id']; + } + $where = ['is_pay' => 1, 'is_fail' => 0]; + $cart_select = Cart::whereIn('cart_id', $cart_arr)->where($where)->field('goods_id as goods,cart_num')->select()->toArray(); + if (empty($cart_select)) { + self::setError('购物车为空'); + return false; + } + /** 计算价格 */ + foreach ($cart_select as $k => $v) { + $sell = Goods::where(['id' => $v['goods']])->value('sell'); + $cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2); + $cart_select[$k]['price'] = $sell; + } + $order = [ + 'time' => time(), + 'number' => static::getNewOrderId('CG'), + 'total' => array_sum(array_column($cart_select, 'total')), + 'pay_type' => $params['pay_type'] ?? 0, + 'cart_id' => implode(',', $cart_arr), + 'order_arr' => implode(',', $order_id) + ]; + return ['order' => $order, 'cart_list' => $cart_select]; + } + /** + * 创建购货订单 + * @return Object|bool + */ + static public function createOpurchaseOrder($user = null, $params = []) + { + + if (!$user) { + self::setError('没有用户信息,请先登录'); + return false; + } + $mer_id = $user['merchant']['mer_id']; + // $merchant = Merchant::where('mer_id', $mer_id)->find(); + $orderInfo = self::cartIdByPurchaseOrderInfo($user, $params); + if (!$orderInfo) { + return false; + } + $_order = $orderInfo['order']; + // if ($_order['total'] < $merchant['mer_money']) { + // self::setError('商户余额不足'); + // return false; + // } + $_order['merchant'] = $mer_id; + $_order['money'] = $_order['total']; + $_order['actual'] = $_order['total']; + $_order['paid'] = 1; + Db::startTrans(); + try { + $order = Opurchaseclass::create($_order); + + $goods_list = $orderInfo['cart_list']; + foreach ($goods_list as $k => $v) { + $goods_list[$k]['nums'] = $v['cart_num']; + $goods_list[$k]['pid'] = $order->id; + } + (new Opurchaseinfo())->saveAll($goods_list); + // $merchant->mer_money = bcsub($merchant->mer_money, $_order['total'], 2); + $order_arr = explode(',', $_order['order_arr']); + Cashierclass::where('id', 'in', $order_arr)->update(['is_opurchase' => 1]); + Db::commit(); + return $order; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + /** + * @notes 获取订单号 + * @param $type + * @return string + * @author likeadmin + * @date 2021/7/28 17:05 + */ + static public function getNewOrderId($type) + { + list($msec, $sec) = explode(' ', microtime()); + $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); + $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)); + return $orderId; + } + + + + + /** + * 余额订单支付 + * @param User $user + * @param $order + * @return bool + * @throws Exception + * @throws ValidateException + */ + static public function payBalance(User $user, $order) + { + if ($user['user_money'] < $order['actual']) { + self::setError('余额不足,请更换支付方式'); + return false; + } + Db::startTrans(); + try { + $user->user_money = bcsub($user->user_money, $order['actual'], 2); + $user->save(); + Db::commit(); + return true; + } catch (Exception $e) { + Db::rollback(); + Log::error('余额支付失败' . $e->getMessage() . '。line:' . $e->getLine() . '。file:' . $e->getFile()); + self::setError('余额支付失败' . $e->getMessage()); + return false; + } + } + + /** + * @notes 订单支付成功 + * @param $order 订单 + * @param $CallbackData 回调数据 + * @date 2021/7/8 00:40 + */ + static function paySuccess($order, $CallbackData = []) + { + Db::startTrans(); + try { + $order->money = $CallbackData['money']; + $order->paid = 1; + $order->save(); + Log::info('支付成功'); + // 提交事务 + Db::commit(); + return true; + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine()); + self::setError('支付失败' . $e->getMessage()); + return false; + } + } + + /** + * 获取用户常用购买记录 + */ + public static function frequentlyPurchase($params) + { + try { + $goods_id = StoreOrderCartInfo::where('uid', Request()->userId)->page($params['page_no'])->limit(50)->column('product_id'); + if(!$goods_id){ + return []; + } + $goods_arr = array_unique($goods_id); + $select = StoreProduct::where('id', 'in', $goods_arr)->with('unitName')->field('id,name,sell,imgs,unit')->select(); + return $select->toArray(); + } catch (\Exception $e) { + self::setError($e->getMessage()); + return false; + } + } + + public static function purchaseAgain($order_id){ + $arr= StoreOrderCartInfo::where('oid',$order_id)->field('product_id,cart_num,staff_id')->select(); + $data=[]; + foreach ($arr as $k=>$v){ + $data[$k]['product_id']=$v['product_id']; + $unique = StoreProductAttrValue::where('product_id',$v['product_id'])->value('v'); + $data[$k]['product_attr_unique']=$unique; + $data[$k]['cart_num']=$v['cart_num']; + $data[$k]['type']=''; + $data[$k]['uid']=Request()->userId; + $store_id = StoreProduct::where('id',$v['product_id'])->value('store_id'); + $data[$k]['store_id']=$store_id; + $data[$k]['staff_id']=$v['staff_id']; + $data[$k]['add_time']=time(); + $data[$k]['combination_id']=0; + $data[$k]['seckill_id']=0; + $data[$k]['bargain_id']=0; + $data[$k]['discount_id']=0; + $data[$k]['status']=1; + $data[$k]['staff_id']=0; + $data[$k]['is_new']=0; + $data[$k]['is_del']=0; + $data[$k]['is_pay']=0; + } + if($data){ + ( new Cart())->saveAll($data); + return true; + } + return false; + } + + + + public static function detail($params): array + { + $find=StoreOrder::where($params)->findOrEmpty()->toArray(); + if($find){ + + $find['goods_list']= StoreOrderCartInfo::where('oid',$find['id']) + ->with('goodsName') + ->field('product_id,cart_num nums')->select()->each(function($item){ + $item['msg']='预计48小时发货'; + $item['unit_name']=StoreProductUnit::where('id',$item['unit'])->value('name'); + }); + $merchant=Merchant::where('mer_id',$find['merchant'])->field('mer_id,uid,mer_name,service_phone,mer_address')->find(); + $merchant['real_name']=User::where('id',$merchant['uid'])->value('real_name'); + $find['merchant_info']=$merchant; + + } + return $find; + } +} diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php new file mode 100644 index 000000000..7421be36e --- /dev/null +++ b/app/common/logic/PayNotifyLogic.php @@ -0,0 +1,298 @@ +getFile(), + $e->getLine(), + $e->getMessage() + ])); + self::setError($e->getMessage()); + return $e->getMessage(); + } + } + + + /** + * @notes 零售回调 + * @param $orderSn + * @param array $extra + * @author 段誉 + * @date 2023/2/27 15:28 + */ + public static function cashierclass($orderSn, $extra = []) + { + $order = Cashierclass::where('number', $orderSn)->findOrEmpty(); + + if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { + return true; + } + $financial_type = OrderEnum::USER_ORDER_PAY; + $financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS; + if ($order->pay_type != 10) { + $order->money = bcdiv($extra['amount']['payer_total'], 100, 2); + $order->paid = 1; + $order->status = 1; + $order->save(); + } else { + $financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY; + $extra['transaction_id'] = time(); + } + if ($order->pay_type == 9) { + $order->status = 2; + $financial_type2 = OrderEnum::CASHIER_ORDER_PAY; + } + if ($order->pay_type != 9 || $order->pay_type != 10) { + //用户支出流水 + $record[] = [ + 'financial_record_sn' => $extra['transaction_id'], + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => $financial_type, + 'financial_pm' => OrderEnum::EXPENDITURE, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::USER, + 'mer_id' => $order['merchant'], + ]; + } + //商户获得流水 + $record[] = [ + 'financial_record_sn' => $extra['transaction_id'], + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => $financial_type2, + 'financial_pm' => OrderEnum::INCOME, + 'number' => $order['actual'], + 'status' => 0, + 'type' => OrderEnum::MERCHANT, + 'mer_id' => $order['merchant'], + ]; + (new FinancialRecord())->saveAll($record); + + + if ($order->pay_type == 9) { + $extra['create_time'] = $order['create_time']; + PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]); + } else { + PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']); + Redis::send('push-platform-print', ['order_id' => $order['id']], 60); + Db::name('order_middle')->insert(['c_order_id' =>$order['id']]); + } + if (!empty($extra['payer']['openid']) && $order->pay_type != 9) { + Redis::send('push-delivery', ['order_id' => $orderSn, 'openid' => $extra['payer']['openid']], 5); + } + return true; + } + /** + * 采购订单支付成功 + */ + public static function opurchaseclass($orderSn, $extra = []) + { + $order = Opurchaseclass::where('number', $orderSn)->findOrEmpty(); + $order_arr = explode(',', $order['order_arr']); + (new FinancialRecord())->where('order_id', 'in', $order_arr)->update(['status' => 1]); + Db::name('order_middle')->where('c_order_id', 'in', $order_arr)->update(['b_order_id' => $order['id']]); + $time = time(); + //商户支出流水 + $record[] = [ + 'financial_record_sn' => $time, + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => OrderEnum::MERCHANT_ORDER_PAY, + 'financial_pm' => OrderEnum::EXPENDITURE, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::MERCHANT, + 'mer_id' => $order['merchant'], + ]; + //平台获得流水 + $record[] = [ + 'financial_record_sn' => $time, + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => OrderEnum::PLATFORM_ORDER_OBTAINS, + 'financial_pm' => OrderEnum::INCOME, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::PLATFORM, + 'mer_id' => $order['merchant'], + ]; + (new FinancialRecord())->saveAll($record); + } + + /** + * 平台采购订单支付成功 + */ + public static function operated($orderSn, $extra = []) + { + $order = Opurchaseclass::where('number', $orderSn)->findOrEmpty(); + $order_arr = explode(',', $order['order_arr']); + (new FinancialRecord())->where('order_id', 'in', $order_arr)->update(['status' => 1]); + Db::name('order_middle')->where('b_order_id', 'in', $order_arr)->update(['p_order_id' => $order['id']]); + $time = time(); + //平台支出流水 + $record[] = [ + 'financial_record_sn' => $time, + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => OrderEnum::PLATFORM_ORDER_PAY, + 'financial_pm' => OrderEnum::EXPENDITURE, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::PLATFORM, + 'mer_id' => getenv('OPERATED'), + ]; + // //平台获得流水 + // $record[] = [ + // 'financial_record_sn' => $time, + // 'order_id' => $order['id'], + // 'number_sn' => $order['number'], + // 'user_id' => $order['uid'], + // 'financial_type' => OrderEnum::PLATFORM_ORDER_OBTAINS, + // 'financial_pm' => OrderEnum::INCOME, + // 'number' => $order['actual'], + // 'status' => 1, + // 'type' => OrderEnum::PLATFORM, + // 'mer_id' => $order['merchant'], + // ]; + (new FinancialRecord())->saveAll($record); + } + /** + * 现金支付 + */ + public static function cash_pay($orderSn) + { + $order = Cashierclass::where('number', $orderSn)->findOrEmpty(); + + if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { + return true; + } + $order->paid = 1; + $order->status = 2; + $order->save(); + //商户获得流水 + $record[] = [ + 'financial_record_sn' => time(), + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => OrderEnum::CASHIER_CASH_ORDER_PAY, + 'financial_pm' => OrderEnum::INCOME, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::MERCHANT, + 'mer_id' => $order['merchant'], + ]; + (new FinancialRecord())->saveAll($record); + } + + /** + * @notes 零售回调 + * @param $orderSn + * @param array $extra + * @author 段誉 + * @date 2023/2/27 15:28 + */ + public static function alipay_cashier($orderSn, $extra = []) + { + $order = Cashierclass::where('number', $orderSn)->findOrEmpty(); + + if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { + return true; + } + $financial_type = OrderEnum::USER_ORDER_PAY; + $financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS; + if ($order->pay_type != 10) { + $order->money = $extra['buyer_pay_amount']; + $order->paid = 1; + $order->status = 1; + $order->save(); + } else { + $financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY; + $extra['transaction_id'] = time(); + } + if ($order->pay_type == 9) { + $order->status = 2; + $financial_type2 = OrderEnum::CASHIER_ORDER_PAY; + } + if ($order->pay_type != 9 || $order->pay_type != 10) { + //用户支出流水 + $record[] = [ + 'financial_record_sn' => $extra['trade_no'], + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => $financial_type, + 'financial_pm' => OrderEnum::EXPENDITURE, + 'number' => $order['actual'], + 'status' => 1, + 'type' => OrderEnum::USER, + 'mer_id' => $order['merchant'], + ]; + } + //商户获得流水 + $record[] = [ + 'financial_record_sn' => $extra['trade_no'], + 'order_id' => $order['id'], + 'number_sn' => $order['number'], + 'user_id' => $order['uid'], + 'financial_type' => $financial_type2, + 'financial_pm' => OrderEnum::INCOME, + 'number' => $order['actual'], + 'status' => 0, + 'type' => OrderEnum::MERCHANT, + 'mer_id' => $order['merchant'], + ]; + (new FinancialRecord())->saveAll($record); + + + if ($order->pay_type == 9) { + $extra['create_time'] = $order['create_time']; + PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'cash_register', 'msg' => '您有一笔订单已支付', 'data' => $extra]); + } else { + PushService::push('store_merchant_' . $order['merchant'], $order['merchant'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']); + Redis::send('push-platform-print', ['order_id' => $order['id']], 60); + } + return true; + } +} diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php new file mode 100644 index 000000000..628fac197 --- /dev/null +++ b/app/common/logic/PaymentLogic.php @@ -0,0 +1,138 @@ + PayEnum::BALANCE_PAY]; + } + switch ($payWay) { + case PayEnum::WECHAT_PAY: + $auth = UserAuth::where(['user_id' => $order['uid'], 'terminal' => $terminal])->findOrEmpty(); + $order = [ + 'out_trade_no' => $paySn, + 'description' => '商品', + 'amount' => [ + 'total' => intval($order['actual'] * 100), + 'currency' => 'CNY', + ], + "payer" => [ + "openid" => $auth['openid'] ?? 0 + ], + 'attach' => $from + ]; + $wechat = new PayService(1); + $result = $wechat->wechat->mini($order)->toArray(); + break; + default: + self::$error = '订单异常'; + $result = false; + } + return $result; + } + + /** + * 微信条码支付 + */ + public static function codepay($auth_code, $order) + { + $pattern = '/^(10|11|12|13|14|15)\d{16}$/'; + + if (!preg_match($pattern, (string)$auth_code)) { + self::$error = '请使用正确的微信收付款条码'; + return false; + } + $order = [ + 'description' => '条码商品', + 'out_trade_no' => $order['number'], + 'payer' => [ + 'auth_code' => (string)$auth_code + ], + 'amount' => [ + 'total' => intval($order['actual'] * 100), + ], + 'scene_info' => [ + "store_info" => [ + 'id' => (string)$order['merchant'] + ] + ], + ]; + $wechat = new PayService(1); + try { + $result = $wechat->wechat->pos($order)->toArray(); + } catch (ExceptionException $e) { + if (getenv('APP_DEBUG') == true) { + self::$error = $e->extra['message'] ?? $e->getMessage(); + } else { + self::$error = $e->getMessage(); + } + return false; + } + return $result; + } + + /** + * 支付宝条码支付 + */ + public static function ali_auth_code($auth_code, $order) + { + $pattern = '/^(25|26|27|28|29|30)[0-9A-Za-z]{14,23}$/'; + + if (!preg_match($pattern, (string)$auth_code)) { + self::$error = '请使用正确的支付宝收付款条码'; + return false; + } + $order = [ + 'subject' => '条码商品', + 'out_trade_no' => $order['number'], + 'auth_code' => (string)$auth_code, + 'total_amount' => $order['actual'], + 'extend_params'=>['attach'=>'alipay_cashier'] + ]; + $wechat = new PayService(); + try { + $result = $wechat->alipay->pos($order)->toArray(); + } catch (ExceptionException $e) { + if (getenv('APP_DEBUG') == true) { + self::$error = $e->extra['message'] ?? $e->getMessage(); + } else { + self::$error = $e->getMessage(); + } + return false; + } + return $result; + } +} diff --git a/app/common/model/store_order_cart_info/StoreOrderCartInfo.php b/app/common/model/store_order_cart_info/StoreOrderCartInfo.php new file mode 100644 index 000000000..c1e1d212c --- /dev/null +++ b/app/common/model/store_order_cart_info/StoreOrderCartInfo.php @@ -0,0 +1,19 @@ +hasOne(StoreProduct::class,'id','product_id')->bind(['store_name','image','unit','price']); + } +} \ No newline at end of file diff --git a/app/common/model/store_product/StoreProduct.php b/app/common/model/store_product/StoreProduct.php index fd9659f08..639635600 100644 --- a/app/common/model/store_product/StoreProduct.php +++ b/app/common/model/store_product/StoreProduct.php @@ -4,6 +4,7 @@ namespace app\common\model\store_product; use app\common\model\BaseModel; +use app\common\model\store_product_unit\StoreProductUnit; use think\model\concern\SoftDelete; @@ -18,5 +19,11 @@ class StoreProduct extends BaseModel protected $name = 'store_product'; protected $deleteTime = 'delete_time'; + + public function unitName() + { + return $this->hasOne(StoreProductUnit::class,'id','unit')->bind(['unit_name'=>'name','is_bulk']); + } + } \ No newline at end of file