ReOrderWechatProfitSharingJob.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * ReOrderWechatProfitSharingJob.php
  4. * todo 文件描述
  5. * Created on 2024/12/2 下午2:49
  6. * @author: hankaige
  7. */
  8. namespace app\jobs;
  9. use app\models\Option;
  10. use app\models\Order;
  11. use app\models\ReOrder;
  12. use app\models\SaasUser;
  13. use app\models\SaasUserPriceLog;
  14. use app\models\SharingReceiver;
  15. use app\models\User;
  16. use app\utils\Wechat\Wechat;
  17. use app\utils\Wechat\WechatMini;
  18. use app\utils\Wechat\WechatShare;
  19. use yii\base\BaseObject;
  20. use yii\db\Expression;
  21. use yii\queue\JobInterface;
  22. class ReOrderWechatProfitSharingJob extends BaseObject implements JobInterface
  23. {
  24. public $store_id;
  25. public $order;
  26. public function execute($queue)
  27. {
  28. $order = $this->order;
  29. if (!\Yii::$app->isSaas()) {
  30. return;
  31. }
  32. if (!$order) {
  33. \Yii::error('<=======================> 微信分账订单不存在');
  34. return;
  35. }
  36. if (in_array($order->pay_type, [Order::PAY_TYPE_ADAPAY_WX, Order::PAY_TYPE_ADAPAY_ALIPAY]) && $this->is_yunst == 0) {
  37. return $this->adapayProfitSharing($order);
  38. }
  39. if (in_array($order->pay_type, [Order::PAY_TYPE_YUNST_WECHAT_PAY, Order::PAY_TYPE_YUNST_ALI_PAY])) {
  40. //通联支付分账
  41. return $this->yunstProfitSharing($order);
  42. }
  43. // 此方法目前为多次分账,故分账结束后,剩余金额需要主动完结分账去触发解冻资金
  44. $order_id = $order->order_no;
  45. $receivers_share = SharingReceiver::find()->where([
  46. 'store_id' => $order->store_id,
  47. 'transaction_id' => $order->transaction_id, 'is_pay' => SharingReceiver::PAY_WAIT, 'execute_type' => SharingReceiver::EXECUTE_TYPE_NORMAL
  48. ])
  49. ->asArray()->all();
  50. $profitOrderId = 0;
  51. $receivers_share_ok = SharingReceiver::find()->where([
  52. 'store_id' => $order->store_id,
  53. 'transaction_id' => $order->transaction_id, 'is_pay' => SharingReceiver::PAY_SUCCESS, 'execute_type' => SharingReceiver::EXECUTE_TYPE_NORMAL
  54. ])
  55. ->orderBy('id DESC')->asArray()->one();
  56. $receivers_share_ok && $profitOrderId = $receivers_share_ok['order_id'];
  57. $cacheKey = 'wechatProfitSharing11:' . $order->transaction_id;
  58. if (\Yii::$app->cache->get($cacheKey)) {
  59. \Yii::error('<=======================> 微信分账进程已存在');
  60. return;
  61. }
  62. \Yii::$app->cache->set($cacheKey, 1, 160);
  63. $order_id_arr = [];
  64. foreach ($receivers_share as $receivers_share1) {
  65. sleep(1);
  66. //检测微信是否收货
  67. Wechat::init($this->store_id);
  68. $app = WechatMini::getWechatConfig($this->store_id);
  69. if (!$app) {
  70. array_push($order_id_arr, $order->id);
  71. continue;
  72. }
  73. $form = new \app\modules\admin\models\WechatThirdForm(['store_id' => $this->store_id]);
  74. $orderNoHead = substr($receivers_share1['order_no'], 0, 2);
  75. $model = Order::find();
  76. $where = [];
  77. switch ($orderNoHead) {
  78. case 'ML':
  79. case 'AL':
  80. //商盟会员购物订单
  81. $model = Order::find();
  82. $where = [
  83. 'order_no' => $receivers_share1['order_no'],
  84. 'is_delete' => 0,
  85. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  86. ];
  87. break;
  88. case 'SC':
  89. //当面付订单
  90. $model = \app\plugins\scanCodePay\models\Order::find();
  91. $where = [
  92. 'order_no' => $receivers_share1['order_no'],
  93. 'is_delete' => 0,
  94. 'is_pay' => 1
  95. ];
  96. break;
  97. case 'RG':
  98. $model = ReOrder::find();
  99. $where = [
  100. 'order_no' => $receivers_share1['order_no'],
  101. 'is_delete' => 0,
  102. 'is_pay' => 1
  103. ];
  104. }
  105. $order_ = $model->where($where)->asArray()->one();
  106. $res = $form->wxOrderStateIsConfirm($app, $order_);
  107. if (!$res) {
  108. array_push($order_id_arr, $order->id);
  109. continue;
  110. }
  111. $profitOrderId || $profitOrderId = $order->order_no;
  112. $res = WechatShare::share($order->transaction_id, $profitOrderId, [$receivers_share1], FALSE, FALSE, $order->store_id, $order->is_platform, $order);
  113. \Yii::error(['wechatProfitSharingwechatProfitSharing11:', $res]);
  114. if ($res['code'] > 0) {
  115. $faildDes = !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg'];
  116. $faildDes && $faildDes = addslashes('(失败:' . $faildDes . ')');
  117. $receivers_share_platform = [
  118. [
  119. 'type' => SharingReceiver::RECEIVE_MERCHANT_ID,
  120. 'account' => Option::get('sp_mch_id', 0, 'saas')['value'],
  121. 'amount' => $receivers_share1['amount'],
  122. 'name' => Option::get('sp_name', 0, 'saas')['value'],
  123. ]
  124. ];
  125. //分到平台
  126. if ($receivers_share1['type'] == 2) {
  127. SharingReceiver::updateAll([
  128. 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_FAIL, 'err_code' => !empty($res['res']['err_code']) ? $res['res']['err_code'] : $res['msg'],
  129. 'err_code_des' => !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg']
  130. ], ['id' => $receivers_share1['id']]);
  131. continue;
  132. }
  133. sleep(1);
  134. $res = WechatShare::share($order->transaction_id, $profitOrderId, $receivers_share_platform, FALSE, FALSE, $order->store_id, $order->is_platform, $order);
  135. \Yii::error(['分到平台wechatProfitSharingwechatProfitSharing11:', $res]);
  136. if ($res['code'] > 0) {
  137. SharingReceiver::updateAll([
  138. 'description' => new Expression('CONCAT(description, "' . $faildDes . '-分账失败,转到服务商")'),
  139. 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_FAIL, 'err_code' => !empty($res['res']['err_code']) ? $res['res']['err_code'] : $res['msg'],
  140. 'err_code_des' => !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg']
  141. ], ['id' => $receivers_share1['id']]);
  142. $this->saasUserPriceLog($order, [$receivers_share1]);
  143. $this->saasUserPriceLog($order, [$receivers_share1], TRUE, '-分账失败');
  144. } else {
  145. SharingReceiver::updateAll([
  146. 'description' => new Expression('CONCAT(description, "' . $faildDes . '-分账失败,转到服务商")'),
  147. 'updated_at' => time(), 'is_pay' => SharingReceiver::PAY_SUCCESS, 'order_id' => $res['res']['order_id']
  148. ], ['id' => $receivers_share1['id']]);
  149. $this->saasUserPriceLog($order, [$receivers_share1]);
  150. $profitOrderId = $res['res']['order_id'];
  151. }
  152. } else {
  153. $order_id = $res['res']['order_id'];
  154. SharingReceiver::updateAll(['updated_at' => time(), 'is_pay' => SharingReceiver::PAY_SUCCESS, 'order_id' => $res['res']['order_id']], ['id' => $receivers_share1['id']]);
  155. $this->saasUserPriceLog($order, [$receivers_share1]);
  156. $this->saasUserPriceLog($order, [$receivers_share1], TRUE);
  157. $profitOrderId = $res['res']['order_id'];
  158. }
  159. }
  160. // 单次分账结束后,剩余金额自动解冻返给商户, 无需主动触发分账完结接口,多次分账时需要最终触发分账完结,最终解冻给商户
  161. // 分账完结
  162. $res = WechatShare::complete($order->transaction_id, $order_id, FALSE, '分账完结', $order->store_id, $order->is_platform);
  163. return $order_id_arr;
  164. }
  165. private function saasUserPriceLog($order, $SharingReceivers = [], $expend = false, $log = '') {
  166. try {
  167. $log_type = SaasUserPriceLog::LOG_TYPE_INCOME;
  168. $remark = '';
  169. if($expend){
  170. $log_type = SaasUserPriceLog::LOG_TYPE_EXPEND;
  171. $remark = '-分账成功';
  172. }
  173. $log && $remark = $log;
  174. foreach ($SharingReceivers as $item) {
  175. \Yii::warning(['---- saasUserPriceLog Err11 ----', $item]);
  176. if(in_array($item['type'], [SharingReceiver::RECEIVE_MERCHANT_ID])){
  177. continue;
  178. }
  179. $desc = $item['remark'] . $remark;
  180. $saas_id = $item['saas_id'];
  181. if(!$saas_id){
  182. if(!$order->is_platform){
  183. $saas_id = $item['user_id'];
  184. }else{
  185. $userInfo = User::findOne($item['user_id']);
  186. if(!empty($userInfo)){
  187. $saas = SaasUser::findOne(['mobile' => $userInfo->binding]);
  188. $saas_id = $saas->id;
  189. }
  190. }
  191. }
  192. \Yii::warning(['---- saasUserPriceLog Err22 ----', $saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc]);
  193. SaasUserPriceLog::saveLog($saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc);
  194. }
  195. } catch (\Exception $e) {
  196. \Yii::warning('---- saasUserPriceLog Err ----' . __LINE__);
  197. }
  198. }
  199. }