| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace addons\YunfastPay\common\models;
- use addons\YunfastPay\common\enums\YunfastPayEnum;
- use addons\YunfastPay\common\enums\YunfastPayTypeEnum;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_yunfast_pay_payment_order}}".
- *
- * @property int $id
- * @property int $mall_id mall_id
- * @property int $store_id store_id
- * @property int $user_id
- * @property int $trade_id
- * @property string $order_no 订单号
- * @property float $amount 支付金额
- * @property string|null $req_data 请求参数
- * @property string|null $res_data 返回参数
- * @property string $status 状态
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class PaymentOrder extends ActiveRecord
- {
- protected static $json_fields = [
- 'req_data', 'res_data'
- ];
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_yunfast_pay_payment_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'user_id', 'trade_id', 'created_at', 'updated_at'], 'integer'],
- [['order_no'], 'required'],
- [['amount'], 'number'],
- [['req_data', 'res_data'], 'string'],
- [['order_no'], 'string', 'max' => 30],
- [['status'], 'string', 'max' => 10],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'user_id' => 'User ID',
- 'trade_id' => 'Trade ID',
- 'order_no' => 'Order No',
- 'amount' => 'Amount',
- 'req_data' => 'Req Data',
- 'res_data' => 'Res Data',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getTrade()
- {
- return $this->hasOne(PaymentTrade::class, ['id' => 'trade_id']);
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getSplitBill()
- {
- return $this->hasOne(SplitbillLog::class, ['payment_id' => 'id'])
- ->where(['status' => 1]);
- }
- /**
- * 订单号获取订单
- *
- * @param integer $mall_id
- * @param string $order_no
- * @return self
- */
- public static function getOrderByNo(int $mall_id, string $order_no)
- {
- return static::find()
- ->where(['mall_id' => $mall_id, 'order_no' => $order_no])
- ->one();
- }
- /**
- * 交易成功
- *
- * @param string $third_trade_no
- * @param integer $code
- * @return boolean
- */
- public function success(string $third_trade_no, $code = '100')
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $payLog = Yii::$app->services->pay->getByTradeNo($this->trade->out_trade_no);
- // 支付完成
- if ($payLog->is_pay == 1) return true;
-
- $payLog->pay_type = YunfastPayTypeEnum::YUNFAST_PAY_NUM;
- $payLog->is_pay = 1;
- $payLog->pay_time = time();
- $payLog->third_trade_no = $third_trade_no;
- $payLog->save();
-
- // 业务回调
- $orders = Yii::$app->services->pay->changePaymentTradeOrder($payLog, YunfastPayTypeEnum::YUNFAST_PAY_NUM);
- Yii::$app->services->pay->notify($orders);
-
- $this->status = $code;
- $this->save();
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::error('银典支付修改支付成功订单失败, 订单ID:' . $this->id);
- return false;
- }
- return true;
- }
- /**
- * 原交易订单号
- *
- * @return string
- */
- public function getOglOrdId()
- {
- $res_data = Json::decode($this->res_data);
- if (empty($res_data['orderCd'])) {
- throw new \Exception(sprintf(
- '订单ID:%s的支付数据不存在', $this->id
- ));
- }
- return $res_data['orderCd'];
- }
- /**
- * 交易失败
- *
- * @param integer $code
- * @return void
- */
- public function fail($code = '102')
- {
- $this->status = $code;
- $this->save();
- }
- /**
- * 通过收银台订单
- *
- * @param string $order_no
- * @return PaymentOrder
- */
- public static function getPaymentByCashierOrderNo(string $order_no)
- {
- return static::find()
- ->where([
- 'trade_id' => PaymentTradeOrder::getPaymentIdByCashierOrderNo($order_no)
- ])
- ->one();
- }
- /**
- * 设置当前订单分账
- *
- * @return bool whether the saving succeeded (i.e. no validation errors occurred).
- */
- public function splitBill()
- {
- $this->is_split_bill = 1;
- return $this->save();
- }
- /**
- * 订单是否分账
- *
- * @return boolean
- */
- public function isSplitBill()
- {
- return $this->is_split_bill
- ? true
- : false;
- }
- /**
- * 通过退款订单ID获取支付订单
- *
- * @param integer $id
- * @param string $order_no
- * @return static|null
- */
- public static function getPaymentByOrderId(int $id, string $order_no = '', string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- if ($order_no = $order_no ?: Order::getOrderNoById($id)) {
- return static::getPaymentByMallOrderNo($order_no, $refund_source);
- }
- return null;
- }
- /**
- * 通过商城订单号查找支付订单
- *
- * @param string $order_no
- * @param string $refund_source
- * @return static|null
- */
- public static function getPaymentByMallOrderNo(string $order_no, string $refund_source = YunfastPayEnum::MALL_ORDER)
- {
- return static::find()
- ->where([
- 'trade_id' => PaymentTradeOrder::getPaymentIdByMallOrderNo($order_no, $refund_source)
- ])
- ->one();
- }
- }
|