PayNotifyLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\user\User;
  20. use app\common\model\works\ServiceWork;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. /**
  24. * 支付成功后处理订单状态
  25. * Class PayNotifyLogic
  26. * @package app\api\logic
  27. */
  28. class PayNotifyLogic extends BaseLogic
  29. {
  30. public static function handle($action, $orderSn, $extra = [])
  31. {
  32. Db::startTrans();
  33. try {
  34. self::$action($orderSn, $extra);
  35. Db::commit();
  36. return true;
  37. } catch (\Exception $e) {
  38. Db::rollback();
  39. Log::write(implode('-', [
  40. __CLASS__,
  41. __FUNCTION__,
  42. $e->getFile(),
  43. $e->getLine(),
  44. $e->getMessage()
  45. ]));
  46. self::setError($e->getMessage());
  47. return $e->getMessage();
  48. }
  49. }
  50. /**
  51. * @notes 充值回调
  52. * @param $orderSn
  53. * @param array $extra
  54. */
  55. public static function recharge($orderSn, array $extra = [])
  56. {
  57. $order = RechargeOrder::where('sn', $orderSn)->findOrEmpty();
  58. // 增加用户累计充值金额及用户余额
  59. $user = User::findOrEmpty($order->user_id);
  60. $user->total_recharge_amount += $order->order_amount;
  61. $user->user_money += $order->order_amount;
  62. $user->save();
  63. // 记录账户流水
  64. AccountLogLogic::add(
  65. $order->user_id,
  66. AccountLogEnum::UM_INC_RECHARGE,
  67. AccountLogEnum::INC,
  68. $order->order_amount,
  69. $order->sn,
  70. '用户充值'
  71. );
  72. // 更新充值订单状态
  73. $order->transaction_id = $extra['transaction_id'] ?? '';
  74. $order->pay_status = PayEnum::ISPAID;
  75. $order->pay_time = time();
  76. $order->save();
  77. }
  78. /**
  79. * @notes 服务回调
  80. * @param $orderSn
  81. * @param array $extra
  82. */
  83. public static function goods($orderSn, array $extra = [])
  84. {
  85. $order = RechargeOrder::where('sn', $orderSn)->findOrEmpty();
  86. // 更新充值订单状态
  87. $order->transaction_id = $extra['transaction_id'] ?? '';
  88. $order->pay_status = PayEnum::ISPAID;
  89. $order->pay_time = time();
  90. $order->save();
  91. $work = ServiceWork::findOrEmpty($order->work_id);
  92. $work->work_pay_status = WorkEnum::IS_PAY_STATUS;
  93. $orders = \app\common\model\orders\RechargeOrder::where(['work_id'=>$order->work_id])->select()->toArray();
  94. $order_total = 0;
  95. $order_amount = 0;
  96. foreach ($orders as $k=>$v){
  97. $order_total += $v['order_total'];
  98. $order_amount += $v['order_amount'];
  99. }
  100. $work->work_total = $order_total;
  101. $work->work_amount = $order_amount;
  102. $work->work_status = 7;
  103. $work->user_confirm_status = 5;
  104. $work->service_status = 3;
  105. $work->work_pay_status = 1;
  106. $work->finished_time = time();
  107. $work->save();
  108. }
  109. }