[ '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(); $contactProducer = $producer->getMainContact(); $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(); } $paymentIntent = $event->data->object; $paymentIntentMetadata = $paymentIntent->metadata; $amount = $paymentIntent->amount / 100; $idUser = $paymentIntentMetadata->user_id; $user = User::findOne($paymentIntentMetadata->user_id); $idProducer = $paymentIntentMetadata->producer_id; if(isset($paymentIntentMetadata->order_id)) { $order = Order::searchOne([ 'id' => $paymentIntentMetadata->order_id ]); $pointSale = PointSale::searchOne(['id' => $order->id_point_sale]); $distribution = Distribution::find()->where(['id' => $order->id_distribution])->one(); } // Handle the event switch ($event->type) { case 'charge.succeeded': // 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(); if(isset($order) && $order) { // paiement de la commande $order->saveCreditHistory( CreditHistory::TYPE_PAYMENT, $amount, $idProducer, $order->id_user, $order->id_user ); // client : envoi d'un email de confirmation de paiement /*$paramsEmail = [ 'from_email' => $producer->getEmailOpendistrib(), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $user->getUsername(), 'subject' => '['.$producer->name.'] Confirmation de paiement', 'content_view_text' => '@common/mail/paymentOrderConfirm-text.php', 'content_view_html' => '@common/mail/paymentOrderConfirm-html.php', 'content_params' => [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ] ] ; Mailjet::sendMail($paramsEmail);*/ // producteur : mail de confirmation Mailjet::sendMail([ 'from_email' => $producer->getEmailOpendistrib(), 'from_name' => $producer->name, 'to_email' => $contactProducer->email, 'to_name' => $contactProducer->name, 'subject' => '[' . $producer->name . '] Confirmation de commande', 'content_view_text' => '@common/mail/orderConfirmProducer-text.php', 'content_view_html' => '@common/mail/orderConfirmProducer-html.php', 'content_params' => [ 'order' => $order, 'pointSale' => $pointSale, 'distribution' => $distribution, 'user' => $user, 'producer' => $producer ] ]); } else { // envoi d'un email de confirmation $userProducer = UserProducer::find() ->where([ 'id_user' => $idUser, 'id_producer' => $idProducer ]) ->one(); Mailjet::sendMail([ '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, ] ]); } break; case 'charge.failed': // client Mailjet::sendMail([ 'from_email' => $producer->getEmailOpendistrib(), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $user->getUsername(), 'subject' => '['.$producer->name.'] Erreur de paiement', 'content_view_text' => '@common/mail/paymentError-text.php', 'content_view_html' => '@common/mail/paymentError-html.php', 'content_params' => [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ] ]); // producteur if(isset($order) && $order) { Mailjet::sendMail([ 'from_email' => $producer->getEmailOpendistrib(), 'from_name' => $producer->name, 'to_email' => $contactProducer->email, 'to_name' => $contactProducer->name, 'subject' => '[' . $producer->name . '] Erreur de paiement', 'content_view_text' => '@common/mail/paymentErrorProducer-text.php', 'content_view_html' => '@common/mail/paymentErrorProducer-html.php', 'content_params' => [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, 'order' => $order, 'distribution' => $distribution ] ]); } break; // handle other event types default: echo 'Received unknown event type ' . $event->type; } http_response_code(200); die(); } }