| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- /**
- * ReOrderWechatProfitSharingJob.php
- * todo 文件描述
- * Created on 2024/12/2 下午2:49
- * @author: hankaige
- */
- namespace app\jobs;
- use app\models\Option;
- use app\models\Order;
- use app\models\ReOrder;
- use app\models\SaasUser;
- use app\models\SaasUserPriceLog;
- use app\models\SharingReceiver;
- use app\models\User;
- use app\utils\Wechat\Wechat;
- use app\utils\Wechat\WechatMini;
- use app\utils\Wechat\WechatShare;
- use yii\base\BaseObject;
- use yii\db\Expression;
- use yii\queue\JobInterface;
- class ReOrderWechatProfitSharingJob extends BaseObject implements JobInterface
- {
- public $store_id;
- public $order;
- public function execute($queue)
- {
- $order = $this->order;
- if (!\Yii::$app->isSaas()) {
- return;
- }
- if (!$order) {
- \Yii::error('<=======================> 微信分账订单不存在');
- return;
- }
- if (in_array($order->pay_type, [Order::PAY_TYPE_ADAPAY_WX, Order::PAY_TYPE_ADAPAY_ALIPAY]) && $this->is_yunst == 0) {
- return $this->adapayProfitSharing($order);
- }
- if (in_array($order->pay_type, [Order::PAY_TYPE_YUNST_WECHAT_PAY, Order::PAY_TYPE_YUNST_ALI_PAY])) {
- //通联支付分账
- return $this->yunstProfitSharing($order);
- }
- // 此方法目前为多次分账,故分账结束后,剩余金额需要主动完结分账去触发解冻资金
- $order_id = $order->order_no;
- $receivers_share = SharingReceiver::find()->where([
- 'store_id' => $order->store_id,
- 'transaction_id' => $order->transaction_id, 'is_pay' => SharingReceiver::PAY_WAIT, 'execute_type' => SharingReceiver::EXECUTE_TYPE_NORMAL
- ])
- ->asArray()->all();
- $profitOrderId = 0;
- $receivers_share_ok = SharingReceiver::find()->where([
- 'store_id' => $order->store_id,
- 'transaction_id' => $order->transaction_id, 'is_pay' => SharingReceiver::PAY_SUCCESS, 'execute_type' => SharingReceiver::EXECUTE_TYPE_NORMAL
- ])
- ->orderBy('id DESC')->asArray()->one();
- $receivers_share_ok && $profitOrderId = $receivers_share_ok['order_id'];
- $cacheKey = 'wechatProfitSharing11:' . $order->transaction_id;
- if (\Yii::$app->cache->get($cacheKey)) {
- \Yii::error('<=======================> 微信分账进程已存在');
- return;
- }
- \Yii::$app->cache->set($cacheKey, 1, 160);
- $order_id_arr = [];
- foreach ($receivers_share as $receivers_share1) {
- sleep(1);
- //检测微信是否收货
- Wechat::init($this->store_id);
- $app = WechatMini::getWechatConfig($this->store_id);
- if (!$app) {
- array_push($order_id_arr, $order->id);
- continue;
- }
- $form = new \app\modules\admin\models\WechatThirdForm(['store_id' => $this->store_id]);
- $orderNoHead = substr($receivers_share1['order_no'], 0, 2);
- $model = Order::find();
- $where = [];
- switch ($orderNoHead) {
- case 'ML':
- case 'AL':
- //商盟会员购物订单
- $model = Order::find();
- $where = [
- 'order_no' => $receivers_share1['order_no'],
- 'is_delete' => 0,
- 'trade_status' => Order::ORDER_FLOW_CONFIRM
- ];
- break;
- case 'SC':
- //当面付订单
- $model = \app\plugins\scanCodePay\models\Order::find();
- $where = [
- 'order_no' => $receivers_share1['order_no'],
- 'is_delete' => 0,
- 'is_pay' => 1
- ];
- break;
- case 'RG':
- $model = ReOrder::find();
- $where = [
- 'order_no' => $receivers_share1['order_no'],
- 'is_delete' => 0,
- 'is_pay' => 1
- ];
- }
- $order_ = $model->where($where)->asArray()->one();
- $res = $form->wxOrderStateIsConfirm($app, $order_);
- if (!$res) {
- array_push($order_id_arr, $order->id);
- continue;
- }
- $profitOrderId || $profitOrderId = $order->order_no;
- $res = WechatShare::share($order->transaction_id, $profitOrderId, [$receivers_share1], FALSE, FALSE, $order->store_id, $order->is_platform, $order);
- \Yii::error(['wechatProfitSharingwechatProfitSharing11:', $res]);
- if ($res['code'] > 0) {
- $faildDes = !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg'];
- $faildDes && $faildDes = addslashes('(失败:' . $faildDes . ')');
- $receivers_share_platform = [
- [
- 'type' => SharingReceiver::RECEIVE_MERCHANT_ID,
- 'account' => Option::get('sp_mch_id', 0, 'saas')['value'],
- 'amount' => $receivers_share1['amount'],
- 'name' => Option::get('sp_name', 0, 'saas')['value'],
- ]
- ];
- //分到平台
- if ($receivers_share1['type'] == 2) {
- SharingReceiver::updateAll([
- 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_FAIL, 'err_code' => !empty($res['res']['err_code']) ? $res['res']['err_code'] : $res['msg'],
- 'err_code_des' => !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg']
- ], ['id' => $receivers_share1['id']]);
- continue;
- }
- sleep(1);
- $res = WechatShare::share($order->transaction_id, $profitOrderId, $receivers_share_platform, FALSE, FALSE, $order->store_id, $order->is_platform, $order);
- \Yii::error(['分到平台wechatProfitSharingwechatProfitSharing11:', $res]);
- if ($res['code'] > 0) {
- SharingReceiver::updateAll([
- 'description' => new Expression('CONCAT(description, "' . $faildDes . '-分账失败,转到服务商")'),
- 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_FAIL, 'err_code' => !empty($res['res']['err_code']) ? $res['res']['err_code'] : $res['msg'],
- 'err_code_des' => !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg']
- ], ['id' => $receivers_share1['id']]);
- $this->saasUserPriceLog($order, [$receivers_share1]);
- $this->saasUserPriceLog($order, [$receivers_share1], TRUE, '-分账失败');
- } else {
- SharingReceiver::updateAll([
- 'description' => new Expression('CONCAT(description, "' . $faildDes . '-分账失败,转到服务商")'),
- 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_SUCCESS, 'order_id' => $res['res']['order_id']
- ], ['id' => $receivers_share1['id']]);
- $this->saasUserPriceLog($order, [$receivers_share1]);
- $profitOrderId = $res['res']['order_id'];
- }
- } else {
- $order_id = $res['res']['order_id'];
- SharingReceiver::updateAll(['updated_at' => time(), 'is_pay' => SharingReceiver::PAY_SUCCESS, 'order_id' => $res['res']['order_id']], ['id' => $receivers_share1['id']]);
- $this->saasUserPriceLog($order, [$receivers_share1]);
- $this->saasUserPriceLog($order, [$receivers_share1], TRUE);
- $profitOrderId = $res['res']['order_id'];
- }
- }
- // 单次分账结束后,剩余金额自动解冻返给商户, 无需主动触发分账完结接口,多次分账时需要最终触发分账完结,最终解冻给商户
- // 分账完结
- $res = WechatShare::complete($order->transaction_id, $order_id, FALSE, '分账完结', $order->store_id, $order->is_platform);
- return $order_id_arr;
- }
- private function saasUserPriceLog($order, $SharingReceivers = [], $expend = false, $log = '') {
- try {
- $log_type = SaasUserPriceLog::LOG_TYPE_INCOME;
- $remark = '';
- if($expend){
- $log_type = SaasUserPriceLog::LOG_TYPE_EXPEND;
- $remark = '-分账成功';
- }
- $log && $remark = $log;
- foreach ($SharingReceivers as $item) {
- \Yii::warning(['---- saasUserPriceLog Err11 ----', $item]);
- if(in_array($item['type'], [SharingReceiver::RECEIVE_MERCHANT_ID])){
- continue;
- }
- $desc = $item['remark'] . $remark;
- $saas_id = $item['saas_id'];
- if(!$saas_id){
- if(!$order->is_platform){
- $saas_id = $item['user_id'];
- }else{
- $userInfo = User::findOne($item['user_id']);
- if(!empty($userInfo)){
- $saas = SaasUser::findOne(['mobile' => $userInfo->binding]);
- $saas_id = $saas->id;
- }
- }
- }
- \Yii::warning(['---- saasUserPriceLog Err22 ----', $saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc]);
- SaasUserPriceLog::saveLog($saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc);
- }
- } catch (\Exception $e) {
- \Yii::warning('---- saasUserPriceLog Err ----' . __LINE__);
- }
- }
- }
|