| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- namespace hd\controllers;
- use biz\ghs\classes\GhsClass;
- use bizGhs\custom\classes\AccountMoneyClass;
- use bizHd\purchase\classes\OrderCgClearClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- use bizHd\shop\classes\ShopClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use Yii;
- use common\components\util;
- use common\components\lakala\Lakala;
- class PurchaseClearController extends BaseController
- {
- public $guestAccess = ['wx-pay', 'create-order', 'ali-pay'];
- //生成结帐订单 ssh 2021.1.26
- public function actionCreateOrder()
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $post = Yii::$app->request->post();
- $ghsId = $post['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('没有供货商');
- }
- // 花店向供货商结账建单前:挂账并入净 balance
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- //解决重复请求问题
- $staffId = $this->shopAdminId ?? 0;
- $cacheKey = 'hd_clear_order_pay_' . $ghsId . '_' . $staffId;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请稍等5秒再操作');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
- //老的没有清的结账单直接过期掉
- $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
- if (!empty($list)) {
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- foreach ($list as $item) {
- $item->status = PurchaseClearClass::STATUS_EXPIRE;
- $item->save();
- OrderCgClearClass::markExpireByClearId($item->id);
- if (!empty($ghsShop)) {
- $orderSn = $item->orderSn ?? '';
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $orderSn,
- ];
- $response = $laResource->close($closeParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- //$msg = $response['msg'] ?? '';
- //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单失败了\n{$msg} ", '15280215347');
- } else {
- //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单成功", '15280215347');
- }
- }
- }
- }
- $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;
- $getPayType = dict::getDict('getPayType');
- $respond['getPayType'] = $getPayType;
- //如果是虚拟关系,暂时允许花店一键结账
- $live = $ghs->live ?? 1;
- $respond['live'] = $live;
- $transaction->commit();
- util::success($respond);
- } catch (\Exception $e) {
- $transaction->rollBack();
- util::fail('创建结账单失败:' . $e->getMessage());
- noticeUtil::push('创建结账单失败:' . $e->getMessage(), '15280215347');
- }
- }
- public function actionWxPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $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('账单已取消');
- }
- //避免重复提交
- util::checkRepeatCommit($orderSn, 8);
- $oldOrderSn = $orderSn;
- $current = time();
- $id = $order->id;
- $deadline = $order->deadline;
- if ($current > strtotime($deadline)) {
- $order->status = PurchaseClearClass::STATUS_EXPIRE;
- $order->save();
- util::fail('订单已经失效,请重新发起结算');
- }
- $totalFee = $order->actPrice;
- $openId = '';
- if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
- $openId = $post['currentMiniOpenId'];
- //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347');
- }
- if (empty($openId)) {
- if (isset($this->admin) && !empty($this->admin)) {
- if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
- $openId = $this->admin->miniOpenId;
- noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347');
- }
- }
- }
- if (empty($openId)) {
- util::fail('没有找到openId');
- }
- $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
- $ghsShopId = $order->ghsShopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有供货商门店信息');
- }
- if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
- util::fail('商家支付功能未开通');
- }
- //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
- if ($order->changePrice == 2) {
- //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $oldOrderSn,
- ];
- $response = $laResource->close($closeParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- $customMobile = $order->customMobile ?? '';
- $ghsName = $order->ghsName ?? '';
- //noticeUtil::push('第一个,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347');
- }
- //收银台关单
- $closeParams = ['orderSn' => $orderSn];
- $response = $laResource->cashClose($closeParams);
- if (!isset($response['code']) || $response['code'] != '000000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- //noticeUtil::push('第二个,花店结账,单号:' . $oldOrderSn . ',收银台关单没有成功,原因:【' . $msg . '】,' . $code, '15280215347');
- }
- $newOrderSn = orderSn::getPurchaseClearSn();
- $orderSn = $newOrderSn;
- $order->orderSn = $newOrderSn;
- $order->save();
- OrderCgClearClass::updateClearSnByClearId($id, $newOrderSn);
- // 将老单号与订单 ID 的映射存入 Redis,有效期 24 小时 (86400秒)
- $redisKey = 'hd_clear_old_sn_map_' . $oldOrderSn;
- Yii::$app->redis->executeCommand('SETEX', [$redisKey, 86400, $id]);
- }
- $order->changePrice = 2;
- $order->save();
- //花卉宝代为申请的微信支付
- $merchantExtend = WxOpenClass::getWxInfo();
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
- $name = '采购单结算 ' . $orderSn;
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $capitalType,
- 'notifyUrl' => $notifyUrl,
- 'wxAppId' => $merchantExtend['miniAppId'],
- 'openId' => $openId,
- 'subject' => $name,
- 'couponId' => $couponId,
- ];
- $response = $laResource->driveWxPay($wxParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- $ghsName = $order->ghsName ?? '';
- $ghsShopName = $order->ghsShopName ?? '';
- $ghsFullName = $ghsName . $ghsShopName;
- $customName = $order->customName ?? '';
- $customFullName = $customName;
- noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
- util::fail('结账付款失败,' . $errMsg . ',请联系门店');
- }
- $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
- $appId = $newParams['app_id'] ?? '';
- $nonceStr = $newParams['nonce_str'] ?? '';
- $paySign = $newParams['pay_sign'] ?? '';
- $package = $newParams['package'] ?? '';
- $signType = $newParams['sign_type'] ?? '';
- $timeStamp = $newParams['time_stamp'] ?? '';
- $new = [
- 'id' => $id,
- 'appId' => $appId,
- 'nonceStr' => $nonceStr,
- 'paySign' => $paySign,
- 'package' => $package,
- 'signType' => $signType,
- 'timeStamp' => $timeStamp,
- 'orderSn' => $orderSn,
- ];
- $transaction->commit();
- util::success($new);
- } catch (\Exception $e) {
- $transaction->rollBack();
- util::fail('发起微信支付失败:' . $e->getMessage());
- noticeUtil::push('结账发起微信支付失败:' . $e->getMessage(), '15280215347');
- }
- }
- public function actionAliPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $post = Yii::$app->request->post();
- $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('账单已取消!');
- }
- $oldOrderSn = $orderSn;
- $current = time();
- $deadline = $order->deadline;
- if ($current > strtotime($deadline)) {
- $order->status = PurchaseClearClass::STATUS_EXPIRE;
- $order->save();
- util::fail('订单已经失效,请重新发起结算');
- }
- $totalFee = $order->actPrice;
- //避免重复提交
- util::checkRepeatCommit($orderSn, 8);
- $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
- $ghsShopId = $order->ghsShopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有供货商门店信息');
- }
- if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
- util::fail('商家支付功能未开通');
- }
- //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
- if ($order->changePrice == 2) {
- //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $oldOrderSn,
- ];
- $response = $laResource->cashClose($closeParams);
- if (isset($response['code']) == false || $response['code'] != '000000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- if (!in_array($code, ['000091'])) {
- noticeUtil::push("花店结账模块 " . $oldOrderSn . " 关单没有成功 收银台-支付宝模块 " . $msg, '15280215347');
- }
- }
- $newOrderSn = orderSn::getPurchaseClearSn();
- $orderSn = $newOrderSn;
- $order->orderSn = $newOrderSn;
- $order->save();
- OrderCgClearClass::updateClearSnByClearId($order->id, $newOrderSn);
- // 将老单号与订单 ID 的映射存入 Redis,有效期 24 小时 (86400秒)
- $redisKey = 'hd_clear_old_sn_map_' . $oldOrderSn;
- Yii::$app->redis->executeCommand('SETEX', [$redisKey, 86400, $order->id]);
- }
- $order->changePrice = 2;
- $order->save();
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $capitalType,
- 'notifyUrl' => $notifyUrl,
- 'subject' => $orderSn,
- ];
- $response = $laResource->cashierPay($wxParams);
- if (!isset($response['code']) || $response['code'] != '000000') {
- $errMsg = $response['msg'] ?? '';
- $ghsName = $order->ghsName ?? '';
- $ghsShopName = $order->ghsShopName ?? '';
- $ghsFullName = $ghsName . $ghsShopName;
- $customName = $order->customName ?? '';
- $customFullName = $customName;
- noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
- util::fail('结账付款失败,' . $errMsg . ',请联系门店');
- }
- $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
- $hasRenew = $ghsShop->hasRenew ?? 0;
- $new = [
- 'payUrl' => $payUrl,
- 'orderSn' => $orderSn,
- 'hasRenew' => 1,
- ];
- $transaction->commit();
- util::success($new);
- } catch (\Exception $e) {
- $transaction->rollBack();
- noticeUtil::push('结账发起支付宝支付失败:' . $e->getMessage(), '15280215347');
- }
- }
- }
|