PayNotifyLogic.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\model\recharge\RechargeOrder;
  18. use app\common\model\user\User;
  19. use think\facade\Db;
  20. use think\facade\Log;
  21. /**
  22. * 支付成功后处理订单状态
  23. * Class PayNotifyLogic
  24. * @package app\api\logic
  25. */
  26. class PayNotifyLogic extends BaseLogic
  27. {
  28. public static function handle($action, $orderSn, $extra = [])
  29. {
  30. Db::startTrans();
  31. try {
  32. self::$action($orderSn, $extra);
  33. Db::commit();
  34. return true;
  35. } catch (\Exception $e) {
  36. Db::rollback();
  37. Log::write(implode('-', [
  38. __CLASS__,
  39. __FUNCTION__,
  40. $e->getFile(),
  41. $e->getLine(),
  42. $e->getMessage()
  43. ]));
  44. self::setError($e->getMessage());
  45. return $e->getMessage();
  46. }
  47. }
  48. /**
  49. * @notes 充值回调
  50. * @param $orderSn
  51. * @param array $extra
  52. * @author 段誉
  53. * @date 2023/2/27 15:28
  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. }