PayNotifyLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\logic;
  15. use app\common\enum\PayEnum;
  16. use app\common\enum\user\AccountLogEnum;
  17. use app\common\enum\WorkEnum;
  18. use app\common\model\recharge\RechargeOrder;
  19. use app\common\model\shops\ShopOrders;
  20. use app\common\model\user\User;
  21. use app\common\model\works\ServiceWork;
  22. use think\facade\Db;
  23. use think\facade\Log;
  24. /**
  25. * 支付成功后处理订单状态
  26. * Class PayNotifyLogic
  27. * @package app\api\logic
  28. */
  29. class PayNotifyLogic extends BaseLogic
  30. {
  31. public static function handle($action, $orderSn, $extra = [])
  32. {
  33. Db::startTrans();
  34. try {
  35. self::$action($orderSn, $extra);
  36. Db::commit();
  37. return true;
  38. } catch (\Exception $e) {
  39. Db::rollback();
  40. Log::write(implode('-', [
  41. __CLASS__,
  42. __FUNCTION__,
  43. $e->getFile(),
  44. $e->getLine(),
  45. $e->getMessage()
  46. ]));
  47. self::setError($e->getMessage());
  48. return $e->getMessage();
  49. }
  50. }
  51. /**
  52. * @notes 充值回调
  53. * @param $orderSn
  54. * @param array $extra
  55. */
  56. public static function recharge($orderSn, array $extra = [])
  57. {
  58. $order = RechargeOrder::where('sn', $orderSn)->findOrEmpty();
  59. // 增加用户累计充值金额及用户余额
  60. $user = User::findOrEmpty($order->user_id);
  61. $user->total_recharge_amount += $order->order_amount;
  62. $user->user_money += $order->order_amount;
  63. $user->save();
  64. // 记录账户流水
  65. AccountLogLogic::add(
  66. $order->user_id,
  67. AccountLogEnum::UM_INC_RECHARGE,
  68. AccountLogEnum::INC,
  69. $order->order_amount,
  70. $order->sn,
  71. '用户充值'
  72. );
  73. // 更新充值订单状态
  74. $order->transaction_id = $extra['transaction_id'] ?? '';
  75. $order->pay_status = PayEnum::ISPAID;
  76. $order->paid_amount = $order->order_amount;
  77. $order->pay_time = time();
  78. $order->save();
  79. }
  80. /**
  81. * @notes 服务回调
  82. * @param $orderSn
  83. * @param array $extra
  84. */
  85. public static function goods($orderSn, array $extra = [])
  86. {
  87. $order = RechargeOrder::where('sn', $orderSn)->findOrEmpty();
  88. Log::write($order->toArray(),JSON_UNESCAPED_UNICODE);
  89. if(!$order->isEmpty()){
  90. // 更新充值订单状态
  91. $order->transaction_id = $extra['transaction_id'] ?? '';
  92. $order->pay_status = PayEnum::ISPAID;
  93. $order->pay_time = time();
  94. $order->paid_amount = $order->order_amount;
  95. $order->save();
  96. $work = ServiceWork::findOrEmpty($order->work_id);
  97. if(!$work->isEmpty()){
  98. $work->work_pay_status = WorkEnum::IS_PAY_STATUS;
  99. $orders = \app\common\model\orders\RechargeOrder::where(['work_id'=>$order->work_id])->select()->toArray();
  100. $order_total = 0;
  101. $order_amount = 0;
  102. foreach ($orders as $k=>$v){
  103. $order_total += $v['order_total'];
  104. $order_amount += $v['order_amount'];
  105. }
  106. $work->work_total = $order_total;
  107. $work->work_amount = $order_amount;
  108. if($work->work_status != 0){
  109. $work->work_status = 7;
  110. $work->user_confirm_status = 5;
  111. $work->service_status = 3;
  112. }
  113. $work->work_pay_status = 1;
  114. $work->save();
  115. }
  116. }
  117. }
  118. public static function shop_goods($orderSn, array $extra = [])
  119. {
  120. $order = ShopOrders::where('sn', $orderSn)->findOrEmpty();
  121. // 更新充值订单状态
  122. $order->transaction_id = $extra['transaction_id'] ?? '';
  123. $order->pay_status = PayEnum::ISPAID;
  124. $order->pay_time = time();
  125. $order->shop_order_type = 2;
  126. $order->save();
  127. }
  128. }