PaymentTrade.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use cores\exception\BaseException;
  15. use app\common\enum\payment\trade\TradeStatus as TradeStatusEnum;
  16. /**
  17. * 模型类:第三方支付交易记录
  18. * Class PaymentTrade
  19. * @package app\common\model
  20. */
  21. class PaymentTrade extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'payment_trade';
  25. // 定义主键
  26. protected $pk = 'trade_id';
  27. /**
  28. * 交易记录详情
  29. * @param $where
  30. * @return static|array|null
  31. */
  32. public static function detail($where)
  33. {
  34. return self::get($where);
  35. }
  36. /**
  37. * 查询第三方支付交易记录详情
  38. * @param string $outTradeNo 交易订单号
  39. * @return static|array|null
  40. * @throws BaseException
  41. */
  42. public static function detailByOutTradeNo(string $outTradeNo)
  43. {
  44. $detail = static::detail(['out_trade_no' => $outTradeNo]);
  45. if (empty($detail)) {
  46. throwError('第三方支付交易记录不存在');
  47. }
  48. return $detail;
  49. }
  50. /**
  51. * 将第三方交易记录更新为已支付状态
  52. * @param int $tradeId 交易记录ID
  53. * @param string $tradeNo 第三方交易流水号
  54. * @return bool
  55. */
  56. public static function updateToPaySuccess(int $tradeId, string $tradeNo): bool
  57. {
  58. return static::updateBase([
  59. 'trade_no' => $tradeNo,
  60. 'trade_state' => TradeStatusEnum::SUCCESS
  61. ], $tradeId);
  62. }
  63. /**
  64. * 将第三方交易记录更新为已退款状态
  65. * @param int $tradeId 交易记录ID
  66. * @return bool
  67. */
  68. public static function updateToRefund(int $tradeId): bool
  69. {
  70. return static::updateBase(['trade_state' => TradeStatusEnum::REFUND], $tradeId);
  71. }
  72. /**
  73. * 根据商户订单号查询记录
  74. * @param int $orderId 订单ID
  75. * @param int $orderType 订单类型
  76. * @return \app\api\model\PaymentTrade|array|\think\Model|null
  77. * @throws BaseException
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. protected function detailByOrderId(int $orderId, string $method, string $client, int $orderType)
  83. {
  84. $detail = $this->where('order_id', '=', $orderId)
  85. ->where('order_type', '=', $orderType)
  86. ->where('client', '=', $client)
  87. ->where('pay_method', '=', $method)
  88. ->find();
  89. if (empty($detail)) {
  90. return null;
  91. }
  92. if (in_array($detail['trade_state'], [TradeStatusEnum::SUCCESS, TradeStatusEnum::REFUND])) {
  93. throwError('商户订单号 out_trade_no 已存在');
  94. }
  95. return $detail;
  96. }
  97. }