NewMerchantForm.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\constants\OptionSetting;
  9. use app\jobs\order\NoAfterSalesOrderJob;
  10. use app\models\AccountLog;
  11. use app\models\AggregateSaasQrcode as AggregateQrcode;
  12. use app\models\GiveIntegralRules;
  13. use app\models\OrderDetail;
  14. use app\models\RulesFaceStore;
  15. use app\models\RulesGiveIntegralStore;
  16. use app\models\SaaSLeaguePriceLog;
  17. use app\models\SaasUser;
  18. use app\models\User;
  19. use app\models\UserShareMoney;
  20. use app\utils\Alipay\alipaySdk\aop\request\AlipaySystemOauthTokenRequest;
  21. use app\models\Goods;
  22. use app\models\MerchantInfo;
  23. use app\models\Option;
  24. use app\models\SaasCategory;
  25. use app\models\Salesman;
  26. use app\models\Store;
  27. use app\models\StoreAliMini;
  28. use app\models\StoreAudit;
  29. use app\models\StoreMini;
  30. use app\models\WechatConfig;
  31. use app\modules\admin\models\AlipayThirdForm;
  32. use app\modules\admin\models\WechatThirdForm;
  33. use app\modules\client\models\v1\DiyForm;
  34. use app\utils\Alipay\AlipayUnion;
  35. use app\utils\OrderNo;
  36. use app\utils\Wechat\WechatAlliancePay;
  37. use app\utils\Wechat\WechatBusinessPay;
  38. use app\utils\Wechat\WechatNewPay;
  39. use app\utils\yunst\src\OrderYunst;
  40. use yii\base\Model;
  41. use EasyWeChat\Factory;
  42. use yii\data\Pagination;
  43. use app\plugins\scanCodePay\models\Order;
  44. use app\utils\Alipay\Alipay;
  45. use app\utils\Alipay\AlipayProfit;
  46. use app\utils\Wechat\Wechat;
  47. use app\utils\Wechat\WechatPay;
  48. use Overtrue\Socialite\SocialiteManager;
  49. use yii\helpers\Json;
  50. use app\models\Admin;
  51. use app\models\StoreShareMoney;
  52. class NewMerchantForm extends Model
  53. {
  54. public $contact_info;
  55. public $subject_info;
  56. public $business_info;
  57. public $bank_account_info;
  58. public $apply_id;
  59. public $mini_apply_info;
  60. public $type; //'weixin'
  61. public $code;
  62. public $pay_fee;
  63. public $id;
  64. public $send_price;
  65. public $take_price;
  66. public $config;
  67. public $openPlatform;
  68. public $order_no;
  69. public $qrcode_id;
  70. public $is_balance;
  71. public $saas_user_id;
  72. public $store_id;
  73. public $order;
  74. public $is_use_yunst;
  75. public $_from;
  76. const PAY_FROM_APP = 'app';
  77. public static $notify_url = 'wechat/notify';
  78. public function rules()
  79. {
  80. return [
  81. [['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'],
  82. [["apply_id", "id", 'qrcode_id', 'is_balance', 'store_id', 'is_use_yunst'], "integer"],
  83. [["mini_apply_info"], "array"]
  84. ];
  85. }
  86. public function __construct($config = [])
  87. {
  88. parent::__construct($config);
  89. $this->config = [
  90. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  91. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  92. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  93. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  94. ];
  95. $this->openPlatform = Factory::openPlatform($this->config);
  96. }
  97. /**
  98. * mini业务
  99. */
  100. public function miniProgram($appid = "")
  101. {
  102. $store = StoreMini::find()->where(['or', ['id' => $this->id], ['appid' => $appid, 'store_id' => get_store_id()]])->one();
  103. return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
  104. }
  105. //商户逻辑 end
  106. //TODO 当面付逻辑 start
  107. public function facePayConfig()
  108. {
  109. try {
  110. $qrcode_id = $this->qrcode_id;
  111. $store_id = $this->store_id ?? 0;
  112. if (!empty($qrcode_id) && ((int)get_store_id() === -1)) {
  113. $qrcode = AggregateQrcode::findOne($qrcode_id);
  114. if ($qrcode) {
  115. $store_id = $qrcode->store_id;
  116. }
  117. if (empty($store_id)) {
  118. return [
  119. 'code' => 1,
  120. 'msg' => "获取商城数据失败",
  121. ];
  122. }
  123. }
  124. $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value'];
  125. // HKG 新增判断商城打烊状态 开始
  126. if($store_id > 0 && Option::get(OptionSetting::MALL_STATUS, $store_id, 'store', 0)['value'] == 1)
  127. return [
  128. 'code' => 1,
  129. 'msg' => '商城已打烊~'
  130. ];
  131. // HKG 新增判断商城打烊状态 结束
  132. $info = Store::find()->where(['id' => $store_id])->select("id, name, logo, league_price")->asArray()->one();
  133. if ((int)get_saas_user_id() !== 0) {
  134. // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例
  135. $send_model= Option::findOne(['name' => 'goods_send_profit','store_id' => $store_id]);
  136. $take_model= Option::findOne(['name' => 'goods_take_price','store_id' => $store_id]);
  137. // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => $store_id]);
  138. // if($this_store_send_profit_model->value > 0){
  139. // $basic_send_profit = $this_store_send_profit_model->value;
  140. // }else{
  141. // $basic_send_profit = $send_profit_model->value;
  142. // }
  143. $new_send_profit = floatval($send_model->value * 0.01);
  144. $new_take_profit = floatval($take_model->value * 0.01);
  145. $saas_user = SaasUser::findOne(get_saas_user_id());
  146. // $store_league_model = Store::findOne($store_id);
  147. $show_saas_alliance_coupon = true;
  148. if (\Yii::$app->prod_is_dandianpu()) {
  149. $show_saas_alliance_coupon = false;
  150. }
  151. }
  152. $face_pay_path = Option::get('face_pay_path', 0, 'saas', '')['value'];
  153. $face_pay_path = json_decode($face_pay_path, true) ?: null;
  154. $isShowYunst = $this->isUsePayType($store_id, 1);
  155. return [
  156. 'code' => 0,
  157. 'msg' => "商户信息获取成功",
  158. 'data' => [
  159. 'send_profit_model' => 100,
  160. 'send_model' =>$send_model->value ?? 0,
  161. 'take_model' =>$take_model->value ?? 0,
  162. 'user_price' => $saas_user->league_price ?? 0,
  163. 'store_price' => $info['league_price'] ?? 0,
  164. 'new_send_profit' => $new_send_profit ?? 0,
  165. 'new_take_profit' => $new_take_profit ?? 0,
  166. 'take_price' => $take_model->value ?? 0,
  167. 'send_profit' => $send_model->value ?? 0,
  168. 'store_info'=>$info,
  169. 'mini_info' => null,
  170. 'user' => User::findOne(['store_id' => $store_id, 'binding' => $saas_user->mobile ?? '0']),
  171. 'show_saas_alliance_coupon' => $show_saas_alliance_coupon ?? false,
  172. 'reLogin' => (int)(intval(get_saas_user_id()) === 0),
  173. 'face_pay_path' => $face_pay_path,
  174. 'isShowYunst' => $isShowYunst
  175. ]
  176. ];
  177. } catch (\Exception $e) {
  178. return [
  179. 'code' => 1,
  180. 'msg' => $e->getMessage()
  181. ];
  182. }
  183. }
  184. /**
  185. * @param $store_id
  186. * @param int $type 0微信支付 1通联支付 1支付宝支付
  187. * @return bool
  188. */
  189. private function isUsePayType($store_id, $type = 0) {
  190. $isShowPayType = false;
  191. if (is_wechat_platform()) {
  192. $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']);
  193. $pay_type_list = Json::decode($pay_type_list_json['value']);
  194. foreach ($pay_type_list as $pay_type_index => $pay_type_item) {
  195. if (intval($type) === 1) {
  196. if ($pay_type_index === 'yunst_wechat_pay') {
  197. $isShowPayType = boolval(Store::findOne($store_id)->cusid);
  198. if (intval($pay_type_item['value']) === 0 && $isShowPayType) {
  199. $isShowPayType = false;
  200. }
  201. }
  202. } elseif (intval($type) === 2) {
  203. if ($pay_type_index === 'alipay') {
  204. if (intval($pay_type_item['value']) === 1) {
  205. $isShowPayType = true;
  206. }
  207. }
  208. } else {
  209. if ($pay_type_index === 'wechat') {
  210. if (intval($pay_type_item['value']) === 1) {
  211. $isShowPayType = true;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. return $isShowPayType;
  218. }
  219. public function submitOrder($store_id = 0, $pay_type = 1)
  220. {
  221. $pay_fee = $this->pay_fee;
  222. $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value'];
  223. $is_combine_pay = 0;
  224. $combine_money = 0;
  225. $user = User::findOne(get_user_id());
  226. //$recharge_id = $this->recharge_id;
  227. $is_balance = $this->is_balance;
  228. //使用联盟券
  229. //计算联盟券抵扣的钱 和赠送的联盟券
  230. $send_model = Option::findOne(['name' => 'goods_send_profit', 'store_id' => get_store_id()]);
  231. $take_model = Option::findOne(['name' => 'goods_take_price', 'store_id' => get_store_id()]);
  232. $new_send_profit = bcmul($send_model->value, 0.01, 2);
  233. $new_take_profit = bcmul($take_model->value, 0.01, 2);
  234. $take_price = 0;
  235. $send_price = 0;
  236. //计算联盟券赠送的金额
  237. $store = Store::findOne(get_store_id());
  238. if ($store && $store->league_price) {
  239. // $pay_fee = bcmul($pay_fee, $new_send_profit, 2);
  240. $send_price = bcmul($pay_fee, $new_send_profit, 2);
  241. if ($send_price > $store->league_price) {
  242. $send_price = 0;
  243. }
  244. }
  245. //计算联盟券抵扣的金额
  246. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  247. if ($saas_user) {
  248. $take_price = bcmul($pay_fee, $new_take_profit, 2);
  249. if ($take_price > $saas_user->league_price) {
  250. $take_price = $saas_user->league_price;
  251. }
  252. $pay_fee = bcsub($pay_fee, $take_price, 2);
  253. }
  254. //使用余额抵扣
  255. $is_finish = 0;
  256. if ($combine_pay && $is_balance) {
  257. $is_combine_pay = 1;
  258. $money = $user->money;
  259. if (($money - $pay_fee) >= 0) {
  260. $pay_fee = 0;
  261. $combine_money = $this->pay_fee;
  262. } else {
  263. $pay_fee = number_format(($pay_fee - $money), 2, '.', '');
  264. $combine_money = $money;
  265. $is_finish = 1;
  266. }
  267. }
  268. $saas_user_id = $this->saas_user_id;
  269. $store_id = ($store_id ?: get_store_id());
  270. $business_model = Store::findOne($store_id)->business_model;
  271. $order = new Order();
  272. $order->store_id = $store_id;
  273. $order->user_id = $user->id ?: 0;
  274. $order->saas_id = ($saas_user_id ?: get_saas_user_id());
  275. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY);
  276. $order->pay_price = $pay_fee ?: 0;
  277. $order->send_price = $send_price;
  278. $order->take_price = $take_price;
  279. // 是否是平台小程序订单
  280. $order->created_at = time();
  281. $order->first_price = 0;
  282. $order->second_price = 0;
  283. $order->third_price = 0;
  284. $order->content = "";
  285. $order->is_offline = 1;
  286. $order->is_platform = 1;
  287. $order->version = cyy_version();
  288. $order->mch_id = 0;
  289. $order->discount = 10; // 当面付订单不享受会员折扣
  290. $order->remark = '';
  291. $order->pay_type = $pay_type;
  292. $order->is_pay = ($pay_fee == 0 ? 1 : 0);
  293. $order->total_price = $this->pay_fee ?: 0;
  294. $order->express_price = 0;
  295. $order->is_combine_pay = $is_combine_pay;
  296. $order->combine_money = $combine_money;
  297. if ($this->_from === self::PAY_FROM_APP) {
  298. $order->order_origin = 2;
  299. }
  300. if (!Store::hasIncoming($order->store_id)) {
  301. $order->is_use_platform_mch = 1;
  302. }
  303. $res = $order->save();
  304. $this->order = $order;
  305. if ($res) {
  306. if ($pay_fee <= 0) {
  307. $user->money -= $combine_money;
  308. }
  309. if (!$user->save()) {
  310. $res = [
  311. 'code' => 1,
  312. 'msg' => "创建订单失败"
  313. ];
  314. } else {
  315. $res = [
  316. 'code' => 0,
  317. 'data' => [
  318. 'order_id'=>$order->id,
  319. 'add_time'=>time(),
  320. 'order_no'=>$order->order_no,
  321. 'pay_fee' => $pay_fee,
  322. 'is_recharge' => 0,
  323. 'is_finish' => $is_finish
  324. ]
  325. ];
  326. }
  327. } else {
  328. $res = [
  329. 'code' => 1,
  330. 'msg' => "创建订单失败"
  331. ];
  332. }
  333. return $res;
  334. }
  335. public function facePay()
  336. {
  337. $qrcode_id = $this->qrcode_id;
  338. $store_id = $this->store_id ?? 0;
  339. if (!empty($qrcode_id) && ((int)get_store_id() === -1)) {
  340. $qrcode = AggregateQrcode::findOne($qrcode_id);
  341. if ($qrcode) {
  342. $store_id = $qrcode->store_id;
  343. }
  344. if (empty($store_id)) {
  345. return [
  346. 'code' => 1,
  347. 'msg' => "获取商城数据失败",
  348. ];
  349. }
  350. }
  351. if ($this->type == "weixin") {
  352. if ($this->is_use_yunst) {
  353. $isShowYunst = $this->isUsePayType($store_id, 1);
  354. if (!$isShowYunst) {
  355. return [
  356. 'code' => 1,
  357. 'msg' => "商城未配置或未开启通联支付",
  358. ];
  359. }
  360. $res = $this->submitOrder($store_id, \app\models\Order::PAY_TYPE_YUNST_WECHAT_PAY);
  361. if ($res['code'] === 0) {
  362. $data = $res['data'];
  363. $data['pay_fee'] = round($data['pay_fee'],2);
  364. if ($data['pay_fee'] == 0) {
  365. if (get_user_id() > 0) {
  366. $this->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']);
  367. }
  368. $this->order_no = $data['order_no'];
  369. return [
  370. 'code' => 0,
  371. 'data' => [
  372. 'is_pay' => 1,
  373. 'order_no'=>$data['order_no'],
  374. ]
  375. ];
  376. }
  377. } else {
  378. return $res;
  379. }
  380. $order_no = $data['order_no'];
  381. //如果是商户进件
  382. $store = Store::findOne(['id' => $store_id ?: get_store_id()]);
  383. // print_r($store);die;
  384. if ($store->cusid) {
  385. // print_r($store->cusid);die;
  386. $OrderYunst = new OrderYunst();
  387. $result2 = $OrderYunst->payYunst($store_id, $order_no, $store->cusid, $data['pay_fee'] * 100);
  388. if ($result2['subCode']== 'SUCCESS'){
  389. if ($result2['data']['trxstatus'] == 3045){
  390. return [
  391. 'code' => 1,
  392. 'msg' => $result2['data']['errmsg']
  393. ];
  394. }
  395. $result2 = json_decode($result2['data']['payinfo'],true);
  396. $result2['order_no'] = $order_no;
  397. $result2['is_pay'] = 0;
  398. $result2['is_finish'] = 1;
  399. return [
  400. 'code'=>0,
  401. 'data'=>$result2,
  402. 'msg'=>'success',
  403. 'res'=>$result2
  404. ];
  405. }else{
  406. return [
  407. 'code' => 1,
  408. 'msg' => '支付异常'
  409. ];
  410. }
  411. }
  412. } else {
  413. if (is_wechat_platform()) {
  414. $isShowWechat = $this->isUsePayType($store_id);
  415. if (!$isShowWechat) {
  416. return [
  417. 'code' => 1,
  418. 'msg' => "商城未开启微信支付",
  419. ];
  420. }
  421. }
  422. //静默登陆获取用户openId
  423. $wechatMini = isset(\Yii::$app->controller->wechatMini) ? \Yii::$app->controller->wechatMini : null;
  424. if (!$wechatMini) {
  425. return [
  426. 'code' => 1,
  427. 'msg' => '登录异常'
  428. ];
  429. }
  430. $type = 3;
  431. if ($this->_from !== self::PAY_FROM_APP) {
  432. $type = 1;
  433. $res = $wechatMini->auth->session($this->code);
  434. if (!isset($res['openid'])) {
  435. return [
  436. 'code' => 1,
  437. 'msg' => 'openId错误'
  438. ];
  439. }
  440. $openid = $res['openid'];
  441. //根据用户ID 拿到联盟用户ID
  442. $saas_user = SaasUser::find()->where(['platform_open_id' => $openid])->select('id')->one();
  443. if ($saas_user) {
  444. $this->saas_user_id = $saas_user->id;
  445. }
  446. }
  447. $res = $this->submitOrder($store_id);
  448. if ($res['code'] === 0) {
  449. $data = $res['data'];
  450. $data['pay_fee'] = round($data['pay_fee'],2);
  451. if ($data['pay_fee'] == 0) {
  452. if (get_user_id() > 0) {
  453. $this->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']);
  454. }
  455. $this->order_no = $data['order_no'];
  456. $this->payResult();
  457. return [
  458. 'code' => 0,
  459. 'data' => [
  460. 'is_pay' => 1,
  461. 'order_no'=>$data['order_no'],
  462. ]
  463. ];
  464. }
  465. } else {
  466. return $res;
  467. }
  468. }
  469. //获取商户配置信息
  470. // $wechat_config = WechatConfig::find()->where(['store_id' => ($store_id ?: get_store_id()), 'type' => $type])->select(['mch_id'])->asArray()->one();
  471. $wechat_config = WechatConfig::find()->where([
  472. 'and',
  473. ['store_id' => ($store_id ?: get_store_id())],
  474. [
  475. '>',
  476. 'mch_id',
  477. 0
  478. ]
  479. ])->select(['mch_id'])->asArray()->one();
  480. $config = [
  481. 'body' => '当面付',
  482. 'out_trade_no' => $data['order_no'],
  483. 'total_fee' => $data['pay_fee'] * 100,
  484. 'notify_url' => pay_notify_url(self::$notify_url),
  485. 'trade_type' => 'JSAPI',
  486. ];
  487. $order_no = $data['order_no'];
  488. if (!Store::hasIncoming($store_id ?: get_store_id())) {
  489. $res = WechatPay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], '0', false, '0', false, false, $openid ?? '');
  490. if ($res['res']['return_code'] === 'SUCCESS' && $res['res']['result_code'] === 'SUCCESS' && isset($res['data'])) {
  491. $res['data'] = array_merge($res['data'], [
  492. 'order_no'=>$data['order_no'],
  493. 'is_pay' => 0,
  494. 'is_finish' => 1
  495. ]);
  496. }
  497. debug_log('支付数据' . json_encode($res));
  498. return $res;
  499. }
  500. if ($this->_from != self::PAY_FROM_APP) {
  501. $res = WechatAlliancePay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], 0, false, '0');
  502. if ($res['res']['return_code'] === 'SUCCESS' && $res['res']['result_code'] === 'SUCCESS' && isset($res['data'])) {
  503. $res['data'] = array_merge($res['data'], [
  504. 'order_no'=>$data['order_no'],
  505. 'is_pay' => 0,
  506. 'is_finish' => 1
  507. ]);
  508. }
  509. debug_log('支付数据' . json_encode($res));
  510. return $res;
  511. }
  512. $wechatPay = \Yii::$app->controller->wechatPay ?? null;
  513. if (!$wechatPay) {
  514. return [
  515. 'code' => 1,
  516. 'msg' => '登录异常'
  517. ];
  518. }
  519. // 判断是否走为独立商城
  520. $mch_id = $wechat_config['mch_id'];
  521. $data = [
  522. 'openid' => (get_saas_user()->platform_open_id ?: $openid),
  523. 'sub_mch_id' => $mch_id
  524. ];
  525. //$result = $wechatPay->order->unify($config);
  526. $config = array_merge($config, $data);
  527. if (is_profit_sharing()) {
  528. $config['profit_sharing'] = 'Y';
  529. }
  530. if ($this->_from === self::PAY_FROM_APP) {
  531. unset($config['openid']);
  532. $config['trade_type'] = 'APP';
  533. }
  534. $result = $wechatPay->order->unify($config);
  535. if ($result['return_code'] == "FAIL" || $result['result_code'] == "FAIL") {
  536. return [
  537. 'code' => 1,
  538. 'data' => $data['pay_fee'] * 100,
  539. 'data2' => $data['pay_fee'],
  540. 'msg' => $result['err_code_des'] ?? $result['return_msg'],
  541. ];
  542. }
  543. if ($this->_from === self::PAY_FROM_APP) {
  544. $config = $wechatPay->jssdk->appConfig($result['prepay_id']);
  545. } else {
  546. $config = $wechatPay->jssdk->bridgeConfig($result['prepay_id'], false);
  547. }
  548. $data = [
  549. 'order_no' => $order_no
  550. ];
  551. if (!empty($result['return_code'])) {
  552. return [
  553. 'code' => 0,
  554. 'res' => $result,
  555. 'data' => array_merge($config, $data)
  556. ];
  557. }
  558. return [
  559. 'code' => 1,
  560. 'msg' => "参数错误"
  561. ];
  562. } elseif ($this->type == "alipay") {
  563. if (is_alipay_platform()) {
  564. $isShowAlipay = $this->isUsePayType($store_id, 2);
  565. if (!$isShowAlipay) {
  566. return [
  567. 'code' => 1,
  568. 'msg' => "商城未开启支付宝支付",
  569. ];
  570. }
  571. }
  572. $keys = [
  573. 'alipay_platform'
  574. ];
  575. $data = Option::get($keys, 0, 'saas');
  576. if (isset($data[0]['value'])) {
  577. $alipay_config = json_decode($data[0]['value'], true);
  578. } else {
  579. return false;
  580. }
  581. $config = [
  582. 'alipay' => [
  583. 'client_id' => $alipay_config['app_id'],
  584. 'rsa_private_key' => $alipay_config['app_private_key'],
  585. ]
  586. ];
  587. if ($this->_from !== self::PAY_FROM_APP) {
  588. //获取支付宝用户信息
  589. $socialite = new SocialiteManager($config);
  590. $res_code = $socialite->create('alipay')->tokenFromCode($this->code);
  591. $open_user_id = $res_code['user_id'];
  592. if (!$open_user_id) {
  593. return [
  594. 'code' => 0,
  595. 'msg' => '参数获取错误'
  596. ];
  597. }
  598. //根据用户ID 拿到联盟用户ID
  599. $saas_user = SaasUser::find()->where(['ali_user_id' => $open_user_id])->select('id')->one();
  600. if ($saas_user) {
  601. $this->saas_user_id = $saas_user->id;
  602. }
  603. }
  604. $OrderRes = $this->submitOrder($store_id, 4);
  605. if ($OrderRes['code'] == 1) {
  606. return $OrderRes;
  607. }
  608. if ($OrderRes['data']['pay_fee'] == 0) {
  609. if (get_user_id() > 0) {
  610. $this->saasGiveIntegral($this->pay_fee, get_user_id(), $OrderRes['data']['order_no']);
  611. }
  612. $this->order_no = $OrderRes['data']['order_no'];
  613. $this->payResult();
  614. return [
  615. 'code' => 0,
  616. 'data' => [
  617. 'is_pay' => 1,
  618. 'order_no'=>$OrderRes['data']['order_no'],
  619. ]
  620. ];
  621. }
  622. $order = Order::findOne($OrderRes['data']['order_id']);
  623. $order->pay_price = $OrderRes['data']['pay_fee'];
  624. if ($this->_from === self::PAY_FROM_APP) {
  625. $result = AlipayUnion::app($order, '当面付', null, 0);
  626. return [
  627. 'code' => 0,
  628. 'msg' => "成功",
  629. 'data' => $result['data']
  630. ];
  631. } else {
  632. $user = (object)['ali_user_id' => $open_user_id ?? ''];
  633. $result = AlipayUnion::mini($order, '当面付', $user, '', '', false, 0, 0);
  634. $data = [
  635. 'trade_no' => $result['data']['trade_no'],
  636. 'order_no' => $order->order_no,
  637. 'is_pay' => 0,
  638. 'is_finish' => 1
  639. ];
  640. }
  641. return [
  642. 'code' => 0,
  643. 'msg' => "成功",
  644. 'data' => $data
  645. ];
  646. }
  647. }
  648. //返回前端展示支付结果
  649. public function payResult()
  650. {
  651. try {
  652. $order_no = $this->order_no;
  653. $order = Order::find()->alias('o')->where(['o.order_no'=>$order_no])
  654. ->leftJoin(['s'=>Store::tableName()],'o.store_id = s.id')
  655. ->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,
  656. o.is_combine_pay, o.combine_money, o.send_price, o.take_price")->asArray()->one();
  657. if(empty($order)){
  658. throw new \Exception("未查找到数据");
  659. }
  660. $goods_list = Goods::find()->where(['store_id' => $order['store_id'], 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0, 'md_food_id' => 0, 'is_recommend' => 1])
  661. ->andWhere(['!=', 'product_type',Goods::GOODS_TYPE_INTEGRAL])
  662. ->orderBy('sort desc')->asArray()->all();
  663. foreach ($goods_list as &$item) {
  664. $item['virtual_sales'] += OrderDetail::find()->where(['goods_id' => $item['id']])->select('id')->count() ?? 0;
  665. }
  666. $data['list'] = $goods_list;
  667. $data = array_merge($order, $data);
  668. if (!empty($order['is_pay']) && $order['is_pay'] == 1) {
  669. queue_push(new NoAfterSalesOrderJob(['order_id' => $order['id'], 'store_id' => $order['store_id'], 'is_scan' => 1]), 30);
  670. $this->order_event();
  671. return [
  672. 'code' => 0,
  673. 'msg' => "支付成功",
  674. 'data' => $data
  675. ];
  676. } else {
  677. return [
  678. 'code' => 0,
  679. 'msg' => "订单尚未支付",
  680. 'data' => $data
  681. ];
  682. }
  683. } catch (\Exception $e) {
  684. return [
  685. 'code' => 1,
  686. 'msg' => $e->getMessage()
  687. ];
  688. }
  689. }
  690. //TODO id 790
  691. public function order_event()
  692. {
  693. $order = Order::findOne(['order_no' => $this->order_no]);
  694. $face_first_profit = Option::findOne(['name' => 'face_first_profit','store_id' => $order['store_id']]);
  695. $face_second_profit = Option::findOne(['name' => 'face_second_profit','store_id' =>$order['store_id']]);
  696. $face_third_profit = Option::findOne(['name' => 'face_third_profit','store_id' => $order['store_id']]);
  697. $order_all_price = $order->total_price + $order->take_price;
  698. $order_first_price = $face_first_profit->value * $order_all_price / 100;
  699. $order_second_price = $face_second_profit->value * $order_all_price / 100;
  700. $order_third_price = $face_third_profit->value * $order_all_price ;
  701. //奖金池补充奖金
  702. \app\utils\Share\BonusPool::poolPush($order, 6);
  703. //处理联盟券
  704. $order = Order::findOne($order->id);
  705. $store = Store::findOne($order->store_id);
  706. // $user = User::findOne($order->user_id);
  707. //已经发放过不再处理
  708. $saaSLeaguePriceLog = SaaSLeaguePriceLog::findOne(['order_id' => $order->id]);
  709. if ($saaSLeaguePriceLog) {
  710. return;
  711. }
  712. //给用户发放联盟券 判断商城联盟券不足就不赠送
  713. if ($order->send_price > $store->league_price) {
  714. $send_price = 0;
  715. } else {
  716. $send_price = $order->send_price;
  717. }
  718. $saas_user = SaasUser::findOne($order->saas_id);
  719. if ($saas_user) {
  720. if ($order->take_price > 0) {
  721. $before = $saas_user->league_price;
  722. $saas_user->updateCounters(['league_price' => -floatval($order->take_price)]);
  723. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  724. $order->store_id,
  725. $saas_user->id,
  726. $order->take_price,
  727. $before,
  728. \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
  729. \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
  730. \app\models\SaaSLeaguePriceLog::ROLE_USER,
  731. $order->id
  732. );
  733. }
  734. if ($send_price > 0) {
  735. $before = $saas_user->league_price;
  736. $saas_user->updateCounters(['league_price' => floatval($send_price)]);
  737. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  738. $order->store_id,
  739. $saas_user->id,
  740. $send_price,
  741. $before,
  742. \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
  743. \app\models\SaaSLeaguePriceLog::SEND_TYPE,
  744. \app\models\SaaSLeaguePriceLog::ROLE_USER,
  745. $order->id
  746. );
  747. }
  748. }
  749. $store = Store::findOne($order['store_id']);
  750. if ($store) {
  751. if ($send_price > 0) {
  752. //给商家扣减 (用户赠送的联盟券钱从商家从扣减)
  753. $before = $store->league_price;
  754. $store->updateCounters(['league_price' => -floatval($send_price)]);
  755. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  756. $order->store_id,
  757. $saas_user->id,
  758. $send_price,
  759. $before,
  760. \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
  761. \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
  762. \app\models\SaaSLeaguePriceLog::ROLE_STORE,
  763. $order->id
  764. );
  765. }
  766. if ($order->take_price > 0) {
  767. //给商家增加 (用户抵扣钱从赠送给商家)
  768. $before = $store->league_price;
  769. $store->updateCounters(['league_price' => floatval($order->take_price)]);
  770. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  771. $order->store_id,
  772. $saas_user->id,
  773. $order->take_price,
  774. $before,
  775. \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER,
  776. \app\models\SaaSLeaguePriceLog::SEND_TYPE,
  777. \app\models\SaaSLeaguePriceLog::ROLE_STORE,
  778. $order->id
  779. );
  780. }
  781. }
  782. }
  783. //
  784. //联盟赠送积分逻辑
  785. public function saasGiveIntegral($pay_fee = 0, $user_id = 0, $order_no = '', $type = 0) {
  786. try {
  787. $user = User::findOne($user_id);
  788. if ($user) {
  789. $store_id = $user->store_id ?? get_store_id();
  790. $store = Store::findOne($store_id);
  791. if (!$type) {
  792. $type_text = "当面付";
  793. $integral_profit = Option::get('face_pay_integral_profit', $store_id, 'saas', 0)['value'];
  794. } else {
  795. $type_text = "订单";
  796. $integral_profit = Option::get('online_pay_integral_profit', $store_id, 'saas', 0)['value'];
  797. }
  798. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  799. if ($saas_user) {
  800. $old_integral = $saas_user->integral;
  801. $integral = intval($pay_fee * ($integral_profit / 100));
  802. if ($integral > 0) {
  803. $saas_user->integral += $integral;
  804. $saas_user->total_integral += $integral;
  805. if (!$saas_user->save()) {
  806. throw new \Exception($saas_user->errors);
  807. }
  808. $account_log = new AccountLog();
  809. $account_log->store_id = 0;
  810. $account_log->saas_id = $saas_user->id;
  811. $account_log->user_id = 0;
  812. $account_log->type = AccountLog::TYPE_INTEGRAL;
  813. $account_log->log_type = AccountLog::LOG_TYPE_INCOME;
  814. $account_log->amount = $integral;
  815. $account_log->desc = "商城{$type_text}赠送积分, 商城:{$store->name}, 订单:{$order_no}";
  816. $account_log->before = $old_integral;
  817. $account_log->after = $saas_user->integral;
  818. $account_log->operator = 'system';
  819. $account_log->operator_id = 0;
  820. $account_log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  821. $account_log->created_at = time();
  822. $account_log->order_id = 0;
  823. $account_log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  824. if (!$account_log->save()) {
  825. throw new \Exception($saas_user->errors);
  826. }
  827. return [
  828. 'code' => 0,
  829. 'msg' => '赠送成功'
  830. ];
  831. }
  832. throw new \Exception('未设置比例');
  833. }
  834. }
  835. throw new \Exception('查找用户失败');
  836. } catch (\Exception $e) {
  837. return [
  838. 'code' => 1,
  839. 'msg' => $e->getMessage()
  840. ];
  841. }
  842. }
  843. }