/** * 记录支付记录 * @param $userId * @param $scheme \common\models\Scheme * @throws Exception */ public static function saveRecord($userId, $scheme) { $record = new PracticeRecord(); $record->userId = $userId; $record->bitcoin = $scheme['payBitcoin']; $record->startDate = DateFunctions::getCurrentDate(); $record->endDate = date('y-m-d H:i:s', strtotime('+' . $scheme['hour'] . ' hour')); if (!$record->save()) { throw new Exception("PracticeRecord save error"); } }
public function beforeAction($action) { $session = Yii::$app->session; $user = $session->get('user'); if (!TestLibrary::checkIsExist($user)) { $url = Url::to(['site/test-library-not-found']); header("Location:{$url}"); } $practiceRecordFlag = $session->getFlash('practiceRecordFlag'); if ($practiceRecordFlag) { //支付方案如果已经生成直接显示过去 return parent::beforeAction($action); } $practiceRecord = PracticeRecord::findByUser($user['userId']); if (!$practiceRecord) { //如果没有练习权 //获取在线练习支付方案 /** @var $scheme \common\models\Scheme */ $schemes = Scheme::findPracticeScheme(); $session->setFlash('practice-schemes', $schemes); //存入session,在练习首页使用 $session->setFlash('practiceRecordFlag', true); //支付方案生成的标志 $url = Url::to(['practice/index', true]); header("Location:{$url}"); return false; } return parent::beforeAction($action); }
/** 在线练习支付 */ public function actionPay() { $request = Yii::$app->request; if ($request->isAjax) { $session = Yii::$app->session; $user = $session->get('user'); //先查询该用户是否有练习记录,防止重复付款 $practiceRecord = PracticeRecord::findByUser($user['userId']); if ($practiceRecord) { return 'success您已经可以在线练习,不需要重新付款,如果重复出现付款页面请刷新该页面。'; } $schemeId = $request->post('schemeId'); /** @var $scheme \common\models\Scheme */ $scheme = Scheme::findOne($schemeId); if (!$scheme) { return '该方案已停用'; } $leftBitcoin = Users::findBitcoin($user['userId']); if ($leftBitcoin < $scheme['payBitcoin']) { return '您的云豆余额不足'; } IncomeConsume::saveRecord($user['userId'], $scheme['payBitcoin'], Scheme::USAGE_PRACTICE, IncomeConsume::TYPE_CONSUME); PracticeRecord::saveRecord($user['userId'], $scheme); return 'success支付成功'; } throw new Exception("非法支付"); }