暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiMerchantLogic.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2020-05-22
  12. */
  13. namespace app\user\logic;
  14. use think\Db;
  15. use think\Model;
  16. use think\Config;
  17. use think\Request;
  18. /**
  19. * 支付API逻辑处理
  20. * @package user\Logic
  21. */
  22. load_trait('controller/Jump');
  23. class MultiMerchantLogic extends Model
  24. {
  25. use \traits\controller\Jump;
  26. private $home_lang = 'cn';
  27. /**
  28. * 初始化操作
  29. */
  30. public function initialize() {
  31. parent::initialize();
  32. // 多语言
  33. $this->home_lang = get_home_lang();
  34. $this->users_db = Db::name('users');
  35. $this->shop_cart_db = Db::name('shop_cart');
  36. $this->shop_order_db = Db::name('shop_order');
  37. $this->shop_address_db = Db::name('shop_address');
  38. $this->pay_api_config_db = Db::name('pay_api_config');
  39. $this->shop_order_details_db = Db::name('shop_order_details');
  40. $this->shipping_template_db = Db::name('shop_shipping_template');
  41. // 会员信息
  42. $this->users = GetUsersLatestData();
  43. $this->users_id = !empty($this->users) ? $this->users['users_id'] : 0;
  44. // 终端判断信息
  45. $this->isMobile = isMobile();
  46. $this->isWeixin = isWeixin();
  47. $this->isWeixinApplets = isWeixinApplets();
  48. }
  49. // 多商家订单处理
  50. public function multiMerchantOrderHandle($list = [], $post = [])
  51. {
  52. // 请选择支付方式
  53. if (empty($post['payment_type'])) $this->error('请选择支付方式');
  54. // 对商品数据以商家进行商家分组
  55. $groupList = group_same_key($list, 'merchant_id');
  56. // 对商品数据以商家为分组进行拆分单操作
  57. $resultOrder = $this->merchantSplitOrderHandle($groupList, $post);
  58. // 查询收货地址及运费处理合并到订单后返回
  59. $merchantOrder = $resultOrder['merchantOrder'];
  60. if (!empty($resultOrder['needQuery'])) $merchantOrder = $this->merchantOrderAddrInfoHandle($merchantOrder, $post);
  61. // 数据验证
  62. $rule = ['payment_method' => 'require|token'];
  63. $message = ['payment_method.require' => '不可为空'];
  64. $validate = new \think\Validate($rule, $message);
  65. if (!$validate->check($post)) $this->error('请勿重复提交订单');
  66. // 删除数据表不存在的字段
  67. foreach ($merchantOrder as $key => $value) {
  68. if (isset($merchantOrder[$key]['free_shipping'])) unset($merchantOrder[$key]['free_shipping']);
  69. }
  70. // 执行批量添加
  71. $insertAll = $this->shop_order_db->insertAll($merchantOrder);
  72. if (!empty($insertAll)) {
  73. // 查询订单信息,补全订单信息
  74. $orderCode = get_arr_column($merchantOrder, 'order_code');
  75. $where = [
  76. 'users_id' => $this->users_id,
  77. 'order_code' => ['IN', $orderCode],
  78. ];
  79. $merchantOrder = $this->shop_order_db->where($where)->getAllWithIndex('merchant_id');
  80. // 对商家订单数据创建订单明细数据
  81. $resultDetails = $this->merchantOrderDetailsHandle($merchantOrder, $list, $this->users_id);
  82. if (empty($resultDetails['code'])) $this->error($resultDetails['msg']);
  83. // 批量添加订单明细信息
  84. $insertAll = !empty($resultDetails['data']['orderDetails']) ? $this->shop_order_details_db->insertAll($resultDetails['data']['orderDetails']) : 0;
  85. if (!empty($insertAll)) {
  86. // 删除商品购物车信息
  87. if (!empty($resultDetails['data']['delCartID'])) $this->delCartProductData($resultDetails['data']['delCartID']);
  88. // 产品库存、销量处理
  89. model('Shop')->ProductStockProcessing($resultDetails['data']['productStock']);
  90. // 添加订单操作记录
  91. AddOrderAction($merchantOrder, $this->users_id);
  92. // 订单支付调用
  93. if (0 === intval($post['payment_method'])) {
  94. if ('yezf_balance' === strval($post['payment_type'])) {
  95. // 使用余额支付订单
  96. $this->useBalancePayOrder($merchantOrder, $post);
  97. } else {
  98. // 微信、支付宝、第三方在线支付
  99. if (empty($this->isMobile) && empty($this->isWeixin)) {
  100. // 电脑PC端下单 WeChatScanCode
  101. $this->computerPCSideSubmitOrder($merchantOrder, $post);
  102. } else if (!empty($this->isMobile) && !empty($this->isWeixin)) {
  103. // 手机端微信下单 WeChatInternal
  104. $this->mobileWeChatSubmitOrder($merchantOrder, $post);
  105. } else if (!empty($this->isMobile) && empty($this->isWeixinApplets)) {
  106. // 手机端浏览器下单 WeChatH5
  107. $this->mobileBrowserSubmitOrder($merchantOrder, $post);
  108. } else if (!empty($this->isMobile) && !empty($this->isWeixinApplets)) {
  109. // 手机微信小程序端下单
  110. dump(4);exit;
  111. }
  112. }
  113. } else {
  114. // 使用货到付款完成订单
  115. $this->useCashOnDeliveryCompletedOrder($merchantOrder, $post);
  116. }
  117. } else {
  118. // 删除刚新增的订单主表数据
  119. $this->shop_order_db->where($where)->delete(true);
  120. $this->error('错误代码:401,订单生成失败,商品数据有误');
  121. }
  122. } else {
  123. $this->error('错误代码:402,订单生成失败,商品数据有误');
  124. }
  125. }
  126. // 对商品数据以商家为分组进行拆分单操作
  127. private function merchantSplitOrderHandle($list = [], $post = [])
  128. {
  129. $needQuery = 0;
  130. $merchantOrder = [];
  131. foreach ($list as $key => $value) {
  132. // 如果为空则跳过
  133. if (empty($value)) continue;
  134. $time = getTime();
  135. $merchantOrder[$key] = [
  136. 'order_code' => date('Ymd') . $time . rand(1000, 9999),
  137. 'users_id' => intval($this->users_id),
  138. 'merchant_id' => intval($key),
  139. 'free_shipping' => 1,
  140. 'order_status' => 0,
  141. 'payment_method' => !empty($post['payment_method']) ? intval($post['payment_method']) : 0,
  142. 'lang' => $this->home_lang,
  143. 'add_time' => $time,
  144. 'pay_details' => '',
  145. 'virtual_delivery' => '',
  146. 'admin_note' => '',
  147. 'user_note' => !empty($post['message']) ? strval($post['message']) : '',
  148. ];
  149. // 判断订单来源
  150. if (empty($this->isMobile) && empty($this->isWeixin)) {
  151. // 电脑PC端
  152. $merchantOrder[$key]['order_terminal'] = 1;
  153. } else if (!empty($this->isMobile) && empty($this->isWeixinApplets)) {
  154. // 手机端
  155. $merchantOrder[$key]['order_terminal'] = 2;
  156. } else if (!empty($this->isMobile) && !empty($this->isWeixinApplets)) {
  157. // 微信小程序
  158. $merchantOrder[$key]['order_terminal'] = 3;
  159. }
  160. // 手机微信端则执行
  161. if (!empty($this->isMobile) && !empty($this->isWeixin)) {
  162. $merchantOrder[$key]['pay_name'] = 'wechat';
  163. $merchantOrder[$key]['wechat_pay_type'] = 'WeChatInternal';
  164. }
  165. // 选择货到付款则执行
  166. if (1 === intval($merchantOrder[$key]['payment_method'])) {
  167. $merchantOrder[$key]['order_status'] = 1;
  168. $merchantOrder[$key]['pay_time'] = $time;
  169. $merchantOrder[$key]['pay_name'] = 'delivery_pay';
  170. $merchantOrder[$key]['wechat_pay_type'] = '';
  171. $merchantOrder[$key]['update_time'] = $time;
  172. }
  173. // 产品数据处理
  174. $PromType = $ContainsVirtual = 1;
  175. $TotalAmount = $TotalNumber = 0;
  176. foreach ($value as $v_key => $v_value) {
  177. // 金额、数量计算
  178. if ($v_value['users_price'] >= 0 && !empty($v_value['product_num'])) {
  179. // 合计金额
  180. $TotalAmount += sprintf("%.2f", $v_value['users_price'] * $v_value['product_num']);
  181. // 合计数量
  182. $TotalNumber += $v_value['product_num'];
  183. // 判断订单类型,目前逻辑:一个订单中,只要存在一个普通产品(实物产品,需要发货物流),则为普通订单,0表示为普通订单
  184. if (empty($v_value['prom_type'])) $PromType = 0;
  185. // 判断是否包含虚拟商品,只要存在一个虚拟商品则表示包含虚拟商品
  186. if (!empty($value['prom_type']) && intval($value['prom_type']) >= 1) $ContainsVirtual = 2;
  187. }
  188. $merchantOrder[$key]['order_amount'] = $TotalAmount;
  189. $merchantOrder[$key]['order_total_amount'] = $TotalAmount;
  190. $merchantOrder[$key]['order_total_num'] = $TotalNumber;
  191. $merchantOrder[$key]['prom_type'] = $PromType;
  192. $merchantOrder[$key]['contains_virtual'] = $ContainsVirtual;
  193. if (empty($v_value['free_shipping'])) $merchantOrder[$key]['free_shipping'] = 0;
  194. }
  195. // 是否需要查看地址信息
  196. if (0 === intval($PromType)) $needQuery = 1;
  197. }
  198. return [
  199. 'needQuery' => $needQuery,
  200. 'merchantOrder' => $merchantOrder
  201. ];
  202. }
  203. // 查询收货地址及运费处理
  204. private function merchantOrderAddrInfoHandle($merchantOrder = [], $post = [])
  205. {
  206. // 如果没有提交地址信息则执行
  207. if (empty($post['addr_id'])) {
  208. // 在微信端并且不在小程序中,跳转至收货地址添加选择页
  209. if (!empty($this->isWeixin) && !empty($this->isWeixinApplets)) {
  210. $this->success('101:选择添加地址方式', url('user/Shop/shop_get_wechat_addr'), ['is_gourl'=>1]);
  211. } else {
  212. $this->error('101:订单生成失败,请添加收货地址', null, ['add_addr' => 1, 'is_mobile' => $this->isMobile]);
  213. }
  214. }
  215. // 查询收货地址
  216. $where = [
  217. 'addr_id' => $post['addr_id'],
  218. 'users_id' => $this->users_id,
  219. ];
  220. $addrInfo = $this->shop_address_db->where($where)->find();
  221. if (empty($addrInfo)) {
  222. // 在微信端并且不在小程序中,跳转至收货地址添加选择页
  223. if (!empty($this->isWeixin) && !empty($this->isWeixinApplets)) {
  224. $this->success('102:选择添加地址方式', url('user/Shop/shop_get_wechat_addr'), ['is_gourl'=>1]);
  225. } else {
  226. $this->error('102:订单生成失败,请添加收货地址', null, ['add_addr' => 1, 'is_mobile' => $this->isMobile]);
  227. }
  228. }
  229. // 查询运费问题
  230. $shippingFee = 0;
  231. $shopOpenShipping = getUsersConfigData('shop.shop_open_shipping');
  232. if (!empty($shopOpenShipping)) {
  233. // 通过省份获取运费模板中的运费价格
  234. $shippingFee = $this->shipping_template_db->where('province_id', $addrInfo['province'])->getField('template_money');
  235. if (0 >= $shippingFee) {
  236. // 省份运费价格为0时,使用统一的运费价格,固定ID为100000
  237. $shippingFee = $this->shipping_template_db->where('province_id', '100000')->getField('template_money');
  238. }
  239. }
  240. // 订单地址信息
  241. $addrData = [
  242. 'consignee' => $addrInfo['consignee'],
  243. 'country' => $addrInfo['country'],
  244. 'province' => $addrInfo['province'],
  245. 'city' => $addrInfo['city'],
  246. 'district' => $addrInfo['district'],
  247. 'address' => $addrInfo['address'],
  248. 'mobile' => $addrInfo['mobile'],
  249. 'shipping_fee' => $shippingFee
  250. ];
  251. // 合并收货地址并计算运费
  252. if (!empty($addrData)) {
  253. $merchantOrder = !empty($merchantOrder) ? $merchantOrder : [];
  254. foreach ($merchantOrder as $key => $value) {
  255. if (isset($value['free_shipping']) && 0 === intval($value['free_shipping']) && !empty($addrData['shipping_fee'])) {
  256. $value['order_amount'] += $addrData['shipping_fee'];
  257. } else {
  258. $addrData['shipping_fee'] = 0;
  259. }
  260. // 删除数据表不存在的字段
  261. unset($value['free_shipping']);
  262. // 整合订单数据
  263. $merchantOrder[$key] = array_merge($value, $addrData);
  264. }
  265. }
  266. return $merchantOrder;
  267. }
  268. // 商家订单详情处理
  269. private function merchantOrderDetailsHandle($merchantOrder = [], $list = [], $users_id = 0)
  270. {
  271. // 订单详情、删除的购物车ID、商品更新库存信息
  272. $orderDetails = $delCartID = $productStock = [];
  273. // 添加到订单明细表
  274. foreach ($list as $key => $value) {
  275. // 旧产品属性处理
  276. $attrValue = model('Shop')->ProductAttrProcessing($value);
  277. // 新产品属性处理
  278. $attrValueNew = model('Shop')->ProductNewAttrProcessing($value);
  279. // 产品规格处理
  280. $specValue = model('Shop')->ProductSpecProcessing($value);
  281. // 商品自定义参数
  282. $customParam = $this->getProductCustomParam($value['aid']);
  283. $data = [
  284. // 产品属性
  285. 'attr_value' => htmlspecialchars($attrValue),
  286. // 产品属性
  287. 'attr_value_new' => htmlspecialchars($attrValueNew),
  288. // 产品规格
  289. 'spec_value' => htmlspecialchars($specValue),
  290. // 产品规格值ID
  291. 'spec_value_id' => $value['spec_value_id'],
  292. // 对应规格值ID的唯一标识ID,数据表主键ID
  293. 'value_id' => $value['value_id'],
  294. // 商品自定义参数
  295. 'custom_param' => $customParam,
  296. // 后续添加
  297. ];
  298. // 订单副表添加数组
  299. $orderDetails[] = [
  300. 'order_id' => intval($merchantOrder[$value['merchant_id']]['order_id']),
  301. 'users_id' => intval($users_id),
  302. 'product_id' => intval($value['aid']),
  303. 'product_name' => strval($value['title']),
  304. 'num' => intval($value['product_num']),
  305. 'data' => serialize($data),
  306. 'product_price' => floatval($value['users_price']),
  307. 'prom_type' => intval($value['prom_type']),
  308. 'litpic' => strval($value['litpic']),
  309. 'add_time' => getTime(),
  310. 'lang' => $this->home_lang
  311. ];
  312. // 处理购物车ID
  313. if (!empty($value['cart_id'])) array_push($delCartID, $value['cart_id']);
  314. // 产品库存处理
  315. $productStock[] = [
  316. 'aid' => $value['aid'],
  317. 'value_id' => $value['value_id'],
  318. 'quantity' => $value['product_num'],
  319. 'spec_value_id' => $value['spec_value_id']
  320. ];
  321. }
  322. $result = [
  323. 'code' => 1,
  324. 'msg' => 'ok',
  325. 'data' => [
  326. 'delCartID' => $delCartID,
  327. 'productStock' => $productStock,
  328. 'orderDetails' => $orderDetails,
  329. ]
  330. ];
  331. return $result;
  332. }
  333. // 获取商品自定义参数
  334. private function getProductCustomParam($aid = 0)
  335. {
  336. $result = '';
  337. if (!empty($aid)) {
  338. $where = [
  339. 'aid' => $aid
  340. ];
  341. $customParam = Db::name('product_custom_param')->field('param_name, param_value')->where($where)->order('sort_order asc')->select();
  342. if (empty($customParam)) return $result;
  343. foreach ($customParam as $value) {
  344. $result .= $value['param_name'] . ':' . $value['param_value'] . '<br/>';
  345. }
  346. }
  347. return $result;
  348. }
  349. // 删除商品购物车信息
  350. private function delCartProductData($delCartID = [])
  351. {
  352. $where = [
  353. 'cart_id' => ['IN', $delCartID]
  354. ];
  355. $this->shop_cart_db->where($where)->delete(true);
  356. }
  357. // 使用余额支付订单
  358. private function useBalancePayOrder($merchantOrder = [], $post = [])
  359. {
  360. // 跳转链接
  361. $url = urldecode(url('user/Shop/shop_centre'));
  362. // 创建统一支付订单(合并订单进行支付)
  363. $unifiedPay = $this->createShopOrderUnifiedPay($merchantOrder, 'balance');
  364. // 判断余额是否足够支付
  365. if (floatval($this->users['users_money']) < floatval($unifiedPay['unified_amount'])) {
  366. $this->error('余额不足,支付失败', null, ['url' => $url]);
  367. } else {
  368. // 进行余额支付
  369. $where = [
  370. 'users_id' => $this->users_id
  371. ];
  372. $update = [
  373. 'users_money' => Db::raw('users_money-' . $unifiedPay['unified_amount']),
  374. 'update_time' => getTime(),
  375. ];
  376. $updateID = Db::name('users')->where($where)->update($update);
  377. if (!empty($updateID)) {
  378. // 添加余额记录
  379. $users = empty($this->users) ? Db::name('users')->find($this->users_id) : $this->users;
  380. UsersMoneyRecording($unifiedPay['unified_number'], $users, $unifiedPay['unified_amount'], '商品购买', 3);
  381. // 统一支付成功处理
  382. $this->unifiedPaySuccessHandle($unifiedPay, 'balance');
  383. } else {
  384. $this->error('错误代码:403,余额支付异常,请在订单列表进行支付', null, ['url' => $url]);
  385. }
  386. }
  387. }
  388. // 电脑PC端下单
  389. private function computerPCSideSubmitOrder($merchantOrder = [], $post = [])
  390. {
  391. // 查询支付方式
  392. $payApiConfig = $this->getSpecifyPayApiConfig($post['payment_type']);
  393. // 创建统一支付订单(合并订单进行支付)
  394. $unifiedPay = $this->createShopOrderUnifiedPay($merchantOrder, $payApiConfig['pay_mark'], 'WeChatScanCode');
  395. // 判断是否生成统一支付订单信息
  396. if (!empty($unifiedPay)) {
  397. // 返回支付所需参数
  398. $payData = [
  399. 'transaction_type' => 99, // 多商家处理标识
  400. 'code' => 'order_status_0',
  401. 'pay_id' => $payApiConfig['pay_id'],
  402. 'pay_mark' => $payApiConfig['pay_mark'],
  403. 'unified_id' => $unifiedPay['unified_id'],
  404. 'unified_number' => $unifiedPay['unified_number'],
  405. ];
  406. $this->success('正在支付中', url('user/Shop/shop_centre'), $payData);
  407. } else {
  408. $this->error('错误代码:404,批量支付异常,请在订单列表进行支付', urldecode(url('user/Shop/shop_centre')));
  409. }
  410. }
  411. // 手机端浏览器下单
  412. private function mobileBrowserSubmitOrder($merchantOrder = [], $post = [])
  413. {
  414. // 查询支付方式
  415. $payApiConfig = $this->getSpecifyPayApiConfig($post['payment_type']);
  416. // 创建统一支付订单(合并订单进行支付)
  417. $unifiedPay = $this->createShopOrderUnifiedPay($merchantOrder, $payApiConfig['pay_mark'], 'WeChatH5');
  418. // 判断是否生成统一支付订单信息
  419. if (!empty($unifiedPay)) {
  420. // 返回支付所需参数
  421. $payData = [
  422. 'transaction_type' => 99, // 多商家处理标识
  423. 'code' => 'order_status_0',
  424. 'pay_id' => $payApiConfig['pay_id'],
  425. 'pay_mark' => $payApiConfig['pay_mark'],
  426. 'unified_id' => $unifiedPay['unified_id'],
  427. 'unified_number' => $unifiedPay['unified_number'],
  428. ];
  429. $this->success('正在支付中', url('user/Shop/shop_centre'), $payData);
  430. } else {
  431. $this->error('错误代码:405,批量支付异常,请在订单列表进行支付', urldecode(url('user/Shop/shop_centre')));
  432. }
  433. }
  434. // 手机端微信下单
  435. private function mobileWeChatSubmitOrder($merchantOrder = [], $post = [])
  436. {
  437. // 查询支付方式
  438. $payApiConfig = $this->getSpecifyPayApiConfig($post['payment_type']);
  439. // 创建统一支付订单(合并订单进行支付)
  440. $unifiedPay = $this->createShopOrderUnifiedPay($merchantOrder, $payApiConfig['pay_mark'], 'WeChatInternal');
  441. // 判断是否生成统一支付订单信息
  442. if (!empty($unifiedPay)) {
  443. // 返回支付所需参数
  444. $payData = [
  445. 'transaction_type' => 99, // 多商家处理标识
  446. 'code' => 'order_status_0',
  447. 'pay_id' => $payApiConfig['pay_id'],
  448. 'pay_mark' => $payApiConfig['pay_mark'],
  449. 'unified_id' => $unifiedPay['unified_id'],
  450. 'unified_number' => $unifiedPay['unified_number'],
  451. ];
  452. $this->success('正在支付中', url('user/Shop/shop_centre'), $payData);
  453. } else {
  454. $this->error('错误代码:406,批量支付异常,请在订单列表进行支付', urldecode(url('user/Shop/shop_centre')));
  455. }
  456. }
  457. // 使用货到付款完成订单
  458. private function useCashOnDeliveryCompletedOrder($merchantOrder = [], $post = [])
  459. {
  460. // 跳转链接
  461. $url = urldecode(url('user/Shop/shop_centre'));
  462. // 添加订单操作记录
  463. $orderID = get_arr_column($merchantOrder, 'order_id');
  464. AddOrderAction($orderID, $this->users_id, 0, 1, 0, 1, '货到付款', '会员选择货到付款,款项由快递代收');
  465. // 邮箱发送
  466. $data['email'] = GetEamilSendData();
  467. // $SmtpConfig = tpCache('smtp');
  468. // $data['email'] = GetEamilSendData($SmtpConfig, $this->users, $merchantOrder, 1, 'delivery_pay');
  469. // 手机发送
  470. $data['mobile'] = GetMobileSendData();
  471. // $SmsConfig = tpCache('sms');
  472. // $data['mobile'] = GetMobileSendData($SmsConfig, $this->users, $merchantOrder, 1, 'delivery_pay');
  473. // 返回结束
  474. $this->success('下单成功,跳转订单列表...', $url, $data);
  475. }
  476. // 创建统一支付订单(合并订单进行支付)
  477. private function createShopOrderUnifiedPay($merchantOrder = [], $payName = '', $wechatPayType = '')
  478. {
  479. // 支付总额处理
  480. $unifiedAmount = 0;
  481. $orderID = [];
  482. foreach ($merchantOrder as $value) {
  483. $unifiedAmount += $value['order_amount'];
  484. array_push($orderID, $value['order_id']);
  485. }
  486. // 添加统一支付订单数据
  487. $time = getTime();
  488. $insert = [
  489. 'unified_number' => date('Ymd') . $time . rand(1000, 9999),
  490. 'unified_amount' => $unifiedAmount,
  491. 'users_id' => $this->users_id,
  492. 'order_ids' => serialize($orderID),
  493. 'pay_status' => 0,
  494. 'pay_time' => '',
  495. 'pay_name' => $payName,
  496. 'wechat_pay_type' => $wechatPayType,
  497. 'add_time' => $time,
  498. 'update_time' => $time
  499. ];
  500. $insertID = Db::name('shop_order_unified_pay')->insertGetId($insert);
  501. if (!empty($insertID)) {
  502. $insert['unified_id'] = $insertID;
  503. return $insert;
  504. } else {
  505. $this->error('错误代码:490,批量支付异常,请在订单列表进行支付', urldecode(url('user/Shop/shop_centre')));
  506. }
  507. }
  508. // 查询支付方式
  509. private function getSpecifyPayApiConfig($paymentType = '')
  510. {
  511. // 内置第三方在线支付
  512. $paymentTypeArr = explode('_', $paymentType);
  513. $payMark = !empty($paymentTypeArr[1]) ? $paymentTypeArr[1] : '';
  514. $payApiRow = Db::name('pay_api_config')->where(['pay_mark' => $payMark, 'lang' => $this->home_lang])->find();
  515. if (empty($payApiRow)) $this->error('错误代码:491,请选择正确的支付方式');
  516. return $payApiRow;
  517. }
  518. // 统一支付成功后续订单处理
  519. public function unifiedPaySuccessHandle($unifiedPay = [], $payName = '', $payDetails = [], $notify = true)
  520. {
  521. // 跳转链接
  522. $url = urldecode(url('user/Shop/shop_centre'));
  523. // 判断是否进行下一步操作
  524. if (empty($unifiedPay)) $this->error('错误代码:492,支付异常,请在订单列表进行支付', null, ['url' => $url]);
  525. // 下单成功
  526. if (!empty($unifiedPay['pay_status']) && 1 === intval($unifiedPay['pay_status'])) $this->success('下单成功,跳转订单列表...', $url);
  527. // 订单未处理,进行处理
  528. if (isset($unifiedPay['pay_status']) && 0 === intval($unifiedPay['pay_status'])) {
  529. // 解析统一订单包含的子订单ID
  530. $unifiedPay['order_ids'] = !empty($unifiedPay['order_ids']) ? unserialize($unifiedPay['order_ids']) : [];
  531. // 更新订单为已支付
  532. $where = [
  533. 'users_id' => $unifiedPay['users_id'],
  534. 'unified_id' => $unifiedPay['unified_id']
  535. ];
  536. $update = [
  537. 'pay_status' => 1,
  538. 'pay_name' => $payName,
  539. 'pay_time' => getTime(),
  540. 'update_time' => getTime(),
  541. ];
  542. if ('balance' === strval($payName)) {
  543. $update['wechat_pay_type'] = '';
  544. } else if ('alipay' === strval($payName)) {
  545. $update['wechat_pay_type'] = '';
  546. }
  547. $updateID = Db::name('shop_order_unified_pay')->where($where)->update($update);
  548. if (!empty($updateID)) {
  549. // 更新订单变量,保存最新数据
  550. $unifiedPay = array_merge($unifiedPay, $update);
  551. // 更新统一支付订单下的所有订单
  552. if (empty($payDetails)) {
  553. $payDetails = [
  554. 'transaction_type' => 2,
  555. 'payment_type' => "余额支付",
  556. 'unified_id' => $unifiedPay['unified_id'],
  557. 'unified_number' => $unifiedPay['unified_number'],
  558. 'unified_amount' => $unifiedPay['unified_amount']
  559. ];
  560. }
  561. $where = [
  562. 'users_id' => $unifiedPay['users_id'],
  563. 'order_id' => ['IN', $unifiedPay['order_ids']]
  564. ];
  565. $update = [
  566. 'order_status' => 1,
  567. 'pay_time' => $unifiedPay['pay_time'],
  568. 'pay_name' => $unifiedPay['pay_name'],
  569. 'wechat_pay_type' => $unifiedPay['wechat_pay_type'],
  570. 'pay_details' => serialize($payDetails),
  571. 'update_time' => getTime(),
  572. ];
  573. $this->shop_order_db->where($where)->update($update);
  574. // 添加订单操作记录
  575. $orderID = [];
  576. foreach ($unifiedPay['order_ids'] as $key => $value) {
  577. $orderID[$key]['order_id'] = $value;
  578. }
  579. $pay_method_arr = config('global.pay_method_arr');
  580. AddOrderAction($orderID, $unifiedPay['users_id'], 0, 1, 0, 1, "支付成功", "使用{$pay_method_arr[$unifiedPay['pay_name']]}完成支付");
  581. // 下单成功
  582. $this->success('下单成功,跳转订单列表...', $url);
  583. } else {
  584. $this->error('错误代码:493,订单处理异常,请联系管理员处理', null, ['url' => $url]);
  585. }
  586. }
  587. }
  588. // 获取多商家地址信息(用于售后服务商家地址显示)
  589. public function getMultiMerchantContact($merchantID = 0)
  590. {
  591. $where = [
  592. 'merchant_id' => $merchantID,
  593. ];
  594. $merchantContact = Db::name('weapp_multi_merchant')->where($where)->getField('merchant_contact');
  595. if (!empty($merchantContact)) {
  596. $merchantContact = unserialize($merchantContact);
  597. $contactProvince = !empty($merchantContact['contactProvince']) ? get_province_name($merchantContact['contactProvince']) : '';
  598. $contactCity = !empty($merchantContact['contactCity']) ? get_city_name($merchantContact['contactCity']) : '';
  599. $contactDistrict = !empty($merchantContact['contactDistrict']) ? get_area_name($merchantContact['contactDistrict']) : '';
  600. $result = [
  601. 'addr_contact_person' => $merchantContact['contactName'],
  602. 'addr_contact_phone' => $merchantContact['contactPhone'],
  603. 'addr_shipping_addr' => $contactProvince . ' ' . $contactCity . ' ' . $contactDistrict . ' ' . $merchantContact['contactAddress'],
  604. ];
  605. return $result;
  606. } else {
  607. $this->error('商家未设置收货地址,请联系商家..');
  608. }
  609. }
  610. // 处理多商家订单金额结算
  611. public function handleMultiMerchantOrderSettle($shopOrder = [])
  612. {
  613. if (!empty($shopOrder['merchant_id']) && !empty($shopOrder['order_amount'])) {
  614. // 将订单金额存入商家可用余额
  615. $where = [
  616. 'is_del' => 0,
  617. 'audit_status' => 2,
  618. 'merchant_status' => 1,
  619. 'merchant_id' => $shopOrder['merchant_id']
  620. ];
  621. Db::name('weapp_multi_merchant')->where($where)->setInc('available_balance', $shopOrder['order_amount']);
  622. }
  623. }
  624. }