OrderController.php 5.6 KB

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