PayOrderModel.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 支付订单
  7. */
  8. class PayOrderModel extends Model
  9. {
  10. // 表名
  11. protected $name = 'pay_order';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. // 定义时间戳字段名
  15. protected $createTime = false;
  16. protected $updateTime = false;
  17. protected $deleteTime = false;
  18. /**
  19. * 余额充值支付回调
  20. * @param $out_trade_no
  21. * @return array
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * @throws \think\exception\PDOException
  27. */
  28. public static function recharge($out_trade_no)
  29. {
  30. Db::startTrans();
  31. if (!$order = self::where('out_trade_no',$out_trade_no)->lock(true)->find()) {
  32. Db::rollback();
  33. return [false,'未找到订单'];
  34. }
  35. if ($order['order_status'] != 0) {
  36. Db::rollback();
  37. return [true,'订单已支付'];
  38. }
  39. //更新订单状态
  40. if (!self::where(['out_trade_no' => $out_trade_no])->update(['order_status' => 1, 'notifytime' => time()])) {
  41. Db::rollback();
  42. return [false,'订单状态更新失败'];
  43. }
  44. //钱包更新
  45. $walletService = new Wallet();
  46. if (!$walletService->change($order['user_id'], $order['order_amount'], 'money', 10, '充值', 'pay_order', $order['table_id'])) {
  47. Db::rollback();
  48. return [false,'余额充值失败'];
  49. }
  50. Db::commit();
  51. return [true,'操作成功'];
  52. }
  53. /**
  54. * 老年大学活动支付回调
  55. * @param $out_trade_no
  56. * @return array
  57. */
  58. public static function university_event($out_trade_no)
  59. {
  60. Db::startTrans();
  61. if (!$info = self::where('out_trade_no',$out_trade_no)->lock(true)->find()){
  62. Db::rollback();
  63. return [false,'未找到订单'];
  64. }
  65. if ($info['order_status'] == 1){
  66. Db::rollback();
  67. return [true,'订单已支付'];
  68. }
  69. //更新订单状态
  70. if (!self::where(['id' => $info['id']])->update(['status' => 1])) {
  71. Db::rollback();
  72. return [false,'订单状态更新失败'];
  73. }
  74. if (!UniversityEventApplyModel::where('id',$info['table_id'])->update(['status' => 1,'pay_time' => time()])) {
  75. Db::rollback();
  76. return [false,'活动订单状态更新失败'];
  77. }
  78. Db::commit();
  79. return [true,'操作成功'];
  80. }
  81. }