| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace hd\controllers;
- use biz\ghs\classes\GhsClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use Yii;
- use common\components\util;
- class PurchaseClearController extends BaseController
- {
- public $guestAccess = ['wx-pay'];
- //生成结帐订单 ssh 2021.1.26
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $current = time();
- $ghsId = $post['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- GhsClass::valid($ghs, $this->shopId);
- //查找有没结帐订单还没有过期
- $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
- if (!empty($list)) {
- foreach ($list as $item) {
- $deadline = $item->deadline;
- $deadTime = strtotime($deadline);
- if ($current > $deadTime) {
- $item->status = PurchaseClearClass::STATUS_EXPIRE;
- $item->save();
- } else {
- util::success(['hasUnComplete' => 1], '还有待完成结账单');
- }
- }
- }
- $post['ghsShopId'] = $ghs->shopId ?? 0;
- $post['sjId'] = $this->sjId;
- $post['shopId'] = $this->shopId;
- $post['clearStyle'] = dict::getDict('clearStyle', 'hd2Gys');
- $post['customShopAdminId'] = $this->shopAdminId ?? 0;
- $post['customShopId'] = $this->shopId ?? 0;
- $shopAdmin = $this->shopAdmin;
- $shopAdminName = $shopAdmin['name'] ?? '';
- $post['customShopAdminName'] = $shopAdminName;
- $respond = PurchaseClearClass::addOrder($post, $ghs);
- //前端显示的付款倒计时
- $hasTime = dict::getDict('order_pay_has_time');
- $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
- $remainSecond = $hasTime - $aheadTime;
- $respond['remainSecond'] = $remainSecond;
- util::success($respond);
- }
- public function actionWxPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $couponId = $post['couponId'] ?? 0;
- $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
- $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($order)) {
- util::fail('账单号无效');
- }
- if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
- util::fail('账单已结清了');
- }
- if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
- util::fail('账单已取消了');
- }
- $current = time();
- $id = $order->id;
- $deadline = $order->deadline;
- if ($current > strtotime($deadline)) {
- $order->status = PurchaseClearClass::STATUS_EXPIRE;
- $order->save();
- util::fail('订单已经失效,请重新发起结算');
- }
- $name = '采购单结算';
- $totalFee = $order->actPrice;
- //强制使用小程序的miniOpenId
- $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
- if (empty($openId)) {
- if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
- $openId = $post['currentMiniOpenId'];
- }
- }
- if (empty($openId)) {
- util::fail('没有openId');
- }
- $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
- //小程序支付
- $wxPayType = 1;
- $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
- //订单30分钟后过期
- $expireTime = $current + 1800;
- $wx = Yii::getAlias("@vendor/weixin");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderSn);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $current));
- $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
- $input->SetTime_expire(date("YmdHis", $expireTime));
- $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
- $input->SetTrade_type("JSAPI");
- //花卉宝代为申请的微信支付
- $merchantExtend = WxOpenClass::getWxInfo();
- if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
- $input->SetSub_openid($openId);
- } else {
- $input->SetOpenid($openId);
- }
- //强制使用小程序的miniAppId
- $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
- $newParams = json_decode($jsApiParameters, true);
- $newParams['id'] = $id;
- $newParams['orderSn'] = $orderSn;
- util::success($newParams);
- }
- }
|