| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\constants\OptionSetting;
- use app\jobs\order\NoAfterSalesOrderJob;
- use app\models\AccountLog;
- use app\models\AggregateSaasQrcode as AggregateQrcode;
- use app\models\GiveIntegralRules;
- use app\models\OrderDetail;
- use app\models\RulesFaceStore;
- use app\models\RulesGiveIntegralStore;
- use app\models\SaaSLeaguePriceLog;
- use app\models\SaasUser;
- use app\models\User;
- use app\models\UserShareMoney;
- use app\utils\Alipay\alipaySdk\aop\request\AlipaySystemOauthTokenRequest;
- use app\models\Goods;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\SaasCategory;
- use app\models\Salesman;
- use app\models\Store;
- use app\models\StoreAliMini;
- use app\models\StoreAudit;
- use app\models\StoreMini;
- use app\models\WechatConfig;
- use app\modules\admin\models\AlipayThirdForm;
- use app\modules\admin\models\WechatThirdForm;
- use app\modules\client\models\v1\DiyForm;
- use app\utils\Alipay\AlipayUnion;
- use app\utils\OrderNo;
- use app\utils\Wechat\WechatAlliancePay;
- use app\utils\Wechat\WechatBusinessPay;
- use app\utils\Wechat\WechatNewPay;
- use app\utils\yunst\src\OrderYunst;
- use yii\base\Model;
- use EasyWeChat\Factory;
- use yii\data\Pagination;
- use app\plugins\scanCodePay\models\Order;
- use app\utils\Alipay\Alipay;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\Wechat\Wechat;
- use app\utils\Wechat\WechatPay;
- use Overtrue\Socialite\SocialiteManager;
- use yii\helpers\Json;
- use app\models\Admin;
- use app\models\StoreShareMoney;
- class NewMerchantForm extends Model
- {
- public $contact_info;
- public $subject_info;
- public $business_info;
- public $bank_account_info;
- public $apply_id;
- public $mini_apply_info;
- public $type; //'weixin'
- public $code;
- public $pay_fee;
- public $id;
- public $send_price;
- public $take_price;
- public $config;
- public $openPlatform;
- public $order_no;
- public $qrcode_id;
- public $is_balance;
- public $saas_user_id;
- public $store_id;
- public $order;
- public $is_use_yunst;
- public $_from;
- const PAY_FROM_APP = 'app';
- public static $notify_url = 'wechat/notify';
- public function rules()
- {
- return [
- [['order_no', "license_pic", "license_no", "license_name", "legal_personal_name", "contact_info", "subject_info", "business_info", "bank_account_info", "type", "code", "pay_fee", '_from'], 'string'],
- [["apply_id", "id", 'qrcode_id', 'is_balance', 'store_id', 'is_use_yunst'], "integer"],
- [["mini_apply_info"], "array"]
- ];
- }
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->config = [
- 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
- 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
- 'token' => Option::get("platform_token", 0, 'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
- ];
- $this->openPlatform = Factory::openPlatform($this->config);
- }
- /**
- * mini业务
- */
- public function miniProgram($appid = "")
- {
- $store = StoreMini::find()->where(['or', ['id' => $this->id], ['appid' => $appid, 'store_id' => get_store_id()]])->one();
- return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
- }
- //商户逻辑 end
- //TODO 当面付逻辑 start
- public function facePayConfig()
- {
- try {
- $qrcode_id = $this->qrcode_id;
- $store_id = $this->store_id ?? 0;
- if (!empty($qrcode_id) && ((int)get_store_id() === -1)) {
- $qrcode = AggregateQrcode::findOne($qrcode_id);
- if ($qrcode) {
- $store_id = $qrcode->store_id;
- }
- if (empty($store_id)) {
- return [
- 'code' => 1,
- 'msg' => "获取商城数据失败",
- ];
- }
- }
- $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value'];
- // HKG 新增判断商城打烊状态 开始
- if($store_id > 0 && Option::get(OptionSetting::MALL_STATUS, $store_id, 'store', 0)['value'] == 1)
- return [
- 'code' => 1,
- 'msg' => '商城已打烊~'
- ];
- // HKG 新增判断商城打烊状态 结束
- $info = Store::find()->where(['id' => $store_id])->select("id, name, logo, league_price")->asArray()->one();
- if ((int)get_saas_user_id() !== 0) {
- // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例
- $send_model= Option::findOne(['name' => 'goods_send_profit','store_id' => $store_id]);
- $take_model= Option::findOne(['name' => 'goods_take_price','store_id' => $store_id]);
- // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => $store_id]);
- // if($this_store_send_profit_model->value > 0){
- // $basic_send_profit = $this_store_send_profit_model->value;
- // }else{
- // $basic_send_profit = $send_profit_model->value;
- // }
- $new_send_profit = floatval($send_model->value * 0.01);
- $new_take_profit = floatval($take_model->value * 0.01);
- $saas_user = SaasUser::findOne(get_saas_user_id());
- // $store_league_model = Store::findOne($store_id);
- $show_saas_alliance_coupon = true;
- if (\Yii::$app->prod_is_dandianpu()) {
- $show_saas_alliance_coupon = false;
- }
- }
- $face_pay_path = Option::get('face_pay_path', 0, 'saas', '')['value'];
- $face_pay_path = json_decode($face_pay_path, true) ?: null;
- $isShowYunst = $this->isUsePayType($store_id, 1);
- return [
- 'code' => 0,
- 'msg' => "商户信息获取成功",
- 'data' => [
- 'send_profit_model' => 100,
- 'send_model' =>$send_model->value ?? 0,
- 'take_model' =>$take_model->value ?? 0,
- 'user_price' => $saas_user->league_price ?? 0,
- 'store_price' => $info['league_price'] ?? 0,
- 'new_send_profit' => $new_send_profit ?? 0,
- 'new_take_profit' => $new_take_profit ?? 0,
- 'take_price' => $take_model->value ?? 0,
- 'send_profit' => $send_model->value ?? 0,
- 'store_info'=>$info,
- 'mini_info' => null,
- 'user' => User::findOne(['store_id' => $store_id, 'binding' => $saas_user->mobile ?? '0']),
- 'show_saas_alliance_coupon' => $show_saas_alliance_coupon ?? false,
- 'reLogin' => (int)(intval(get_saas_user_id()) === 0),
- 'face_pay_path' => $face_pay_path,
- 'isShowYunst' => $isShowYunst
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * @param $store_id
- * @param int $type 0微信支付 1通联支付 1支付宝支付
- * @return bool
- */
- private function isUsePayType($store_id, $type = 0) {
- $isShowPayType = false;
- if (is_wechat_platform()) {
- $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']);
- $pay_type_list = Json::decode($pay_type_list_json['value']);
- foreach ($pay_type_list as $pay_type_index => $pay_type_item) {
- if (intval($type) === 1) {
- if ($pay_type_index === 'yunst_wechat_pay') {
- $isShowPayType = boolval(Store::findOne($store_id)->cusid);
- if (intval($pay_type_item['value']) === 0 && $isShowPayType) {
- $isShowPayType = false;
- }
- }
- } elseif (intval($type) === 2) {
- if ($pay_type_index === 'alipay') {
- if (intval($pay_type_item['value']) === 1) {
- $isShowPayType = true;
- }
- }
- } else {
- if ($pay_type_index === 'wechat') {
- if (intval($pay_type_item['value']) === 1) {
- $isShowPayType = true;
- }
- }
- }
- }
- }
- return $isShowPayType;
- }
- public function submitOrder($store_id = 0, $pay_type = 1)
- {
- $pay_fee = $this->pay_fee;
- $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value'];
- $is_combine_pay = 0;
- $combine_money = 0;
- $user = User::findOne(get_user_id());
- //$recharge_id = $this->recharge_id;
- $is_balance = $this->is_balance;
- //使用联盟券
- //计算联盟券抵扣的钱 和赠送的联盟券
- $send_model = Option::findOne(['name' => 'goods_send_profit', 'store_id' => get_store_id()]);
- $take_model = Option::findOne(['name' => 'goods_take_price', 'store_id' => get_store_id()]);
- $new_send_profit = bcmul($send_model->value, 0.01, 2);
- $new_take_profit = bcmul($take_model->value, 0.01, 2);
- $take_price = 0;
- $send_price = 0;
- //计算联盟券赠送的金额
- $store = Store::findOne(get_store_id());
- if ($store && $store->league_price) {
- // $pay_fee = bcmul($pay_fee, $new_send_profit, 2);
- $send_price = bcmul($pay_fee, $new_send_profit, 2);
- if ($send_price > $store->league_price) {
- $send_price = 0;
- }
- }
- //计算联盟券抵扣的金额
- $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- if ($saas_user) {
- $take_price = bcmul($pay_fee, $new_take_profit, 2);
- if ($take_price > $saas_user->league_price) {
- $take_price = $saas_user->league_price;
- }
- $pay_fee = bcsub($pay_fee, $take_price, 2);
- }
- //使用余额抵扣
- $is_finish = 0;
- if ($combine_pay && $is_balance) {
- $is_combine_pay = 1;
- $money = $user->money;
- if (($money - $pay_fee) >= 0) {
- $pay_fee = 0;
- $combine_money = $this->pay_fee;
- } else {
- $pay_fee = number_format(($pay_fee - $money), 2, '.', '');
- $combine_money = $money;
- $is_finish = 1;
- }
- }
- $saas_user_id = $this->saas_user_id;
- $store_id = ($store_id ?: get_store_id());
- $business_model = Store::findOne($store_id)->business_model;
- $order = new Order();
- $order->store_id = $store_id;
- $order->user_id = $user->id ?: 0;
- $order->saas_id = ($saas_user_id ?: get_saas_user_id());
- $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY);
- $order->pay_price = $pay_fee ?: 0;
- $order->send_price = $send_price;
- $order->take_price = $take_price;
- // 是否是平台小程序订单
- $order->created_at = time();
- $order->first_price = 0;
- $order->second_price = 0;
- $order->third_price = 0;
- $order->content = "";
- $order->is_offline = 1;
- $order->is_platform = 1;
- $order->version = cyy_version();
- $order->mch_id = 0;
- $order->discount = 10; // 当面付订单不享受会员折扣
- $order->remark = '';
- $order->pay_type = $pay_type;
- $order->is_pay = ($pay_fee == 0 ? 1 : 0);
- $order->total_price = $this->pay_fee ?: 0;
- $order->express_price = 0;
- $order->is_combine_pay = $is_combine_pay;
- $order->combine_money = $combine_money;
- if ($this->_from === self::PAY_FROM_APP) {
- $order->order_origin = 2;
- }
- if (!Store::hasIncoming($order->store_id)) {
- $order->is_use_platform_mch = 1;
- }
- $res = $order->save();
- $this->order = $order;
- if ($res) {
- if ($pay_fee <= 0) {
- $user->money -= $combine_money;
- }
- if (!$user->save()) {
- $res = [
- 'code' => 1,
- 'msg' => "创建订单失败"
- ];
- } else {
- $res = [
- 'code' => 0,
- 'data' => [
- 'order_id'=>$order->id,
- 'add_time'=>time(),
- 'order_no'=>$order->order_no,
- 'pay_fee' => $pay_fee,
- 'is_recharge' => 0,
- 'is_finish' => $is_finish
- ]
- ];
- }
- } else {
- $res = [
- 'code' => 1,
- 'msg' => "创建订单失败"
- ];
- }
- return $res;
- }
- public function facePay()
- {
- $qrcode_id = $this->qrcode_id;
- $store_id = $this->store_id ?? 0;
- if (!empty($qrcode_id) && ((int)get_store_id() === -1)) {
- $qrcode = AggregateQrcode::findOne($qrcode_id);
- if ($qrcode) {
- $store_id = $qrcode->store_id;
- }
- if (empty($store_id)) {
- return [
- 'code' => 1,
- 'msg' => "获取商城数据失败",
- ];
- }
- }
- if ($this->type == "weixin") {
- if ($this->is_use_yunst) {
- $isShowYunst = $this->isUsePayType($store_id, 1);
- if (!$isShowYunst) {
- return [
- 'code' => 1,
- 'msg' => "商城未配置或未开启通联支付",
- ];
- }
- $res = $this->submitOrder($store_id, \app\models\Order::PAY_TYPE_YUNST_WECHAT_PAY);
- if ($res['code'] === 0) {
- $data = $res['data'];
- $data['pay_fee'] = round($data['pay_fee'],2);
- if ($data['pay_fee'] == 0) {
- if (get_user_id() > 0) {
- $this->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']);
- }
- $this->order_no = $data['order_no'];
- return [
- 'code' => 0,
- 'data' => [
- 'is_pay' => 1,
- 'order_no'=>$data['order_no'],
- ]
- ];
- }
- } else {
- return $res;
- }
- $order_no = $data['order_no'];
- //如果是商户进件
- $store = Store::findOne(['id' => $store_id ?: get_store_id()]);
- // print_r($store);die;
- if ($store->cusid) {
- // print_r($store->cusid);die;
- $OrderYunst = new OrderYunst();
- $result2 = $OrderYunst->payYunst($store_id, $order_no, $store->cusid, $data['pay_fee'] * 100);
- if ($result2['subCode']== 'SUCCESS'){
- if ($result2['data']['trxstatus'] == 3045){
- return [
- 'code' => 1,
- 'msg' => $result2['data']['errmsg']
- ];
- }
- $result2 = json_decode($result2['data']['payinfo'],true);
- $result2['order_no'] = $order_no;
- $result2['is_pay'] = 0;
- $result2['is_finish'] = 1;
- return [
- 'code'=>0,
- 'data'=>$result2,
- 'msg'=>'success',
- 'res'=>$result2
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => '支付异常'
- ];
- }
- }
- } else {
- if (is_wechat_platform()) {
- $isShowWechat = $this->isUsePayType($store_id);
- if (!$isShowWechat) {
- return [
- 'code' => 1,
- 'msg' => "商城未开启微信支付",
- ];
- }
- }
- //静默登陆获取用户openId
- $wechatMini = isset(\Yii::$app->controller->wechatMini) ? \Yii::$app->controller->wechatMini : null;
- if (!$wechatMini) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $type = 3;
- if ($this->_from !== self::PAY_FROM_APP) {
- $type = 1;
- $res = $wechatMini->auth->session($this->code);
- if (!isset($res['openid'])) {
- return [
- 'code' => 1,
- 'msg' => 'openId错误'
- ];
- }
- $openid = $res['openid'];
- //根据用户ID 拿到联盟用户ID
- $saas_user = SaasUser::find()->where(['platform_open_id' => $openid])->select('id')->one();
- if ($saas_user) {
- $this->saas_user_id = $saas_user->id;
- }
- }
- $res = $this->submitOrder($store_id);
- if ($res['code'] === 0) {
- $data = $res['data'];
- $data['pay_fee'] = round($data['pay_fee'],2);
- if ($data['pay_fee'] == 0) {
- if (get_user_id() > 0) {
- $this->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']);
- }
- $this->order_no = $data['order_no'];
- $this->payResult();
- return [
- 'code' => 0,
- 'data' => [
- 'is_pay' => 1,
- 'order_no'=>$data['order_no'],
- ]
- ];
- }
- } else {
- return $res;
- }
- }
- //获取商户配置信息
- // $wechat_config = WechatConfig::find()->where(['store_id' => ($store_id ?: get_store_id()), 'type' => $type])->select(['mch_id'])->asArray()->one();
- $wechat_config = WechatConfig::find()->where([
- 'and',
- ['store_id' => ($store_id ?: get_store_id())],
- [
- '>',
- 'mch_id',
- 0
- ]
- ])->select(['mch_id'])->asArray()->one();
- $config = [
- 'body' => '当面付',
- 'out_trade_no' => $data['order_no'],
- 'total_fee' => $data['pay_fee'] * 100,
- 'notify_url' => pay_notify_url(self::$notify_url),
- 'trade_type' => 'JSAPI',
- ];
- $order_no = $data['order_no'];
- if (!Store::hasIncoming($store_id ?: get_store_id())) {
- $res = WechatPay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], '0', false, '0', false, false, $openid ?? '');
- if ($res['res']['return_code'] === 'SUCCESS' && $res['res']['result_code'] === 'SUCCESS' && isset($res['data'])) {
- $res['data'] = array_merge($res['data'], [
- 'order_no'=>$data['order_no'],
- 'is_pay' => 0,
- 'is_finish' => 1
- ]);
- }
- debug_log('支付数据' . json_encode($res));
- return $res;
- }
- if ($this->_from != self::PAY_FROM_APP) {
- $res = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], 0, false, '0');
- if ($res['res']['return_code'] === 'SUCCESS' && $res['res']['result_code'] === 'SUCCESS' && isset($res['data'])) {
- $res['data'] = array_merge($res['data'], [
- 'order_no'=>$data['order_no'],
- 'is_pay' => 0,
- 'is_finish' => 1
- ]);
- }
- debug_log('支付数据' . json_encode($res));
- return $res;
- }
- $wechatPay = \Yii::$app->controller->wechatPay ?? null;
- if (!$wechatPay) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- // 判断是否走为独立商城
- $mch_id = $wechat_config['mch_id'];
- $data = [
- 'openid' => (get_saas_user()->platform_open_id ?: $openid),
- 'sub_mch_id' => $mch_id
- ];
- //$result = $wechatPay->order->unify($config);
- $config = array_merge($config, $data);
- if (is_profit_sharing()) {
- $config['profit_sharing'] = 'Y';
- }
- if ($this->_from === self::PAY_FROM_APP) {
- unset($config['openid']);
- $config['trade_type'] = 'APP';
- }
- $result = $wechatPay->order->unify($config);
- if ($result['return_code'] == "FAIL" || $result['result_code'] == "FAIL") {
- return [
- 'code' => 1,
- 'data' => $data['pay_fee'] * 100,
- 'data2' => $data['pay_fee'],
- 'msg' => $result['err_code_des'] ?? $result['return_msg'],
- ];
- }
- if ($this->_from === self::PAY_FROM_APP) {
- $config = $wechatPay->jssdk->appConfig($result['prepay_id']);
- } else {
- $config = $wechatPay->jssdk->bridgeConfig($result['prepay_id'], false);
- }
- $data = [
- 'order_no' => $order_no
- ];
- if (!empty($result['return_code'])) {
- return [
- 'code' => 0,
- 'res' => $result,
- 'data' => array_merge($config, $data)
- ];
- }
- return [
- 'code' => 1,
- 'msg' => "参数错误"
- ];
- } elseif ($this->type == "alipay") {
- if (is_alipay_platform()) {
- $isShowAlipay = $this->isUsePayType($store_id, 2);
- if (!$isShowAlipay) {
- return [
- 'code' => 1,
- 'msg' => "商城未开启支付宝支付",
- ];
- }
- }
- $keys = [
- 'alipay_platform'
- ];
- $data = Option::get($keys, 0, 'saas');
- if (isset($data[0]['value'])) {
- $alipay_config = json_decode($data[0]['value'], true);
- } else {
- return false;
- }
- $config = [
- 'alipay' => [
- 'client_id' => $alipay_config['app_id'],
- 'rsa_private_key' => $alipay_config['app_private_key'],
- ]
- ];
- if ($this->_from !== self::PAY_FROM_APP) {
- //获取支付宝用户信息
- $socialite = new SocialiteManager($config);
- $res_code = $socialite->create('alipay')->tokenFromCode($this->code);
- $open_user_id = $res_code['user_id'];
- if (!$open_user_id) {
- return [
- 'code' => 0,
- 'msg' => '参数获取错误'
- ];
- }
- //根据用户ID 拿到联盟用户ID
- $saas_user = SaasUser::find()->where(['ali_user_id' => $open_user_id])->select('id')->one();
- if ($saas_user) {
- $this->saas_user_id = $saas_user->id;
- }
- }
- $OrderRes = $this->submitOrder($store_id, 4);
- if ($OrderRes['code'] == 1) {
- return $OrderRes;
- }
- if ($OrderRes['data']['pay_fee'] == 0) {
- if (get_user_id() > 0) {
- $this->saasGiveIntegral($this->pay_fee, get_user_id(), $OrderRes['data']['order_no']);
- }
- $this->order_no = $OrderRes['data']['order_no'];
- $this->payResult();
- return [
- 'code' => 0,
- 'data' => [
- 'is_pay' => 1,
- 'order_no'=>$OrderRes['data']['order_no'],
- ]
- ];
- }
- $order = Order::findOne($OrderRes['data']['order_id']);
- $order->pay_price = $OrderRes['data']['pay_fee'];
- if ($this->_from === self::PAY_FROM_APP) {
- $result = AlipayUnion::app($order, '当面付', null, 0);
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result['data']
- ];
- } else {
- $user = (object)['ali_user_id' => $open_user_id ?? ''];
- $result = AlipayUnion::mini($order, '当面付', $user, '', '', false, 0, 0);
- $data = [
- 'trade_no' => $result['data']['trade_no'],
- 'order_no' => $order->order_no,
- 'is_pay' => 0,
- 'is_finish' => 1
- ];
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $data
- ];
- }
- }
- //返回前端展示支付结果
- public function payResult()
- {
- try {
- $order_no = $this->order_no;
- $order = Order::find()->alias('o')->where(['o.order_no'=>$order_no])
- ->leftJoin(['s'=>Store::tableName()],'o.store_id = s.id')
- ->select("o.id,o.is_pay,o.pay_type,o.pay_time,o.order_no,o.store_id,s.name,s.logo,o.pay_price,
- o.is_combine_pay, o.combine_money, o.send_price, o.take_price")->asArray()->one();
- if(empty($order)){
- throw new \Exception("未查找到数据");
- }
- $goods_list = Goods::find()->where(['store_id' => $order['store_id'], 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0, 'md_food_id' => 0, 'is_recommend' => 1])
- ->andWhere(['!=', 'product_type',Goods::GOODS_TYPE_INTEGRAL])
- ->orderBy('sort desc')->asArray()->all();
- foreach ($goods_list as &$item) {
- $item['virtual_sales'] += OrderDetail::find()->where(['goods_id' => $item['id']])->select('id')->count() ?? 0;
- }
- $data['list'] = $goods_list;
- $data = array_merge($order, $data);
- if (!empty($order['is_pay']) && $order['is_pay'] == 1) {
- queue_push(new NoAfterSalesOrderJob(['order_id' => $order['id'], 'store_id' => $order['store_id'], 'is_scan' => 1]), 30);
- $this->order_event();
- return [
- 'code' => 0,
- 'msg' => "支付成功",
- 'data' => $data
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => "订单尚未支付",
- 'data' => $data
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //TODO id 790
- public function order_event()
- {
- $order = Order::findOne(['order_no' => $this->order_no]);
- $face_first_profit = Option::findOne(['name' => 'face_first_profit','store_id' => $order['store_id']]);
- $face_second_profit = Option::findOne(['name' => 'face_second_profit','store_id' =>$order['store_id']]);
- $face_third_profit = Option::findOne(['name' => 'face_third_profit','store_id' => $order['store_id']]);
- $order_all_price = $order->total_price + $order->take_price;
- $order_first_price = $face_first_profit->value * $order_all_price / 100;
- $order_second_price = $face_second_profit->value * $order_all_price / 100;
- $order_third_price = $face_third_profit->value * $order_all_price ;
-
- //奖金池补充奖金
- \app\utils\Share\BonusPool::poolPush($order, 6);
- //处理联盟券
- $order = Order::findOne($order->id);
- $store = Store::findOne($order->store_id);
- // $user = User::findOne($order->user_id);
- //已经发放过不再处理
- $saaSLeaguePriceLog = SaaSLeaguePriceLog::findOne(['order_id' => $order->id]);
- if ($saaSLeaguePriceLog) {
- return;
- }
- //给用户发放联盟券 判断商城联盟券不足就不赠送
- if ($order->send_price > $store->league_price) {
- $send_price = 0;
- } else {
- $send_price = $order->send_price;
- }
- $saas_user = SaasUser::findOne($order->saas_id);
- if ($saas_user) {
- if ($order->take_price > 0) {
- $before = $saas_user->league_price;
- $saas_user->updateCounters(['league_price' => -floatval($order->take_price)]);
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $order->store_id,
- $saas_user->id,
- $order->take_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
- \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_USER,
- $order->id
- );
- }
- if ($send_price > 0) {
- $before = $saas_user->league_price;
- $saas_user->updateCounters(['league_price' => floatval($send_price)]);
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $order->store_id,
- $saas_user->id,
- $send_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
- \app\models\SaaSLeaguePriceLog::SEND_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_USER,
- $order->id
- );
- }
- }
- $store = Store::findOne($order['store_id']);
- if ($store) {
- if ($send_price > 0) {
- //给商家扣减 (用户赠送的联盟券钱从商家从扣减)
- $before = $store->league_price;
- $store->updateCounters(['league_price' => -floatval($send_price)]);
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $order->store_id,
- $saas_user->id,
- $send_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
- \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_STORE,
- $order->id
- );
- }
- if ($order->take_price > 0) {
- //给商家增加 (用户抵扣钱从赠送给商家)
- $before = $store->league_price;
- $store->updateCounters(['league_price' => floatval($order->take_price)]);
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $order->store_id,
- $saas_user->id,
- $order->take_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
- \app\models\SaaSLeaguePriceLog::SEND_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_STORE,
- $order->id
- );
- }
- }
- }
- //
- //联盟赠送积分逻辑
- public function saasGiveIntegral($pay_fee = 0, $user_id = 0, $order_no = '', $type = 0) {
- try {
- $user = User::findOne($user_id);
- if ($user) {
- $store_id = $user->store_id ?? get_store_id();
- $store = Store::findOne($store_id);
- if (!$type) {
- $type_text = "当面付";
- $integral_profit = Option::get('face_pay_integral_profit', $store_id, 'saas', 0)['value'];
- } else {
- $type_text = "订单";
- $integral_profit = Option::get('online_pay_integral_profit', $store_id, 'saas', 0)['value'];
- }
- $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- if ($saas_user) {
- $old_integral = $saas_user->integral;
- $integral = intval($pay_fee * ($integral_profit / 100));
- if ($integral > 0) {
- $saas_user->integral += $integral;
- $saas_user->total_integral += $integral;
- if (!$saas_user->save()) {
- throw new \Exception($saas_user->errors);
- }
- $account_log = new AccountLog();
- $account_log->store_id = 0;
- $account_log->saas_id = $saas_user->id;
- $account_log->user_id = 0;
- $account_log->type = AccountLog::TYPE_INTEGRAL;
- $account_log->log_type = AccountLog::LOG_TYPE_INCOME;
- $account_log->amount = $integral;
- $account_log->desc = "商城{$type_text}赠送积分, 商城:{$store->name}, 订单:{$order_no}";
- $account_log->before = $old_integral;
- $account_log->after = $saas_user->integral;
- $account_log->operator = 'system';
- $account_log->operator_id = 0;
- $account_log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
- $account_log->created_at = time();
- $account_log->order_id = 0;
- $account_log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
- if (!$account_log->save()) {
- throw new \Exception($saas_user->errors);
- }
- return [
- 'code' => 0,
- 'msg' => '赠送成功'
- ];
- }
- throw new \Exception('未设置比例');
- }
- }
- throw new \Exception('查找用户失败');
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|