[ 'class' => VerbFilter::class, '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 = '') { if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); } $userModule = $this->getUserModule(); $paymentModule = $this->getPaymentModule(); $userCurrent = $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', [ 'dataProvider' => $paymentModule->getRepository() ->queryPaymentsCreditHistoryByUser($userCurrent)->getDataProvider(20), 'creditUser' => $userModule->getCredit($userCurrent) ]); } public function actionAdd() { $featureChecker = $this->getFeatureModule()->getChecker(); $producer = $this->getProducerCurrent(); if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); } if ($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT) && ($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->getProducerModule()->getPrivateKeyApiStripe($producer) ); $checkout_session = \Stripe\Checkout\Session::create([ 'line_items' => [ [ 'price_data' => [ 'currency' => 'eur', 'product_data' => [ 'name' => 'Alimentation cagnotte', ], '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() { $orderModule = $this->getOrderModule(); $paymentManager = $this->getPaymentModule(); $producerModule = $this->getProducerModule(); $userModule = $this->getUserModule(); $featureChecker = $this->getFeatureModule()->getChecker(); if ($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)) { $producer = $this->getProducerCurrent(); $contactProducer = $producerModule->getMainContact($producer); $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $producerModule->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; if ($idUser) { $user = $userModule->findOneUserById($idUser); if (isset($paymentIntentMetadata->order_id)) { $order = $this->getOrderModule()->findOneOrderById($paymentIntentMetadata->order_id); $orderModule->initOrder($order); $pointSale = $this->getPointSaleModule()->findOnePointSaleById($order->id_point_sale); $distribution = $this->getDistributionModule()->findOneDistributionById($order->id_distribution); } // Handle the event switch ($event->type) { case 'charge.succeeded': $paymentExist = Payment::searchOne([ 'id_user' => $idUser, 'amount' => $amount, ], [ 'conditions' => [ 'date > DATE_SUB(NOW(), INTERVAL 1 MINUTE)' ] ]); if (!$paymentExist) { if (isset($order) && $order) { $paymentManager->payOrder($order, MeanPayment::CREDIT_CARD, $user, true); // client : envoi d'un email de confirmation de paiement /*\Yii::$app->mailerService->sendFromProducer( 'Confirmation de commande', 'paymentOrderConfirm', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ], $user->email, $producer );*/ // producteur : mail de confirmation \Yii::$app->mailerService->sendFromSite( 'Confirmation de commande', 'orderConfirmProducer', [ 'order' => $order, 'pointSale' => $pointSale, 'distribution' => $distribution, 'user' => $user, 'producer' => $producer ], $contactProducer->email ); } else { $paymentManager->creditUser($user, $amount, MeanPayment::CREDIT_CARD, $user); $userProducer = $this->getUserProducerModule()->findOneUserProducer($user); \Yii::$app->mailerService->sendFromProducer( 'Alimentation de votre cagnotte', 'creditConfirm', [ 'user' => $user, 'userProducer' => $userProducer, 'producer' => $producer, 'amount' => $amount, ], $user->email, $producer ); } } break; case 'charge.failed': // client \Yii::$app->mailerService->sendFromProducer( 'Erreur de paiement', 'paymentError', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ], $user->email, $producer ); // producteur if (isset($order) && $order) { \Yii::$app->mailerService->sendFromSite( 'Erreur de paiement', 'paymentErrorProducer', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, 'order' => $order, 'distribution' => $distribution ], $contactProducer->email, ); } break; // handle other event types default: echo 'Received unknown event type ' . $event->type; } http_response_code(200); } else { http_response_code(200); } } die(); } }