PaymentOrder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace addons\YunfastPay\common\models;
  3. use addons\YunfastPay\common\enums\YunfastPayEnum;
  4. use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
  5. use Exception;
  6. use Yii;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "{{%addons_yunfast_pay_payment_order}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id mall_id
  13. * @property int $store_id store_id
  14. * @property int $user_id
  15. * @property int $trade_id
  16. * @property string $order_no 订单号
  17. * @property float $amount 支付金额
  18. * @property string|null $req_data 请求参数
  19. * @property string|null $res_data 返回参数
  20. * @property string $status 状态
  21. * @property int $created_at 创建时间
  22. * @property int $updated_at 修改时间
  23. */
  24. class PaymentOrder extends ActiveRecord
  25. {
  26. protected static $json_fields = [
  27. 'req_data', 'res_data'
  28. ];
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%addons_yunfast_pay_payment_order}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'store_id', 'user_id', 'trade_id', 'created_at', 'updated_at'], 'integer'],
  43. [['order_no'], 'required'],
  44. [['amount'], 'number'],
  45. [['req_data', 'res_data'], 'string'],
  46. [['order_no'], 'string', 'max' => 30],
  47. [['status'], 'string', 'max' => 10],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'mall_id' => 'Mall ID',
  58. 'store_id' => 'Store ID',
  59. 'user_id' => 'User ID',
  60. 'trade_id' => 'Trade ID',
  61. 'order_no' => 'Order No',
  62. 'amount' => 'Amount',
  63. 'req_data' => 'Req Data',
  64. 'res_data' => 'Res Data',
  65. 'status' => 'Status',
  66. 'created_at' => 'Created At',
  67. 'updated_at' => 'Updated At',
  68. ];
  69. }
  70. /**
  71. * @return ActiveQueryInterface the relational query object.
  72. */
  73. public function getTrade()
  74. {
  75. return $this->hasOne(PaymentTrade::class, ['id' => 'trade_id']);
  76. }
  77. /**
  78. * @return ActiveQueryInterface the relational query object.
  79. */
  80. public function getUser()
  81. {
  82. return $this->hasOne(User::class, ['id' => 'user_id']);
  83. }
  84. /**
  85. * @return ActiveQueryInterface the relational query object.
  86. */
  87. public function getSplitBill()
  88. {
  89. return $this->hasOne(SplitbillLog::class, ['payment_id' => 'id'])
  90. ->where(['status' => 1]);
  91. }
  92. /**
  93. * 订单号获取订单
  94. *
  95. * @param integer $mall_id
  96. * @param string $order_no
  97. * @return self
  98. */
  99. public static function getOrderByNo(int $mall_id, string $order_no)
  100. {
  101. return static::find()
  102. ->where(['mall_id' => $mall_id, 'order_no' => $order_no])
  103. ->one();
  104. }
  105. /**
  106. * 交易成功
  107. *
  108. * @param string $third_trade_no
  109. * @param integer $code
  110. * @return boolean
  111. */
  112. public function success(string $third_trade_no, $code = '100')
  113. {
  114. $transaction = Yii::$app->db->beginTransaction();
  115. try {
  116. $payLog = Yii::$app->services->pay->getByTradeNo($this->trade->out_trade_no);
  117. // 支付完成
  118. if ($payLog->is_pay == 1) return true;
  119. $payLog->pay_type = YunfastPayTypeEnum::YUNFAST_PAY_NUM;
  120. $payLog->is_pay = 1;
  121. $payLog->pay_time = time();
  122. $payLog->third_trade_no = $third_trade_no;
  123. $payLog->save();
  124. // 业务回调
  125. $orders = Yii::$app->services->pay->changePaymentTradeOrder($payLog, YunfastPayTypeEnum::YUNFAST_PAY_NUM);
  126. Yii::$app->services->pay->notify($orders);
  127. $this->status = $code;
  128. $this->save();
  129. $transaction->commit();
  130. } catch (\Exception $e) {
  131. $transaction->rollBack();
  132. Yii::error('银典支付修改支付成功订单失败, 订单ID:' . $this->id);
  133. return false;
  134. }
  135. return true;
  136. }
  137. /**
  138. * 原交易订单号
  139. *
  140. * @return string
  141. */
  142. public function getOglOrdId()
  143. {
  144. $res_data = Json::decode($this->res_data);
  145. if (empty($res_data['orderCd'])) {
  146. throw new \Exception(sprintf(
  147. '订单ID:%s的支付数据不存在', $this->id
  148. ));
  149. }
  150. return $res_data['orderCd'];
  151. }
  152. /**
  153. * 交易失败
  154. *
  155. * @param integer $code
  156. * @return void
  157. */
  158. public function fail($code = '102')
  159. {
  160. $this->status = $code;
  161. $this->save();
  162. }
  163. /**
  164. * 通过收银台订单
  165. *
  166. * @param string $order_no
  167. * @return PaymentOrder
  168. */
  169. public static function getPaymentByCashierOrderNo(string $order_no)
  170. {
  171. return static::find()
  172. ->where([
  173. 'trade_id' => PaymentTradeOrder::getPaymentIdByCashierOrderNo($order_no)
  174. ])
  175. ->one();
  176. }
  177. /**
  178. * 设置当前订单分账
  179. *
  180. * @return bool whether the saving succeeded (i.e. no validation errors occurred).
  181. */
  182. public function splitBill()
  183. {
  184. $this->is_split_bill = 1;
  185. return $this->save();
  186. }
  187. /**
  188. * 订单是否分账
  189. *
  190. * @return boolean
  191. */
  192. public function isSplitBill()
  193. {
  194. return $this->is_split_bill
  195. ? true
  196. : false;
  197. }
  198. /**
  199. * 通过退款订单ID获取支付订单
  200. *
  201. * @param integer $id
  202. * @param string $order_no
  203. * @return static|null
  204. */
  205. public static function getPaymentByOrderId(int $id, string $order_no = '', string $refund_source = YunfastPayEnum::MALL_ORDER)
  206. {
  207. if ($order_no = $order_no ?: Order::getOrderNoById($id)) {
  208. return static::getPaymentByMallOrderNo($order_no, $refund_source);
  209. }
  210. return null;
  211. }
  212. /**
  213. * 通过商城订单号查找支付订单
  214. *
  215. * @param string $order_no
  216. * @param string $refund_source
  217. * @return static|null
  218. */
  219. public static function getPaymentByMallOrderNo(string $order_no, string $refund_source = YunfastPayEnum::MALL_ORDER)
  220. {
  221. return static::find()
  222. ->where([
  223. 'trade_id' => PaymentTradeOrder::getPaymentIdByMallOrderNo($order_no, $refund_source)
  224. ])
  225. ->one();
  226. }
  227. }