OrderPayOffline.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * This is the model class for table "{{%order_pay_offline}}".
  13. *
  14. * @property integer $id
  15. */
  16. class OrderPayOffline extends \yii\db\ActiveRecord
  17. {
  18. const PAY_TYPE_OFFLINE_WECHAT = 1;
  19. const PAY_TYPE_OFFLINE_ALIPAY = 2;
  20. const PAY_TYPE_OFFLINE_BANK = 3;
  21. public static $type = [
  22. self::PAY_TYPE_OFFLINE_WECHAT => '微信',
  23. self::PAY_TYPE_OFFLINE_ALIPAY => '支付宝',
  24. self::PAY_TYPE_OFFLINE_BANK => '银行卡',
  25. ];
  26. /**
  27. * @inheritdoc
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%order_pay_offline}}';
  32. }
  33. public function behaviors()
  34. {
  35. return [
  36. [
  37. 'class' => TimestampBehavior::class,
  38. ]
  39. ];
  40. }
  41. public static function addPay($store_id, $user_id, $order_id, $type, $desc, $pic_list = []) {
  42. try{
  43. if(!in_array($type, array_keys(self::$type))){
  44. throw new \Exception('操作失败,渠道类型不符');
  45. }
  46. $order = Order::findOne($order_id);
  47. if($order->is_pay == 1){
  48. throw new \Exception('操作失败,订单状态不符');
  49. }
  50. if($order->user_id != $user_id){
  51. throw new \Exception('操作失败,用户信息不符');
  52. }
  53. $model = self::find()->where(['store_id' => $store_id, 'order_id' => $order_id, 'is_delete' => 0])->orderBy('id DESC')->one();
  54. if($model && $model->status != 2){
  55. throw new \Exception('操作失败,支付状态不符');
  56. }
  57. if(!$model){
  58. $model = new self();
  59. $model->store_id = $store_id;
  60. $model->order_id = $order_id;
  61. }
  62. $model->add_at = time();
  63. $model->status = 0;
  64. $model->audit_at = 0;
  65. $model->admin_remark = '';
  66. $model->type = $type;
  67. $model->desc = $desc;
  68. $model->pic_list = json_encode($pic_list);
  69. if(!$model->save()){
  70. throw new \Exception('操作失败,' . array_shift($model->getFirstErrors()));
  71. }
  72. } catch (\Exception $ex) {
  73. \Yii::error($ex);
  74. return [
  75. 'code' => 1,
  76. 'msg' => $ex->getMessage(),
  77. ];
  78. }
  79. return [
  80. 'code' => 0,
  81. 'msg' => '操作成功',
  82. 'data' => $model,
  83. ];
  84. }
  85. public static function auditPay($store_id, $id, $status, $admin_remark = '') {
  86. $t = \Yii::$app->db->beginTransaction();
  87. try{
  88. $model = self::findOne($id);
  89. if($model->store_id != $store_id){
  90. throw new \Exception('操作失败,店铺信息错误');
  91. }
  92. if($model->status == 1){
  93. throw new \Exception('操作失败,订单状态不符');
  94. }
  95. $model->status = $status;
  96. $model->audit_at = time();
  97. if($admin_remark){
  98. $model->admin_remark = $admin_remark;
  99. }
  100. if(!$model->save()){
  101. throw new \Exception('操作失败,' . array_shift($model->getFirstErrors()));
  102. }
  103. if($status == 1){
  104. $order = Order::findOne($model->order_id);
  105. $order->pay_time = time();
  106. $order->is_pay = 1;
  107. $order->pay_type = Order::PAY_TYPE_OFFLINE;
  108. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  109. if (!$order->save()) {
  110. $t->rollBack();
  111. return [
  112. 'code' => 1,
  113. 'msg' => '订单保存失败'
  114. ];
  115. }
  116. (new \app\modules\client\models\v1\order\OrderPayDataForm())->setReturnData($order);
  117. (new \app\modules\common\models\NotifyForm())->videoGoodsShare($order);
  118. // 支付完成后,相关操作
  119. $form = new \app\modules\client\models\OrderComplete();
  120. $form->order_id = $order->id;
  121. $form->order_type = 0;
  122. $form->store_id = $model->store_id;
  123. $form->notify();
  124. }
  125. $t->commit();
  126. } catch (\Exception $ex) {
  127. $t->rollBack();
  128. \Yii::error($ex);
  129. return [
  130. 'code' => 1,
  131. 'msg' => $ex->getMessage(),
  132. ];
  133. }
  134. return [
  135. 'code' => 0,
  136. 'msg' => '操作成功',
  137. 'data' => $model,
  138. ];
  139. }
  140. public static function delPay($store_id, $id) {
  141. $ids = explode(',', $id);
  142. if($ids){
  143. self::updateAll(['is_delete' => 1], [
  144. 'store_id' => $store_id,
  145. 'id' => $ids,
  146. ]);
  147. }
  148. }
  149. public static function getPayByOrderId($store_id, $order_id) {
  150. $model = self::find()->where(['store_id' => $store_id, 'order_id' => $order_id, 'is_delete' => 0])->orderBy('id DESC')->one();
  151. return $model;
  152. }
  153. }