[ 'class' => VerbFilter::className(), 'actions' => [ 'stripe-verification' => ['post'], ], ] ]; } public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } public function beforeAction($action) { $this->enableCsrfValidation = false; return parent::beforeAction($action); } /** * Affiche l'historique du crédit client. * */ public function actionHistory($returnPayment = '') { $producer = $this->getProducer(); if(\Yii::$app->user->isGuest) { return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } $searchModel = new CreditHistorySearch(); $searchModel->id_user = User::getCurrentId(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $userProducer = UserProducer::searchOne([ 'id_producer' => $producer->id, 'id_user' => User::getCurrentId() ]); if (strlen($returnPayment)) { if ($returnPayment == 'success') { Yii::$app->getSession()->setFlash('success', "Paiement accepté : votre compte vient d'être crédité."); } if ($returnPayment == 'cancel') { Yii::$app->getSession()->setFlash('error', 'Paiement annulé.'); } } return $this->render('history', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'creditUser' => $userProducer->credit ]); } public function actionAdd() { $producer = $this->getProducer(); if(\Yii::$app->user->isGuest) { return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } if ($producer->online_payment || $producer->option_stripe_mode_test) { $creditForm = new CreditForm; if ($creditForm->load(Yii::$app->request->post()) && $creditForm->validate()) { $user = User::getCurrent(); \Stripe\Stripe::setApiKey( $producer->getPrivateKeyApiStripe() ); $checkout_session = \Stripe\Checkout\Session::create([ 'line_items' => [ [ 'price_data' => [ 'currency' => 'eur', 'product_data' => [ 'name' => 'Alimentation crédit', ], 'unit_amount' => (float)$creditForm->amount * 100, ], 'quantity' => 1, ] ], 'payment_method_types' => ['card'], 'mode' => 'payment', 'customer_email' => $user->email, 'client_reference_id' => $user->id, 'payment_intent_data' => [ 'metadata' => [ 'user_id' => $user->id, 'producer_id' => $producer->id ], ], 'success_url' => \Yii::$app->urlManagerProducer->createAbsoluteUrl( [ 'credit/history', 'returnPayment' => 'success' ] ), 'cancel_url' => \Yii::$app->urlManagerProducer->createAbsoluteUrl( [ 'credit/history', 'returnPayment' => 'cancel' ] ), ]); header("HTTP/1.1 303 See Other"); header("Location: " . $checkout_session->url); die(); } return $this->render('add', [ 'creditForm' => $creditForm ]); } else { throw new \yii\base\UserException('Cette option est désactivée chez ce producteur.'); } } public function actionStripeVerification() { $producer = $this->getProducer(); $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; $event = null; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $producer->getPrivateKeyEndpointStripe() ); } catch (\UnexpectedValueException $e) { // Invalid payload http_response_code(400); exit(); } catch (\Stripe\Exception\SignatureVerificationException $e) { // Invalid signature http_response_code(400); exit(); } // Handle the event switch ($event->type) { case 'charge.succeeded': $paymentIntent = $event->data->object; $paymentIntentMetadata = $paymentIntent->metadata; $amount = $paymentIntent->amount / 100; $idUser = $paymentIntentMetadata->user_id; $idProducer = $paymentIntentMetadata->producer_id; // on crédite le crédit du client $creditHistory = new CreditHistory; $creditHistory->id_user = $idUser; $creditHistory->id_user_action = $idUser; $creditHistory->id_producer = $idProducer; $creditHistory->type = CreditHistory::TYPE_CREDIT; $creditHistory->comment = null; $creditHistory->amount = $amount; $creditHistory->mean_payment = MeanPayment::CREDIT_CARD; $creditHistory->save(); // envoi d'un email de confirmation $user = User::findOne($paymentIntentMetadata->user_id); $userProducer = UserProducer::find() ->where([ 'id_user' => $idUser, 'id_producer' => $idProducer ]) ->one(); $paramsEmail = [ 'from_email' => $producer->getEmailOpendistrib(), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $user->getUsername(), 'subject' => '['.$producer->name.'] Alimentation de votre crédit', 'content_view_text' => '@common/mail/creditConfirm-text.php', 'content_view_html' => '@common/mail/creditConfirm-html.php', 'content_params' => [ 'user' => $user, 'userProducer' => $userProducer, 'producer' => $producer, 'amount' => $amount, ] ] ; Mailjet::sendMail($paramsEmail); break; // ... handle other event types default: echo 'Received unknown event type ' . $event->type; } http_response_code(200); die(); } }