Notify.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace addons\Area\common\logic\notify;
  3. use addons\Area\common\models\AreaOrder;
  4. use addons\Area\common\enums\AreaEnum;
  5. use addons\Area\common\models\AreaAgent;
  6. use addons\Area\common\models\AreaApply;
  7. use common\enums\StatusEnum;
  8. use common\enums\UserWalletEnum;
  9. use common\models\common\PaymentTrade;
  10. use services\common\UserWalletService;
  11. use common\models\order\Order;
  12. use common\traits\ErrorTrait;
  13. use Exception;
  14. /**
  15. * Class Notify
  16. * @Author: hua
  17. * @DateTime: 2021/10/18 9:39
  18. * @Copyright: copyright (c) 2021 广东七件事集团
  19. * @package common\logic\notify
  20. */
  21. class Notify
  22. {
  23. use ErrorTrait;
  24. /**
  25. * @Author: hua
  26. * @DateTime: 2021/10/18 9:57
  27. * @Copyright: copyright (c) 2021 广东七件事集团
  28. * @param $order_info
  29. * @return bool
  30. */
  31. public function notify($order_info)
  32. {
  33. try {
  34. $payment_trade = PaymentTrade::findOne(['id' => $order_info['trade_id']]);
  35. $order = AreaOrder::findOne(['order_no' => $order_info['order_no']]);
  36. $configs = \Yii::$app->services->setting->addons->get($order['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
  37. //维护订单数据
  38. if ($order->status != StatusEnum::ENABLED) throw new Exception('该订单已被处理');
  39. if ($order->is_pay == 1) throw new Exception('订单已支付,请不要重复支付');
  40. $order->is_pay = StatusEnum::ENABLED;
  41. $order->pay_time = time();
  42. $order->out_trade_no = $payment_trade['out_trade_no'];
  43. $order->total_commission = bcmul($order->price * ($configs['add_price_pre']/100) * ($configs['price_commission_pre'] / 100), 1, 2);
  44. $order->commission = $order->total_commission;
  45. //获取是否存在已购买过相同区代的订单
  46. $area_order = AreaOrder::getOne([
  47. ['mall_id' => $order->mall_id],
  48. ['level' => $order->level],
  49. ['area_id' => $order->area_id],
  50. ['is_pay' => StatusEnum::ENABLED],
  51. ['status' => StatusEnum::ENABLED]
  52. ]);
  53. $insert_wallet = [];
  54. //如果存在则需要处理佣金
  55. if (!empty($area_order)) {
  56. //判断是否修改过每次加价、分红设置配置数据,未修改则需扣除上个订单的佣金,出现修改则不需要扣除
  57. $total_commission = bcmul($area_order->price * ($configs['add_price_pre']/100) * ($configs['price_commission_pre'] / 100), 1, 2);
  58. if ($total_commission == $area_order->total_commission) {
  59. $order->commission = $order->total_commission - $area_order->total_commission;
  60. } else {
  61. $area_order->total_commission = $total_commission;
  62. $area_order->commission = $total_commission;
  63. $order->commission = $order->total_commission - $area_order->total_commission;
  64. }
  65. $area_order->status = StatusEnum::DELETE;
  66. //退回购买支付金额
  67. $insert_wallet[] = [
  68. 'mall_id' => $area_order->mall_id,
  69. 'user_id' => $area_order->user_id,
  70. 'is_frozen' => false,
  71. 'wallet_type' => 'balance',
  72. 'money' => $area_order->price, //转入金额
  73. 'desc' => '其他用户加价占用代理身份',
  74. 'source_table' => 'addons_area_order',
  75. 'source_table_id' => $area_order->id,//结算周期id
  76. 'from_type' => UserWalletEnum::REFUND,
  77. 'capital_type' => UserWalletEnum::AREA_BONUS_REFUND,
  78. // 'not_add_total' => true,
  79. ];
  80. //加价分红
  81. $insert_wallet[] = [
  82. 'mall_id' => $area_order->mall_id,
  83. 'user_id' => $area_order->user_id,
  84. 'is_frozen' => false,
  85. 'wallet_type' => 'commission',
  86. 'money' => $area_order->commission, //转入金额
  87. 'desc' => '代理身份被其他用户加价占用,获得加价分红',
  88. 'source_table' => 'addons_area_order',
  89. 'source_table_id' => $area_order['id'],//
  90. 'from_type' => UserWalletEnum::FROM_AREA,
  91. 'capital_type' => UserWalletEnum::AREA_BONUS_REWARD,
  92. ];
  93. $area_order->save();
  94. //维护区代申请数据
  95. AreaApply::updateAll(['check_status' => 2, 'refund_time' => time(), 'is_refund' => 1, 'refund_remark' => '代理身份被其他用户加价占用,获得加价分红'], ['order_id' => $area_order->id, 'mall_id' => $area_order->mall_id]);
  96. }
  97. if (!$order->save()) throw new Exception('修改订单失败:'. $order->getErrorMessage());
  98. //维护区代身份
  99. AreaAgent::updateAll(['status' => StatusEnum::DELETE], ['level' => $order->level, 'district_id' => $order->area_id, 'mall_id' => $order->mall_id]);
  100. if (!empty($insert_wallet)) {
  101. UserWalletService::batchHandleByQueue($insert_wallet);
  102. }
  103. // $res = Order::pay($order,$order_info['pay_type'],$payment_trade['out_trade_no']);
  104. // if($res === false) throw new Exception(Order::getStaticError());
  105. return true;
  106. } catch (Exception $e) {
  107. \Yii::$app->custom->logs('notify:'.$e->getMessage());
  108. $this->setStaticError($e->getMessage());
  109. return false;
  110. }
  111. }
  112. }