Kirin 3 年 前
コミット
908d7df3f1

+ 70 - 0
app/models/store/StoreOrder.php

@@ -9,6 +9,7 @@ namespace app\models\store;
 
 use app\admin\model\system\ShippingTemplatesFree;
 use app\admin\model\system\ShippingTemplatesRegion;
+use app\admin\model\system\SystemAdmin;
 use app\admin\model\system\SystemStorePoint;
 use crmeb\basic\BaseModel;
 use think\db\exception\DataNotFoundException;
@@ -19,6 +20,7 @@ use crmeb\traits\ModelTrait;
 use think\facade\Log;
 use app\models\system\SystemStore;
 use app\models\routine\RoutineTemplate;
+use think\Model;
 use app\models\user\{User, UserAddress, UserBill, UserSpread, WechatUser};
 use crmeb\services\{MiniProgramService,
     OtherSMSService,
@@ -2259,4 +2261,72 @@ class StoreOrder extends BaseModel
         return compact('list', 'count');
     }
 
+
+    /**
+     * 处理奖励
+     */
+    public static function treatmentAward($order_id)
+    {
+        $order = StoreOrder::get($order_id);
+        if (!$order) return self::setErrorInfo('找不到订单');
+        if (!$order['transaction_id']) return true;
+        $store = SystemStorePoint::get($order['store_id']);
+        if (!$store) return true;
+
+        $store_leader = SystemAdmin::where('role', sys_config('default_store_admin', 7))
+            ->where('store_id', $store['id'])->where('is_del', 0)->where('status', 1)
+            ->find();
+        if (!$store_leader) return true;
+        $user = User::where('admin_id', $store_leader['id'])->find();
+        if (!$user) return true;
+        $p_user_info = WechatUser::get($user['uid']);
+
+        // 选择发起人的订单作为分账的账单
+        $transaction_id = $order['transaction_id']; // 微信订单号
+        $out_order_no = $order['order_id'];
+
+        // 计算发起人和参与者的分账金额
+        $price = $order['pay_price'] - $order['pay_postage_sh'] - $order['pay_postage'];
+        $p_amount = bcmul($price, sys_config('store_admin_sharing', 0) / 100, 2);  //bcmul:2个任意精度数字乘法计算
+        $p_separate_account_amount = floor($p_amount * 100); // 发起人分账金额,以分为单位
+
+        $receivers[] = array(
+            'type' => 'PERSONAL_OPENID',
+            'account' => $p_user_info['routine_openid'],
+            'amount' => $p_separate_account_amount,
+            'description' => '门店订单'
+        );
+
+        // 记录发起人分账开始的信息
+        $p_separate_account_record = new StoreSeparateAccountRecord();
+        $p_separate_account_record['money'] = $p_amount;
+        $p_separate_account_record['order_id'] = $order_id;  // OK
+        $p_separate_account_record['uid'] = $user['uid'];
+        $p_separate_account_record['state'] = 0; // 默认为失败
+        $p_separate_account_record['notice'] = "分账失败";
+        $p_separate_account_record['store_id'] = $order['store_id'];
+        $p_separate_account_record['start_time'] = time();
+        $p_separate_account_record['end_time'] = 0;
+        $p_separate_account_record->save();
+
+        // 添加发起人为分账接受方
+        MiniProgramService::addReceiver($p_user_info['routine_openid']); // 添加发起人为分账接受方
+
+        if ($transaction_id) {
+            $res = MiniProgramService::multi_profit_sharing($transaction_id, $out_order_no, $receivers);
+            $res = json_decode($res, true);
+
+            $separateAccount = StoreSeparateAccountRecord::where('order_id', $order_id)->select();
+            if ($res['return_code'] == 'SUCCESS') {
+                foreach ($separateAccount as $key => $value) {
+                    $value['state'] = 1;
+                    $value['notice'] = "分账成功";
+                    $value['end_time'] = time();
+                    $value->save();
+                }
+                MiniProgramService::profit_sharing_finish($order['transaction_id'], $order['order_id']);
+            }
+        }
+        return true;
+    }
 }

+ 24 - 0
app/models/store/StoreSeparateAccountRecord.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace app\models\store;
+
+use crmeb\services\UtilService;
+use crmeb\traits\ModelTrait;
+use crmeb\basic\BaseModel;
+
+class StoreSeparateAccountRecord extends BaseModel
+{
+	/**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'store_separate_account_record';
+
+    use ModelTrait;
+}

+ 30 - 0
crmeb/services/MiniProgramService.php

@@ -7,6 +7,7 @@
 
 namespace crmeb\services;
 
+use app\models\store\StoreOrder;
 use crmeb\repositories\PaymentRepositories;
 use crmeb\utils\Hook;
 use EasyWeChat\Foundation\Application;
@@ -332,6 +333,7 @@ class MiniProgramService
                     if (($count = strpos($notify->out_trade_no, '_')) !== false) {
                         $notify->out_trade_no = substr($notify->out_trade_no, $count + 1);
                     }
+                    StoreOrder::edit(['transaction_id' => $notify->transaction_id], $notify->out_trade_no, 'order_id');
                     (new Hook(PaymentRepositories::class, 'wechat'))->listen($notify->attach, $notify->out_trade_no);
                 }
                 return false;
@@ -354,4 +356,32 @@ class MiniProgramService
     }
 
 
+    /**
+     * 添加分账接收方
+     */
+    public static function addReceiver($account)
+    {
+        $receiver = array(
+            'type' => 'PERSONAL_OPENID',
+            'account' => $account, // openid
+            'relation_type' => 'USER',
+        );
+        return self::paymentService()->addReceiver($receiver);
+    }
+
+    /**
+     * 多次分账
+     */
+    public static function multi_profit_sharing($orderNo, $sharingNo, $receivers)
+    {
+        return self::paymentService()->multiProfitSharing($orderNo, $sharingNo, $receivers);
+    }
+
+    /**
+     * 完结分账
+     */
+    public static function profit_sharing_finish($orderNo, $sharingNo)
+    {
+        return self::paymentService()->profitSharingFinish($orderNo, $sharingNo);
+    }
 }

+ 61 - 0
vendor/overtrue/wechat/src/Payment/API.php

@@ -561,4 +561,65 @@ class API extends AbstractAPI
     {
         return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
     }
+
+    /**
+     * @param $orderNo
+     * @param $sharingNo
+     * @param $receivers
+     * @param string $type
+     * @return Collection
+     */
+    public function profit_sharing(
+        $orderNo,
+        $sharingNo,
+        $receivers,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'receivers' => json_encode($receivers)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFITSHARING), $params, 'post','hash_hmac');
+    }
+
+    public function addReceiver(
+        $receiver
+    )
+    {
+        $params = [
+            'receiver' => json_encode($receiver)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFIT_SHARING_ADD_RECEIVER), $params, 'post', 'hash_hmac');
+    }
+
+    public function multiProfitSharing(
+        $orderNo,
+        $sharingNo,
+        $receivers,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'receivers' => json_encode($receivers)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_MULTI_PROFIT_SHARING), $params, 'post', 'hash_hmac');
+    }
+
+    public function profitSharingFinish(
+        $orderNo,
+        $sharingNo,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'description' => '分账已完成'
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFIT_SHARING_FINISH), $params, 'post', 'hash_hmac');
+    }
 }