| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Delivery\Alipay;
- use app\modules\admin\models\AlipayThirdForm;
- use app\models\User;
- use app\models\Store;
- use app\models\SaasUser;
- use yii\base\Model;
- use yii\data\Pagination;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\StoreAliMini;
- use app\models\DeliveryInfo;
- use app\utils\Delivery\Delivery;
- use app\models\Order;
- class ADelivery extends Model
- {
- public $mini_id;
- public $store_id;
- public $pageSize = 5;
- public $page = 1;
- public $status;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'mini_id', 'page', 'pageSize'], 'integer'],
- [['status'], 'safe'],
- ];
- }
-
- public static function getShopNo($mini_id) {
- $mini = StoreAliMini::findOne($mini_id);
- return $mini->auth_app_id . '_' . $mini->store_id . '_' . $mini->id;
- }
- public static function getConf($store_id, $mini_id, $check = null, $account = null, $md = null) {
- $shop_no = Option::get(OptionSetting::ALIPAY_DELIVERY_SHOP_NO . $mini_id, $store_id, 'alipay', '')['value'];
- if(!$check && !$shop_no){
- throw new \Exception('请管理员先配置支付宝同城配送的门店信息,小程序ID:'.$mini_id);
- }
- $company = Option::get(OptionSetting::ALIPAY_DELIVERY_COMPANY . $mini_id, $store_id, 'alipay', '')['value'];
- if(!$check && !$company){
- throw new \Exception('请管理员先配置支付宝同城配送的配送公司信息,小程序ID:'.$mini_id);
- }
- if($check){
- $account = 1;
- $md = 1;
- }
- $account && $account = self::getAccount($store_id, $mini_id);
- if(is_array($account) && empty($account['data']->logistics_account_status)){
- $account['data']->logistics_account_status = [];
- }
- $md && $md = self::getMd($store_id, $mini_id);
- if($check){
- $check = self::check($company, $account, $md);
- }
- $res = [
- 'shop_no' => $shop_no,
- 'company' => $company,
- ];
- $check && $res['check'] = $check;
- $account && $res['account'] = $account;
- $md && $res['md'] = $md;
- return $res;
- }
- public static function check($company, $account, $md) {
- if($account['code'] != 0){
- return ['code' => 1, 'msg' => '账户信息异常'];
- }
- if($md['code'] != 0){
- return ['code' => 1, 'msg' => '门店信息异常'];
- }
- $logistics_code = $company;
- if(isset($md['data']->logistics_shop_status)){
- $has = false;
- foreach ($md['data']->logistics_shop_status as $item) {
- if($item->logistics_code == $logistics_code){
- $has = true;
- if($item->status == 'VALID'){
- break;
- }else{
- return ['code' => 1, 'msg' => '门店当前状态:'.$item->status .' (AUDITING:审核中,AUDIT_REJECT:审核驳回,VALID:已生效,UPDATING:更新中,WAIT_CREATE:待开通)'];
- }
- }
- }
- if(!$has){
- return ['code' => 1, 'msg' => '门店无配送公司信息'];
- }
- }else{
- return ['code' => 1, 'msg' => '门店状态异常错误'];
- }
- if(isset($account['data']->logistics_account_status)){
- $has = false;
- foreach ($account['data']->logistics_account_status as $item) {
- if($item->logistics_code == $logistics_code){
- $has = true;
- if($item->status == 'VALID'){
- break;
- }else{
- return ['code' => 1, 'msg' => '账户当前状态:'.$item->status .' (AUDITING:审核中,AUDIT_REJECT:审核驳回,VALID:已生效)'];
- }
- }
- }
- if(!$has){
- return ['code' => 1, 'msg' => '账户无配送公司信息'];
- }
- }else{
- return ['code' => 1, 'msg' => '账户状态异常错误'];
- }
- if($account['data']->balance < 50){
- return ['code' => 1, 'msg' => '商家支付宝账户可用余额不足50元,请充值'];
- }
- return ['code' => 0, 'msg' => 'ok'];
- }
- public static function getAccount($store_id, $mini_id) {
- try{
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $res = $form->AlipayOpenInstantdeliveryAccountQueryRequest();
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $store_id, $mini_id, $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function createAccount($store_id, $mini_id, $logistics_codes) {
- try{
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $out_biz_no = microtime(true);
- $res = $form->AlipayOpenInstantdeliveryAccountCreate($out_biz_no, $logistics_codes);
- if($res['code'] == 0){
- Option::set(OptionSetting::ALIPAY_DELIVERY_COMPANY . $mini_id, $logistics_codes[0], $store_id, 'alipay');
- }
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $store_id, $mini_id, $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function createMd($store_id, $mini_id, $biz_content) {
- try{
- $shop_no = Option::get(OptionSetting::ALIPAY_DELIVERY_SHOP_NO . $mini_id, $store_id, 'alipay', '')['value'];
- if($shop_no){
- return self::updateMd($store_id, $mini_id, $biz_content);
- }
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $biz_content['shop_no'] = self::getShopNo($mini_id);
- $res = $form->AlipayOpenInstantdeliveryMerchantshopCreate($biz_content);
- if($res['code'] == 0){
- Option::set(OptionSetting::ALIPAY_DELIVERY_SHOP_NO . $mini_id, self::getShopNo($mini_id), $store_id, 'alipay');
- }
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $store_id, $mini_id, $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function updateMd($store_id, $mini_id, $biz_content) {
- try{
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $biz_content2['shop_no'] = self::getShopNo($mini_id);
- $biz_content2['contact_name'] = $biz_content['contact_name'];
- $biz_content2['phone'] = $biz_content['phone'];
- $biz_content2['logistics_codes'] = [];
- $md = self::getMd($store_id, $mini_id);
- if($md['code'] != 0){
- return [
- 'code'=>1,
- 'msg'=>'获取信息错误',
- ];
- }
- foreach ($md['data']->logistics_shop_status as $item) {
- $biz_content2['logistics_codes'][] = $item->logistics_code;
- }
- $res = $form->AlipayOpenInstantdeliveryMerchantshopModify($biz_content2);
- if($res['code'] == 0){
- Option::set(OptionSetting::ALIPAY_DELIVERY_SHOP_NO . $mini_id, self::getShopNo($mini_id), $store_id, 'alipay');
- }
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $store_id, $mini_id, $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function getMd($store_id, $mini_id) {
- try{
- $shop_no = self::getShopNo($mini_id);
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $res = $form->AlipayOpenInstantdeliveryMerchantshopQueryRequest($shop_no);
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $store_id, $mini_id, $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function createOrder($pre, $store_id, $mini_id, $alipay_open_id, $goods_list, $order_no, $address_id) {
- try{
- if(!$address_id){
- throw new \Exception('请先选择收货地址');
- }
- $conf = self::getConf($store_id, $mini_id);
- $store = Store::findOne($store_id);
- $address_info = \app\models\Address::findOne($address_id);
- if (!$address_info) {
- return [
- 'code' => 1,
- 'msg' => '请先设置收货地址'
- ];
- }
- $biz_content = [];
- $biz_content['out_order_no'] = $order_no;
- $biz_content['logistics_companies']['logistics_code'] = $conf['company'];
- $biz_content['shop_no'] = $conf['shop_no'];
- $biz_content['consumer_source'] = 'alipay';
- $biz_content['consumer_id'] = $alipay_open_id;
- $biz_content['receiver'] = [
- 'name' => $address_info->name,
- 'city' => $address_info->city,
- 'address' => $address_info->province . $address_info->city . $address_info->district,
- 'address_detail' => $address_info->detail,
- 'mobile_no' => $address_info->mobile,
- 'lng' => $address_info->longitude,
- 'lat' => $address_info->latitude,
- 'coordinate_type' => 0,
- ];
- $weight = 0.5;
- $total_price = 0;
- foreach ($goods_list as $value) {
- $total_price += $value['price'];
- }
- $biz_content['goods_info'] = [
- 'price' => $total_price,
- 'weight' => $weight,
- 'first_class' => '同城',
- 'second_class' => '配送',
- ];
- $gimg = $goods_list[0]['pic'];
- if(strpos($gimg, 'http') !== 0){
- $gimg = 'https:' . $gimg;
- }
- $biz_content['consumer_notify'] = [
- 'tiny_app_id' => '',
- 'goods_img' => $gimg,
- 'goods_name' => $goods_list[0]['name'],
- 'goods_count' => 1,
- 'merchant_name' => $store['name'],
- 'merchant_mobile' => $store->contact_tel,
- ];
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- if($pre){
- $res = $form->AlipayCommerceLogisticsOrderInstantdeliveryPrecreateRequest($biz_content);
- \Yii::error([__METHOD__, '同城配送下单数据', $res, $biz_content]);
- if (isset($res['code']) && $res['code'] == 0) {
- DeliveryInfo::deleteAll(['order_no' => $order_no, 'store_id' => $store_id, 'status' => 0]);
- // 入表
- $delivery_info = new DeliveryInfo();
- $delivery_info->mini_id = $mini_id;
- $delivery_info->address_id = $address_id;
- $delivery_info->store_id = $store_id;
- $delivery_info->order_no = $order_no;
- $delivery_info->fee = $res['data']->waybills->fee;
- $delivery_info->delivery_token = $res['data']->waybills->logistics_token;
- $delivery_info->delivery_type = $res['data']->waybills->logistics_code;
- $delivery_info->created_at = time();
- $delivery_info->save();
- }
- }else{
- $delivery_info_token = DeliveryInfo::findOne(['order_no' => $order_no])->delivery_token;
- if ($delivery_info_token) {
- $biz_content['logistics_token'] = $delivery_info_token;
- }
- $res = $form->AlipayCommerceLogisticsOrderInstantdeliveryCreateRequest($biz_content);
- \Yii::error([__METHOD__, '同城配送下单数据', $res, $biz_content]);
- if (isset($res['code']) && $res['code'] == 0) {
- // 入表
- $delivery_info = DeliveryInfo::findOne(['store_id' => $store_id, 'order_no' => $order_no]);
- if ($delivery_info) {
- $delivery_info->status = $res['data']->status;
- $delivery_info->waybill_id = $res['data']->waybill_no;
- // $delivery_info->fee = $res['fee'];ML20211101195946262565
- $delivery_info->updated_at = time();
- $delivery_info->save();
- }
- }
- }
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, func_get_args(), $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function cancelOrder($pre, $store_id, $mini_id, $order_no, $reason_id = 5, $reason = '其他原因') {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '缺少订单号参数'
- ];
- }
- $delivery_info = DeliveryInfo::findOne(['store_id' => $store_id, 'order_no' => $order_no]);
- if (!$delivery_info) {
- return [
- 'code' => 1,
- 'msg' => '未找到配送单信息'
- ];
- }
- try{
- $biz_content = [];
- $biz_content['out_order_no'] = $order_no;
- $biz_content['logistics_code'] = $delivery_info->delivery_type;
- $biz_content['waybill_no'] = $delivery_info->waybill_id;
- $biz_content['cancel_reason_id'] = $reason_id;
- $biz_content['cancel_reason'] = $reason;
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- if($pre){
- $res = $form->AlipayCommerceLogisticsOrderIstdcancelPreconsultRequest($biz_content);
- }else{
- $res = $form->AlipayCommerceLogisticsOrderInstantdeliveryCancelRequest($biz_content);
- if (isset($res['code']) && $res['code'] == 0) {
- $delivery_info->reason_id = $reason_id . $reason;
- $delivery_info->save();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- }
- }
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, func_get_args(), $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- /**
- * 拉取配送单信息
- * @return array
- */
- public static function getOrder($store_id, $mini_id, $order_no) {
- if (empty($order_no)) {
- return [
- 'code' => 1,
- 'msg' => '订单号不能为空'
- ];
- }
- $delivery_info = DeliveryInfo::findOne(['store_id' => $store_id, 'order_no' => $order_no]);
- if (!$delivery_info) {
- return [
- 'code' => 1,
- 'msg' => '未找到配送单信息'
- ];
- }
- try{
- $biz_content = [];
- $biz_content['out_order_no'] = $order_no;
- $biz_content['logistics_code'] = $delivery_info->delivery_type;
- $biz_content['waybill_no'] = $delivery_info->waybill_id;
- $biz_content['shop_no'] = self::getShopNo($mini_id);
- $form = new AlipayThirdForm();
- $form->mini_id = $mini_id;
- $res = $form->AlipayCommerceLogisticsWaybillIstddetailQueryRequest($biz_content);
- return $res;
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, func_get_args(), $e]);
- return [
- 'code'=>1,
- 'msg'=>$e->getMessage(),
- ];
- }
- }
- public static function alipayCommerceLogisticsWaybillIstdstatusChanged($data) {
-
- $delivery_info = DeliveryInfo::findOne(['order_no' => $data['out_order_no'], 'waybill_id' => $data['waybill_no']]);
- if (!$delivery_info) {
- \Yii::error(['即时配送状态更改回调===============>', '获取商户信息失败']);
- return;
- }
- $data['status'] = str_replace('i_', '', $data['status']);
- $delivery_info->status = $data['status'];
- $delivery_info->updated_at = strtotime($data['action_time']);
- if (!empty($data['rider_name']) && !empty($data['rider_mobile'])) {
- $delivery_info->rider_name = $data['rider_name'];
- $delivery_info->rider_mobile = $data['rider_mobile'];
- }
- if (!$delivery_info->save()) {
- \Yii::error(['即时配送状态更改回调===============>', '更新数据状态失败']);
- return;
- }
- $order = Order::findOne(['order_no' => $data['out_order_no']]);
- if (in_array($data['status'], Delivery::$validRiderGetGoods)) {
- $order->trade_status = Order::ORDER_FLOW_SEND;
- }
- if (in_array($data['status'], Delivery::$validComplete)) {
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- }
- if (in_array($data['status'], Delivery::$validError)) {
- $order->trade_status = Order::ORDER_FLOW_CANCEL;
- }
- $order->save();
- }
- }
|