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(); } }