PaymentTrade.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\api\model;
  13. use app\common\model\PaymentTrade as PaymentTradeModel;
  14. use app\common\enum\payment\trade\TradeStatus as TradeStatusEnum;
  15. use cores\exception\BaseException;
  16. /**
  17. * 模型类:第三方支付交易记录
  18. * Class PaymentTrade
  19. * @package app\api\model
  20. */
  21. class PaymentTrade extends PaymentTradeModel
  22. {
  23. /**
  24. * 隐藏字段
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'store_id',
  29. 'create_time',
  30. 'update_time'
  31. ];
  32. /**
  33. * 新增第三方交易信息
  34. * @param mixed $orderInfo 订单信息
  35. * @param string $method 支付方式
  36. * @param string $client 指定的客户端
  37. * @param int $orderType 订单类型
  38. * @param array $payment 第三方支付数据
  39. * @return bool
  40. * @throws \cores\exception\BaseException
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public static function record($orderInfo, string $method, string $client, int $orderType, array $payment): bool
  46. {
  47. // 实例化模型
  48. $model = new static;
  49. // 查询是否存在交易记录
  50. $record = $model->detailByOrderId($orderInfo['order_id'], $method, $client, $orderType);
  51. // 新增或者更新记录
  52. return ($record ?: $model)->save([
  53. 'client' => $client,
  54. 'pay_method' => $method,
  55. 'order_type' => $orderType,
  56. 'order_id' => $orderInfo['order_id'],
  57. 'order_no' => $orderInfo['order_no'],
  58. 'user_id' => $orderInfo['user_id'],
  59. 'out_trade_no' => $payment['out_trade_no'] ?? '',
  60. 'prepay_id' => $payment['prepay_id'] ?? '',
  61. 'trade_status' => TradeStatusEnum::UNPAID,
  62. 'store_id' => self::$storeId,
  63. ]);
  64. }
  65. /**
  66. * 根据订单ID查询记录
  67. * @param int $orderId 订单ID
  68. * @param int $orderType 订单类型
  69. * @return static|array|\think\Model|null
  70. * @throws BaseException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. private function detailByOrderId(int $orderId, string $method, string $client, int $orderType)
  76. {
  77. $detail = $this->where('order_id', '=', $orderId)
  78. ->where('order_type', '=', $orderType)
  79. ->where('client', '=', $client)
  80. ->where('pay_method', '=', $method)
  81. ->find();
  82. if (empty($detail)) {
  83. return null;
  84. }
  85. if (\in_array($detail['trade_state'], [TradeStatusEnum::SUCCESS, TradeStatusEnum::REFUND])) {
  86. throwError('该支付订单已完成交易');
  87. }
  88. return $detail;
  89. }
  90. }