Răsfoiți Sursa

Merge branch 'redesign‌-260706' into dev

ouyang 1 zi în urmă
părinte
comite
564923b15e

+ 140 - 0
biz-hd/shop/classes/MainWalletRechargeClass.php

@@ -0,0 +1,140 @@
+<?php
+/**
+ * 中央钱包微信充值业务类
+ * 谁用:MainWalletController 建单、payUtil 支付回调
+ * 解决:未支付单落库;回调后增加 walletBalance 并写 xhMainWalletChange
+ */
+namespace bizHd\shop\classes;
+
+use bizHd\base\classes\BaseClass;
+use common\components\dict;
+use common\components\noticeUtil;
+use common\components\orderSn;
+use common\components\util;
+use Yii;
+
+class MainWalletRechargeClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\shop\models\MainWalletRecharge';
+
+    /** 单笔充值上限(元) */
+    const MAX_AMOUNT = 50000;
+
+    /**
+     * 创建未支付充值单
+     * @param int $mainId
+     * @param int $shopId
+     * @param float $amount
+     * @return object
+     */
+    public static function createPending($mainId, $shopId, $amount)
+    {
+        $mainId = (int) $mainId;
+        $shopId = (int) $shopId;
+        $amount = round((float) $amount, 2);
+        if ($mainId <= 0) {
+            util::fail('账户无效');
+        }
+        if ($amount <= 0) {
+            util::fail('请输入充值金额');
+        }
+        if ($amount > self::MAX_AMOUNT) {
+            util::fail('单笔充值不能超过' . self::MAX_AMOUNT . '元');
+        }
+
+        $orderSn = orderSn::getMainWalletRechargeSn($mainId);
+        $row = self::add([
+            'orderSn' => $orderSn,
+            'mainId' => $mainId,
+            'shopId' => $shopId,
+            'amount' => $amount,
+            'payStatus' => 0,
+            'payWay' => 0,
+            'returnCode' => '',
+            'remark' => '',
+        ], true);
+        if (empty($row) || empty($row->id)) {
+            util::fail('创建充值单失败');
+        }
+        return $row;
+    }
+
+    /**
+     * 微信支付回调入账
+     * @param int $payWay
+     * @param string $orderSn
+     * @param float $totalFee 回调实付金额(元)
+     * @param string $attach
+     * @param string $transactionId
+     * @return bool|object
+     */
+    public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '')
+    {
+        $recharge = self::getByCondition(['orderSn' => $orderSn], true);
+        if (empty($recharge)) {
+            noticeUtil::push('中央钱包充值回调未找到订单,orderSn:' . $orderSn, '15280215347');
+            return false;
+        }
+        // 金额必须与建单一致,避免短款/长款误入账
+        if (bccomp((string) $totalFee, (string) $recharge->amount, 2) !== 0) {
+            noticeUtil::push(
+                "中央钱包充值金额不一致 {$totalFee} {$recharge->amount},orderSn:{$orderSn}",
+                '15280215347'
+            );
+            return false;
+        }
+        if ((int) $recharge->payStatus === 1) {
+            Yii::info('中央钱包充值已支付,忽略重复回调 orderSn:' . $orderSn);
+            return false;
+        }
+
+        $id = (int) $recharge->id;
+        $recharge = self::getLockById($id);
+        if (empty($recharge)) {
+            noticeUtil::push('中央钱包充值锁单失败,orderSn:' . $orderSn, '15280215347');
+            return false;
+        }
+        if ((int) $recharge->payStatus === 1) {
+            return false;
+        }
+
+        $mainId = (int) $recharge->mainId;
+        $amount = round((float) $recharge->amount, 2);
+        $main = MainClass::getLockById($mainId);
+        if (empty($main)) {
+            util::fail('中央账户不存在');
+        }
+
+        $beforeWallet = round((float) ($main->walletBalance ?? 0), 2);
+        $afterWallet = round($beforeWallet + $amount, 2);
+        MainClass::updateById($mainId, ['walletBalance' => $afterWallet]);
+
+        $capitalType = dict::getDict('capitalType', 'mainWalletRecharge', 'id');
+        $ptStyle = dict::getDict('ptStyle', 'hd');
+        $fromType = dict::getDict('fromType', 'shop');
+        $ioIncome = dict::getDict('io', 'income');
+
+        MainWalletChangeClass::add([
+            'relateId' => $id,
+            'ptStyle' => $ptStyle,
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $afterWallet,
+            'io' => $ioIncome,
+            'payWay' => $payWay,
+            'fromType' => $fromType,
+            'event' => '微信充值' . $orderSn,
+            'mainId' => $mainId,
+            'remark' => '中央钱包微信充值',
+        ]);
+
+        $recharge->payStatus = 1;
+        $recharge->payWay = $payWay;
+        $recharge->returnCode = (string) $transactionId;
+        $recharge->save(false);
+
+        return $recharge;
+    }
+
+}

+ 18 - 0
biz-hd/shop/models/MainWalletRecharge.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * 中央钱包微信充值订单 xhMainWalletRecharge
+ * 用途:hdApp 可用余额微信充值待支付/已支付单据
+ */
+namespace bizHd\shop\models;
+
+use bizHd\base\models\Base;
+
+class MainWalletRecharge extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhMainWalletRecharge';
+    }
+
+}