OrderController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use bizGhs\admin\classes\AdminClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\merchant\services\ShopService;
  7. use bizGhs\order\classes\OrderClass;
  8. use bizGhs\order\services\OrderService;
  9. use biz\saas\services\RegionService;
  10. use biz\user\services\UserService;
  11. use bizGhs\product\classes\ProductClass;
  12. use common\components\configDict;
  13. use common\components\httpUtil;
  14. use common\services\xhPayToolService;
  15. use Yii;
  16. use common\components\util;
  17. use common\components\stringUtil;
  18. class OrderController extends BaseController
  19. {
  20. public $withoutLogin = ['order-relate', 'fast-pay'];
  21. //订单列表 shish 2021.1.20
  22. public function actionList()
  23. {
  24. $get = Yii::$app->request->get();
  25. $status = $get['status']?? 0;
  26. if (!empty($status)) {
  27. $where['status'] = $status;
  28. }
  29. $where['merchantId'] = $this->merchantId;
  30. if (isset($get['shopId'])) {
  31. $shopId = $get['shopId'] ?? 0;
  32. if (!empty($shopId)) {
  33. $where['shopId'] = $this->shopId;
  34. }
  35. } else {
  36. $where['shopId'] = $this->shopId;
  37. }
  38. $data = OrderClass::getOrderList($where);
  39. util::success($data);
  40. }
  41. //商城下单操作 shish 2021.1.14
  42. public function actionCreateOrder()
  43. {
  44. $post = Yii::$app->request->post();
  45. $adminId = $this->adminId;
  46. $shopId = $this->shopId;
  47. if (!empty($shopId)) {
  48. //供货商端 员工 开单
  49. $post['customId'] = $post['customId'] ?? 0;
  50. $post['adminId'] = $adminId;
  51. $post['userId'] = 0;
  52. } else {
  53. //供货商端 客户 开单
  54. $post['userId'] = $adminId;
  55. $post['adminId'] = 0;
  56. $post['customId'] = CustomClass::addCustom();
  57. }
  58. $post['payWay'] = $this->isWx == true ? 0 : 1;
  59. $post['merchantId'] = $this->merchantId;
  60. $post['shopId'] = 0;
  61. $post['deadline'] = time() + 1800;//订单30分钟后过期
  62. $post['fromType'] = 1;//商城
  63. $post['prePrice'] = 0.01;
  64. $post['actPrice'] = 0.01;
  65. $post['sendSide'] = $post['sendSide'] ?? 0;
  66. $productJson = $post['product'] ?? '';
  67. if (empty($productJson)) {
  68. util::fail('请选择花材');
  69. }
  70. $productList = json_decode($productJson, true);
  71. if (empty($productList)) {
  72. util::fail('请选择花材');
  73. }
  74. $productIds = array_column($productList, 'productId');
  75. ProductClass::valid($productIds, $this->merchantId);
  76. $post['product'] = $productList;
  77. $order = OrderClass::addOrder($post);
  78. $orderSn = $order['orderSn'] ?? '';
  79. $orderId = $order['id'] ?? 0;
  80. util::success([
  81. 'orderId' => $orderId,
  82. 'orderSn' => $orderSn,
  83. 'totalPrice' => 0.01,
  84. 'sendType' => '快递送',
  85. 'sendTime' => '15:00',
  86. 'qrCode' => Yii::$app->params['imgHost'] . '/hhb_small.png',
  87. ]);
  88. }
  89. //延期支付 shish 2021.1.19
  90. public function actionDebt()
  91. {
  92. $get = Yii::$app->request->get();
  93. $orderId = $get['orderId'] ?? 0;
  94. util::complete();
  95. }
  96. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  97. public function actionWxPay()
  98. {
  99. ini_set('date.timezone', 'Asia/Shanghai');
  100. $post = Yii::$app->request->post();
  101. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  102. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  103. $order = OrderClass::getByOrderSn($orderSn);
  104. $orderId = $order['id'];
  105. //支付前验证订单有效性
  106. OrderClass::valid($order, $this->adminId);
  107. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  108. $totalFee = $order['actPrice'];
  109. //小程序使用miniOpenId
  110. if (httpUtil::isMiniProgram()) {
  111. $openId = $this->user['miniOpenId'];
  112. } else {
  113. $openId = $this->user['openId'];
  114. }
  115. $typeList = configDict::getConfig('capitalType');
  116. $capitalType = $typeList['xhOrder']['id'];
  117. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  118. $wxPayType = 0;
  119. if (httpUtil::isMiniProgram()) {
  120. $wxPayType = 1;
  121. }
  122. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  123. //订单30分钟后过期
  124. $now = time();
  125. $expireTime = $now + 1800;
  126. $wx = Yii::getAlias("@vendor/weixin");
  127. require_once($wx . '/lib/WxPay.Api.php');
  128. require_once($wx . '/example/WxPay.JsApiPay.php');
  129. $input = new \WxPayUnifiedOrder();
  130. $input->SetBody($name);
  131. $input->SetOut_trade_no($orderSn);
  132. $input->SetTotal_fee($totalFee * 100);
  133. $input->SetTime_start(date("YmdHis", $now));
  134. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  135. $input->SetTime_expire(date("YmdHis", $expireTime));
  136. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  137. $input->SetTrade_type("JSAPI");
  138. //花卉宝代为申请的微信支付
  139. $merchantExtend = $this->merchantExtend;
  140. if ($merchantExtend['wxPayApply'] == 1) {
  141. $input->SetSub_openid($openId);
  142. } else {
  143. $input->SetOpenid($openId);
  144. }
  145. //小程序使用miniAppId
  146. if (httpUtil::isMiniProgram()) {
  147. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  148. }
  149. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  150. $tools = new \JsApiPay();
  151. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  152. $newParams = json_decode($jsApiParameters, true);
  153. //已请求微信不能修改价格
  154. $updateData = [];
  155. $updateData['modPrice'] = 0;
  156. if (empty($order['deadline'])) {
  157. $updateData['deadline'] = $expireTime;
  158. }
  159. OrderService::updateById($orderId, $updateData);
  160. util::success($newParams);
  161. }
  162. //已经支付
  163. public function actionHasPay()
  164. {
  165. util::complete();
  166. }
  167. }