/**
  * Finds the OrganisationMembers model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $organisation_id
  * @param integer $user_id
  * @return OrganisationMembers the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($organisation_id, $user_id)
 {
     if (($model = OrganisationMembers::findOne(['organisation_id' => $organisation_id, 'user_id' => $user_id])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
    /**
     * Displays a single Organisation model.
     * @param string $id
     * @return mixed
     */
    public function actionConnect($code, $state)
    {
        $sql = "SELECT * FROM organisation WHERE SHA1(CONCAT(`id`, :salt, `name`)) = :state";
        $organisation = Organisation::findBySql($sql, [':salt' => 'jiejieugs9837', ':state' => $state])->one();
        if (\Yii::$app->user->isGuest) {
            return $this->redirect('/site/denied/');
        }
        $user = \Yii::$app->user->identity;
        if (!$user->admin && count($user->organisations) == 0) {
            return $this->redirect('/site/denied/');
        } else {
            if (!$user->admin) {
                $organisations = $user->organisations;
                foreach ($organisations as $org) {
                    if ($organisation->id == $org->id) {
                        break 2;
                    }
                }
                return $this->redirect('/site/denied');
            }
        }
        if (!empty($code)) {
            $token_request_body = array('grant_type' => 'authorization_code', 'client_id' => Yii::$app->params['stripeClientID'], 'code' => $code, 'client_secret' => Yii::$app->params['stripeSecretKey']);
            $req = curl_init('https://connect.stripe.com/oauth/token');
            curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($req, CURLOPT_POST, true);
            curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
            curl_setopt($req, CURLOPT_SSL_VERIFYPEER, true);
            //curl_setopt($req, CURLOPT_CAINFO, '/home/web/tickets/cacert.pem');
            $respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
            $resp = json_decode(curl_exec($req), true);
            curl_close($req);
            $organisation = $this->findModel($organisation->id);
            $organisation->stripe_access_token = $resp['access_token'];
            $organisation->stripe_public_key = $resp['stripe_publishable_key'];
            $organisation->stripe_user_id = $resp['stripe_user_id'];
            $organisation->stripe_refresh_token = $resp['refresh_token'];
            $organisation->save();
            $organisation_test = Organisation::findOne($organisation->id);
            $result = $organisation_test->stripe_user_id ? "successful" : "unsuccessful";
            $founder = User::findOne(OrganisationMembers::findOne(['organisation_id' => $organisation->id, 'founder' => 1])->user_id);
            $email = new Email();
            $email->to_name = $founder->name;
            $email->to_email = $founder->email;
            $email->subject = "Authorisation Attempt";
            $email->body = <<<EOT
You tried to connect {$organisation->name} to Tixty. That was {$result}.

Tixty
EOT;
            $email->save();
            $email->send();
            $response = print_r($resp, true);
            $email = new Email();
            $email->to_name = $email->sender_name;
            $email->to_email = $email->sender_email;
            $email->subject = "Authorisation Attempt for {$organisation->name} {$result}";
            $email->body = <<<EOT
<pre>{$response}</pre>
EOT;
            $email->save();
            $email->send();
        }
        return $this->redirect('/organisation/view', ['id' => $organisation->id]);
    }
示例#3
0
 public function getMemberships()
 {
     return $this->hasMany(OrganisationMembers::className(), ['user_id' => 'id']);
 }