[ '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(string $returnPayment = '') { $producer = $this->getProducerCurrent(); if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); } $searchModel = new CreditHistorySearch(); $searchModel->id_user = GlobalParam::getCurrentUserId(); $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); $userProducer = $this->getUserProducerManager()->findOneUserProducer($this->getUserCurrent()); if (strlen($returnPayment)) { if ($returnPayment == 'success') { $this->setFlash('success', "Paiement accepté : votre compte vient d'être crédité."); } if ($returnPayment == 'cancel') { $this->setFlash('error', 'Paiement annulé.'); } } return $this->render('history', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'creditUser' => $userProducer->credit ]); } public function actionAdd() { $producer = $this->getProducerCurrent(); if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->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 = GlobalParam::getCurrentUser(); \Stripe\Stripe::setApiKey( $this->getProducerManager()->getPrivateKeyApiStripe($producer) ); $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' => $this->getUrlManagerProducer()->createAbsoluteUrl( [ 'credit/history', 'returnPayment' => 'success' ] ), 'cancel_url' => $this->getUrlManagerProducer()->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() { $creditHistoryManager = $this->getCreditHistoryManager(); $producerManager = $this->getProducerManager(); $userManager = $this->getUserManager(); $producer = $this->getProducerCurrent(); $contactProducer = $producerManager->getMainContact($producer); $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $producerManager->getPrivateKeyEndpointStripe($producer) ); } 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 = $userManager->findOneUserById($paymentIntentMetadata->user_id); $idProducer = $paymentIntentMetadata->producer_id; if (isset($paymentIntentMetadata->order_id)) { $order = $this->getOrderManager()->findOneOrderById($paymentIntentMetadata->order_id); $pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale); $distribution = $this->getDistributionManager()->findOneDistributionById($order->id_distribution); } // Handle the event switch ($event->type) { case 'charge.succeeded': $creditHistoryExist = CreditHistory::searchOne([ 'id_user' => $idUser, 'amount' => $amount, ], [ 'conditions' => [ 'date > DATE_SUB(NOW(), INTERVAL 1 MINUTE)' ] ]); if (!$creditHistoryExist) { $creditHistoryManager->createCreditHistory( CreditHistory::TYPE_CREDIT, $amount, $producer, $user, $user, MeanPayment::CREDIT_CARD ); if (isset($order) && $order) { $creditHistoryManager->createCreditHistory( CreditHistory::TYPE_PAYMENT, $amount, $producer, $user, $user, MeanPayment::CREDIT, $order ); // client : envoi d'un email de confirmation de paiement $paramsEmail = [ 'from_email' => $producerManager->getEmailOpendistrib($producer), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $userManager->getUsername($user), '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' => $producerManager->getEmailOpendistrib($producer), '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 { $userProducer = $this->getUserProducerManager()->findOneUserProducer($user); Mailjet::sendMail([ 'from_email' => $producerManager->getEmailOpendistrib($producer), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $userManager->getUsername($user), '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' => $producerManager->getEmailOpendistrib($producer), 'from_name' => $producer->name, 'to_email' => $user->email, 'to_name' => $userManager->getUsername($user), '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' => $producerManager->getEmailOpendistrib($producer), '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(); } }